-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathbuild.ts
158 lines (150 loc) · 4.13 KB
/
build.ts
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
151
152
153
154
155
156
157
158
import { belongsTo, report, camelize } from "rogo";
import * as rollup from "rollup";
import * as path from "path";
const babel = require("rollup-plugin-babel");
const node = require("@rollup/plugin-node-resolve");
const cjs = require("@rollup/plugin-commonjs");
const json = require("@rollup/plugin-json");
import { terser } from "rollup-plugin-terser"; // to minify bundle
const typescript = require("rollup-plugin-typescript2");
const vue = require("rollup-plugin-vue");
const postcss = require("rollup-plugin-postcss");
const pkg = require("../package.json");
const external = ["tslib"];
// quick config
const input = "src/index.ts";
const outDir = "dist";
const outputName = pkg.name; // the built file name is outDir/outputName.format.js
const moduleName = camelize(pkg.name); // for umd, amd
const extractCssPath = path.resolve(outDir, `${outputName}.css`);
const getBabelConfig = () => ({
// .babelrc
presets: [
[
"@vue/cli-plugin-babel/preset",
{
useBuiltIns: false,
polyfills: [],
targets: { browsers: "defaults" }, // default browsers, coverage 90%
},
],
],
plugins: [
"@babel/plugin-transform-runtime",
["@babel/plugin-proposal-optional-chaining", { loose: false }],
],
// for rollup babel plugin
runtimeHelpers: true,
exclude: [/@babel\/runtime/, /@babel\\runtime/, /regenerator-runtime/],
extensions: [".js", ".jsx", ".es6", ".es", ".mjs", ".vue", ".ts", ".tsx"],
babelrc: false,
});
const esmBabelConfig = <any>getBabelConfig();
const cjsBabelConfig = <any>getBabelConfig();
cjsBabelConfig.plugins.push(["module-extension", { mjs: "js" }]); // replace .mjs to .js
const umdBabelConfig = <any>getBabelConfig();
export default <rollup.RollupOptions[]>[
// esm
{
input,
external: (source) =>
belongsTo(source, Object.keys(pkg.dependencies || {})) ||
belongsTo(source, Object.keys(pkg.peerDependencies || {})) ||
belongsTo(source, external),
plugins: [
vue(),
postcss({ extract: extractCssPath }),
typescript(), // node must be in front of typescript. babel must behind typescript.
babel(esmBabelConfig),
node(),
cjs(),
json(),
],
output: {
dir: `${outDir}/esm`,
format: "esm",
banner: getBanner(pkg),
sourcemap: false,
},
},
// cjs
{
input,
external: (source) =>
belongsTo(source, Object.keys(pkg.dependencies || {})) ||
belongsTo(source, Object.keys(pkg.peerDependencies || {})) ||
belongsTo(source, external),
plugins: [
vue(),
postcss({ extract: extractCssPath }),
typescript(), // node must be in front of typescript. babel must behind typescript.
babel(cjsBabelConfig),
node(),
cjs(),
json(),
],
output: {
dir: `${outDir}/cjs`,
format: "cjs",
banner: getBanner(pkg),
sourcemap: false,
},
},
// umd
{
input,
external: (source) =>
belongsTo(source, Object.keys(pkg.peerDependencies || {})),
plugins: [
vue(),
postcss({ extract: extractCssPath }),
typescript(), // node must be in front of typescript. babel must behind typescript.
babel(umdBabelConfig),
node(),
cjs(),
json(),
],
output: {
dir: `${outDir}/umd`,
format: "umd",
banner: getBanner(pkg),
sourcemap: false,
name: moduleName,
},
},
// umd min
{
input,
external: (source) =>
belongsTo(source, Object.keys(pkg.peerDependencies || {})),
plugins: [
vue(),
postcss({ extract: extractCssPath }),
typescript(), // node must be in front of typescript. babel must behind typescript.
babel(umdBabelConfig),
node(),
cjs(),
json(),
terser(), // to minify bundle
],
output: {
dir: `${outDir}/umd-min`,
format: "umd",
banner: getBanner(pkg),
sourcemap: false,
name: moduleName,
},
},
];
if (process.argv.includes("--report")) {
report(outDir);
}
function getBanner(pkg) {
return `
/*!
* ${pkg.name} v${pkg.version}
* (c) ${pkg.author}
* Homepage: ${pkg.homepage}
* Released under the ${pkg.license} License.
*/`.trim();
}