Skip to content

Commit efcbdff

Browse files
Add envify in watch mode (bankai start) (#436)
1 parent f168483 commit efcbdff

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

lib/graph-script.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var sheetify = require('sheetify')
1515
var yoyoify = require('yo-yoify')
1616
var tinyify = require('tinyify')
1717
var glslify = require('glslify')
18+
var envify = require('envify/custom')
1819
var brfs = require('brfs')
1920

2021
var ttyError = require('./tty-error')
@@ -103,13 +104,19 @@ function node (state, createEdge) {
103104
b.on('split.pipeline', function (pipeline) {
104105
tinyify.applyToPipeline(pipeline, b._options)
105106
})
107+
} else {
108+
var env = Object.assign({
109+
NODE_ENV: 'development'
110+
}, process.env)
111+
b.transform(envify(env), { global: true })
106112
}
107113

108114
bundleScripts()
109115
b.on('update', bundleScripts)
110116

111117
var dynamicBundles
112-
function bundleScripts () {
118+
function bundleScripts (files) {
119+
if (files) debug('triggering update because of changes in', files)
113120
self.emit('progress', 'scripts', 30)
114121

115122
dynamicBundles = []

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"dedent": "^0.7.0",
3535
"disc": "^1.3.3",
3636
"documentify": "^3.1.0",
37+
"envify": "^4.1.0",
3738
"exorcist": "^1.0.0",
3839
"explain-error": "^1.0.4",
3940
"fast-json-parse": "^1.0.3",

test/script.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,46 @@ tape('use custom browserslist config', function (assert) {
137137
assert.ok(/yield/.test(content), 'did not transpile yield keyword')
138138
})
139139
})
140+
141+
tape('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+
})

0 commit comments

Comments
 (0)