-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathbuild.js
147 lines (147 loc) · 4.5 KB
/
build.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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const rogo_1 = require("rogo");
const path = require("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");
const rollup_plugin_terser_1 = require("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 = rogo_1.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" },
},
],
],
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 = getBabelConfig();
const cjsBabelConfig = getBabelConfig();
cjsBabelConfig.plugins.push(["module-extension", { mjs: "js" }]); // replace .mjs to .js
const umdBabelConfig = getBabelConfig();
exports.default = [
// esm
{
input,
external: (source) => rogo_1.belongsTo(source, Object.keys(pkg.dependencies || {})) ||
rogo_1.belongsTo(source, Object.keys(pkg.peerDependencies || {})) ||
rogo_1.belongsTo(source, external),
plugins: [
vue(),
postcss({ extract: extractCssPath }),
typescript(),
babel(esmBabelConfig),
node(),
cjs(),
json(),
],
output: {
dir: `${outDir}/esm`,
format: "esm",
banner: getBanner(pkg),
sourcemap: false,
},
},
// cjs
{
input,
external: (source) => rogo_1.belongsTo(source, Object.keys(pkg.dependencies || {})) ||
rogo_1.belongsTo(source, Object.keys(pkg.peerDependencies || {})) ||
rogo_1.belongsTo(source, external),
plugins: [
vue(),
postcss({ extract: extractCssPath }),
typescript(),
babel(cjsBabelConfig),
node(),
cjs(),
json(),
],
output: {
dir: `${outDir}/cjs`,
format: "cjs",
banner: getBanner(pkg),
sourcemap: false,
},
},
// umd
{
input,
external: (source) => rogo_1.belongsTo(source, Object.keys(pkg.peerDependencies || {})),
plugins: [
vue(),
postcss({ extract: extractCssPath }),
typescript(),
babel(umdBabelConfig),
node(),
cjs(),
json(),
],
output: {
dir: `${outDir}/umd`,
format: "umd",
banner: getBanner(pkg),
sourcemap: false,
name: moduleName,
},
},
// umd min
{
input,
external: (source) => rogo_1.belongsTo(source, Object.keys(pkg.peerDependencies || {})),
plugins: [
vue(),
postcss({ extract: extractCssPath }),
typescript(),
babel(umdBabelConfig),
node(),
cjs(),
json(),
rollup_plugin_terser_1.terser(),
],
output: {
dir: `${outDir}/umd-min`,
format: "umd",
banner: getBanner(pkg),
sourcemap: false,
name: moduleName,
},
},
];
if (process.argv.includes("--report")) {
rogo_1.report(outDir);
}
function getBanner(pkg) {
return `
/*!
* ${pkg.name} v${pkg.version}
* (c) ${pkg.author}
* Homepage: ${pkg.homepage}
* Released under the ${pkg.license} License.
*/`.trim();
}