/
compiler.js
85 lines (76 loc) · 2.01 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
77
78
79
80
81
82
83
84
85
/* eslint-disable
import/order,
multiline-ternary,
no-param-reassign,
*/
import del from 'del';
import path from 'path';
import webpack from 'webpack';
import { createFsFromVolume, Volume } from 'memfs';
const module = (config) => {
return {
rules: config.rules
? config.rules
: [
{
test: (config.loader && config.loader.test) || /\.s[ac]ss$/i,
resolve: config.loader.resolve,
use: [
{
loader: require.resolve('./testLoader'),
},
{
loader: path.join(__dirname, '../../src/cjs.js'),
options: (config.loader && 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',
};
};
export default function(fixture, config = {}, options = {}) {
// webpack Config
config = {
mode: config.mode || 'development',
devtool: config.devtool || false,
// context: path.resolve(__dirname, '..', 'fixtures'),
context: path.resolve(__dirname, '..'),
entry: config.entry || `./${fixture}`,
output: output(config),
module: module(config),
plugins: plugins(config),
optimization: {
runtimeChunk: false,
minimizer: [],
},
// eslint-disable-next-line no-undefined
resolve: config.resolve || undefined,
};
// Compiler Options
options = Object.assign({ output: false }, options);
if (options.output) {
del.sync(config.output.path);
}
const compiler = webpack(config);
if (!options.output) {
compiler.outputFileSystem = new createFsFromVolume(new Volume()); // eslint-disable-line
}
return new Promise((resolve, reject) =>
compiler.run((err, stats) => {
if (err) {
return reject(err);
}
return resolve(stats);
})
);
}