UNPKG

8.65 kBJavaScriptView Raw
1var dedent = require('dedent')
2var mkdirp = require('mkdirp')
3var path = require('path')
4var tape = require('tape')
5var tmp = require('tmp')
6var fs = require('fs')
7var vm = require('vm')
8
9var bankai = require('../')
10
11tape('run a JS pipeline', function (assert) {
12 assert.plan(4)
13 var file = dedent`
14 1 + 1
15 `
16
17 var tmpFile = tmp.fileSync({ dir: path.join(__dirname, '../tmp'), discardDescriptor: true, postfix: '.js' })
18 assert.on('end', tmpFile.removeCallback)
19 fs.writeFileSync(tmpFile.name, file)
20
21 var compiler = bankai(tmpFile.name, { watch: false })
22 compiler.scripts('bundle.js', function (err, res) {
23 assert.error(err, 'no error writing script')
24 assert.ok(res, 'output exists')
25 assert.ok(res.buffer, 'output buffer exists')
26 assert.ok(res.hash, 'output hash exists')
27 })
28})
29
30tape('use a folder as an entry point', function (assert) {
31 assert.plan(2)
32 var file = dedent`
33 console.log(1 + 1)
34 `
35
36 var tmpDir = tmp.dirSync({ dir: path.join(__dirname, '../tmp'), unsafeCleanup: true })
37 assert.on('end', tmpDir.removeCallback)
38 var tmpScriptname = path.join(tmpDir.name, 'index.js')
39 fs.writeFileSync(tmpScriptname, file)
40
41 var compiler = bankai(tmpDir.name, { watch: false })
42 compiler.scripts('bundle.js', function (err, res) {
43 assert.error(err, 'no error writing script')
44 assert.notEqual(res.buffer.toString('utf8').indexOf('console.log'), -1, 'contains js')
45 })
46})
47
48tape('return an error if an incorrect script is selected', function (assert) {
49 assert.plan(1)
50 var file = dedent`
51 1 + 1
52 `
53
54 var tmpFile = tmp.fileSync({ dir: path.join(__dirname, '../tmp'), discardDescriptor: true, postfix: '.js' })
55 assert.on('end', tmpFile.removeCallback)
56 fs.writeFileSync(tmpFile.name, file)
57
58 var compiler = bankai(tmpFile.name, { watch: false })
59 compiler.scripts('bad-bad-not-good.js', function (err, res) {
60 assert.ok(err, 'error writing script')
61 })
62})
63
64tape('output multiple bundles if `split-require` is used', function (assert) {
65 assert.plan(2)
66
67 var file = `
68 require('split-require')('./dynamic')
69 `
70 var dynamicFile = `
71 console.log('loaded')
72 module.exports = null
73 `
74
75 var tmpDir = tmp.dirSync({ dir: path.join(__dirname, '../tmp'), unsafeCleanup: true })
76 assert.on('end', tmpDir.removeCallback)
77 fs.writeFileSync(path.join(tmpDir.name, 'app.js'), file)
78 fs.writeFileSync(path.join(tmpDir.name, 'dynamic.js'), dynamicFile)
79
80 var compiler = bankai(path.join(tmpDir.name, 'app.js'), { watch: false })
81 compiler.scripts('bundle.js', function (err, res) {
82 assert.error(err, 'no error writing main bundle')
83 var dynamicName = /bundle-\d+\.js/.exec(res.buffer.toString('utf-8'))
84 compiler.scripts(dynamicName[0], function (err, res) {
85 assert.error(err, 'no error writing dynamic bundle')
86 })
87 })
88})
89
90tape('use custom babel config for local files, but not for dependencies', function (assert) {
91 assert.plan(2)
92
93 var babelPlugin = `
94 module.exports = function (b) {
95 return {
96 visitor: {
97 Program: function (path) {
98 path.unshiftContainer('body', b.types.stringLiteral('hello'))
99 }
100 }
101 }
102 }
103 `
104 var babelrc = JSON.stringify({
105 plugins: ['./plugin']
106 })
107 var file = `
108 require('a-module-with-babelrc')
109 `
110
111 var tmpDir = tmp.dirSync({ dir: path.join(__dirname, '../tmp'), unsafeCleanup: true })
112 assert.on('end', tmpDir.removeCallback)
113 fs.writeFileSync(path.join(tmpDir.name, 'plugin.js'), babelPlugin)
114 fs.writeFileSync(path.join(tmpDir.name, '.babelrc'), babelrc)
115 fs.writeFileSync(path.join(tmpDir.name, 'app.js'), file)
116
117 var compiler = bankai(path.join(tmpDir.name, 'app.js'), { watch: false })
118 compiler.on('error', assert.error)
119 compiler.scripts('bundle.js', function (err, res) {
120 assert.error(err, 'error building .babelrc dependency')
121 assert.pass('should build')
122 })
123})
124
125tape('skip babel for dependencies if babelifyDeps is false', function (assert) {
126 assert.plan(4)
127 var file = dedent`
128 const depFunc = require('mydep').depFunc
129 depFunc(1)
130 `
131 var depFile = dedent`
132 const depFunc = (arg) => {
133 console.log(arg)
134 }
135 module.exports = {
136 depFunc
137 }
138`
139
140 var filename = 'js-pipeline-' + (Math.random() * 1e4).toFixed() + '.js'
141 var tmpDir = tmp.dirSync({ dir: path.join(__dirname, '../tmp'), unsafeCleanup: true })
142 assert.on('end', tmpDir.removeCallback)
143
144 var tmpFilename = path.join(tmpDir.name, filename)
145 fs.writeFileSync(tmpFilename, file)
146 mkdirp.sync(path.join(tmpDir.name, 'node_modules'))
147 fs.writeFileSync(path.join(tmpDir.name, 'node_modules', 'mydep.js'), depFile)
148
149 var compiler = bankai(tmpFilename, { watch: false, babelifyDeps: false })
150 compiler.scripts('bundle.js', function (err, node) {
151 assert.error(err, 'no error writing script')
152 assert.ok(node, 'output exists')
153 assert.ok(node.buffer, 'output buffer exists')
154
155 const compiledJs = node.buffer.toString('utf8')
156 assert.notOk(/['"]use strict['"]/.test(compiledJs))
157 })
158})
159
160tape('use custom browserslist config', function (assert) {
161 assert.plan(5)
162
163 var browserslist = `
164 last 1 Chrome versions
165 `
166 var file = `
167 for (const value of generator()) {}
168 function * generator () {
169 yield 'foo'
170 }
171 `
172
173 var tmpDir = tmp.dirSync({ dir: path.join(__dirname, '../tmp'), unsafeCleanup: true })
174 assert.on('end', tmpDir.removeCallback)
175 fs.writeFileSync(path.join(tmpDir.name, '.browserslistrc'), browserslist)
176 fs.writeFileSync(path.join(tmpDir.name, 'app.js'), file)
177
178 var compiler = bankai(path.join(tmpDir.name, 'app.js'), { watch: false })
179 compiler.on('error', assert.error)
180 compiler.scripts('bundle.js', function (err, res) {
181 assert.error(err, 'no error writing script')
182 var content = res.buffer.toString('utf8')
183 assert.ok(/for\s*\(\w+ \w+ of /.test(content), 'did not transpile for...of')
184 assert.ok(/const/.test(content), 'did not transpile const keyword')
185 assert.ok(/function\s*\*/.test(content), 'did not transpile generator function')
186 assert.ok(/yield/.test(content), 'did not transpile yield keyword')
187 })
188})
189
190tape('does not transform top level `this` in dependencies', function (assert) {
191 assert.plan(4)
192 var file = `
193 T.equal(require('a')(), 10)
194 `
195 var dependency = `
196 module.exports = this.number || (() => 10)
197 `
198
199 var tmpDir = tmp.dirSync({ dir: path.join(__dirname, '../tmp'), unsafeCleanup: true })
200 assert.on('end', tmpDir.removeCallback)
201
202 mkdirp.sync(path.join(tmpDir.name, 'node_modules'))
203 fs.writeFileSync(path.join(tmpDir.name, 'app.js'), file)
204 fs.writeFileSync(path.join(tmpDir.name, 'node_modules', 'a.js'), dependency)
205
206 var compiler = bankai(path.join(tmpDir.name, 'app.js'), { watch: false })
207 compiler.on('error', assert.error)
208 compiler.scripts('bundle.js', function (err, res) {
209 assert.error(err, 'no error writing script')
210 var content = res.buffer.toString('utf8')
211
212 assert.ok(/this\.number/.test(content), 'did not rewrite `this`')
213 assert.ok(/return 10/.test(content), 'did rewrite arrow function')
214
215 vm.runInNewContext(content, { T: assert })
216 })
217})
218
219tape('envify in watch mode', function (assert) {
220 assert.plan(5)
221
222 var file = `
223 console.log(process.env.BANKAI_TEST_VALUE)
224 `
225 var file2 = `
226 var a = process.env.BANKAI_TEST_VALUE
227 console.log({ a: a })
228 `
229
230 var tmpDir = tmp.dirSync({ dir: path.join(__dirname, '../tmp'), unsafeCleanup: true })
231 assert.on('end', tmpDir.removeCallback)
232 fs.writeFileSync(path.join(tmpDir.name, 'app.js'), file)
233
234 process.env.BANKAI_TEST_VALUE = 'replacement'
235 var compiler = bankai(path.join(tmpDir.name, 'app.js'), { watch: true, reload: false })
236 compiler.on('error', assert.error)
237 compiler.scripts('bundle.js', function (err, res) {
238 assert.error(err, 'no error writing script')
239 assert.notEqual(res.buffer.toString('utf8').indexOf('replacement'), -1, 'contains replacement value')
240
241 compiler.graph.on('change', next)
242
243 // Wait for a bit before changing the source file, because the watcher setup isn't instant.
244 setTimeout(function () {
245 fs.writeFileSync(path.join(tmpDir.name, 'app.js'), file2)
246 }, 500)
247 })
248
249 function next (stepName, nodeName) {
250 if (stepName !== 'scripts' || nodeName !== 'bundle') return
251 compiler.scripts('bundle.js', function (err, res) {
252 assert.error(err, 'no error writing script')
253 assert.notEqual(res.buffer.toString('utf8').indexOf('replacement'), -1, 'contains replacement value')
254 assert.notEqual(res.buffer.toString('utf8').indexOf('a: a'), -1, 'is the updated file')
255
256 compiler.close()
257 })
258 compiler.graph.removeListener('change', next)
259 }
260})