This repository has been archived by the owner on Apr 26, 2023. It is now read-only.
/
javascript.js
150 lines (130 loc) · 4.1 KB
/
javascript.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import path from 'path'
import fs from 'fs-extra'
import minimatch from 'minimatch'
import browserify from 'browserify'
import forgetify from 'forgetify'
import babelify from 'babelify'
import envify from 'loose-envify'
import uglifyify from 'uglifyify'
import aliasify from 'aliasify'
import uglify from 'uglify-js'
import resolve from 'resolve'
import exorcist from 'exorcist'
import { debounce, convertIfWin32Path } from '../utils/common'
import { depTree } from '../utils/deptree'
import { socketMessage } from '../cmds/serve'
import { CONFIG } from '../utils/config'
const projectNodeModsDir = path.resolve('node_modules')
let bundler
export function initBundle () {
var reactDir = convertIfWin32Path(resolve.sync('react', { basedir: projectNodeModsDir }))
const b = browserify({
plugin: [forgetify],
paths: [path.resolve('node_modules'), CONFIG.sourceDir],
debug: process.env.NODE_ENV === 'development' || CONFIG.prodSourceMaps,
cache: {},
packageCache: {},
standalone: 'jumpsuit-app',
insertGlobalVars: Object.assign(CONFIG.browserify.globals, {
React: (file, basedir) => `require("${reactDir}")`
})
})
b.transform(babelify, {
presets: [
resolve.sync('babel-preset-es2015', { basedir: __dirname }),
resolve.sync('babel-preset-react', { basedir: __dirname }),
resolve.sync('babel-preset-stage-2', { basedir: __dirname })
]
})
b.transform({
global: true
}, envify)
if (process.env.NODE_ENV === 'production') {
b.transform({
global: true,
sourcemap: CONFIG.prodSourceMaps
}, uglifyify)
}
CONFIG.browserify.transforms.forEach((t) => {
if (typeof t === 'string') {
b.transform(resolve.sync(t, { basedir: process.cwd() }))
} else if (typeof t === 'object' && t.transform) {
b.transform(t.transform, t.options)
}
})
b.transform(aliasify, {
global: true,
aliases: {
// resolve to the react package location, not the file
'react': path.resolve(resolve.sync('react', { basedir: projectNodeModsDir }), '../')
}
})
return b
}
const createBundle = debounce((cb) => {
const file = convertIfWin32Path(path.resolve(CONFIG.outputDir, path.basename(CONFIG.entry)))
let sourceMapFile = path.basename(CONFIG.entry).split('.')
sourceMapFile.splice(sourceMapFile.length - 1, 0, 'map')
sourceMapFile = sourceMapFile.join('.')
const stream = fs.createWriteStream(file)
.on('error', cb)
.on('finish', () => {
if (process.env.NODE_ENV === 'production') {
const code = fs.readFileSync(file, 'utf8')
const newCode = uglify.minify(code, {
outSourceMap: CONFIG.prodSourceMaps ? sourceMapFile : undefined,
fromString: true
})
fs.writeFileSync(file, newCode.code)
return
}
socketMessage({
type: 'refresh'
})
})
if (process.env.NODE_ENV === 'production' && CONFIG.prodSourceMaps) {
bundler.bundle((err) => {
if (err) cb(err)
})
.pipe(exorcist(path.resolve(CONFIG.outputDir, sourceMapFile)))
.pipe(stream)
.on('finish', () => cb())
} else {
bundler.bundle((err) => {
if (err) cb(err)
})
.pipe(stream)
.on('finish', () => cb())
}
}, { wait: 300 })
let firstRun = true
const entries = new Set()
export function buildJs (evt, file) {
var posixFileName = convertIfWin32Path(file)
return new Promise((resolve, reject) => {
if (!bundler) bundler = initBundle()
/* only worry about bundling the main file on first run */
if (firstRun && !posixFileName.match(new RegExp(`${CONFIG.entry}$`))) {
return resolve()
}
const invalidate = new Set([file])
if (depTree[file]) {
depTree[file].forEach((f) => invalidate.add(f))
}
CONFIG.browserify.rebundles.forEach((f) => {
if (minimatch(file, f.match)) {
invalidate.add(path.resolve(f.file))
}
})
invalidate.forEach((f) => forgetify.forget(bundler, f))
if (!entries.has(file)) {
entries.add(file)
bundler.add(file)
}
createBundle((err) => {
if (err) reject(err)
else resolve()
firstRun = false
})
})
}