Skip to content

Commit

Permalink
feat: support external in legalComments (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Jan 12, 2023
1 parent 20e573b commit 6fe85b4
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ Type: `boolean`
Minify JS using equivalent but shorter syntax.

#### legalComments
Type: `'none' | 'inline' | 'eof'`
Type: `'none' | 'inline' | 'eof' | 'external'`

Default: `'inline'`

Expand Down
7 changes: 7 additions & 0 deletions src/@types/webpack.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ declare module 'webpack' {
namespace compilation {
interface Compilation {
getAssets(): Asset[];

// From Webpack 5
emitAsset(
file: string,
source: Source,
assetInfo?: AssetInfo,
): void;
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/minify-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ class ESBuildMinifyPlugin {
sourcefile: asset.name,
});

if (result.legalComments) {
compilation.emitAsset(
`${asset.name}.LEGAL.txt`,
new RawSource(result.legalComments),
);
}

compilation.updateAsset(
asset.name,
(
Expand Down
25 changes: 25 additions & 0 deletions tests/specs/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,31 @@ export default testSuite(({ describe }, webpack: typeof webpack4 | typeof webpac
const file = built.fs.readFileSync('/dist/index.js', 'utf8');
expect(file).not.toMatch('//! legal comment');
});

test('minify w/ legalComments - external', async () => {
const built = await build(
fixtures.legalComments,
(config) => {
configureEsbuildMinifyPlugin(config, {
legalComments: 'external',
});
},
webpack,
);

expect(built.stats.hasWarnings()).toBe(false);
expect(built.stats.hasErrors()).toBe(false);

expect(Object.keys(built.stats.compilation.assets)).toStrictEqual([
'index.js',
'index.js.LEGAL.txt',
]);
const file = built.fs.readFileSync('/dist/index.js', 'utf8');
expect(file).not.toMatch('//! legal comment');

const extracted = built.fs.readFileSync('/dist/index.js.LEGAL.txt', 'utf8');
expect(extracted).toMatch('//! legal comment');
});
});
});

Expand Down
4 changes: 2 additions & 2 deletions tests/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type webpack4 from 'webpack';
import type webpack5 from 'webpack5';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import { ESBuildMinifyPlugin } from '../dist/index.js';
import type { MinifyPluginOptions } from '../dist/interfaces.js';
import { ESBuildMinifyPlugin } from '../src/index.js';
import type { MinifyPluginOptions } from '../src/interfaces.js';

const esbuildLoaderPath = require.resolve('../src/');

Expand Down

0 comments on commit 6fe85b4

Please sign in to comment.