-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathcreateWebpackBundle.js
116 lines (108 loc) · 3.21 KB
/
createWebpackBundle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import path from 'path';
import webpack from 'webpack';
import Logger from './Logger';
const { VERBOSE = 'false' } = process.env;
function generateBaseConfig({ entry, type, tmpdir }) {
const babelLoader = require.resolve('babel-loader');
const baseConfig = {
devtool: 'nosources-source-map',
entry,
output: {
filename: 'happo-bundle.js',
path: tmpdir,
devtoolModuleFilenameTemplate: '[absolute-resource-path]',
},
resolve: {
extensions: ['*', '.js', '.jsx', '.json', '.ts', '.tsx'],
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: babelLoader,
options: {
plugins: [require.resolve('babel-plugin-dynamic-import-node')],
},
},
},
],
},
plugins: [],
};
if (/^[4567]\./.test(webpack.version)) {
if (VERBOSE === 'true') {
console.log('Detected webpack version >=4. Using `mode: "development"`.');
}
baseConfig.mode = 'development';
} else if (VERBOSE === 'true') {
console.log('Detected webpack version <4. If you upgrade to >=4, stack traces from Happo will be a little better.');
}
if (type === 'react') {
let babelPresetReact;
try {
// try with the babel 7 package
babelPresetReact = require.resolve('@babel/preset-react');
} catch (e) {
// fall back to regular
babelPresetReact = require.resolve('babel-preset-react');
}
const [babelRule] = baseConfig.module.rules;
babelRule.test = /\.jsx?$/;
babelRule.use.options.presets = [babelPresetReact];
}
return baseConfig;
}
export default async function createWebpackBundle(
entry,
{ type, customizeWebpackConfig, plugins, tmpdir },
{ onBuildReady },
) {
let config = generateBaseConfig({ entry, type, tmpdir });
for (const plugin of plugins) {
if (typeof plugin.customizeWebpackConfig === 'function') {
config = await plugin.customizeWebpackConfig(config); // eslint-disable-line no-await-in-loop
}
}
config = await customizeWebpackConfig(config);
if (VERBOSE === 'true') {
console.log('Using this webpack config:');
console.log(config);
}
const compiler = webpack(config);
const bundleFilePath = path.join(config.output.path, config.output.filename);
if (onBuildReady) {
// We're in watch/dev mode
let hash;
compiler.watch({}, (err, stats) => {
if (err) {
new Logger().error(err);
} else if (stats.compilation.errors && stats.compilation.errors.length) {
stats.compilation.errors.forEach(e => new Logger().error(e));
} else if (hash !== stats.hash) {
hash = stats.hash;
onBuildReady(bundleFilePath);
}
});
return;
}
// We're not in watch/dev mode
return new Promise((resolve, reject) => {
compiler.run((err, stats) => {
if (err) {
reject(err);
return;
}
if (VERBOSE === 'true') {
console.log('Webpack stats:');
console.log(stats.toJson('verbose'));
}
if (stats.compilation.errors && stats.compilation.errors.length) {
reject(stats.compilation.errors[0]);
return;
}
resolve(bundleFilePath);
});
});
}