Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Restore initial cleanup of development build assets https://github.com/siyuan-note/siyuan/issues/20605

Daniel6630dcc7c8d9
diff --git a/app/scripts/preserve-watch-assets.js b/app/scripts/preserve-watch-assets.js
new file mode 100644
index 0000000000..71d1cf9f94
--- /dev/null
+++ b/app/scripts/preserve-watch-assets.js
@@ -0,0 +1,17 @@
+// Clean the first output normally, then retain assets used by already-open windows.
+class PreserveWatchAssetsPlugin {
+ apply(compiler) {
+ if (compiler.options.mode === "production") return;
+ let emitted = false;
+ compiler.hooks.thisCompilation.tap("PreserveWatchAssetsPlugin", compilation => {
+ compiler.webpack.CleanPlugin.getCompilationHooks(compilation).keep.tap(
+ "PreserveWatchAssetsPlugin", () => emitted ? true : undefined,
+ );
+ });
+ compiler.hooks.afterEmit.tap("PreserveWatchAssetsPlugin", () => {
+ emitted = true;
+ });
+ }
+}
+
+module.exports = PreserveWatchAssetsPlugin;
diff --git a/app/scripts/preserve-watch-assets.test.js b/app/scripts/preserve-watch-assets.test.js
new file mode 100644
index 0000000000..ee280aaa2a
--- /dev/null
+++ b/app/scripts/preserve-watch-assets.test.js
@@ -0,0 +1,60 @@
+const test = require("node:test");
+const assert = require("node:assert/strict");
+const fs = require("node:fs");
+const os = require("node:os");
+const path = require("node:path");
+const webpack = require("webpack");
+const PreserveWatchAssetsPlugin = require("./preserve-watch-assets");
+
+const run = compiler => new Promise((resolve, reject) => {
+ compiler.run((error, stats) => {
+ if (error) reject(error);
+ else if (stats.hasErrors()) reject(new Error(stats.toString()));
+ else resolve(stats.toJson({all: false, assets: true}).assets.map(asset => asset.name));
+ });
+});
+const close = compiler => new Promise((resolve, reject) => compiler.close(error => error ? reject(error) : resolve()));
+
+for (const target of ["config", "desktop", "mobile", "export"]) {
+ for (const mode of ["development", "production"]) {
+ test(`${target} ${mode} cleans on startup and ${mode === "development" ? "retains" : "removes"} previous outputs`, async t => {
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), "siyuan-webpack-clean-"));
+ t.after(() => {
+ assert.equal(path.dirname(path.resolve(root)), path.resolve(os.tmpdir()));
+ fs.rmSync(root, {recursive: true, force: true});
+ });
+ const configuration = require(`../webpack.${target}`)({}, {mode});
+ const output = path.join(root, "output");
+ const entry = path.join(root, "entry.js");
+ fs.mkdirSync(output);
+ fs.writeFileSync(path.join(output, "stale.js"), "old session");
+ fs.writeFileSync(entry, "console.log(1);");
+ const options = {
+ mode, entry, devtool: false,
+ optimization: {minimize: false},
+ output: {path: output, filename: "[contenthash].js", clean: configuration.output.clean},
+ plugins: configuration.plugins.filter(plugin => plugin instanceof PreserveWatchAssetsPlugin),
+ };
+ const compiler = webpack(options);
+ let first;
+ try {
+ [first] = await run(compiler);
+ assert.equal(fs.existsSync(path.join(output, "stale.js")), false);
+ assert.equal(fs.existsSync(path.join(output, first)), true);
+ fs.writeFileSync(entry, "console.log(2);");
+ await run(compiler);
+ assert.equal(fs.existsSync(path.join(output, first)), mode === "development");
+ } finally {
+ await close(compiler);
+ }
+ // A fresh compiler represents restarting pnpm dev, including reused plugin instances.
+ const restarted = webpack(options);
+ try {
+ await run(restarted);
+ assert.equal(fs.existsSync(path.join(output, first)), false);
+ } finally {
+ await close(restarted);
+ }
+ });
+ }
+}
diff --git a/app/webpack.config.js b/app/webpack.config.js
index 3226f1bbb2..2f16a8890a 100644
--- a/app/webpack.config.js
+++ b/app/webpack.config.js
@@ -1,4 +1,5 @@
const path = require("path");
+const PreserveWatchAssetsPlugin = require("./scripts/preserve-watch-assets");
const webpack = require("webpack");
const pkg = require("./package.json");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
@@ -12,8 +13,8 @@ module.exports = (env, argv) => {
devtool: argv.mode !== "production" ? "source-map" : false,
target: "electron-renderer",
output: {
- // 开发期间保留旧资源,避免已打开窗口引用的哈希文件被清除。
- clean: argv.mode === "production",
+ // 首次输出时清理,开发期间的后续输出由插件保留旧资源。
+ clean: true,
globalObject: "globalThis",
publicPath: "auto",
filename: "[name].[chunkhash].js",
@@ -141,6 +142,7 @@ module.exports = (env, argv) => {
],
},
plugins: [
+ new PreserveWatchAssetsPlugin(),
new webpack.DefinePlugin({
SIYUAN_VERSION: JSON.stringify(pkg.version),
NODE_ENV: JSON.stringify(argv.mode),
diff --git a/app/webpack.desktop.js b/app/webpack.desktop.js
index ef13f621df..44c9698582 100644
--- a/app/webpack.desktop.js
+++ b/app/webpack.desktop.js
@@ -1,4 +1,5 @@
const path = require("path");
+const PreserveWatchAssetsPlugin = require("./scripts/preserve-watch-assets");
const webpack = require("webpack");
const pkg = require("./package.json");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
@@ -12,8 +13,8 @@ module.exports = (env, argv) => {
watch: argv.mode !== "production",
devtool: argv.mode !== "production" ? "eval-source-map" : false,
output: {
- // 开发期间保留旧资源,避免已打开窗口引用的哈希文件被清除。
- clean: argv.mode === "production",
+ // 首次输出时清理,开发期间的后续输出由插件保留旧资源。
+ clean: true,
publicPath: "/stage/build/desktop/",
filename: "[name].[chunkhash].js",
path: path.resolve(__dirname, "stage/build/desktop"),
@@ -112,6 +113,7 @@ module.exports = (env, argv) => {
],
},
plugins: [
+ new PreserveWatchAssetsPlugin(),
// new BundleAnalyzerPlugin(),
new webpack.DefinePlugin({
SIYUAN_VERSION: JSON.stringify(pkg.version),
diff --git a/app/webpack.export.js b/app/webpack.export.js
index b77dd93e55..46d8df7287 100644
--- a/app/webpack.export.js
+++ b/app/webpack.export.js
@@ -1,4 +1,5 @@
const path = require("path");
+const PreserveWatchAssetsPlugin = require("./scripts/preserve-watch-assets");
const webpack = require("webpack");
const pkg = require("./package.json");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
@@ -11,8 +12,8 @@ module.exports = (env, argv) => {
watch: argv.mode !== "production",
devtool: argv.mode !== "production" ? "eval-source-map" : false,
output: {
- // 开发期间保留旧资源,避免已打开窗口引用的哈希文件被清除。
- clean: argv.mode === "production",
+ // 首次输出时清理,开发期间的后续输出由插件保留旧资源。
+ clean: true,
publicPath: "auto",
filename: "[name].js",
path: path.resolve(__dirname, "stage/build/export"),
@@ -91,6 +92,7 @@ module.exports = (env, argv) => {
],
},
plugins: [
+ new PreserveWatchAssetsPlugin(),
// new BundleAnalyzerPlugin(),
new webpack.DefinePlugin({
NODE_ENV: JSON.stringify(argv.mode),
diff --git a/app/webpack.mobile.js b/app/webpack.mobile.js
index 6b609a21ac..6c0a589763 100644
--- a/app/webpack.mobile.js
+++ b/app/webpack.mobile.js
@@ -1,4 +1,5 @@
const path = require("path");
+const PreserveWatchAssetsPlugin = require("./scripts/preserve-watch-assets");
const webpack = require("webpack");
const pkg = require("./package.json");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
@@ -12,8 +13,8 @@ module.exports = (env, argv) => {
watch: argv.mode !== "production",
devtool: argv.mode !== "production" ? "eval-source-map" : false,
output: {
- // 开发期间保留旧资源,避免已打开窗口引用的哈希文件被清除。
- clean: argv.mode === "production",
+ // 首次输出时清理,开发期间的后续输出由插件保留旧资源。
+ clean: true,
// 不能使用 auto,否则 ios 导出图片获取不到 css。 https://github.com/siyuan-note/siyuan/issues/8532
publicPath: "/stage/build/mobile/",
filename: "[name].[chunkhash].js",
@@ -114,6 +115,7 @@ module.exports = (env, argv) => {
],
},
plugins: [
+ new PreserveWatchAssetsPlugin(),
// new BundleAnalyzerPlugin(),
new webpack.DefinePlugin({
SIYUAN_VERSION: JSON.stringify(pkg.version),