-
Notifications
You must be signed in to change notification settings - Fork 6
/
compiler.js
71 lines (64 loc) · 1.72 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
const path = require('path')
const webpack = require('webpack')
const Memoryfs = require('memory-fs')
module.exports = (fixture, options = {}) => {
const compiler = webpack({
stats: {
// Examine all modules
maxModules: Infinity,
// Display bailout reasons
optimizationBailout: true
},
mode: options.mode || 'development',
context: __dirname,
entry: `./${fixture}`,
output: {
path: path.resolve(__dirname),
filename: 'bundle.js'
},
resolve: {
extensions: ['.ts', '.tsx', '.js']
},
module: {
rules: [
...(options.prerules || []),
{
test: /\.tsx?$/,
use: {
loader: path.resolve(__dirname, '../lib/index.js'),
options: {
tsconfig: path.resolve(__dirname, options.tsconfig != null ? options.tsconfig : 'tsconfig.json'),
externDir: options.externDir
}
}
}, ...(options.rules || [])
]
},
plugins: [
new webpack.optimize.ModuleConcatenationPlugin()
],
optimization: {
minimize: true,
// minimizer: options.minimizer,
usedExports: true,
splitChunks: {
minSize: 0
},
concatenateModules: true
}
})
const mfs = new Memoryfs()
compiler.outputFileSystem = mfs
const getCode = (stats) =>
stats.toJson().modules[0].source
return new Promise((resolve, reject) => {
compiler.run((err, stats) => {
if (err || stats.hasErrors()) {
reject(err != null ? err : stats.compilation.errors)
return
}
const result = mfs.readFileSync(path.resolve(__dirname, 'bundle.js')).toString()
resolve([result, stats, getCode(stats)])
})
})
}