Skip to content

Commit 0812ccc

Browse files
HazATkamilogorek
andauthored
ref: Add child run to test webpack build (#2524)
* ref: Add child run to test webpack build * fix: Restore dynamicRequire but for perf_hooks only * fix: Tests on node6 Co-authored-by: Kamil Ogórek <[email protected]>
1 parent 1d12783 commit 0812ccc

File tree

5 files changed

+61
-17
lines changed

5 files changed

+61
-17
lines changed

packages/browser/test/package/npm-build.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,14 @@ const fs = require('fs');
22
const path = require('path');
33
const webpack = require('webpack');
44
const { JSDOM } = require('jsdom');
5-
// runTests();
5+
66
webpack(
77
{
88
entry: path.join(__dirname, 'test-code.js'),
99
output: {
1010
path: __dirname,
1111
filename: 'tmp.js',
1212
},
13-
// resolve: {
14-
// mainFields: ['main'],
15-
// },
1613
mode: 'development',
1714
},
1815
(err, stats) => {
@@ -28,10 +25,12 @@ webpack(
2825

2926
if (stats.hasErrors()) {
3027
console.error(info.errors);
28+
process.exit(1);
3129
}
3230

3331
if (stats.hasWarnings()) {
3432
console.warn(info.warnings);
33+
process.exit(1);
3534
}
3635

3736
runTests();

packages/node/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"test:jest": "jest",
6161
"test:watch": "jest --watch",
6262
"test:express": "node test/manual/express-scope-separation/start.js",
63-
"test:webpack": "cd test/manual/webpack-domain/ && yarn && yarn webpack && node dist/bundle.js",
63+
"test:webpack": "cd test/manual/webpack-domain/ && yarn && node npm-build.js",
6464
"version": "node ../../scripts/versionbump.js src/version.ts"
6565
},
6666
"jest": {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const path = require('path');
2+
const webpack = require('webpack');
3+
const { execSync } = require('child_process');
4+
5+
// prettier-ignore
6+
webpack(
7+
{
8+
entry: './index.js',
9+
output: {
10+
path: path.resolve(__dirname, 'dist'),
11+
filename: 'bundle.js',
12+
},
13+
target: 'node',
14+
mode: 'development',
15+
},
16+
function(err, stats) {
17+
if (err) {
18+
console.error(err.stack || err);
19+
if (err.details) {
20+
console.error(err.details);
21+
}
22+
return;
23+
}
24+
25+
const info = stats.toJson();
26+
27+
if (stats.hasErrors()) {
28+
console.error(info.errors);
29+
process.exit(1);
30+
}
31+
32+
if (stats.hasWarnings()) {
33+
console.warn(info.warnings);
34+
process.exit(1);
35+
}
36+
runTests();
37+
}
38+
);
39+
40+
function runTests() {
41+
try {
42+
execSync('node ' + path.resolve(__dirname, 'dist', 'bundle.js'));
43+
} catch (_) {
44+
process.exit(1);
45+
}
46+
}

packages/node/test/manual/webpack-domain/webpack.config.js

-10
This file was deleted.

packages/utils/src/misc.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ interface SentryGlobal {
2020
};
2121
}
2222

23+
/**
24+
* Requires a module which is protected against bundler minification.
25+
*
26+
* @param request The module path to resolve
27+
*/
28+
export function dynamicRequire(mod: any, request: string): any {
29+
// tslint:disable-next-line: no-unsafe-any
30+
return mod.require(request);
31+
}
32+
2333
/**
2434
* Checks whether we're in the Node.js or Browser environment
2535
*
@@ -365,8 +375,7 @@ const performanceFallback: CrossPlatformPerformance = {
365375
export const crossPlatformPerformance: CrossPlatformPerformance = (() => {
366376
if (isNodeEnv()) {
367377
try {
368-
const req = require;
369-
const perfHooks = req('perf_hooks') as { performance: CrossPlatformPerformance };
378+
const perfHooks = dynamicRequire(module, 'perf_hooks') as { performance: CrossPlatformPerformance };
370379
return perfHooks.performance;
371380
} catch (_) {
372381
return performanceFallback;

0 commit comments

Comments
 (0)