Skip to content

Commit

Permalink
fix: 🐛 implemention of source map dev tool plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
maoxiaoke committed Jun 15, 2021
1 parent fa89838 commit 8dd105a
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions packages/plugin-icestark/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { IPlugin, Json } from '@alib/build-scripts';

const plugin: IPlugin = async ({ onGetWebpackConfig, getValue, applyMethod, context }, options = {}) => {
const { uniqueName, umd, library } = options as Json;
const { rootDir, webpack, pkg, commandArgs, command, userConfig } = context;
const { sourceMap } = userConfig;
const { rootDir, webpack, pkg, commandArgs, userConfig, command } = context;
const iceTempPath = getValue('TEMP_PATH') || path.join(rootDir, '.ice');
// remove output.jsonpFunction in webpack5 see: https://webpack.js.org/blog/2020-10-10-webpack-5-release/#automatic-unique-naming
const isWebpack5 = (webpack as any).version?.startsWith('5');
const isDev = command === 'start';

const hasDefaultLayout = glob.sync(`${path.join(rootDir, 'src/layouts/index')}.@(ts?(x)|js?(x))`).length;
onGetWebpackConfig((config) => {
Expand All @@ -28,16 +28,30 @@ const plugin: IPlugin = async ({ onGetWebpackConfig, getValue, applyMethod, cont
}

// source-map
if (sourceMap || command === 'start') {
const publicPath = config.output.get('publicPath') ?? '/';
const devtool = String(config.toConfig().devtool);
const inlineOrHiddenSourceMap = ['inline', 'eval', 'hidden'].some(type => devtool.includes(type));

if (!inlineOrHiddenSourceMap) {
const { devtoolFallbackModuleFilenameTemplate, devtoolNamespace } = config.toConfig().output;
const cheap = devtool.includes('cheap');
const moduleMaps = devtool.includes('module');
const noSources = devtool.includes('nosources');

const publicPath = isDev ? userConfig.devPublicPath : config.output.get('publicPath') ?? '/';
const port = commandArgs.port;
const servePath = publicPath === '/' ? `http://localhost:${port}` : publicPath;
const isHttps = commandArgs?.https;
const servePath = publicPath === '/' ? `http${isHttps ? 's' : ''}://localhost:${port}` : publicPath;

config.devtool(false);
config.plugin('SourceMapDevToolPlugin')
.use((webpack as any).SourceMapDevToolPlugin, [{
append: `\n//# sourceMappingURL=${servePath}/[file].map`,
filename: '[file].map',
fallbackModuleFilenameTemplate: devtoolFallbackModuleFilenameTemplate,
module: moduleMaps ? true : !cheap,
columns: !cheap,
noSources,
namespace: devtoolNamespace
}]);
}

Expand All @@ -47,7 +61,7 @@ const plugin: IPlugin = async ({ onGetWebpackConfig, getValue, applyMethod, cont
config.output
.library(libraryName)
.libraryTarget('umd');

// collect entry
const entries = config.toConfig().entry;
const entryList = [];
Expand Down

0 comments on commit 8dd105a

Please sign in to comment.