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 26, 2022
1 parent f2a815b commit 93db9bd
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 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": 13,
"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
10 changes: 10 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { fork } = require('child_process');
const filewatcher = require('filewatcher');
const path = require('path');
const semver = require('semver');
const { pathToFileURL } = require('url');

Expand All @@ -20,6 +21,7 @@ module.exports = function (
debounce,
dedupe,
deps,
'experimental-loader': wrapLoader,
graceful_ipc: gracefulIPC,
ignore,
interval,
Expand Down Expand Up @@ -98,6 +100,14 @@ module.exports = function (
? 'loader-legacy.mjs'
: 'loader.mjs';
const loader = pathToFileURL(resolveMain(localPath(loaderPath)));
if (wrapLoader) {
if (wrapLoader.startsWith('.')) {
const customLoaderAbsPath = path.resolve(process.cwd(), wrapLoader);
loader.searchParams.append('wrap', pathToFileURL(customLoaderAbsPath));
} else {
loader.searchParams.append('wrap', wrapLoader);
}
}
if (semver.satisfies(process.version, '<12.17.0')) {
args.push('--experimental-modules');
}
Expand Down
13 changes: 12 additions & 1 deletion lib/loader-legacy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,28 @@ import { send } from './ipc.mjs';

const require = createRequire(import.meta.url);

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

export const resolve = customLoader.resolve;

export async function getFormat(url, context, defaultGetFormat) {
const getPackageType = require('get-package-type');
const filePath = fileURLToPath(url);

send({ required: filePath });
const customGetFormat = customLoader.load || defaultGetFormat;
try {
return await defaultGetFormat(url, context, defaultGetFormat);
return await customGetFormat(url, context, defaultGetFormat);
} catch (err) {
if (err.code === 'ERR_UNKNOWN_FILE_EXTENSION') {
return { format: await getPackageType(filePath) };
}
throw err;
}
}

export const getSource = customLoader.getSource;

export const transformSource = customLoader.transformSource;
export const getGlobalPreloadCode = customLoader.getGlobalPreloadCode;
10 changes: 9 additions & 1 deletion lib/loader.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,25 @@ import { send } from './ipc.mjs';

const require = createRequire(import.meta.url);

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

export const resolve = customLoader.resolve;

export async function load(url, context, defaultLoad) {
const getPackageType = require('get-package-type');
const filePath = fileURLToPath(url);

send({ required: filePath });
const customLoad = customLoader.load || defaultLoad;
try {
return await defaultLoad(url, context, defaultLoad);
return await customLoad(url, context, defaultLoad);
} catch (err) {
if (err.code === 'ERR_UNKNOWN_FILE_EXTENSION') {
return { format: await getPackageType(filePath), source: null };
}
throw err;
}
}

export const globalPreload = customLoader.globalPreload;

0 comments on commit 93db9bd

Please sign in to comment.