/
build-bundle.js
136 lines (127 loc) · 4.33 KB
/
build-bundle.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
const fs = require('fs')
const chalk = require('chalk')
const babelify = require('babelify')
const tinyify = require('tinyify')
const browserify = require('browserify')
const exorcist = require('exorcist')
const glob = require('glob')
const path = require('path')
const { minify } = require('terser')
const { transformFileAsync } = require('@babel/core')
function handleErr (err) {
console.error(chalk.red('✗ Error:'), chalk.red(err.message))
}
// eslint-disable-next-line no-shadow
function buildBundle (srcFile, bundleFile, { minify = false, standalone = '' } = {}) {
const b = browserify(srcFile, { debug: true, standalone })
if (minify) {
b.plugin(tinyify)
}
b.transform(babelify)
b.on('error', handleErr)
return new Promise((resolve) => {
b.bundle()
.pipe(exorcist(`${bundleFile}.map`))
.pipe(fs.createWriteStream(bundleFile), 'utf8')
.on('error', handleErr)
.on('finish', () => {
if (minify) {
console.info(chalk.green(`✓ Built Minified Bundle [${standalone}]:`), chalk.magenta(bundleFile))
} else {
console.info(chalk.green(`✓ Built Bundle [${standalone}]:`), chalk.magenta(bundleFile))
}
resolve([bundleFile, standalone])
})
})
}
async function minifyBundle ([bundleFile, standalone]) {
const minifiedFilePath = bundleFile.replace(/\.js$/, '.min.js')
const sourceMapPath = `${minifiedFilePath}.map`
const js = await fs.promises.readFile(bundleFile, 'utf-8')
const { code, map } = await minify(js, {
sourceMap: {
content: fs.readFileSync(`${bundleFile}.map`, 'utf-8'),
url:sourceMapPath,
},
toplevel: true,
})
return Promise.all([
fs.promises.writeFile(minifiedFilePath, code),
fs.promises.writeFile(sourceMapPath, map),
])
.then(() => console.info(chalk.green(`✓ Built Minified Bundle [${standalone}]:`), chalk.magenta(minifiedFilePath)))
}
async function transpileDownForIE ([bundleFile, standalone]) {
const minifiedFilePath = bundleFile.replace(/\.js$/, '.min.js')
const sourceMapPath = `${minifiedFilePath}.map`
const { code: js, map: inputMap } = await transformFileAsync(bundleFile, {
compact: false,
highlightCode: false,
inputSourceMap: true,
browserslistEnv: 'legacy',
presets: [['@babel/preset-env', {
loose: true,
targets: { ie:11 },
useBuiltIns: 'entry',
corejs: { version: '3.15', proposals: true },
}]],
})
const { code, map } = await minify(js, {
sourceMap: {
content: inputMap,
url: sourceMapPath,
},
toplevel: true,
})
return Promise.all([
fs.promises.writeFile(bundleFile, js),
fs.promises.writeFile(`${bundleFile}.map`, JSON.stringify(inputMap)),
fs.promises.writeFile(minifiedFilePath, code),
fs.promises.writeFile(sourceMapPath, map),
]).then(() => {
console.info(chalk.green(`✓ Built Bundle [${standalone} (ES5)]:`), chalk.magenta(bundleFile))
console.info(chalk.green(`✓ Built Minified Bundle [${standalone} (ES5)]:`), chalk.magenta(minifiedFilePath))
})
}
fs.mkdirSync('./packages/uppy/dist', { recursive: true })
fs.mkdirSync('./packages/@uppy/robodog/dist', { recursive: true })
fs.mkdirSync('./packages/@uppy/locales/dist', { recursive: true })
const methods = [
buildBundle(
'./packages/uppy/index.js',
'./packages/uppy/dist/uppy.js',
{ standalone: 'Uppy' },
).then(minifyBundle),
buildBundle(
'./packages/uppy/bundle.js',
'./packages/uppy/dist/uppy.legacy.js',
{ standalone: 'Uppy (with polyfills)' },
).then(transpileDownForIE),
buildBundle(
'./packages/@uppy/robodog/bundle.js',
'./packages/@uppy/robodog/dist/robodog.js',
{ standalone: 'Robodog' },
).then(minifyBundle),
]
// Build minified versions of all the locales
const localePackagePath = path.join(__dirname, '..', 'packages', '@uppy', 'locales', 'src', '*.js')
glob.sync(localePackagePath).forEach((localePath) => {
const localeName = path.basename(localePath, '.js')
methods.push(
buildBundle(
`./packages/@uppy/locales/src/${localeName}.js`,
`./packages/@uppy/locales/dist/${localeName}.min.js`,
{ minify: true },
),
)
})
// Add BUNDLE-README.MD
methods.push(
fs.promises.copyFile(
`${__dirname}/../BUNDLE-README.md`,
`./packages/uppy/dist/README.md`,
),
)
Promise.all(methods).then(() => {
console.info(chalk.yellow('✓ JS bundles 🎉'))
})