UNPKG

5.9 kBJavaScriptView Raw
1var dedent = require('dedent')
2var rimraf = require('rimraf')
3var mkdirp = require('mkdirp')
4var path = require('path')
5var tape = require('tape')
6var fs = require('fs')
7var os = require('os')
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 filename = 'js-pipeline-' + (Math.random() * 1e4).toFixed() + '.js'
18 var tmpFilename = path.join(os.tmpdir(), filename)
19 fs.writeFileSync(tmpFilename, file)
20
21 var compiler = bankai(tmpFilename, { 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 rimraf.sync(tmpFilename)
28 })
29})
30
31tape('return an error if an incorrect script is selected', function (assert) {
32 assert.plan(1)
33 var file = dedent`
34 1 + 1
35 `
36
37 var filename = 'js-pipeline-' + (Math.random() * 1e4).toFixed() + '.js'
38 var tmpFilename = path.join(os.tmpdir(), filename)
39 fs.writeFileSync(tmpFilename, file)
40
41 var compiler = bankai(tmpFilename, { watch: false })
42 compiler.scripts('bad-bad-not-good.js', function (err, res) {
43 assert.ok(err, 'error writing script')
44 rimraf.sync(tmpFilename)
45 })
46})
47
48tape('output multiple bundles if `split-require` is used', function (assert) {
49 assert.plan(2)
50
51 var file = `
52 require('split-require')('./dynamic')
53 `
54 var dynamicFile = `
55 console.log('loaded')
56 module.exports = null
57 `
58
59 var tmpDirname = path.join(__dirname, '../tmp', 'js-pipeline-' + (Math.random() * 1e4).toFixed())
60 mkdirp.sync(tmpDirname)
61 fs.writeFileSync(path.join(tmpDirname, 'app.js'), file)
62 fs.writeFileSync(path.join(tmpDirname, 'dynamic.js'), dynamicFile)
63
64 var compiler = bankai(path.join(tmpDirname, 'app.js'), { watch: false })
65 compiler.scripts('bundle.js', function (err, res) {
66 assert.error(err, 'error writing main bundle')
67 var dynamicName = /bundle-\d+\.js/.exec(res.buffer.toString('utf-8'))
68 compiler.scripts(dynamicName[0], function (err, res) {
69 assert.error(err, 'error writing dynamic bundle')
70
71 rimraf.sync(tmpDirname)
72 })
73 })
74})
75
76tape('use custom babel config for local files, but not for dependencies', function (assert) {
77 assert.plan(2)
78
79 var babelPlugin = `
80 module.exports = function (b) {
81 return {
82 visitor: {
83 Program: function (path) {
84 path.unshiftContainer('body', b.types.stringLiteral('hello'))
85 }
86 }
87 }
88 }
89 `
90 var babelrc = JSON.stringify({
91 plugins: ['./plugin']
92 })
93 var file = `
94 require('a-module-with-babelrc')
95 `
96
97 var tmpDirname = path.join(__dirname, '../tmp', 'js-pipeline-' + (Math.random() * 1e4).toFixed())
98 mkdirp.sync(tmpDirname)
99 fs.writeFileSync(path.join(tmpDirname, 'plugin.js'), babelPlugin)
100 fs.writeFileSync(path.join(tmpDirname, '.babelrc'), babelrc)
101 fs.writeFileSync(path.join(tmpDirname, 'app.js'), file)
102
103 var compiler = bankai(path.join(tmpDirname, 'app.js'), { watch: false })
104 compiler.on('error', assert.error)
105 compiler.scripts('bundle.js', function (err, res) {
106 assert.error(err, 'error building .babelrc dependency')
107 assert.pass('should build')
108 })
109})
110
111tape('use custom browserslist config', function (assert) {
112 assert.plan(5)
113
114 var browserslist = `
115 last 1 Chrome versions
116 `
117 var file = `
118 for (const value of generator()) {}
119 function * generator () {
120 yield 'foo'
121 }
122 `
123
124 var tmpDirname = path.join(__dirname, '../tmp', 'js-pipeline-' + (Math.random() * 1e4).toFixed())
125 mkdirp.sync(tmpDirname)
126 fs.writeFileSync(path.join(tmpDirname, '.browserslistrc'), browserslist)
127 fs.writeFileSync(path.join(tmpDirname, 'app.js'), file)
128
129 var compiler = bankai(path.join(tmpDirname, 'app.js'), { watch: false })
130 compiler.on('error', assert.error)
131 compiler.scripts('bundle.js', function (err, res) {
132 assert.error(err, 'no error writing script')
133 var content = res.buffer.toString('utf8')
134 assert.ok(/for\s*\(\w+ \w+ of /.test(content), 'did not transpile for...of')
135 assert.ok(/const/.test(content), 'did not transpile const keyword')
136 assert.ok(/function\s*\*/.test(content), 'did not transpile generator function')
137 assert.ok(/yield/.test(content), 'did not transpile yield keyword')
138 })
139})
140
141tape('envify in watch mode', function (assert) {
142 assert.plan(5)
143
144 var file = `
145 console.log(process.env.BANKAI_TEST_VALUE)
146 `
147 var file2 = `
148 var a = process.env.BANKAI_TEST_VALUE
149 console.log({ a: a })
150 `
151
152 var tmpDirname = path.join(__dirname, '../tmp', 'js-pipeline-' + (Math.random() * 1e4).toFixed())
153 mkdirp.sync(tmpDirname)
154 fs.writeFileSync(path.join(tmpDirname, 'app.js'), file)
155
156 process.env.BANKAI_TEST_VALUE = 'replacement'
157 var compiler = bankai(path.join(tmpDirname, 'app.js'), { watch: true, reload: false })
158 compiler.on('error', assert.error)
159 compiler.scripts('bundle.js', function (err, res) {
160 assert.error(err, 'no error writing script')
161 assert.notEqual(res.buffer.toString('utf8').indexOf('replacement'), -1, 'contains replacement value')
162
163 compiler.graph.on('change', next)
164
165 // Wait for a bit before changing the source file, because the watcher setup isn't instant.
166 setTimeout(function () {
167 fs.writeFileSync(path.join(tmpDirname, 'app.js'), file2)
168 }, 500)
169 })
170
171 function next (stepName, nodeName) {
172 if (stepName !== 'scripts' || nodeName !== 'bundle') return
173 compiler.scripts('bundle.js', function (err, res) {
174 assert.error(err, 'no error writing script')
175 assert.notEqual(res.buffer.toString('utf8').indexOf('replacement'), -1, 'contains replacement value')
176 assert.notEqual(res.buffer.toString('utf8').indexOf('a: a'), -1, 'is the updated file')
177
178 compiler.close()
179 })
180 compiler.graph.removeListener('change', next)
181 }
182})