Skip to content

Support node 16.12 loaders #8

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

Merged
merged 5 commits into from
Nov 9, 2021
Merged
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
14 changes: 9 additions & 5 deletions .github/workflows/build-and-test.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
name: Build and Test

on:
- push
- pull_request
push:
branches:
- main
pull_request:
branches:
- "*"

jobs:
build:
Expand All @@ -12,10 +16,10 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: "15"
- uses: pnpm/action-setup@v1.2.1
node-version: "16.12"
- uses: pnpm/action-setup@v2.0.1
with:
version: 5.17.2
version: 6.20.3
- run: pnpm install --frozen-lockfile
- run: pnpm test
- run: pnpm run check-format
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ A configurable NodeJS loader that combines multiple other loaders into one.

```sh
npm install --save @node-loader/core

# Or, if you prefer yarn
yarn add @node-loader/core
```

For NodeJS@<16.12, use `@node-loader/core@1`. For NodeJS@>=16.12, use `@node-loader/core@latest`.

## Usage

Create a file `node-loader.config.js` in your current working directory:
Expand Down
19 changes: 2 additions & 17 deletions lib/node-loader-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,7 @@ function die(msg, err) {

export const resolve = createHook("resolve");

export const getFormat = createHook("getFormat");

export const getSource = createHook("getSource");

export const transformSource = createHook("transformSource");
export const load = createHook("load");

// getGlobalPreloadCode is synchronous, which means we can't import the config file in time :'(
// export const getGlobalPreloadCode = createHook("getGlobalPreloadCode")
Expand Down Expand Up @@ -122,18 +118,7 @@ function createHook(name) {
function resolveLoaders(config) {
return {
resolve: flattenSequentialLoaders(config, "resolve"),
getFormat: flattenSequentialLoaders(config, "getFormat"),
getSource: flattenSequentialLoaders(config, "getSource"),
transformSource: flattenSequentialLoaders(config, "transformSource"),
// getGlobalPreloadCode not currently supported
// getGlobalPreloadCode: function getGlobalPreloadCode() {
// return getLoaders(config, "getGlobalPreloadCode").reduce(
// (result, loader) => {
// return result + loader() + ";";
// },
// ""
// );
// },
load: flattenSequentialLoaders(config, "load"),
};
}

Expand Down
1 change: 1 addition & 0 deletions test/fixtures/krool.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/krool2.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";
93 changes: 93 additions & 0 deletions test/load.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import assert from "assert";

describe("load hook", () => {
it(`works with a single load hook`, async () => {
global.nodeLoader.setConfigPromise(
Promise.resolve({
loaders: [
{
load: async function (url, context, defaultLoad) {
if (url.includes("krool.js")) {
const { source: originalSource } = await defaultLoad(
url,
context
);
const finalSource = `${originalSource};\nexport const more = "I mean what did he even do to deserve Donkey Kong's wrath?"`;
return {
format: "module",
source: finalSource,
};
} else {
return defaultLoad(url, context);
}
},
},
],
})
);

const ns = await import("./fixtures/krool.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?"
);
});

it(`works with multiple load hooks`, async () => {
global.nodeLoader.setConfigPromise(
Promise.resolve({
loaders: [
{
load: async function (url, context, defaultLoad) {
if (url.includes("krool2.js")) {
const { source: originalSource } = await defaultLoad(
url,
context
);
const finalSource = `${originalSource};\nexport const more = "I mean what did he even do to deserve Donkey Kong's wrath?"`;
return {
format: "module",
source: finalSource,
};
} else {
return defaultLoad(url, context);
}
},
},
{
load: async function (url, context, defaultLoad) {
if (url.includes("krool2.js")) {
const { source: originalSource } = await defaultLoad(
url,
context
);
const finalSource = `${originalSource};\nexport const evenMore = "What if we got it all wrong and DK is the evil one?"`;
return {
format: "module",
source: finalSource,
};
} else {
return defaultLoad(url, context);
}
},
},
],
})
);

const ns = await import("./fixtures/krool2.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?"
);

assert.equal(
ns.evenMore,
"What if we got it all wrong and DK is the evil one?"
);
});
});
1 change: 0 additions & 1 deletion test/resolve.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ describe("resolve hook", () => {
});

it(`works with multiple resolve hooks`, async () => {
console.log("multiple resolve!");
global.nodeLoader.setConfigPromise(
Promise.resolve({
loaders: [
Expand Down
1 change: 1 addition & 0 deletions test/run-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Mocha from "mocha";
const mocha = new Mocha();

mocha.addFile("./test/resolve.test.js");
mocha.addFile("./test/load.test.js");

mocha.loadFilesAsync().then(() => {
mocha.run();
Expand Down