-
Notifications
You must be signed in to change notification settings - Fork 2
/
rollup.config.js
103 lines (92 loc) · 3.02 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
import { readFileSync } from "fs";
import { join } from "path";
import { cwd } from "process";
import { nodeResolve } from "@rollup/plugin-node-resolve";
import typescript from "@rollup/plugin-typescript";
import terser from "@rollup/plugin-terser";
const OUTPUT_DIR = "dist-js";
/**
* Create a base rollup config
*
* @param {object} [options] Configuration object
* @param {string} [options.input] Input path
* @param {import('rollup').ExternalOption} [options.external] External dependencies list
* @param {import('rollup').RollupOptions | import('rollup').RollupOptions[]} [options.additionalConfigs] Additional rollup configurations
*
* @returns {import('rollup').RollupOptions}
*/
export function createConfig(options = {}) {
const {
input = "guest-js/index.ts",
external = [/^@tauri-apps\/api/],
additionalConfigs = [],
} = options;
// eslint-disable-next-line security/detect-non-literal-fs-filename
const pkg = JSON.parse(readFileSync(join(cwd(), "package.json"), "utf8"));
const pluginJsName = pkg.name.replaceAll("-", (x) => "_");
const iifeVarName = pkg.name.replaceAll("-", (x) => "_");
// const pluginJsName = pkg.name
// .replace("@tauri-apps/plugin-", "")
// .replace(/-./g, (x) => x[1].toUpperCase());
// const iifeVarName = `__TAURI_PLUGIN_${pkg.name
// .replace("@tauri-apps/plugin-", "")
// .replace("-", (x) => "_")
// .toUpperCase()}__`;
const declarationDir = `./${pkg.exports.import.split("/")[0]}`;
/** @type {import('rollup').RollupOptions} */
const config = [
{
input,
output: [
{
file: pkg.exports.import,
format: "esm",
// dir: OUTPUT_DIR
},
{
file: pkg.exports.require,
format: "cjs",
// dir: OUTPUT_DIR
},
],
plugins: [
typescript({
declaration: true,
emitDeclarationOnly: true,
outDir: OUTPUT_DIR
})
],
external: [
...external,
...Object.keys(pkg.dependencies || {}),
...Object.keys(pkg.peerDependencies || {}),
],
onwarn: (warning) => {
throw Object.assign(new Error(), warning);
},
},
{
input,
output: {
format: "iife",
name: iifeVarName,
// IIFE is in the format `var ${iifeVarName} = (() => {})()`
// we check if __TAURI__ exists and inject the API object
banner: "if ('__TAURI__' in window) {",
// the last `}` closes the if in the banner
footer: `Object.defineProperty(window.__TAURI__, '${pluginJsName}', { value: ${iifeVarName} }) }`,
file: "api-iife.js",
},
// and var is not guaranteed to assign to the global `window` object so we make sure to assign it
plugins: [typescript(), terser(), nodeResolve()],
onwarn: (warning) => {
throw Object.assign(new Error(), warning);
},
},
...(Array.isArray(additionalConfigs)
? additionalConfigs
: [additionalConfigs]),
];
return config;
}
export default createConfig();