-
Notifications
You must be signed in to change notification settings - Fork 16
/
build.mjs
149 lines (139 loc) · 3.55 KB
/
build.mjs
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
import path from 'path';
import fse from 'fs-extra';
import {
build,
} from 'tsup';
import {
replace,
} from 'esbuild-plugin-replace';
/**
* @typedef Configuration
* @property {string} name
* @property {import('tsup').Options | (isProd: boolean) => import('tsup').Options} options
*/
/**
* Shims used to replace global constants
*/
const SHIMS = {
devEnv: 'process.env.NODE_ENV === "development"',
prodDevtools: '(typeof __VUE_PROD_DEVTOOLS__ !== \'undefined\' && __VUE_PROD_DEVTOOLS__)',
};
/**
* @type import('tsup').Options
*/
const baseOptions = {
target: 'es2018',
sourcemap: false,
clean: false,
};
/**
* @type Configuration[]
*/
const builds = [
{
name: 'commonjs',
options: isProd => ({
format: ['cjs'],
minify: isProd,
outExtension: () => ({
js: isProd ? '.prod.cjs' : '.cjs',
}),
esbuildPlugins: [
replace({
values: {
'__DEV__': isProd ? 'false' : SHIMS.devEnv,
'__VUE_PROD_DEVTOOLS__': isProd ? 'false' : SHIMS.prodDevtools,
},
}),
],
}),
},
{
name: 'esm-bundler',
options: {
format: ['esm'],
outExtension: () => ({
js: '.bundler.mjs',
}),
esbuildPlugins: [
replace({
values: {
'__DEV__': SHIMS.devEnv,
'__VUE_PROD_DEVTOOLS__': SHIMS.prodDevtools,
},
}),
],
},
},
{
name: 'esm-browser',
options: isProd => ({
format: ['esm'],
minify: isProd,
outExtension: () => ({
js: isProd ? '.browser.prod.mjs' : '.browser.mjs',
}),
esbuildPlugins: [
replace({
values: {
'__DEV__': 'false',
'__VUE_PROD_DEVTOOLS__': 'false',
},
}),
],
}),
},
{
name: 'iife',
options: isProd => ({
format: ['iife'],
minify: isProd,
outExtension: () => ({
js: isProd ? '.iife.prod.js' : '.iife.js',
}),
esbuildPlugins: [
replace({
values: {
'__DEV__': `${!isProd}`,
'__VUE_PROD_DEVTOOLS__': `${!isProd}`,
},
}),
],
}),
},
{
name: 'types',
options: {
dts: {
only: true,
},
},
},
];
/**
*
* @param {string} cwd
* @param {string} entry
* @param {import('tsup').Options} options
*/
export default async function(cwd, entry, options) {
const entryFile = path.resolve(cwd, entry);
const outDir = path.resolve(cwd, './dist');
await fse.emptyDir(outDir);
for (const { options: buildOptions } of builds) {
const resolvedOptionSets = typeof buildOptions === 'function'
? [buildOptions(false), buildOptions(true)]
: [buildOptions];
for (const resolvedOptions of resolvedOptionSets) {
await build({
...baseOptions,
...resolvedOptions,
...options,
outDir,
entry: [
entryFile,
],
});
}
}
}