UNPKG

2.63 kBJavaScriptView Raw
1var writeFileTree = require('write-file-tree')
2var spawn = require('child_process').spawn
3var dedent = require('dedent')
4var tape = require('tape')
5var path = require('path')
6var tmp = require('tmp')
7var fs = require('fs')
8
9var BIN_PATH = require.resolve('../bin')
10function build (entry, output, cb) {
11 var args = [BIN_PATH, 'build', entry]
12 if (output) args.push(output)
13 var proc = spawn(process.execPath, args)
14 proc.on('error', cb)
15 proc.on('exit', function (code) {
16 if (code === 0) cb(null)
17 else cb(new Error(code))
18 })
19 return proc
20}
21
22tape('default output directory', function (assert) {
23 assert.plan(4)
24
25 var tmpDir = tmp.dirSync({ dir: path.join(__dirname, '../tmp'), unsafeCleanup: true })
26 assert.on('end', tmpDir.removeCallback)
27
28 writeFileTree.sync(tmpDir.name, {
29 'index.js': dedent`
30 var choo = require('choo')
31 var raw = require('choo/html/raw')
32 var app = choo()
33 app.route('/', function () {
34 return raw('<body>hello world</body>')
35 })
36
37 module.exports = app.mount('body')
38 `
39 })
40
41 build(path.join(tmpDir.name, '/index.js'), null, function (err) {
42 assert.ifError(err)
43 try {
44 assert.ok(fs.statSync(path.join(tmpDir.name, 'dist')).isDirectory())
45 } catch (err) {
46 assert.error(err, 'should have placed output in ./dist')
47 }
48 })
49 build(tmpDir.name, null, function (err) {
50 assert.ifError(err)
51 try {
52 assert.ok(fs.statSync(path.join(tmpDir.name, 'dist')).isDirectory())
53 } catch (err) {
54 assert.error(err, 'should have placed output in ./dist')
55 }
56 })
57})
58
59tape('outputs split bundles', function (assert) {
60 assert.plan(4)
61
62 var tmpDir = tmp.dirSync({ dir: path.join(__dirname, '../tmp'), unsafeCleanup: true })
63 assert.on('end', tmpDir.removeCallback)
64
65 writeFileTree.sync(tmpDir.name, {
66 'index.js': dedent`
67 var sr = require('split-require')
68
69 sr('./a')
70 sr('./b')
71 `,
72 'a.js': dedent`
73 module.exports = 'THIS IS A'
74 `,
75 'b.js': dedent`
76 module.exports = 'THIS IS B'
77 `
78 })
79
80 var output = path.join(tmpDir.name, 'dist')
81 build(tmpDir.name, output, function (err) {
82 assert.ifError(err)
83
84 // maybe these should use globs instead of hardcoded hashes
85 // eg glob.sync('dist/*/bundle.js')
86 assert.ok(fs.existsSync(path.join(output, '102124f5fbe2468e', 'bundle.js')))
87 assert.notEqual(read(path.join(output, '98abfdc06765c024', 'bundle-2.js')).indexOf('THIS IS A'), -1)
88 assert.notEqual(read(path.join(output, 'd045ba5484611349', 'bundle-3.js')).indexOf('THIS IS B'), -1)
89 })
90})
91
92function read (p) {
93 return fs.readFileSync(p, 'utf8')
94}