Skip to content

Commit

Permalink
Add an option for self-registering
Browse files Browse the repository at this point in the history
  • Loading branch information
sebamarynissen committed May 31, 2024
1 parent ff77a3e commit adcc6d3
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
16 changes: 14 additions & 2 deletions lib/create-esm-loader.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
// # create-esm-loader.js
import path from 'node:path';
import module from 'node:module';
import { isMainThread } from 'node:worker_threads';

console.log(isMainThread);

// # createLoader(config)
export default function createLoader(config = {}) {

// If we're running from the main thread, then we just export a `register`
// method that registers the loader *itself*.
if (isMainThread && (config.register || typeof config === 'string')) {
const url = typeof config === 'string' ? config : config.register;
return {
register(options) {
module.register(url, {
data: options,
});
},
};
}

// Bind the resolve and load hooks from the config object to the hooks
// object we'll be exporting. If they were not specified, we do nothing.
const hooks = {};
Expand Down
20 changes: 20 additions & 0 deletions test/loaders-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ describe('ESM loaders', function() {
`data:text/javascript,import {register} from "node:module"; register(${JSON.stringify(url)});`,
);
};
this.register = function(specifier, options = {}) {
const url = new URL(specifier, import.meta.url).href;
return this.run(
`data:text/javascript,import {register} from ${JSON.stringify(url)};register(${JSON.stringify(options)})`,
);
};
});

it('an http loader', async function() {
Expand Down Expand Up @@ -72,6 +78,20 @@ describe('ESM loaders', function() {

});

it('a self-registering loader', async function() {

const run = this.register('./loaders/self-register.js', {
extension: '.ts',
loader: 'ts',
});
let result = await run(`
import fn from './files/fn.ts';
export default fn('I like TypeScript');
`);
expect(result).to.equal('I don\'t like TypeScript');

});

it('a composite loader', async function() {

const server = new http.Server((req, res) => {
Expand Down
20 changes: 20 additions & 0 deletions test/loaders/self-register.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// # self-register.js
import create from 'create-esm-loader';

export const { register, load, initialize } = create({
register: import.meta.url,
async load(url, ctx, nextLoad) {
const { extension } = this.options;
if (!url.endsWith(extension)) return nextLoad(url);
const { source } = await nextLoad(url, { format: 'module' });
const { transform } = await import('esbuild');
const { loader = 'js' } = this.options || {};
const { code } = await transform(source, {
loader,
});
return {
source: code,
format: 'module',
};
},
});

0 comments on commit adcc6d3

Please sign in to comment.