UNPKG

@lynx-js/rspeedy

Version:

A webpack/rspack-based frontend toolchain for Lynx

107 lines (106 loc) 4.73 kB
import { isLynx } from "./1~is-lynx.js"; var isProduction = 'production' === process.env.NODE_ENV; var prefix = 'Invariant failed'; function invariant(condition, message) { if (condition) return; if (isProduction) throw new Error(prefix); var provided = 'function' == typeof message ? message() : message; var value = provided ? "".concat(prefix, ": ").concat(provided) : prefix; throw new Error(value); } class EvalSourceMapDevToolPlugin_EvalSourceMapDevToolPlugin { options; constructor(options){ this.options = options; } apply(compiler) { const { EvalSourceMapDevToolPlugin } = compiler.webpack; new EvalSourceMapDevToolPlugin(this.options).apply(compiler); } } class SourceMapDevToolPlugin_SourceMapDevToolPlugin { options; constructor(options){ this.options = options; } apply(compiler) { const { SourceMapDevToolPlugin } = compiler.webpack; new SourceMapDevToolPlugin(this.options).apply(compiler); } } function pluginSourcemap() { return { name: 'lynx:rsbuild:sourcemap', pre: [ 'lynx:rsbuild:dev' ], setup (api) { api.modifyBundlerChain((chain, { isDev, environment })=>{ const { dev, output, server } = api.getRsbuildConfig('current'); const publicPath = isDev ? dev?.assetPrefix : output?.assetPrefix; if (false === publicPath) return; invariant('string' == typeof publicPath || void 0 === publicPath, `dev.assetPrefix should be normalized to string, got ${dev?.assetPrefix}`); applySourceMapPlugin(chain, getDevtoolFromSourceMap(), publicPath?.replaceAll('<port>', String(api.context.devServer?.port ?? server?.port))); if (isLynx(environment)) applyDropSourceMapAssets(chain); function getDevtoolFromSourceMap() { const DEFAULT_DEV_DEVTOOL = 'cheap-module-source-map'; switch(typeof output?.sourceMap){ case 'boolean': if (output.sourceMap) return isDev ? DEFAULT_DEV_DEVTOOL : 'source-map'; return false; case 'undefined': case 'object': return output?.sourceMap?.js ?? (isDev ? DEFAULT_DEV_DEVTOOL : isLynx(environment) ? 'source-map' : false); } } }); } }; } function applyDropSourceMapAssets(chain) { chain.plugin('lynx:sourcemap-drop').use(class { apply(compiler) { const { Compilation } = compiler.webpack; compiler.hooks.compilation.tap('LynxDropSourceMapAssetsPlugin', (compilation)=>{ compilation.hooks.processAssets.tap({ name: 'LynxDropSourceMapAssetsPlugin', stage: Compilation.PROCESS_ASSETS_STAGE_REPORT + 1 }, ()=>{ for (const name of Object.keys(compilation.assets))if (name.endsWith('.map')) compilation.deleteAsset(name); }); }); } }, []); } function applySourceMapPlugin(chain, devtool, publicPath) { if (false === devtool) return; const CHAIN_ID_DEV_SOURCEMAP = 'lynx:sourcemap'; const output = chain.get('output'); if (devtool.includes('source-map')) { const hidden = devtool.includes('hidden'); const inline = devtool.includes('inline'); const evalWrapped = devtool.includes('eval'); const cheap = devtool.includes('cheap'); const moduleMaps = devtool.includes('module'); const noSources = devtool.includes('nosources'); const debugIds = devtool.includes('debugids'); const options = { filename: inline ? null : output?.sourceMapFilename ?? '[file].map[query]', moduleFilenameTemplate: output?.devtoolModuleFilenameTemplate ?? 'file://[absolute-resource-path]', fallbackModuleFilenameTemplate: output?.devtoolFallbackModuleFilenameTemplate ?? 'file://[absolute-resource-path]?[hash]', append: hidden ? false : void 0, module: moduleMaps ? true : !cheap, columns: !cheap, noSources, namespace: output?.devtoolNamespace, publicPath, debugIds }; chain.devtool(false).plugin(CHAIN_ID_DEV_SOURCEMAP).when(evalWrapped, (plugin)=>plugin.use(EvalSourceMapDevToolPlugin_EvalSourceMapDevToolPlugin, [ options ]), (plugin)=>plugin.use(SourceMapDevToolPlugin_SourceMapDevToolPlugin, [ options ])); } } export { pluginSourcemap };