refactor: simplify rollup config and match @tauri-apps/api output style (#722)

* refactor: simplify rollup config and match `@tauri-apps/api` output style

* add license headers

* update api-iife.js [skip ci]

---------

Co-authored-by: Lucas Nogueira <lucas@tauri.app>
This commit is contained in:
Amr Bashir
2023-11-14 21:09:53 +02:00
committed by GitHub
co-authored by Lucas Nogueira
parent fc62ead565
commit 1b98b85a1f
102 changed files with 491 additions and 777 deletions
+88
View File
@@ -0,0 +1,88 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
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";
/**
* 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;
const pkg = JSON.parse(readFileSync(join(cwd(), "package.json"), "utf8"));
const pluginJsName = pkg.name
.replace("@tauri-apps/plugin-", "")
.replace(/-./g, (x) => x[1].toUpperCase());
const iifeVarName = `__TAURI_PLUGIN_${pluginJsName.toUpperCase()}__`;
return [
{
input,
output: [
{
file: pkg.exports.import,
format: "esm",
},
{
file: pkg.exports.require,
format: "cjs",
},
],
plugins: [
typescript({
declaration: true,
declarationDir: `./${pkg.exports.import.split("/")[0]}`,
}),
],
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: "src/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]),
];
}
-82
View File
@@ -1,82 +0,0 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
import { builtinModules } from "module";
import typescript from "@rollup/plugin-typescript";
import resolve from "@rollup/plugin-node-resolve";
import terser from "@rollup/plugin-terser";
/**
* Create a base rollup config
* @param {Record<string,any>} pkg Imported package.json
* @param {string[]} external Imported package.json
* @returns {import('rollup').RollupOptions}
*/
export function createConfig({ input = "index.ts", pkg, external = [] }) {
const pluginJsName = pkg.name
.replace("@tauri-apps/plugin-", "")
.replace(/-./g, (x) => x[1].toUpperCase());
const iifeVarName = `__TAURI_${pluginJsName.toUpperCase()}__`;
return [
{
input,
external: Object.keys(pkg.dependencies || {})
.concat(Object.keys(pkg.peerDependencies || {}))
.concat(builtinModules)
.concat(external),
onwarn: (warning) => {
throw Object.assign(new Error(), warning);
},
strictDeprecations: true,
output: {
file: pkg.module,
format: "es",
sourcemap: true,
},
plugins: [typescript({ sourceMap: true })],
},
{
input,
onwarn: (warning) => {
throw Object.assign(new Error(), warning);
},
strictDeprecations: true,
output: {
file: pkg.browser,
format: "es",
sourcemap: true,
entryFileNames: "[name].min.js",
},
plugins: [
resolve(),
// terser(),
typescript({ sourceMap: true }),
],
},
{
input,
output: {
file: "src/api-iife.js",
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} }) }`,
},
// and var is not guaranteed to assign to the global `window` object so we make sure to assign it
plugins: [
resolve(),
typescript({
sourceMap: false,
declaration: false,
declarationDir: undefined,
}),
terser(),
],
},
];
}
+2 -9
View File
@@ -6,26 +6,19 @@
"Tauri Programme within The Commons Conservancy"
],
"type": "module",
"browser": "dist-js/index.min.js",
"module": "dist-js/index.mjs",
"types": "dist-js/index.d.ts",
"exports": {
"import": "./dist-js/index.mjs",
"types": "./dist-js/index.d.ts",
"browser": "./dist-js/index.min.js"
"import": "./dist-js/index.js",
"require": "./dist-js/index.cjs"
},
"scripts": {
"build": "rollup -c"
},
"files": [
"dist-js",
"!dist-js/**/*.map",
"README.md",
"LICENSE"
],
"devDependencies": {
"tslib": "2.6.2"
},
"dependencies": {
"@tauri-apps/api": "2.0.0-alpha.11"
}
+7
View File
@@ -0,0 +1,7 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
import { createConfig } from "../../shared/rollup.config.js";
export default createConfig();
-11
View File
@@ -1,11 +0,0 @@
import { readFileSync } from "fs";
import { createConfig } from "../rollup.config.mjs";
export default createConfig({
input: "guest-js/index.ts",
pkg: JSON.parse(
readFileSync(new URL("./package.json", import.meta.url), "utf8"),
),
external: [/^@tauri-apps\/api/],
});