From e69a510ae80db47d8ad232bc0791c9a1625396ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Cs=C3=A1sz=C3=A1r?= Date: Sun, 2 Nov 2025 10:21:12 +0100 Subject: [PATCH] fix: use CommonJS import for webpack-sources webpack-sources is a CommonJS module and requires default import syntax in ESM. This fixes the package import to work with Node's ESM loader. Fixes #313 --- src/hooks.ts | 4 +++- test/integration/usage.ts | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 test/integration/usage.ts diff --git a/src/hooks.ts b/src/hooks.ts index 3dee3b1..f50f743 100644 --- a/src/hooks.ts +++ b/src/hooks.ts @@ -5,13 +5,15 @@ import { SyncWaterfallHook } from 'tapable'; import type { Compiler, Module, Compilation, LoaderContext } from 'webpack'; // Note: This was the old delcaration. It appears to be Webpack v3 compat. // const { RawSource } = (webpack as any).sources || require('webpack-sources'); -import { RawSource } from 'webpack-sources'; +import webpackSources from 'webpack-sources'; import type { EmitCountMap, InternalOptions } from './index.js'; import type { CompilationAsset, FileDescriptor } from './helpers.js'; import { generateManifest, reduceAssets, reduceChunk, transformFiles } from './helpers.js'; +const { RawSource } = webpackSources; + interface BeforeRunHookArgs { emitCountMap: EmitCountMap; manifestFileName: string; diff --git a/test/integration/usage.ts b/test/integration/usage.ts new file mode 100644 index 0000000..8dfbc61 --- /dev/null +++ b/test/integration/usage.ts @@ -0,0 +1,14 @@ +import { execSync } from 'node:child_process'; + +import test from '../helpers/ava-compat'; + +// make implicit dependency explicit: +import '../../src/index'; + +test.before(() => execSync('pnpm build')); + +test('can be used as documented', (t) => { + const readmeCode = "import { WebpackManifestPlugin } from 'webpack-manifest-plugin'"; + + t.deepEqual(execSync(`node -e "${readmeCode}"`, { encoding: 'utf-8' }), ''); +});