-
Notifications
You must be signed in to change notification settings - Fork 439
/
build.js
101 lines (92 loc) · 3.31 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
import { rollup } from "npm:[email protected]";
import { minify } from "npm:[email protected]";
import { babel } from "npm:@rollup/[email protected]";
// This import is required for presetEnv to work:
import _ from "https://esm.sh/@babel/[email protected]";
import babelPresetEnv from "https://esm.sh/@babel/[email protected]";
let bundle = await rollup({
input: "mod.js",
plugins: [
babel({
babelHelpers: "bundled",
presets: [[babelPresetEnv]],
}),
],
});
// Write legacy UMD files to tinycolor.js and dist/tinycolor-min.js.
// It'd be nice to get rid of these, if we could ensure they aren't used.
async function write_cdn_umd() {
let { output } = await bundle.generate({
format: "umd",
name: "tinycolor",
});
let minified = await minify(output[0].code);
const preamble = `// This file is autogenerated.
// It's here at this path for backwards compatibility for links to it
// but the npm package now exports both CJS and ESM.
// See https://github.com/bgrins/TinyColor/ for instructions.
`;
Deno.writeTextFileSync("npm/tinycolor.js", preamble + output[0].code);
Deno.writeTextFileSync("npm/dist/tinycolor-min.js", preamble + minified.code);
// Keeping these (without preamble) until we can confirm they aren't used by CDNs
// after moving to the npm/ path. At that point these files can be removed in
// https://github.com/bgrins/TinyColor/issues/260.
Deno.writeTextFileSync("tinycolor.js", output[0].code);
Deno.writeTextFileSync("dist/tinycolor-min.js", minified.code);
}
// Write necessary files for testing & publishing commonjs to npm.
// The subdirectory sets { "type": "commonjs" } in the package.json,
// and includes a lightweight test runner to ensure it works like mod.js.
async function write_npm_cjs() {
let { output } = await bundle.generate({
format: "umd",
name: "tinycolor",
});
Deno.writeTextFileSync(
"./npm/cjs/tinycolor.js",
`// This file is autogenerated. It's used to publish CJS to npm.
` + output[0].code
);
const test_template = Deno.readTextFileSync("./npm/cjs/test_template.js");
const test_content = Deno.readTextFileSync("test.js");
Deno.writeTextFileSync(
"./npm/cjs/test.js",
test_template.replace(
"// CONTENT_GOES_HERE",
test_content.substring(
test_content.indexOf("// TEST_BEGINS_HERE"),
test_content.length - 1
)
)
);
}
// Write necessary files for testing & publishing esm to npm.
// The subdirectory sets { "type": "module" } in the package.json,
// and includes a lightweight test runner to ensure it works like mod.js.
async function write_npm_esm() {
let { output } = await bundle.generate({
format: "esm",
});
Deno.writeTextFileSync(
"./npm/esm/tinycolor.js",
`// This file is autogenerated. It's used to publish ESM to npm.
` + output[0].code
);
const test_template = Deno.readTextFileSync("./npm/esm/test_template.js");
const test_content = Deno.readTextFileSync("test.js");
Deno.writeTextFileSync(
"./npm/esm/test.js",
test_template.replace(
"// CONTENT_GOES_HERE",
test_content.substring(
test_content.indexOf("// TEST_BEGINS_HERE"),
test_content.length - 1
)
)
);
}
await write_cdn_umd();
await write_npm_cjs();
await write_npm_esm();
await Deno.copyFile("README.md", "npm/README.md");
await Deno.copyFile("LICENSE", "npm/LICENSE");