TidGi-Desktop/webpack.plugins.js
lin onetwo f44620338a chore: try to imporove dev start speed but no sig improvment
Replaces 'pnpm start' with 'pnpm start:init' for initial setup and documents slow startup in development. Adds a debug Webpack script, disables polling in renderer file watching, and only enables CircularDependencyPlugin in production/CI for faster dev builds. WebpackBar now only shows when DEBUG includes 'electron-forge:*'. ForkTsCheckerWebpackPlugin is now configured for async checks with memory limits and test file exclusion. Updates documentation to reflect these changes.
2025-10-05 18:53:02 +08:00

116 lines
3.8 KiB
JavaScript

/* eslint-disable @typescript-eslint/no-require-imports */
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const CircularDependencyPlugin = require('circular-dependency-plugin');
const _ = require('lodash');
const CopyPlugin = require('copy-webpack-plugin');
const ThreadsPlugin = require('threads-plugin');
const ExternalsPlugin = require('webpack5-externals-plugin');
const WebpackBar = require('webpackbar');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const { isDevelopmentOrTest } = require('./webpack.rules');
exports.main = _.compact([
// we only need one instance of TsChecker, it will check main and renderer all together
new ForkTsCheckerWebpackPlugin({
async: true,
typescript: {
memoryLimit: 4096,
},
issue: {
exclude: [
{ file: '**/node_modules/**' },
{ file: '**/*.test.ts' },
{ file: '**/*.test.tsx' },
],
},
}),
new CopyPlugin({
// to is relative to ./.webpack/main/
patterns: [{ from: 'localization', to: 'localization' }],
}),
// CircularDependencyPlugin is slow in dev, only enable in production/CI
isDevelopmentOrTest
? undefined
: new CircularDependencyPlugin({
// exclude detection of files based on a RegExp
exclude: /node_modules/,
// add errors to webpack instead of warnings
failOnError: true,
// allow import cycles that include an asyncronous import,
// e.g. via import(/* webpackMode: "weak" */ './file.js')
allowAsyncCycles: true,
// set the current working directory for displaying module paths
cwd: process.cwd(),
}),
isDevelopmentOrTest
? undefined
: new ExternalsPlugin({
type: 'commonjs',
// use regex works.
// include: /@tiddlygit\+tiddlywiki@(.+)|dugite(.+)/,
include: /tiddlywiki(.+)|dugite(.+)/,
// when using npm, we can use this. But with pnpm, this won't work ↓
// include: path.join(__dirname, 'node_modules', '.pnpm', '@tiddlygit', 'tiddlywiki'),
}),
(process.platform === 'win32' || isDevelopmentOrTest)
? undefined
: new ExternalsPlugin({
type: 'commonjs',
include: /registry-js(.+)/,
}),
new ThreadsPlugin({
target: 'electron-node-worker',
plugins: isDevelopmentOrTest
? []
: ['ExternalsPlugin'],
}),
// WebpackBar progress bar need `DEBUG=electron-forge:*` to work.
isDevelopmentOrTest && String(process.env.DEBUG || '').includes('electron-forge:*') ? new WebpackBar() : undefined,
isDevelopmentOrTest
? undefined
: new BundleAnalyzerPlugin({
generateStatsFile: true,
analyzerMode: 'disabled',
statsFilename: '../../out/webpack-stats-main.json',
}),
]);
exports.renderer = _.compact([
// new CspHtmlWebpackPlugin(
// {
// 'base-uri': ["'self'"],
// 'object-src': ["'none'"],
// 'script-src': ["'self' 'unsafe-eval'"],
// 'style-src': ["'self' 'unsafe-inline'"],
// 'frame-src': ["'none'"],
// 'worker-src': ["'none'"],
// 'connect-src': ['https://api.github.com https://tidgi-desktop.authing.cn ws://localhost:3012'],
// },
// {
// nonceEnabled: {
// 'style-src': false,
// },
// },
// ),
isDevelopmentOrTest
? undefined
: new BundleAnalyzerPlugin({
generateStatsFile: true,
analyzerMode: 'disabled',
statsFilename: '../../out/webpack-stats-renderer.json',
}),
// WebpackBar progress bar need `DEBUG=electron-forge:*` to work.
isDevelopmentOrTest && String(process.env.DEBUG || '').includes('electron-forge:*') ? new WebpackBar() : undefined,
// Example: copy files for webWorker to use
// new CopyPlugin({
// patterns: [
// // similar to noflo-ui's webpack.config.js
// {
// from: 'node_modules/klayjs/klay.js',
// to: 'webWorkers/klayjs/klay.js',
// },
// ],
// }),
]);