Skip to content

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

3.9 KB
Raw Blame
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");
// const BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
const {EsbuildPlugin} = require("esbuild-loader");

module.exports = (env, argv) => {
    return {
        mode: argv.mode || "development",
        watch: argv.mode !== "production",
        devtool: argv.mode !== "production" ? "eval-source-map" : false,
        output: {
            // 首次输出时清理,开发期间的后续输出由插件保留旧资源。
            clean: true,
            publicPath: "auto",
            filename: "[name].js",
            path: path.resolve(__dirname, "stage/build/export"),
            libraryTarget: "umd",
            library: "Protyle",
            libraryExport: "default",
        },
        entry: {
            "protyle-method": "./src/protyle/method.ts",
        },
        optimization: {
            minimize: true,
            minimizer: [
                new EsbuildPlugin({
                    target: "es6",
                    sourcemap: argv.mode !== "production",
                }),
            ],
        },
        resolve: {
            fallback: {
                "path": require.resolve("path-browserify"),
            },
            extensions: [".ts", ".js", ".scss"],
        },
        module: {
            rules: [
                {
                    test: /task-(canceled|in-progress|unchecked)\.svg$/,
                    type: "asset/inline",
                },
                {
                    test: /\.ts(x?)$/,
                    include: [path.resolve(__dirname, "src")],
                    use: [
                        {
                            loader: "esbuild-loader",
                            options: {
                                target: "es6",
                                sourcemap: argv.mode !== "production",
                            }
                        },
                        {
                            loader: "ifdef-loader",
                            options: {
                                "ifdef-verbose": false,
                                BROWSER: true,
                                // 用于隔离应用界面依赖,不表示导出页的运行平台,见 #5326、#20219。
                                // 导出依赖边界测试同时检查桌面分支,防止渲染逻辑被静默裁剪。
                                MOBILE: true,
                            },
                        },
                    ],
                },
                {
                    test: /\.scss$/,
                    include: [
                        path.resolve(__dirname, "src/assets/scss"),
                    ],
                    use: [
                        MiniCssExtractPlugin.loader,
                        {
                            loader: "css-loader", // translates CSS into CommonJS
                            options: {
                                sourceMap: argv.mode !== "production",
                            },
                        },
                        {
                            loader: "sass-loader", // compiles Sass to CSS
                            options: {
                                sourceMap: argv.mode !== "production",
                            },
                        },
                    ],
                },
            ],
        },
        plugins: [
            new PreserveWatchAssetsPlugin(),
            // new BundleAnalyzerPlugin(),
            new webpack.DefinePlugin({
                NODE_ENV: JSON.stringify(argv.mode),
                SIYUAN_VERSION: JSON.stringify(pkg.version),
            }),
            new MiniCssExtractPlugin({
                filename: "base.css",
            }),
        ],
    };
};