Skip to content

Document webpack change: feat: add new external type "module-import" #7348

@webpack-bot

Description

@webpack-bot

A pull request by @fi3ework was merged and maintainers requested a documentation change.

See pull request: webpack/webpack#18620


What kind of change does this PR introduce?

Resolve #17986.

Documentation update: #7345.


Following content maybe somehow out of context. This PR's final implementation is to resolve #17986.

A new external type has been introduced, mainly for the purpose of output clean artifact during bundling library. The name of this "module-import" is referenced from webpack/webpack#17986 (comment), but I'm not sure if the current implementation fully meets the intention of this comment. This PR is an incomplete implementation, only implementing the handling of external processing for dynamic imports.

Here's what it does:

Given following config:

// webpack.config.js
externals: {
  path: 'path node-commonjs',
  fs: 'module-import fs',
  'node:fs': 'module-import node:fs',
  'node-fs': 'module-import fs',
},

source

it("should allow async externals", async () => {
	const fs1 = await import("fs");
	const fs2 = await import("node:fs");
	const fs3 = await import("node-fs");

	expect(fs1).toStrictEqual(fs2);
	expect(fs1).toStrictEqual(fs3);
});

output

var __webpack_exports__ = {};
/*!******************!*\
  !*** ./index.js ***!
  \******************/
it("should allow async externals", async () => {
	const fs1 = await import("fs");
	const fs2 = await import("node:fs");
	const fs3 = await import("fs");

	expect(fs1).toStrictEqual(fs2);
	expect(fs1).toStrictEqual(fs3);
});

There is a slightly hacky bit here, where we're deleting ImportDependency at finishModules stage to prevent any runtime code from being generated. But I couldn't find a better way to prevent ImportDependencyTemplate from modifying the runtime code. Possibly by introducing a new DependencyLibraryTemplate specifically for library patterns could achieve that. Do you have any suggestions on how to suppress runtime code generation? @alexander-akait

Did you add tests for your changes?

Yes.

Does this PR introduce a breaking change?

No.

What needs to be documented once your changes are merged?

The PR is still in draft mode, it should add update the document before merged.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions