This repository has been archived by the owner on Nov 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.ts
100 lines (93 loc) · 3.37 KB
/
index.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
import rollupCommonJs from "rollup-plugin-commonjs";
import rollupJson from "@rollup/plugin-json";
import rollupNodeResolve from "@rollup/plugin-node-resolve";
import rollupBabel from "rollup-plugin-babel";
import { terser as rollupTerser } from "rollup-plugin-terser";
import rollupSourcemaps from "rollup-plugin-sourcemaps";
import babelPresetEnv from "@babel/preset-env";
import babelPluginDefine from "babel-plugin-transform-define";
import path from "path";
import fs from "fs";
import { BuilderOptions, MessageError } from "@pika/types";
import { rollup } from "rollup";
export async function beforeBuild({ options }: BuilderOptions) {
if (!options.name) {
throw new MessageError('A "name" option is required for IIFE and UMD builds.');
}
}
export async function beforeJob({ out }: BuilderOptions) {
const srcDirectory = path.join(out, "dist-src/");
if (!fs.existsSync(srcDirectory)) {
throw new MessageError(
'"dist-src/" does not exist, or was not yet created in the pipeline.'
);
}
const srcEntrypoint = path.join(out, "dist-src/index.js");
if (!fs.existsSync(srcEntrypoint)) {
throw new MessageError(
'"dist-src/index.js" is the expected standard entrypoint, but it does not exist.'
);
}
}
export async function build({ out, options, reporter }: BuilderOptions): Promise<void> {
const writePath = path.join(out, "dist-browser", "index.js");
const babelOptions = options.babel || {};
const result = await rollup({
input: path.join(out, "dist-src/index.js"),
plugins: [
rollupJson({
include: ["node_modules/**", "**/*.json"],
compact: true,
indent: "\t",
namedExports: true,
}),
rollupNodeResolve({
preferBuiltins: true,
mainFields: ["module", "jsnext", "main", "browser"],
}),
rollupCommonJs({
include: "node_modules/**",
sourceMap: !!options.sourcemap,
namedExports: options.namedExports,
}),
rollupBabel({
exclude: "",
babelrc: false,
compact: false,
sourceMaps: true,
inputSourceMap: false,
presets: [
[
babelPresetEnv,
{
spec: true,
},
],
...(babelOptions.presets || []),
],
plugins: [
[
babelPluginDefine,
{
"process.env.NODE_ENV": process.env.NODE_ENV,
},
],
...(babelOptions.plugins || []),
],
}),
options.minify !== false
? rollupTerser(typeof options.minify === "object" ? options.minify : undefined)
: undefined,
rollupSourcemaps(),
],
external: options.external,
});
await result.write({
file: writePath,
format: options.format || "iife",
name: options.name,
sourcemap: options.sourcemap || false,
globals: options.globals || {},
});
reporter.created(writePath);
}