Repository navigation
Expand file tree
/
Copy pathrollup.lite.config.js
More file actions
162 lines (153 loc) · 7.62 KB
/
Copy pathrollup.lite.config.js
File metadata and controls
162 lines (153 loc) · 7.62 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
157
158
159
160
161
162
// FEAScript lite: a small build with no dependencies, for pages that embed a model (course pages, explainers,
// games, offline tools). It has the full API, for every model that runs on the frontal solver: heat conduction, the
// general-form PDE and front propagation, with the frontal (default) and Jacobi solvers. Creeping flow and the
// Euler-Bernoulli beam need the LU solver (Jacobi diverges on the beam), so they are left out with it.
// Left out, each replaced by a stub that throws a FEAScriptError naming what to use instead:
// - the mathjs LU solver ("lusolve"), and with it mathjs
// - the WebGPU Jacobi solver ("jacobi-gpu", Taichi.js) and the web worker (FEAScriptWorker)
// - the VTK.js plots (plotSolution and plotInterpolatedSolution with Plotly stay)
// The files carry no "http://" or "https://" link, so pages with link checks (such as kidvo.eu games) can ship them
// as they are; the build fails if one is left.
// Run: npm run build:lite (npm run build runs it too)
import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import { minify } from "terser";
import { readFileSync } from "node:fs";
const { version } = JSON.parse(readFileSync(new URL("./package.json", import.meta.url), "utf8"));
const maxSize = 160 * 1024; // Bytes. The lite files are about 144 KB; a much larger file means a dependency got in
const unsupported = (what, instead) =>
`throw new FEAScriptError("UNSUPPORTED_SOLVER", ${JSON.stringify(
`The lite build of FEAScript has no ${what}. ${instead}`,
)});`;
const fullBuild = "Use the full build (feascript instead of feascript/lite) for it.";
// Source modules left out of lite, with the module that replaces each (keyed by the path under src/)
const stubs = {
"solvers/mathjsLUSolver.js": `
import { FEAScriptError } from "../utilities/errors.js";
export function mathjsLUSolver() {
${unsupported('"lusolve" solver', `Use "frontal" or "jacobi", or the full build for "lusolve".`)}
}`,
"solvers/webgpuAvailability.js": `
import { FEAScriptError } from "../utilities/errors.js";
export async function assertWebGPUAvailable() {
${unsupported('"jacobi-gpu" solver', `Use "frontal" or "jacobi", or the full build for "jacobi-gpu".`)}
}`,
"solvers/webgpuJacobiSolver.js": `
import { FEAScriptError } from "../utilities/errors.js";
export class WebGPUComputeEngine {
constructor() { ${unsupported('"jacobi-gpu" solver', fullBuild)} }
}`,
"workers/worker.js": `
import { FEAScriptError } from "../utilities/errors.js";
export class FEAScriptWorker {
constructor() { ${unsupported("web worker (FEAScriptWorker)", fullBuild)} }
}`,
"visualization/vtkPlot.js": `
import { FEAScriptError } from "../utilities/errors.js";
const noVtk = () => { ${unsupported("VTK.js plots", `Use plotSolution (Plotly), or the full build. `)} };
export const plotSolutionVtk = noVtk, plotInterpolatedSolutionVtk = noVtk, createColorScale = noVtk,
createContourLineOptions = noVtk, transformSolverOutputToVtkData = noVtk, transformSolverOutputToVTP = noVtk,
transformSolverOutputToMLBuffers = noVtk;`,
};
// Changes to the sources for lite, per file under src/. Each must be found, so a change in the source stops the
// build here
const liteHint =
" The lite build of FEAScript has the frontal and jacobi solvers only; the full build has them all.";
const modelPatches = [
// The default solver: lusolve is not in lite
[
'this.solverMethod = "lusolve"; // Default solver method',
'this.solverMethod = "frontal"; // Default solver method',
],
// The solvers each model can use in lite, so a model or solver that lite leaves out fails before the mesh is built
[' heatConduction: ["lusolve", "jacobi", "frontal"],', ' heatConduction: ["jacobi", "frontal"],'],
[' generalFormPDE: ["lusolve", "jacobi", "frontal"],', ' generalFormPDE: ["jacobi", "frontal"],'],
[' frontPropagation: ["lusolve", "jacobi", "frontal"],', ' frontPropagation: ["jacobi", "frontal"],'],
[
' creepingFlow: ["lusolve"],\n eulerBernoulliBeam: ["lusolve", "jacobi"],',
" creepingFlow: [],\n eulerBernoulliBeam: [],",
],
// The error for those says why
[" }${solverConfig}.${asyncHint}`,", " }${solverConfig}.${asyncHint}" + liteHint + "`,"],
];
// The WebGPU compute engine: lite refuses "jacobi-gpu" in assertWebGPUAvailable, the line before, so the worker
// that would load src/workers/webgpuWorker.js is dead code. Removing it keeps every worker and "../" path out of
// lite, which pages that check their files for them (kidvo.eu) refuse
const solverPatches = [
[
/\n const base = import\.meta\.url;\n[\s\S]*?\n return \{ computeEngine, worker \};\n/,
"\n return { computeEngine: null, worker: null };\n",
],
];
const patches = { "FEAScript.js": modelPatches, "solvers/linearSystemSolver.js": solverPatches };
const liteSources = {
name: "feascript-lite-sources",
load(id) {
const path = id.replace(/\\/g, "/");
const key = Object.keys(stubs).find((k) => path.endsWith("/src/" + k));
return key ? stubs[key] : null;
},
transform(code, id) {
const path = id.replace(/\\/g, "/");
const key = Object.keys(patches).find((k) => path.endsWith("/src/" + k));
if (!key) return null;
for (const [from, to] of patches[key]) {
const found = typeof from === "string" ? code.includes(from) : from.test(code);
if (!found) this.error(`${key} has changed; review the lite patch: ${from}`);
code = code.replace(from, to);
}
return { code, map: null };
},
};
const banner =
`/*! FEAScript ${version} lite (frontal and Jacobi solvers, no dependencies) | feascript.com | MIT License` +
` (c) 2023-2026 FEAScript. Includes @stdlib code under the Apache-2.0 License. */`;
// Minifies each file, keeps each license header once (the @stdlib Apache-2.0 header is repeated in every module,
// about 20 KB in all), and writes every link without its scheme
const liteOutput = {
name: "feascript-lite-output",
async renderChunk(code, chunk, options) {
const seen = new Set();
const keepLicenseOnce = (_node, comment) => {
if (!/@license|Apache/i.test(comment.value) || seen.has(comment.value)) return false;
seen.add(comment.value);
return true;
};
const minified = await minify(code, {
module: options.format === "es",
compress: { passes: 2 },
mangle: true,
format: { comments: keepLicenseOnce },
});
return { code: `${banner}\n${minified.code.replace(/https?:\/\//g, "")}\n`, map: null };
},
generateBundle(_options, bundle) {
for (const file of Object.values(bundle)) {
if (file.type !== "chunk") continue;
const checks = [
[/https?:\/\//, "a link with http:// or https://"],
[/csAmd|typed-function/, "mathjs code"],
[/ti\.init|wgpuSupport/, "Taichi.js code"],
[/\bimport\s*\(/, "a dynamic import"],
[/\bnew Worker\b/, "a web worker"],
[/\.\.\//, 'a "../" path'],
];
for (const [pattern, what] of checks) {
if (pattern.test(file.code)) this.error(`${file.fileName} contains ${what}`);
}
const size = Buffer.byteLength(file.code);
if (size > maxSize) this.error(`${file.fileName} is ${size} bytes, over the lite limit of ${maxSize}`);
}
},
};
export default {
input: "src/index.js",
output: [
{ file: "dist/feascript-lite.esm.min.js", format: "esm" },
{ file: "dist/feascript-lite.umd.min.js", format: "umd", name: "FEAScript" },
],
plugins: [liteSources, resolve({ browser: true, preferBuiltins: false }), commonjs(), liteOutput],
onwarn(warning, warn) {
if (warning.code !== "CIRCULAR_DEPENDENCY") warn(warning);
},
};