-
Notifications
You must be signed in to change notification settings - Fork 28
/
compiler.js
71 lines (62 loc) · 1.56 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
var MemoryFS = require('memory-fs')
var webpack = require('webpack')
var defaults = require('lodash.defaults')
var path = require('path')
var isWebpack4 = webpack.version && webpack.version.slice(0, 1) === '4'
function webpack3Compiler (config) {
return webpack({
entry: './test/erb/' + config.file,
module: {
loaders: [
{
test: /\.erb$/,
loader: './index',
options: defaults({}, config, {
dependenciesRoot: './test/dependencies',
timeoutMs: 2000
})
}
]
},
output: {
filename: './output.js'
}
})
}
function webpack4Compiler (config) {
return webpack({
entry: './test/erb/' + config.file,
mode: 'development',
module: {
rules: [
{
test: /\.erb$/,
loader: './index',
options: defaults({}, config, {
dependenciesRoot: './test/dependencies',
timeoutMs: 2000
})
}
]
},
output: {
path: __dirname,
filename: 'output.js'
}
})
}
var webpackCompiler = isWebpack4 ? webpack4Compiler : webpack3Compiler
var fs = new MemoryFS()
function compile (config, callback) {
config.runner = config.runner || 'ruby'
config.engine = config.engine || 'erb'
var compiler = webpackCompiler(config)
compiler.outputFileSystem = fs
compiler.run(callback)
}
function readOutput () {
var fileContent = fs.readFileSync(path.resolve(__dirname, './output.js'))
return fileContent.toString()
}
exports.compile = compile
exports.readOutput = readOutput