Skip to content

Commit

Permalink
[ESM] Support wrapping a custom loader
Browse files Browse the repository at this point in the history
  • Loading branch information
kherock committed Mar 25, 2022
1 parent befe479 commit 81289dd
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
{
"files": ["./**/*.mjs"],
"parserOptions": {
"ecmaVersion": 2022,
"sourceType": "module"
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const nodeString = ['require'];

const nodeDevBoolean = ['clear', 'dedupe', 'fork', 'notify', 'poll', 'respawn', 'vm'];
const nodeDevNumber = ['debounce', 'deps', 'interval'];
const nodeDevString = ['graceful_ipc', 'ignore', 'timestamp'];
const nodeDevString = ['graceful_ipc', 'ignore', 'timestamp', 'experimental-loader'];

const alias = Object.assign({}, nodeAlias);
const boolean = [...nodeBoolean, ...nodeDevBoolean];
Expand Down
8 changes: 5 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module.exports = function (
debounce,
dedupe,
deps,
'experimental-loader': customLoader,
graceful_ipc: gracefulIPC,
ignore,
interval,
Expand Down Expand Up @@ -97,18 +98,19 @@ module.exports = function (
const args = nodeArgs.slice();

if (extname(script) === '.mjs' || getPackageType.sync(script) === 'module') {
const customLoaderQs = customLoader ? `?${new URLSearchParams({ customLoader: resolveMain(customLoader) }).toString()}` : '';
if (semver.satisfies(process.version, '>=10 <12.11.1')) {
const resolveLoader = resolveMain(localPath('resolve-loader.mjs'));
args.push('--experimental-modules', `--loader=${resolveLoader}`);
args.push('--experimental-modules', `--loader=${resolveLoader + customLoaderQs}`);
} else if (semver.satisfies(process.version, '>=12.11.1')) {
if (semver.satisfies(process.version, '<12.17.0')) {
args.push('--experimental-modules');
}
const loaderPath = semver.satisfies(process.version, '>=16.12.0')
let loaderPath = semver.satisfies(process.version, '>=16.12.0')
? 'load-loader.mjs'
: 'get-source-loader.mjs';
const experimentalLoader = resolveMain(localPath(loaderPath));
args.push(`--experimental-loader=${experimentalLoader}`);
args.push(`--experimental-loader=${experimentalLoader + customLoaderQs}`);
}
}

Expand Down
6 changes: 5 additions & 1 deletion lib/load-loader.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { send } from './ipc.js';

const customLoaderUrl = new URL(import.meta.url).searchParams.get('customLoader');
const customLoader = customLoaderUrl ? await import(customLoaderUrl) : {};

export async function load(url, context, defaultLoad) {
send({ required: new URL(url).pathname });
return defaultLoad(url, context, defaultLoad);
const customLoad = customLoader.load || defaultLoad;
return customLoad(url, context, defaultLoad);
}

0 comments on commit 81289dd

Please sign in to comment.