-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
126 lines (117 loc) · 3.74 KB
/
Copy pathbuild.js
File metadata and controls
126 lines (117 loc) · 3.74 KB
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
import { nodeResolve } from '@rollup/plugin-node-resolve'
import babel from '@rollup/plugin-babel'
import linaria from 'linaria/rollup.js'
import css from 'rollup-plugin-css-only'
import mri from 'mri'
import * as fs from 'fs'
import * as path from 'path'
import * as rollup from 'rollup'
import json from '@rollup/plugin-json'
import { terser } from 'rollup-plugin-terser'
import url from '@rollup/plugin-url'
import babelConfig from './babel.config.cjs'
const packagesFolder = path.join(process.cwd(), 'packages')
const allPackages = fs.readdirSync(packagesFolder)
const extensions = ['.mjs', '.js', '.ts', '.tsx']
/** @returns {import('rollup').Plugin} */
const customResolver = () => {
return {
name: 'custom-resolver',
async resolveId(source, importer) {
if (source.startsWith('/transformer-'))
return { id: source, external: true }
if (!allPackages.includes(source) || !importer) return
const relative = path.join(
path.relative(path.dirname(importer), packagesFolder),
source,
)
return this.resolve(relative, importer, { skipSelf: true })
},
}
}
/** @type {import('rollup').RollupOptions} */
const uiConfig = {
input: {
ui: 'packages/ui',
},
plugins: [
customResolver(),
nodeResolve({ extensions }),
linaria({ sourceMap: false }),
css({ output: 'dist/styles.css' }),
// @ts-ignore
babel.babel({ extensions, babelHelpers: 'bundled' }),
],
output: {
dir: 'dist',
},
}
const main = async () => {
const args = mri(process.argv.slice(2), { boolean: ['watch'] })
const isWatch = Boolean(args.watch)
const packagesToBuild = args._.length > 0 ? args._ : ['ui']
const configs = packagesToBuild.map((packageName) => {
/** @type {import('rollup').RollupOptions} */
const config =
packageName === 'ui'
? uiConfig
: {
input: { [packageName]: `packages/${packageName}/transform` },
plugins: [
url({
// all files should be copied to dist rather than inlined
limit: 0,
publicPath: '/',
include: [/\.wasm$/],
}),
customResolver(),
nodeResolve({ extensions }),
json(),
// @ts-ignore
babel.babel({
extensions,
babelHelpers: 'bundled',
...babelConfig,
plugins: [...babelConfig.plugins, 'babel-plugin-un-cjs'],
configFile: false,
}),
terser({ module: true, ecma: 2020, compress: { passes: 5 } }),
],
output: { dir: 'dist' },
}
return config
})
if (isWatch) {
const watcher = rollup.watch(configs)
watcher.on('event', (event) => {
if (event.code === 'BUNDLE_START') {
console.log('building:', event.input)
} else if (event.code === 'BUNDLE_END') {
const duration = `${Math.round(event.duration / 100) / 10}s`
console.log(`finished build (${duration}):`, event.input)
} else if (event.code === 'ERROR') {
const errorMsg = event.error.frame
? `${event.error.frame}\n\n${event.error.message}`
: event.error
console.log(`Build failed:
${errorMsg}`)
}
})
} else {
await Promise.all(
configs.map(async (c) => {
const bundle = await rollup.rollup(c)
if (!c.output) {
console.warn('build is missing output', c.input)
return
}
const outputs = Array.isArray(c.output) ? c.output : [c.output]
await Promise.all(outputs.map((o) => bundle.write(o)))
console.log('completed build:', c.input)
}),
)
}
}
main().catch((error) => {
console.error('error with build:\n', error)
})