Skip to content

Loader options #10

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,5 @@ dist

# TernJS port file
.tern-port

.vscode
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ export default {
};
```

If the loader supports options, you pass them down like below.

```js
export default {
loaders: [{ options: { someOption: true }, loader: importMapLoader }],
};
```

Then run node with the `--experimental-loader` flag:

```sh
Expand Down
10 changes: 8 additions & 2 deletions lib/node-loader-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,14 @@ function resolveLoaders(config) {

function getLoaders(config, name) {
return config.loaders
.filter((loader) => loader[name])
.map((loader) => loader[name]);
.filter((loader) => (loader.loader ? loader.loader[name] : loader[name]))
.map((loader) =>
loader.loader
? (...args) => {
return loader.loader[name].apply(this, args.concat(loader.options));
}
: loader[name]
);
}

function flattenSequentialLoaders(config, name) {
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/krool3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default "King K Rool might actually be a nice guy";
1 change: 1 addition & 0 deletions test/fixtures/yoshi-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default "Yoshi doesn't deserve to be subservient to Mario";
47 changes: 47 additions & 0 deletions test/load.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,51 @@ describe("load hook", () => {
"What if we got it all wrong and DK is the evil one?"
);
});

it(`supports passing down options to load hook`, async () => {
global.nodeLoader.setConfigPromise(
Promise.resolve({
loaders: [
{
options: {
capitalize: true,
},
loader: {
load: async function (url, context, defaultLoad, loaderOptions) {
if (url.includes("krool3.js")) {
const { source: originalSource } = await defaultLoad(
url,
context
);

let str =
"I mean what did he even do to deserve Donkey Kong's wrath?";

if (loaderOptions?.capitalize) {
str = str.toUpperCase();
}

const finalSource = `${originalSource};\nexport const more = "${str}"`;
return {
format: "module",
source: finalSource,
};
} else {
return defaultLoad(url, context);
}
},
},
},
],
})
);

const ns = await import("./fixtures/krool3.js");
assert.equal(ns.default, "King K Rool might actually be a nice guy");

assert.equal(
ns.more,
"I mean what did he even do to deserve Donkey Kong's wrath?".toUpperCase()
);
});
});
45 changes: 45 additions & 0 deletions test/resolve.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,49 @@ describe("resolve hook", () => {
const captainFalcon = await import("captainfalcon");
assert.equal(captainFalcon.default, "Captain Falcon 2");
});

it(`supports passing down options to resolve hook`, async () => {
global.nodeLoader.setConfigPromise(
Promise.resolve({
loaders: [
{
options: {
nocache: true,
},
loader: {
resolve: async function (
specifier,
context,
defaultResolve,
loaderOptions
) {
const { parentURL = null } = context;

if (specifier === "yoshi-2") {
const url = new URL("./fixtures/yoshi-2.js", parentURL).href;

return {
url: loaderOptions?.nocache
? url + `?${Math.random()}`
: url,
};
} else {
return defaultResolve(specifier, context);
}
},
},
},
],
})
);

const ns = await import("yoshi-2");
assert.equal(
ns.default,
"Yoshi doesn't deserve to be subservient to Mario"
);

const ns2 = await import("yoshi-2");
assert.notEqual(ns2, ns);
});
});