This repository has been archived by the owner on Oct 18, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: transform dynamic imports to read __esModule (#2)
* feat: transform dynamic imports to read __esModule * fix: handle uninitialized parser * feat: use sync lexer for cjs * fix: transform cjs file that uses import() * refactor: remove unused cjs check in transformAsync * feat: sourcemap support * chore: remove outdated comment
- Loading branch information
1 parent
1bda96a
commit 17494bb
Showing
6 changed files
with
151 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
declare module 'es-module-lexer/js' { | ||
export { parse } from 'es-module-lexer'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './source-map-support'; | ||
export * from './esbuild'; | ||
export * from './transform-dynamic-import'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { parse as parseWasm, init } from 'es-module-lexer'; | ||
import { parse as parseJs } from 'es-module-lexer/js'; // eslint-disable-line import/no-unresolved | ||
import MagicString from 'magic-string'; | ||
import type { TransformResult } from 'esbuild'; | ||
import remapping from '@ampproject/remapping'; | ||
|
||
const checkEsModule = `.then((mod)=>{ | ||
const exports = Object.keys(mod); | ||
if( | ||
exports.length===1&&exports[0]==='default'&&mod.default.__esModule | ||
){ | ||
return mod.default | ||
} | ||
return mod | ||
})`.replace(/[\n\t]+/g, ''); | ||
|
||
let wasmParserInitialized = false; | ||
|
||
// eslint-disable-next-line promise/catch-or-return | ||
init.then(() => { | ||
wasmParserInitialized = true; | ||
}); | ||
|
||
const inlineSourceMapPrefix = '\n//# sourceMappingURL=data:application/json;base64,'; | ||
|
||
export function transformDynamicImport( | ||
{ code, map }: TransformResult, | ||
sourcemap?: boolean | 'inline', | ||
) { | ||
code = code.toString(); | ||
|
||
// Naive check | ||
if (!code.includes('import')) { | ||
return; | ||
} | ||
|
||
if (sourcemap === 'inline') { | ||
const sourceMapIndex = code.indexOf(inlineSourceMapPrefix); | ||
const inlineSourceMap = code.slice(sourceMapIndex + inlineSourceMapPrefix.length); | ||
|
||
map = Buffer.from(inlineSourceMap, 'base64').toString(); | ||
code = code.slice(0, sourceMapIndex); | ||
} | ||
|
||
const [imports] = wasmParserInitialized ? parseWasm(code) : parseJs(code); | ||
|
||
if (imports.length === 0) { | ||
return; | ||
} | ||
|
||
const magicString = new MagicString(code); | ||
|
||
for (const dynamicImport of imports) { | ||
if (dynamicImport.d > -1) { | ||
magicString.appendRight(dynamicImport.se, checkEsModule); | ||
} | ||
} | ||
|
||
code = magicString.toString(); | ||
|
||
if (sourcemap) { | ||
const generatedMap = magicString.generateMap({ hires: true }); | ||
|
||
map = remapping([generatedMap.toString(), map], () => null).toString(); | ||
|
||
if (sourcemap === 'inline') { | ||
code += inlineSourceMapPrefix + Buffer.from(map, 'utf8').toString('base64'); | ||
map = ''; | ||
} | ||
} | ||
|
||
return { | ||
code, | ||
map, | ||
}; | ||
} |