Skip to content
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

[ESM] Support wrapping a custom loader #278

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;