-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrollup.config.mjs
More file actions
156 lines (146 loc) · 4.48 KB
/
rollup.config.mjs
File metadata and controls
156 lines (146 loc) · 4.48 KB
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
import fs from "fs";
import typescript from "@rollup/plugin-typescript";
import terser from "@rollup/plugin-terser";
// import replace from "@rollup/plugin-replace";
import { nodeResolve } from "@rollup/plugin-node-resolve";
import { dts } from "rollup-plugin-dts";
const pkg = JSON.parse(await fs.promises.readFile("./package.json"));
const version = pkg.version;
// Check build mode from environment variable
const isDev = process.env.BUILD === "development";
const enableSourceMaps = isDev; // Enable source maps in dev mode
fs.rmSync("dist", { recursive: true, force: true });
const strProduct = "Dynamsoft Document Scanner JS Edition Bundle";
const terser_format = {
// this func is run by eval in worker, so can't use variable outside
comments: function (node, comment) {
const text = comment.value;
const type = comment.type;
if (type == "comment2") {
// multiline comment
const strProduct = "Dynamsoft Document Scanner JS Edition Bundle";
const regDyComment = new RegExp(String.raw`@product\s${strProduct}`, "i");
return regDyComment.test(text);
}
},
};
const banner = `/*!
* Dynamsoft Document Scanner JavaScript Library
* @product ${strProduct}
* @website http://www.dynamsoft.com
* @copyright Copyright ${new Date().getUTCFullYear()}, Dynamsoft Corporation
* @author Dynamsoft
* @version ${version}
* @fileoverview Dynamsoft Document Scanner (DDS) is a ready-to-use SDK for capturing and enhancing document images with automatic border detection, correction, and customizable workflows. Uses Dynamsoft Capture Vision Bundle v3.2.2000.
* More info on DDS JS: https://www.dynamsoft.com/capture-vision/docs/web/programming/javascript/
*/`;
const plugin_terser_es6 = terser({ ecma: 6, format: terser_format });
const plugin_terser_es5 = terser({ ecma: 5, format: terser_format });
const DCV_CONFIG_PATH = `src/dcv-config`;
const BUNDLE_BUILD_PATH = `src/build`;
const TYPES_PATH = `dist/types/build`;
const copyFiles = () => ({
name: "copy-files",
writeBundle() {
fs.copyFileSync(`${DCV_CONFIG_PATH}/document-scanner.ui.xml`, "dist/document-scanner.ui.xml");
},
});
export default [
// 1. UMD bundle
{
input: `${BUNDLE_BUILD_PATH}/dds.bundle.ts`,
plugins: [
nodeResolve({ browser: true }),
typescript({
tsconfig: "./tsconfig.json",
declaration: true,
sourceMap: enableSourceMaps,
}),
plugin_terser_es5,
copyFiles(),
{
writeBundle(options, bundle) {
let txt = fs
.readFileSync("dist/dds.bundle.js", { encoding: "utf8" })
.replace(/Dynamsoft=\{\}/, "Dynamsoft=t.Dynamsoft||{}");
fs.writeFileSync("dist/dds.bundle.js", txt);
},
},
],
output: [
{
file: "dist/dds.bundle.js",
format: "umd",
name: "Dynamsoft",
banner: banner,
exports: "named",
sourcemap: enableSourceMaps,
},
],
},
// 2. ESM bundle
{
input: `${BUNDLE_BUILD_PATH}/dds.bundle.esm.ts`,
plugins: [
nodeResolve({ browser: true }),
typescript({
tsconfig: "./tsconfig.json",
declaration: true,
sourceMap: enableSourceMaps,
}),
plugin_terser_es6,
],
output: [
{
file: "dist/dds.bundle.esm.js",
format: "es",
banner: banner,
exports: "named",
sourcemap: enableSourceMaps,
},
],
},
// 3. ESM bundle as .mjs
{
input: `${BUNDLE_BUILD_PATH}/dds.bundle.esm.ts`,
plugins: [
nodeResolve({ browser: true }),
typescript({
tsconfig: "./tsconfig.json",
sourceMap: enableSourceMaps,
}),
plugin_terser_es6,
],
output: [
{
file: "dist/dds.bundle.mjs",
format: "es",
banner: banner,
exports: "named",
sourcemap: enableSourceMaps,
},
],
},
// 4. Single type declarations file
{
input: `${TYPES_PATH}/dds.bundle.esm.d.ts`,
plugins: [
dts({ respectExternal:true }),
{
writeBundle(options, bundle) {
fs.rmSync("dist/types", { recursive: true, force: true });
// change `export { type A }` to `export { A }`,
// so project use old typescript still works.
let txt = fs.readFileSync("dist/dds.bundle.d.ts", { encoding: "utf8" }).replace(/([{,]) type /g, "$1 ");
fs.writeFileSync("dist/dds.bundle.d.ts", txt);
},
},
],
output: [
{
file: "dist/dds.bundle.d.ts",
format: "es",
},
],
},
];