Skip to content

Commit

Permalink
chore(require): determine options.sourcefile once per loader
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeed committed Oct 9, 2021
1 parent 329ac1c commit 98dab33
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/require.ts
Expand Up @@ -50,23 +50,24 @@ const tsrequire = 'var $$req=require;require=(' + function () {
})
} + ')();';

function transform(source: string, sourcefile: string, options: Options): string {
function transform(source: string, options: Options): string {
esbuild = esbuild || require('esbuild');
return esbuild.transformSync(source, { ...options, sourcefile }).code;
return esbuild.transformSync(source, options).code;
}

function loader(Module: Module, sourcefile: string) {
let extn = extname(sourcefile);
let options = config[extn] || {};
let pitch = Module._compile!.bind(Module);
options.sourcefile = sourcefile;

if (/\.[mc]?tsx?$/.test(extn)) {
options.banner = tsrequire + (options.banner || '');
}

if (config[extn] != null) {
Module._compile = source => {
let result = transform(source, sourcefile, options);
let result = transform(source, options);
return pitch(result, sourcefile);
};
}
Expand All @@ -78,10 +79,7 @@ function loader(Module: Module, sourcefile: string) {
if (ec !== 'ERR_REQUIRE_ESM') throw err;

let input = readFileSync(sourcefile, 'utf8');
let result = transform(input, sourcefile, {
...options, format: 'cjs'
});

let result = transform(input, { ...options, format: 'cjs' });
return pitch(result, sourcefile);
}
}
Expand Down

0 comments on commit 98dab33

Please sign in to comment.