diff --git a/.github/workflows/build-and-test.yaml b/.github/workflows/build-and-test.yaml index 2103372..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: @@ -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 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: 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();