From 3cb5106125e7275e1a3bf36f6a9708a2872c102a Mon Sep 17 00:00:00 2001 From: Joel Denning Date: Mon, 8 Nov 2021 17:44:31 -0700 Subject: [PATCH 1/5] Support node 16.12 loaders --- lib/node-loader-core.js | 19 +-------- test/fixtures/krool.js | 1 + test/fixtures/krool2.js | 1 + test/load.test.js | 93 +++++++++++++++++++++++++++++++++++++++++ test/resolve.test.js | 1 - test/run-tests.js | 1 + 6 files changed, 98 insertions(+), 18 deletions(-) create mode 100644 test/fixtures/krool.js create mode 100644 test/fixtures/krool2.js create mode 100644 test/load.test.js diff --git a/lib/node-loader-core.js b/lib/node-loader-core.js index e3c2b9a..e281036 100644 --- a/lib/node-loader-core.js +++ b/lib/node-loader-core.js @@ -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") @@ -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"), }; } diff --git a/test/fixtures/krool.js b/test/fixtures/krool.js new file mode 100644 index 0000000..c41022a --- /dev/null +++ b/test/fixtures/krool.js @@ -0,0 +1 @@ +export default "King K Rool might actually be a nice guy"; diff --git a/test/fixtures/krool2.js b/test/fixtures/krool2.js new file mode 100644 index 0000000..c41022a --- /dev/null +++ b/test/fixtures/krool2.js @@ -0,0 +1 @@ +export default "King K Rool might actually be a nice guy"; diff --git a/test/load.test.js b/test/load.test.js new file mode 100644 index 0000000..add002b --- /dev/null +++ b/test/load.test.js @@ -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?" + ); + }); +}); diff --git a/test/resolve.test.js b/test/resolve.test.js index d8c2e60..64b004c 100644 --- a/test/resolve.test.js +++ b/test/resolve.test.js @@ -33,7 +33,6 @@ describe("resolve hook", () => { }); it(`works with multiple resolve hooks`, async () => { - console.log("multiple resolve!"); global.nodeLoader.setConfigPromise( Promise.resolve({ loaders: [ diff --git a/test/run-tests.js b/test/run-tests.js index f7240f7..df823bd 100644 --- a/test/run-tests.js +++ b/test/run-tests.js @@ -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(); From 1d90d42c451b451070e9f93647d86a79cacf13f9 Mon Sep 17 00:00:00 2001 From: Joel Denning Date: Mon, 8 Nov 2021 17:46:44 -0700 Subject: [PATCH 2/5] Docs --- README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 3bb9aad..b1ee56c 100644 --- a/README.md +++ b/README.md @@ -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: From e1b0c76134e085265dd8f54bd59d26d4f48fe9a6 Mon Sep 17 00:00:00 2001 From: Joel Denning Date: Mon, 8 Nov 2021 17:48:03 -0700 Subject: [PATCH 3/5] Upgrade node version --- .github/workflows/build-and-test.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build-and-test.yaml b/.github/workflows/build-and-test.yaml index 2103372..c219453 100644 --- a/.github/workflows/build-and-test.yaml +++ b/.github/workflows/build-and-test.yaml @@ -12,10 +12,10 @@ jobs: - uses: actions/checkout@v2 - uses: actions/setup-node@v2 with: - node-version: "15" + node-version: "16.12" - uses: pnpm/action-setup@v1.2.1 with: - version: 5.17.2 + version: 6.20.3 - run: pnpm install --frozen-lockfile - run: pnpm test - run: pnpm run check-format From 8da3391f2bef119e7810655b77a1f094866b559b Mon Sep 17 00:00:00 2001 From: Joel Denning Date: Mon, 8 Nov 2021 17:49:17 -0700 Subject: [PATCH 4/5] Try again --- .github/workflows/build-and-test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build-and-test.yaml b/.github/workflows/build-and-test.yaml index c219453..0e85086 100644 --- a/.github/workflows/build-and-test.yaml +++ b/.github/workflows/build-and-test.yaml @@ -13,7 +13,7 @@ jobs: - uses: actions/setup-node@v2 with: node-version: "16.12" - - uses: pnpm/action-setup@v1.2.1 + - uses: pnpm/action-setup@v2.0.1 with: version: 6.20.3 - run: pnpm install --frozen-lockfile From 00e7fae23a6e5f3213de379a377ed027058aeaee Mon Sep 17 00:00:00 2001 From: Joel Denning Date: Mon, 8 Nov 2021 17:49:57 -0700 Subject: [PATCH 5/5] Fixes --- .github/workflows/build-and-test.yaml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build-and-test.yaml b/.github/workflows/build-and-test.yaml index 0e85086..3bc4633 100644 --- a/.github/workflows/build-and-test.yaml +++ b/.github/workflows/build-and-test.yaml @@ -1,8 +1,12 @@ name: Build and Test on: - - push - - pull_request + push: + branches: + - main + pull_request: + branches: + - "*" jobs: build: