Skip to content

Commit

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

function transform(source: string, sourcefile: string, options: Options): string {
let banner = options.banner || '';
if (/\.[mc]?tsx?$/.test(sourcefile)) {
banner = tsrequire + banner;
}

esbuild = esbuild || require('esbuild');
return esbuild.transformSync(source, {
...options, banner, sourcefile
}).code;
return esbuild.transformSync(source, { ...options, sourcefile }).code;
}

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

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

if (options != null) {
if (config[extn] != null) {
Module._compile = source => {
let result = transform(source, sourcefile, options);
return pitch(result, sourcefile);
Expand Down

0 comments on commit 329ac1c

Please sign in to comment.