Skip to content

Commit

Permalink
fix: optimize handling on JS files
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed May 16, 2023
1 parent e39a69e commit e4c713a
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@ const { transform } = require('sucrase');
function addHook(extension, options) {
return pirates.addHook(
(code, filePath) => {
code = code.replace(
/\bimport(\(['"]\.[^'"]+\.ts['"]\))/g,
`Promise.resolve(require$1)`,
);
if (!options?.transforms) {
// If there are no Sucrase transform necessary, we can return the code as is.
return code;
}
const { code: transformedCode, sourceMap } = transform(
// Replace dynamic imports of `.ts` files with `require`.
// Hooking into the Node.js ESM resolver would take more effort.
code.replace(
/\bimport(\(['"]\.[^'"]+\.ts['"]\))/g,
`Promise.resolve(require$1)`,
),
code,
{
...options,
sourceMapOptions: { compiledFilename: filePath },
Expand Down

0 comments on commit e4c713a

Please sign in to comment.