Skip to content

Commit

Permalink
refactor: remove sourcemap option (#305)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `sourcemap` option removed
  • Loading branch information
privatenumber committed Feb 8, 2023
1 parent 0681425 commit 53cbc73
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 63 deletions.
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,13 +344,6 @@ Default: `'inline'`

Read more about it in the [esbuild docs](https://esbuild.github.io/api/#legal-comments).

#### sourcemap
Type: `boolean`

Default: Webpack `devtool` configuration

Whether to emit sourcemaps.

#### css
Type: `boolean`

Expand Down
57 changes: 34 additions & 23 deletions src/minify-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,24 @@ class ESBuildMinifyPlugin {
compiler.hooks.compilation.tap(pluginName, (compilation) => {
compilation.hooks.chunkHash.tap(pluginName, (_, hash) => hash.update(meta));

/**
* Check if sourcemaps are enabled
* Webpack 4: https://github.com/webpack/webpack/blob/v4.46.0/lib/SourceMapDevToolModuleOptionsPlugin.js#L20
* Webpack 5: https://github.com/webpack/webpack/blob/v5.75.0/lib/SourceMapDevToolModuleOptionsPlugin.js#LL27
*/
let useSourceMap = false;
compilation.hooks.finishModules.tap(
pluginName,
(modules) => {
const firstModule = (
Array.isArray(modules)
? modules[0]
: (modules as Set<webpack5.Module>).values().next().value as webpack5.Module
);
useSourceMap = firstModule.useSourceMap;
},
);

if ('processAssets' in compilation.hooks) {
compilation.hooks.processAssets.tapPromise(
{
Expand All @@ -64,59 +82,52 @@ class ESBuildMinifyPlugin {
stage: compilation.constructor.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE,
additionalAssets: true,
},
async () => await this.transformAssets(compilation),
async () => await this.transformAssets(compilation, useSourceMap),
);

compilation.hooks.statsPrinter.tap(pluginName, (statsPrinter) => {
statsPrinter.hooks.print
.for('asset.info.minimized')
.tap(
pluginName,
(minimized, { green, formatFlag }) =>
// @ts-expect-error type incorrectly doesn't accept undefined
(
minimized
// @ts-expect-error type incorrectly doesn't accept undefined
? green(formatFlag('minimized'))
: undefined
),
(
minimized,
{ green, formatFlag },
// @ts-expect-error type incorrectly doesn't accept undefined
) => (
minimized
// @ts-expect-error type incorrectly doesn't accept undefined
? green(formatFlag('minimized'))
: undefined
),
);
});
} else {
compilation.hooks.optimizeChunkAssets.tapPromise(
pluginName,
async () => await this.transformAssets(compilation),
async () => await this.transformAssets(compilation, useSourceMap),
);
}
});
}

private async transformAssets(
compilation: Compilation,
useSourceMap: boolean,
): Promise<void> {
const { compiler } = compilation;
const { options: { devtool } } = compiler;

const sources = 'webpack' in compiler && compiler.webpack.sources;
const SourceMapSource = (sources ? sources.SourceMapSource : WP4SourceMapSource);
const RawSource = (sources ? sources.RawSource : WP4RawSource);

const sourcemap = (
// TODO: drop support for esbuild sourcemap in future so it all goes through WP API
// Might still be necessary when SourceMap plugin is used
this.options.sourcemap === undefined
? Boolean(devtool && (devtool as string).includes('source-map'))
: this.options.sourcemap
);

const {
css: minifyCss,
include,
exclude,
...transformOptions
} = this.options;

const assets = (compilation.getAssets() as Asset[]).filter((asset) => (
const assets = (compilation.getAssets() as Asset[]).filter(asset => (

// Filter out already minimized
!asset.info.minimized
Expand Down Expand Up @@ -153,7 +164,7 @@ class ESBuildMinifyPlugin {
? 'css'
: transformOptions.loader
),
sourcemap,
sourcemap: useSourceMap,
sourcefile: asset.name,
});

Expand All @@ -167,7 +178,7 @@ class ESBuildMinifyPlugin {
compilation.updateAsset(
asset.name,
(
sourcemap
result.map
? new SourceMapSource(
result.code,
asset.name,
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export type LoaderOptions = Except<TransformOptions, 'sourcemap' | 'sourcefile'>
implementation?: Implementation;
};

export type MinifyPluginOptions = Except<TransformOptions, 'sourcefile'> & {
export type MinifyPluginOptions = Except<TransformOptions, 'sourcemap' | 'sourcefile'> & {
include?: Filter | Filter[];
exclude?: Filter | Filter[];
css?: boolean;
Expand Down
7 changes: 6 additions & 1 deletion tests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ import { describe } from 'manten';
import webpack4 from 'webpack';
import webpack5 from 'webpack5';

const webpacks = [
webpack4,
webpack5,
];

describe('esbuild-loader', ({ describe, runTestSuite }) => {
for (const webpack of [webpack4, webpack5]) {
for (const webpack of webpacks) {
describe(`Webpack ${webpack.version![0]}`, ({ runTestSuite }) => {
runTestSuite(import('./specs/loader.js'), webpack);
runTestSuite(import('./specs/plugin.js'), webpack);
Expand Down
33 changes: 2 additions & 31 deletions tests/specs/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,39 +206,12 @@ export default testSuite(({ describe }, webpack: typeof webpack4 | typeof webpac
expect(file).toContain('//# sourceMappingURL=index.js.map');
});

// TODO: This doesn't work, so maybe we should just remove the option?
// test('minify w/ plugin sourcemap option', async () => {
// const built = await build({
// '/src/index.js': '',
// }, (config) => {
// delete config.devtool;
// configureEsbuildMinifyPlugin(config, {
// sourcemap: true,
// });
// }, webpack);

// const { stats } = built;
// expect(stats.hasWarnings()).toBe(false);
// expect(stats.hasErrors()).toBe(false);
// expect(
// Object.keys(stats.compilation.assets),
// ).toStrictEqual([
// 'index.js',
// // 'index.js.map',
// ]);

// const file = built.fs.readFileSync('/dist/index.js', 'utf8');
// expect(file).toContain('//# sourceMappingURL=index.js.map');
// });

test('minify w/ source-map option and source-map plugin inline', async () => {
const built = await build(
fixtures.blank,
(config) => {
delete config.devtool;
configureEsbuildMinifyPlugin(config, {
sourcemap: true,
});
configureEsbuildMinifyPlugin(config);

config.plugins!.push(
new webpack.SourceMapDevToolPlugin({}) as any,
Expand All @@ -263,9 +236,7 @@ export default testSuite(({ describe }, webpack: typeof webpack4 | typeof webpac
fixtures.blank,
(config) => {
delete config.devtool;
configureEsbuildMinifyPlugin(config, {
sourcemap: true,
});
configureEsbuildMinifyPlugin(config);

config.plugins!.push(
new webpack.SourceMapDevToolPlugin({
Expand Down

0 comments on commit 53cbc73

Please sign in to comment.