Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: expose importAttributes to loaders via _importAttributes#20232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
samarthsinh2660 wants to merge6 commits intowebpack:main
base:main
Choose a base branch
Loading
fromsamarthsinh2660:feat/loader-import-attributes
Open
Show file tree
Hide file tree
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
NextNext commit
feat: expose importAttributes to loaders via _importAttributes
  • Loading branch information
@samarthsinh2660
samarthsinh2660 committedDec 12, 2025
commitba2c0bab62a76d84ec8c35d4d25ccad84f199302
1 change: 1 addition & 0 deletionsdeclarations/LoaderContext.d.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -63,6 +63,7 @@ export interface NormalModuleLoaderContext<OptionsType> {
_module?: NormalModule;
_compilation?: Compilation;
_compiler?: Compiler;
_importAttributes?: Record<string, unknown>;
}

/** These properties are added by the HotModuleReplacementPlugin */
Expand Down
10 changes: 8 additions & 2 deletionslib/NormalModule.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -97,6 +97,7 @@ const memoize = require("./util/memoize");
/** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
/** @typedef {import("../declarations/WebpackOptions").HashFunction} HashFunction */
/** @typedef {import("./util/identifier").AssociatedObjectForCache} AssociatedObjectForCache */
/** @typedef {import("./javascript/JavascriptParser").ImportAttributes} ImportAttributes */
/**
* @template T
* @typedef {import("./util/deprecation").FakeHook<T>} FakeHook
Expand DownExpand Up@@ -256,6 +257,7 @@ makeSerializable(
* @property {GeneratorOptions=} generatorOptions the options of the generator used
* @property {ResolveOptions=} resolveOptions options used for resolving requests from this module
* @property {boolean} extractSourceMap enable/disable extracting source map
* @property {ImportAttributes=} importAttributes import attributes from the import statement
*/

/**
Expand DownExpand Up@@ -348,7 +350,8 @@ class NormalModule extends Module {
generator,
generatorOptions,
resolveOptions,
extractSourceMap
extractSourceMap,
importAttributes
}) {
super(type, context || getContext(resource), layer);

Expand DownExpand Up@@ -384,6 +387,8 @@ class NormalModule extends Module {
}
/** @type {NormalModuleCreateData['extractSourceMap']} */
this.extractSourceMap = extractSourceMap;
/** @type {NormalModuleCreateData['importAttributes']} */
this.importAttributes = importAttributes;

// Info from Build
/** @type {WebpackError | null} */
Expand DownExpand Up@@ -836,7 +841,8 @@ class NormalModule extends Module {
_module: this,
_compilation: compilation,
_compiler: compilation.compiler,
fs
fs,
_importAttributes: this.importAttributes
};

Object.assign(loaderContext, options.loader);
Expand Down
3 changes: 2 additions & 1 deletionlib/NormalModuleFactory.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -794,7 +794,8 @@ class NormalModuleFactory extends ModuleFactory {
generator: this.getGenerator(type, settings.generator),
generatorOptions: settings.generator,
resolveOptions,
extractSourceMap: settings.extractSourceMap || false
extractSourceMap: settings.extractSourceMap || false,
importAttributes: data.attributes
});
} catch (createDataErr) {
return callback(/** @type {Error} */ (createDataErr));
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
// This file will be processed by the loader
// The content doesn't matter - the loader will replace it with the import attributes
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
// This file will be processed by the loader without import attributes
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
import attributesWithType from "./data.data.js" with { type: "custom", preload: "true" };
import attributesEmpty from "./data2.data.js";

it("should expose import attributes to loaders via _importAttributes", function() {
expect(attributesWithType).toEqual({ type: "custom", preload: "true" });
});

it("should return undefined when no import attributes are present", function() {
expect(attributesEmpty).toBeUndefined();
});
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
/**@type {import("../../../../").LoaderDefinition} */
module.exports=function(source){
constattributes=this._importAttributes;
return`module.exports =${JSON.stringify(attributes)};`;
};
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
"use strict";

/** @type {import("../../../../").Configuration} */
module.exports = {
module: {
rules: [
{
test: /\.data\.js$/,
loader: require.resolve("./loader.js")
}
]
}
};
Loading

[8]ページ先頭

©2009-2025 Movatter.jp