2023-03-15 21:52:13 +00:00
|
|
|
// SPDX-FileCopyrightText: 2022 The CC: Tweaked Developers
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2020-11-12 19:01:50 +00:00
|
|
|
import path from "path";
|
|
|
|
|
2023-10-03 08:19:19 +00:00
|
|
|
import resolve from "@rollup/plugin-node-resolve";
|
2023-09-20 21:09:58 +00:00
|
|
|
import typescript from "@rollup/plugin-typescript";
|
|
|
|
import url from "@rollup/plugin-url";
|
2023-10-08 12:14:02 +00:00
|
|
|
import { minify as minifyJavascript } from "@swc/core";
|
|
|
|
import { transform as transformCss } from "lightningcss";
|
2020-11-12 19:01:50 +00:00
|
|
|
|
2023-10-08 12:14:02 +00:00
|
|
|
const inputDir = "src/frontend";
|
2020-11-12 19:01:50 +00:00
|
|
|
|
2023-10-08 12:14:02 +00:00
|
|
|
/**
|
|
|
|
* Generate an ESM module from a list of {@code ligntingcss} exports.
|
|
|
|
*
|
|
|
|
* @param {string} filename The input file name.
|
|
|
|
* @param {string} input The input code.
|
|
|
|
* @param {boolean} minify Whether to minify the css.
|
|
|
|
* @returns {string} The ESM module, containing the list of exports.
|
|
|
|
*/
|
|
|
|
const compileCss = (filename, input, minify) => {
|
|
|
|
const { code, exports } = transformCss({
|
|
|
|
filename,
|
|
|
|
cssModules: filename.endsWith(".module.css"),
|
|
|
|
pattern: "[local]-[hash]",
|
|
|
|
code: Buffer.from(input),
|
|
|
|
minify,
|
|
|
|
});
|
|
|
|
|
|
|
|
let output = "";
|
|
|
|
let importId = 0;
|
|
|
|
for (const [name, symbol] of Object.entries(exports ?? {})) {
|
|
|
|
let className = JSON.stringify(symbol.name);
|
|
|
|
for (const dep of symbol.composes) {
|
|
|
|
if (dep.type == "dependency") {
|
|
|
|
output += `import { ${dep.name} as import_${++importId}$ } from ${JSON.stringify(dep.specifier)};\n`;
|
|
|
|
className += `+ " " + import_${importId}$`;
|
|
|
|
} else {
|
|
|
|
className += `+ " " + ${JSON.stringify(dep.name)}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
output += `export const ${name} = ${className};\n`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return { js: output, css: new TextDecoder().decode(code) };
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Custom plugin for CC: Tweaked specific build logic.
|
|
|
|
*
|
|
|
|
* This handles:
|
|
|
|
* - Minifying JS using swc, which is faster than using terser.
|
|
|
|
* - Importing CSS files.
|
|
|
|
* - Importing plain text files from our "mount/" directory.
|
|
|
|
* - Resoving our TeaVM compiled classes and resources files.
|
|
|
|
*
|
|
|
|
* @param {boolean} minify Whether to minify our sources.
|
|
|
|
* @returns {@type import("rollup").Plugin} Our plugin.
|
|
|
|
*/
|
|
|
|
const ccTweaked = minify => {
|
|
|
|
let cssChunks = [];
|
|
|
|
return {
|
|
|
|
name: "cc-tweaked",
|
|
|
|
|
|
|
|
buildStart() {
|
|
|
|
cssChunks = [];
|
|
|
|
},
|
|
|
|
|
|
|
|
async renderChunk(code) {
|
|
|
|
// Use swc to minify our Javascript.
|
|
|
|
return minify ? (await minifyJavascript(code, { module: true })).code : code;
|
|
|
|
},
|
|
|
|
|
|
|
|
async transform(code, file) {
|
|
|
|
const ext = path.extname(file);
|
|
|
|
if (ext === ".css") {
|
|
|
|
// Compile our CSS file, emitting a JS module, and saving the CSS for later.
|
|
|
|
const { js, css } = compileCss(file, code, minify);
|
|
|
|
cssChunks.push(css);
|
|
|
|
return js;
|
|
|
|
} else if (ext != ".dfpwm" && path.dirname(file) === path.resolve(`${inputDir}/mount`)) {
|
|
|
|
// Bundle all mount files aside from our dfpwm.
|
|
|
|
return `export default ${JSON.stringify(code)};\n`
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
async resolveId(source) {
|
|
|
|
if (source === "cct/classes") return path.resolve("build/teaVM/classes.js");
|
|
|
|
if (source === "cct/resources") return path.resolve("build/teaVM/resources.js");
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
generateBundle() {
|
|
|
|
this.emitFile({
|
|
|
|
type: 'asset',
|
|
|
|
fileName: "index.css",
|
|
|
|
source: cssChunks.join(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2023-10-03 08:19:19 +00:00
|
|
|
|
|
|
|
/** @type import("rollup").RollupOptionsFunction */
|
|
|
|
export default args => ({
|
2023-10-08 12:14:02 +00:00
|
|
|
input: [`${inputDir}/index.tsx`],
|
2020-11-12 19:01:50 +00:00
|
|
|
output: {
|
2022-11-06 13:37:07 +00:00
|
|
|
// Also defined in build.gradle.kts
|
2021-12-21 22:20:45 +00:00
|
|
|
dir: "build/rollup/",
|
2022-11-06 13:37:07 +00:00
|
|
|
|
2023-10-03 08:19:19 +00:00
|
|
|
format: "esm",
|
2023-03-14 18:41:31 +00:00
|
|
|
generatedCode: {
|
|
|
|
preset: "es2015",
|
|
|
|
constBindings: true,
|
|
|
|
},
|
2020-11-12 19:01:50 +00:00
|
|
|
},
|
|
|
|
context: "window",
|
|
|
|
|
|
|
|
plugins: [
|
|
|
|
typescript(),
|
2023-10-03 08:19:19 +00:00
|
|
|
resolve({ browser: true }),
|
2020-11-12 19:01:50 +00:00
|
|
|
|
2021-12-21 22:20:45 +00:00
|
|
|
url({
|
2023-10-03 08:19:19 +00:00
|
|
|
include: ["**/*.dfpwm", "**/*.worker.js", "**/*.png"],
|
2021-12-21 22:20:45 +00:00
|
|
|
fileName: "[name]-[hash][extname]",
|
|
|
|
publicPath: "/",
|
2023-10-03 08:19:19 +00:00
|
|
|
limit: 0,
|
|
|
|
}),
|
|
|
|
|
2023-10-08 12:14:02 +00:00
|
|
|
ccTweaked(!args.configDebug),
|
2020-11-12 19:01:50 +00:00
|
|
|
],
|
2023-10-03 08:19:19 +00:00
|
|
|
});
|