Skip to content

Commit 7c42835

Browse files
committed
feat(webpack-deep-imports-plugin): add named exports code generation logic
1 parent 8c8cc14 commit 7c42835

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

webpack/webpack-deep-imports-plugin/src/DeepImportsPlugin.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,21 @@
33

44
import path from 'node:path';
55

6-
import { DllPlugin, type Compiler, WebpackError, type Chunk, type NormalModule } from 'webpack';
6+
import {
7+
DllPlugin,
8+
type Compiler,
9+
WebpackError,
10+
type Chunk,
11+
type NormalModule,
12+
type ModuleGraph
13+
} from 'webpack';
714

815
import { Async, FileSystem, LegacyAdapters, Path } from '@rushstack/node-core-library';
916

1017
const PLUGIN_NAME: 'DeepImportsPlugin' = 'DeepImportsPlugin';
1118

1219
type DllPluginOptions = DllPlugin['options'];
20+
type IExportsInfo = ReturnType<ModuleGraph['getExportsInfo']>;
1321

1422
/**
1523
* @public
@@ -147,6 +155,7 @@ export class DeepImportsPlugin extends DllPlugin {
147155
libPathWithoutExtension: string;
148156
moduleId: string | number | null;
149157
secondaryChunkId: string | undefined;
158+
exportsInfo: IExportsInfo;
150159
}
151160

152161
const pathsToIgnoreWithoutExtension: Set<string> = this._pathsToIgnoreWithoutExtensions;
@@ -170,7 +179,8 @@ export class DeepImportsPlugin extends DllPlugin {
170179
libModules.push({
171180
libPathWithoutExtension: relativePathWithoutExtension,
172181
moduleId: compilation.chunkGraph.getModuleId(runtimeChunkModule),
173-
secondaryChunkId
182+
secondaryChunkId,
183+
exportsInfo: compilation.moduleGraph.getExportsInfo(runtimeChunkModule) // Record exportsInfo to generate named exports placeholder code
174184
});
175185

176186
encounteredLibPaths.add(relativePathWithoutExtension);
@@ -233,7 +243,7 @@ export class DeepImportsPlugin extends DllPlugin {
233243

234244
await Async.forEachAsync(
235245
libModules,
236-
async ({ libPathWithoutExtension, moduleId, secondaryChunkId }) => {
246+
async ({ libPathWithoutExtension, moduleId, secondaryChunkId, exportsInfo }) => {
237247
const depth: number = countSlashes(libPathWithoutExtension);
238248
const requirePath: string = '../'.repeat(depth) + libOutFolderRelativeOutputPath;
239249
let moduleText: string;
@@ -250,6 +260,15 @@ export class DeepImportsPlugin extends DllPlugin {
250260
].join('\n');
251261
}
252262

263+
const providedExports: null | true | string[] = exportsInfo.getProvidedExports();
264+
if (Array.isArray(providedExports) && providedExports.length > 0) {
265+
moduleText = [
266+
`${providedExports.map((exportName) => `exports.${exportName}`).join(' = ')} = void 0;`,
267+
'',
268+
moduleText
269+
].join('\n');
270+
}
271+
253272
compilation.emitAsset(
254273
`${outputPathRelativeLibOutFolder}/${libPathWithoutExtension}${JS_EXTENSION}`,
255274
new compiler.webpack.sources.RawSource(moduleText)

0 commit comments

Comments
 (0)