This repository has been archived by the owner on Mar 17, 2021. It is now read-only.
/
compiler.js
76 lines (65 loc) · 1.64 KB
/
compiler.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
import path from 'path';
import del from 'del';
import webpack from 'webpack';
import MemoryFS from 'memory-fs';
const modules = (config) => {
return {
rules: config.rules
? config.rules
: config.loader
? [
{
test: config.loader.test || /\.js$/,
use: {
loader: path.resolve(__dirname, '../../src'),
options: config.loader.options || {},
},
},
]
: [],
};
};
const plugins = (config) => [].concat(config.plugins || []);
const output = (config) => {
return {
path: path.resolve(
__dirname,
`../outputs/${config.output ? config.output : ''}`
),
filename: '[name].bundle.js',
chunkFilename: '[name].chunk.js',
};
};
export default function(fixture, config, options) {
// eslint-disable-next-line no-param-reassign
config = {
mode: config.mode || 'development',
devtool: config.devtool || 'sourcemap',
context: path.resolve(__dirname, '..', 'fixtures'),
entry: `./${fixture}`,
output: output(config),
module: modules(config),
plugins: plugins(config),
optimization: {
minimize: false,
runtimeChunk: false,
},
};
// eslint-disable-next-line no-param-reassign
options = Object.assign({ output: false }, options);
if (options.output) {
del.sync(config.output.path);
}
const compiler = webpack(config);
if (!options.output) {
compiler.outputFileSystem = new MemoryFS();
}
return new Promise((resolve, reject) =>
compiler.run((error, stats) => {
if (error) {
reject(error);
}
return resolve(stats);
})
);
}