Skip to content

Commit

Permalink
fix: support new API for ESM loader hooks (#16)
Browse files Browse the repository at this point in the history
* chore(loader): rename `load` ~> `toConfig` helper

* chore(loader): add deprecated note to self

* fix(loader): add new `load` hook for new API design;

- Closes #13
  • Loading branch information
lukeed authored Oct 27, 2021
1 parent 73b8c77 commit b483e40
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
timeout-minutes: 3
strategy:
matrix:
nodejs: [12, 14, 16]
nodejs: [12, 14, 16.11, 16]
os: [ubuntu-latest, windows-latest]
steps:
- uses: actions/checkout@v2
Expand Down
45 changes: 40 additions & 5 deletions src/loader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { existsSync } from 'fs';
import { existsSync, promises as fs } from 'fs';
import { fileURLToPath, URL } from 'url';
import * as tsm from './utils.js';

Expand All @@ -22,7 +22,10 @@ type Resolve = (
parentURL?: string;
},
fallback: Resolve
) => Promisable<{ url: string }>;
) => Promisable<{
url: string;
format?: Format;
}>;

type Inspect = (
url: string,
Expand All @@ -36,7 +39,16 @@ type Transform = (
fallback: Transform
) => Promisable<{ source: Source }>;

async function load(): Promise<Config> {
type Load = (
url: string,
context: { format?: Format },
fallback: Load
) => Promisable<{
format: Format;
source: Source;
}>;

async function toConfig(): Promise<Config> {
let mod = await setup;
mod = mod && mod.default || mod;
return (tsm as TSM).$finalize(env, mod);
Expand All @@ -46,7 +58,7 @@ const EXTN = /\.\w+(?=\?|$)/;
const isTS = /\.[mc]?tsx?(?=\?|$)/;
const isJS = /\.([mc])?js$/;
async function toOptions(uri: string): Promise<Options|void> {
config = config || await load();
config = config || await toConfig();
let [extn] = EXTN.exec(uri) || [];
return config[extn as `.${string}`];
}
Expand Down Expand Up @@ -91,7 +103,7 @@ export const resolve: Resolve = async function (ident, context, fallback) {
}
}

config = config || await load();
config = config || await toConfig();

for (ext in config) {
path = check(output.href + ext);
Expand All @@ -101,12 +113,35 @@ export const resolve: Resolve = async function (ident, context, fallback) {
return fallback(ident, context, fallback);
}

export const load: Load = async function (uri, context, fallback) {
// note: inline `getFormat`
let options = await toOptions(uri);
if (options == null) return fallback(uri, context, fallback);
let format: Format = options.format === 'cjs' ? 'commonjs' : 'module';

// TODO: decode SAB/U8 correctly
let path = fileURLToPath(uri);
let source = await fs.readFile(path);

// note: inline `transformSource`
esbuild = esbuild || await import('esbuild');
let result = await esbuild.transform(source.toString(), {
...options,
sourcefile: path,
format: format === 'module' ? 'esm' : 'cjs',
});

return { format, source: result.code };
}

/** @deprecated */
export const getFormat: Inspect = async function (uri, context, fallback) {
let options = await toOptions(uri);
if (options == null) return fallback(uri, context, fallback);
return { format: options.format === 'cjs' ? 'commonjs' : 'module' };
}

/** @deprecated */
export const transformSource: Transform = async function (source, context, xform) {
let options = await toOptions(context.url);
if (options == null) return xform(source, context, xform);
Expand Down

0 comments on commit b483e40

Please sign in to comment.