-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
132 lines (117 loc) · 3.17 KB
/
rollup.config.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
import terser from "@rollup/plugin-terser";
import stripCode from "rollup-plugin-strip-code";
import { wasm } from "@rollup/plugin-wasm";
import {spawn} from "child_process";
import {zip} from "compressing";
import * as fs from "fs";
import * as path from "path";
import * as pkg from "./package.json";
let DEBUG = true;
let EXTERNALS = [ "node:fs/promises" ];
spawn( process.execPath, ["./scripts/entities.js"] );
export default args =>
{
DEBUG = !!args.configDebug;
const debugStripper = stripCode( {
start_comment: "@START_DEBUG",
end_comment: "@END_DEBUG"
} ),
unitTestStripper = stripCode( {
start_comment: "@START_UNIT_TESTS",
end_comment: "@END_UNIT_TESTS"
} ),
browserStripper = stripCode( {
start_comment: "@START_BROWSER_ONLY",
end_comment: "@END_BROWSER_ONLY"
} ),
wasmPlugin = wasm({
sync: [ "node_modules/squoosh/codecs/resize/pkg/squoosh_resize_bg.wasm" ]
}),
modulePlugins = [debugStripper, browserStripper, wasmPlugin],
iifePlugins = [debugStripper, unitTestStripper, wasmPlugin],
output = [
{
onwarn,
input: "src/document.js",
plugins: modulePlugins,
external: EXTERNALS,
output: [
module( "esm" ),
//module( "cjs" )
]
},
// {
// onwarn,
// input: "src/document.js",
// plugins: iifePlugins,
// output: module( "iife" )
// }
];
if ( !DEBUG )
{
modulePlugins.push( unitTestStripper, terser( {module: true} ), zipFile() );
iifePlugins.push( terser( {safari10: true} ), zipFile() );
}
else
{
output.push( {
onwarn,
input: "src/document.js",
plugins: [browserStripper, wasmPlugin],
external: EXTERNALS,
output: module( "esm", "tests." )
} );
iifePlugins.push( terser( {compress: false, mangle: false, output: {beautify: true}, safari10: true} ) );
}
return output;
}
function onwarn( warning )
{
if ( warning.code !== "CIRCULAR_DEPENDENCY" ||
!(warning.message.endsWith( "src/node.js -> src/document.js" ) || warning.message.endsWith( "src\\node.js -> src\\document.js" ) ||
warning.message.endsWith( "src/node.js -> src/html-parser.js" ) || warning.message.endsWith( "src\\node.js -> src\\html-parser.js" ) ||
warning.message.endsWith( "src/selectors.js -> src/node.js" ) || warning.message.endsWith( "src\\selectors.js -> src\\node.js" ) ||
warning.message.includes( "js-canvas" )
)
)
console.warn( warning );
}
const fileExts = {
"esm": "mjs",
"cjs": "cjs"
};
function module( format, ext = "" )
{
const output = {
file: `lib/${pkg.name}.${ext}`,
format,
interop: false,
esModule: false,
freeze: false,
exports: "default",
sourcemap: (DEBUG && format === "cjs")
};
output.file += fileExts[format] || "js";
if ( format === "iife" )
output.name = "DOM";
return output
}
function zipFile()
{
return {
name: "Zip",
renderChunk( src, chunk, options )
{
const ext = path.extname( options.file ),
fileName = path.basename( options.file, ext ),
dirName = path.dirname( options.file );
if ( ext === ".js" || ext === ".mjs" )
zip.compressFile(
Buffer.from( src ),
path.join( dirName, fileName + (ext === ".mjs" ? ".module" : "") +".zip" ),
{relativePath: fileName +".js"}
);
return null;
}
};
}