Files
deerflow-code/frontend-web/rspack.firefox52.config.mjs
T

336 lines
9.7 KiB
JavaScript

import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import {
CopyRspackPlugin,
DefinePlugin,
HtmlRspackPlugin,
LightningCssMinimizerRspackPlugin,
SwcJsMinimizerRspackPlugin,
} from "@rspack/core";
import dotenv from "dotenv";
const rootDir = path.dirname(fileURLToPath(import.meta.url));
const firefox52Target = { firefox: "52" };
function loadViteEnvironment(mode) {
const values = {};
const files = [
".env",
`.env.${mode}`,
".env.local",
`.env.${mode}.local`,
];
for (const filename of files) {
const fullPath = path.join(rootDir, filename);
if (!fs.existsSync(fullPath)) continue;
Object.assign(values, dotenv.parse(fs.readFileSync(fullPath)));
}
for (const [key, value] of Object.entries(process.env)) {
if (key.startsWith("VITE_") && typeof value === "string") {
values[key] = value;
}
}
return {
...Object.fromEntries(
Object.entries(values).filter(([key]) => key.startsWith("VITE_")),
),
BASE_URL: process.env.VITE_ASSET_BASE ?? "./",
MODE: mode,
DEV: mode === "development",
PROD: mode === "production",
SSR: false,
};
}
function cssLoaders(postcssConfig) {
const loaders = [
{
loader: "builtin:lightningcss-loader",
options: {
targets: firefox52Target,
include: {
logicalProperties: true,
},
minify: false,
},
},
];
if (postcssConfig) {
loaders.push({
loader: "postcss-loader",
options: {
implementation: "postcss",
postcssOptions: {
config: path.join(rootDir, "build", "firefox52", postcssConfig),
},
},
});
}
return loaders;
}
function swcLoader(isDevelopment) {
return {
loader: "builtin:swc-loader",
options: {
detectSyntax: "auto",
isModule: "unknown",
env: {
targets: firefox52Target,
bugfixes: true,
shippedProposals: true,
},
jsc: {
externalHelpers: false,
transform: {
react: {
runtime: "automatic",
development: isDevelopment,
refresh: false,
},
},
},
},
};
}
export default (_env = {}, argv = {}) => {
const mode = argv.mode === "development" ? "development" : "production";
const isDevelopment = mode === "development";
const isFastBuild = _env.fast === true || _env.fast === "true";
const isPreview = _env.preview === true || _env.preview === "true";
const assetBase = isDevelopment || isPreview
? "/"
: process.env.VITE_ASSET_BASE ?? "./";
const buildEnv = loadViteEnvironment(mode);
return {
name: "firefox52",
mode,
context: rootDir,
target: ["web", "es5"],
entry: {
app: path.join(rootDir, "src", "main.tsx"),
},
output: {
path: path.join(rootDir, "dist-firefox52"),
publicPath: assetBase,
filename: isDevelopment ? "assets/[name].js" : "assets/[name].[contenthash:8].js",
chunkFilename: isDevelopment
? "assets/[name].chunk.js"
: "assets/[name].[contenthash:8].chunk.js",
cssFilename: isDevelopment
? "assets/[name].css"
: "assets/[name].[contenthash:8].css",
cssChunkFilename: isDevelopment
? "assets/[name].chunk.css"
: "assets/[name].[contenthash:8].chunk.css",
clean: true,
module: false,
environment: {
arrowFunction: false,
asyncFunction: false,
bigIntLiteral: false,
const: false,
destructuring: false,
dynamicImport: false,
module: false,
optionalChaining: false,
templateLiteral: false,
},
},
cache: {
type: "persistent",
name: `firefox52-${mode}`,
version: "2-tailwind3",
buildDependencies: [
fileURLToPath(import.meta.url),
path.join(rootDir, "package.json"),
path.join(rootDir, "postcss.config.js"),
path.join(rootDir, "tailwind.config.ts"),
path.join(rootDir, "scripts", "postcss-dark-descendant.mjs"),
path.join(rootDir, "scripts", "postcss-firefox52-rgb-vars.mjs"),
path.join(rootDir, "scripts", "postcss-firefox52-where.mjs"),
path.join(rootDir, "build", "firefox52", "source-compat-loader.cjs"),
path.join(rootDir, "build", "firefox52", "legacy-generator-loader.cjs"),
path.join(rootDir, "build", "firefox52", "postcss-tailwind.config.mjs"),
path.join(rootDir, "build", "firefox52", "postcss-compat.config.mjs"),
],
storage: {
type: "filesystem",
directory: path.join(rootDir, ".rspack-cache"),
},
},
experiments: {
css: true,
asyncWebAssembly: true,
},
devtool: isDevelopment ? "cheap-module-source-map" : false,
module: {
rules: [
{
enforce: "pre",
test(resourcePath) {
const id = resourcePath.replaceAll("\\", "/");
return (
/\/p-queue\/dist\/index\.js$/.test(id) ||
/\/mdast-util-gfm-autolink-literal\/lib\/index\.js$/.test(id) ||
id.includes("/oniguruma-parser/") ||
id.includes("/node_modules/mermaid/dist/chunks/") ||
id.includes("/node_modules/web-streams-polyfill/dist/") ||
id.includes("/node_modules/@xyflow/react/") ||
id.includes("/node_modules/react-router/") ||
id.includes("/node_modules/zod/") ||
id.includes("/node_modules/langsmith/")
);
},
use: path.join(rootDir, "build", "firefox52", "source-compat-loader.cjs"),
},
{
oneOf: [
{
resourceQuery: /raw/,
type: "asset/source",
},
{
test(resourcePath) {
const id = resourcePath.replaceAll("\\", "/");
return (
id.includes("/node_modules/@langchain/core/") ||
id.includes("/node_modules/@langchain/langgraph-sdk/") ||
id.includes("/node_modules/langsmith/")
);
},
// Loaders run right-to-left: normalize modules that declare or
// consume async iterators first, then let the fast built-in SWC
// pass handle the complete package consistently.
use: [
swcLoader(isDevelopment),
path.join(rootDir, "build", "firefox52", "legacy-generator-loader.cjs"),
],
},
{
test: /\.(?:js|mjs|cjs|jsx|ts|tsx)$/,
exclude: [
/node_modules[\\/]core-js[\\/]/,
/node_modules[\\/]regenerator-runtime[\\/]/,
/node_modules[\\/]web-streams-polyfill[\\/]/,
],
use: swcLoader(isDevelopment),
},
{
test: /[\\/]src[\\/]styles[\\/]globals\.css$/,
type: "css",
use: cssLoaders("postcss-tailwind.config.mjs"),
},
{
test: /[\\/]src[\\/].*\.css$/,
type: "css",
use: cssLoaders("postcss-compat.config.mjs"),
},
{
test: /\.css$/,
type: "css",
use: cssLoaders(null),
},
{
test: /\.wasm$/,
type: "webassembly/async",
},
{
test: /\.(?:png|jpe?g|gif|webp|avif|svg|ico|woff2?|ttf|otf|eot)$/i,
type: "asset/resource",
generator: {
filename: "assets/[name].[contenthash:8][ext]",
},
},
],
},
],
},
resolve: {
extensions: [".tsx", ".ts", ".jsx", ".js", ".mjs", ".cjs", ".json"],
alias: {
"highlight.js/lib/languages/sql_more$": "highlight.js/lib/languages/sql",
"@/env$": path.join(rootDir, "src", "env.ts"),
"next/navigation$": path.join(rootDir, "src", "shims", "next-navigation.ts"),
"next/link$": path.join(rootDir, "src", "shims", "next-link.tsx"),
"@vite": path.join(rootDir, "src"),
"@": path.join(rootDir, "src"),
},
},
plugins: [
new DefinePlugin({
"import.meta.env": JSON.stringify(buildEnv),
__FIREFOX52_BUILD__: "true",
}),
new HtmlRspackPlugin({
template: path.join(rootDir, "build", "firefox52", "index.html"),
filename: "index.html",
inject: "body",
scriptLoading: "blocking",
minify: false,
}),
new CopyRspackPlugin({
patterns: [
{
from: path.join(rootDir, "public"),
to: ".",
noErrorOnMissing: true,
},
],
}),
],
optimization: {
minimize: !isDevelopment && !isFastBuild,
minimizer: [
new SwcJsMinimizerRspackPlugin({
minimizerOptions: {
ecma: 5,
},
}),
new LightningCssMinimizerRspackPlugin({
minimizerOptions: {
targets: firefox52Target,
include: {
logicalProperties: true,
},
},
}),
],
},
devServer: {
host: "0.0.0.0",
port: Number(process.env.FIREFOX52_PORT || 5174),
hot: false,
liveReload: true,
historyApiFallback: true,
headers: {
"Access-Control-Allow-Origin": "*",
},
static: {
directory: isPreview
? path.join(rootDir, "dist-firefox52")
: path.join(rootDir, "public"),
watch: !isPreview,
},
devMiddleware: {
publicPath: "/",
writeToDisk: isPreview,
},
client: {
overlay: true,
},
},
stats: "errors-warnings",
performance: false,
};
};