-
Notifications
You must be signed in to change notification settings - Fork 9
/
bundle.js
149 lines (128 loc) · 4.6 KB
/
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
137
138
139
140
141
142
143
144
145
146
147
148
149
const path = require('path');
const fs = require('fs');
const { rollup } = require('rollup');
const replace = require('rollup-plugin-replace');
const typescript = require('rollup-plugin-typescript');
const { terser } = require('rollup-plugin-terser');
const resolver = require('rollup-plugin-node-resolve');
const cleanup = require('rollup-plugin-cleanup');
const CJS_SINGLE_TYPE = Symbol();
const CJS_DOUBLE_TYPE = Symbol();
const ESNEXT_TYPE = Symbol();
const MJS_TYPE = Symbol();
const [input, ...args] = process.argv.slice(process.argv.findIndex(arg => arg.endsWith('bundle.js')) + 1);
const inputPath = path.join(process.cwd(), input);
const cjsDouble = args.indexOf('--cjsx2') !== -1;
const cjsSingle = args.indexOf('--cjs') !== -1;
const esnext = args.indexOf('--esnext') !== -1;
const mjs = args.indexOf('--mjs') !== -1;
if (cjsSingle || cjsDouble) {
const template = args[args.indexOf('--cjsx2') !== -1 ? args.indexOf('--cjsx2') + 1 : args.indexOf('--cjs') + 1];
const developmentCjsTemplate = `${template}${cjsSingle ? '' : '.development'}`;
const developmentCjsBundlePath = resolvePath(developmentCjsTemplate + '.js');
build(inputPath, developmentCjsBundlePath, cjsSingle ? CJS_SINGLE_TYPE : CJS_DOUBLE_TYPE, false).catch(error => {
console.log(error);
process.exit(1);
});
if (cjsDouble) {
const productionCjsTemplate = `${template}.production`;
const productionCjsBundlePath = resolvePath(productionCjsTemplate + '.js');
build(inputPath, productionCjsBundlePath, CJS_DOUBLE_TYPE, true).catch(error => {
console.log(error);
process.exit(1);
});
const entryPath = resolvePath(`${template}.js`);
write(
`'use strict';
if (process.env.NODE_ENV === 'production') {
module.exports = require('./${path.relative(path.dirname(entryPath), resolvePath(productionCjsTemplate))}');
} else {
module.exports = require('./${path.relative(path.dirname(entryPath), resolvePath(developmentCjsTemplate))}');
}
`,
entryPath,
);
}
}
if (mjs) {
const template = args[args.indexOf('--mjs') + 1];
const developmentMjsBundlePath = resolvePath(template + '.js', MJS_TYPE);
build(inputPath, developmentMjsBundlePath, MJS_TYPE, false).catch(error => {
console.log(error);
process.exit(1);
});
}
if (esnext) {
const template = args[args.indexOf('--esnext') + 1];
const developmentEsnextBundlePath = resolvePath(template + '.js', ESNEXT_TYPE);
build(inputPath, developmentEsnextBundlePath, ESNEXT_TYPE, false).catch(error => {
console.log(error);
process.exit(1);
});
}
function resolvePath(relative, type) {
return path.join(process.cwd(), type === MJS_TYPE ? 'mjs' : type === ESNEXT_TYPE ? 'esnext' : 'cjs', relative);
}
async function build(input, output, type, production) {
const bundle = await rollup({
input,
external: [
'@glitz/core',
'@glitz/devtool-transformer',
'@glitz/length-transformer',
'@glitz/prefixer-transformer',
'@glitz/react',
'@glitz/transformers',
'@glitz/reboot',
'react',
'react-is',
'inline-style-prefixer',
'typescript',
],
plugins: [
resolver(),
typescript({
typescript: require('typescript'),
...require('./tsconfig.base.json').compilerOptions,
...require('./tsconfig.json').compilerOptions,
target: type === ESNEXT_TYPE ? 'es2020' : 'es5',
module: 'es6',
declaration: false,
}),
...(type === CJS_DOUBLE_TYPE
? [
replace({
'process.env.NODE_ENV': JSON.stringify(production ? 'production' : 'development'),
}),
]
: []),
...(production ? [terser({ toplevel: true })] : []),
cleanup({ comments: 'none', extensions: ['js', 'jsx', 'mjs', 'ts', 'tsx'] }),
],
});
const { output: chunks } = await bundle.generate({
name: 'glitz',
format: type === MJS_TYPE || type === ESNEXT_TYPE ? 'es' : 'cjs',
exports: 'named',
globals: { react: 'React' },
});
for (const { code } of chunks) {
write(code, output);
}
}
async function write(code, filename) {
const { gzipSize } = await import('gzip-size');
const gzip = await gzipSize(code);
const dir = path.dirname(filename);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
fs.writeFileSync(filename, code, 'utf-8');
if (!fs.existsSync(filename)) {
throw new Error(`Rollup was not able to create bundle ${filename}`);
}
console.info(`Package: ${path.relative(__dirname, filename)} (${filesize(code.length, gzip)})`);
}
function filesize(origSize, gzipSize) {
return `${(origSize / 1024).toFixed(2)}KB / ${(gzipSize / 1024).toFixed(2)}KB gz`;
}