Skip to content

Commit

Permalink
refactor: Simplify Sucrase calling
Browse files Browse the repository at this point in the history
  • Loading branch information
Septh committed Feb 6, 2024
1 parent eae305d commit 8a3f1ac
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 20 deletions.
9 changes: 2 additions & 7 deletions source/cjs-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,8 @@ function transpile(m: Module, format: NodeJS.ModuleType, filePath: string) {
// for the fist time, but we'll live with it.
const { transform } = require('./cjs-transform.cjs') as typeof import('./cjs-transform.cjs')
const source = readFileSync(filePath).toString()
const { code, sourceMap } = transform(source, format, filePath)
return m._compile(
code
+ '\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,'
+ Buffer.from(JSON.stringify(sourceMap)).toString('base64'),
filePath
)
const code = transform(source, format, path.basename(filePath))
return m._compile(code, filePath)
}

const unknownType = Symbol()
Expand Down
13 changes: 9 additions & 4 deletions source/cjs-transform.cts
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
import { transform as _transform, type Transform } from 'sucrase'
import { transform as sucrase, type Transform } from 'sucrase'

const transforms: Record<NodeJS.ModuleType, Transform[]> = {
commonjs: [ 'typescript', 'imports' ],
module: [ 'typescript' ]
}

export function transform(source: string, format: NodeJS.ModuleType, filePath: string) {
const { code, sourceMap } = _transform(source, {
const { code, sourceMap } = sucrase(source, {
filePath,
transforms: transforms[format],
preserveDynamicImport: true,
disableESTransforms: true,
injectCreateRequireForImportRequire: true,
keepUnusedImports: true,
filePath,
sourceMapOptions: {
compiledFilename: filePath
}
})

return { code, sourceMap }
sourceMap!.sourceRoot = ''
sourceMap!.sources = [ filePath ]
// sourceMap.sourcesContent = [ source ]

return code + '\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,'
+ Buffer.from(JSON.stringify(sourceMap)).toString('base64')
}
11 changes: 2 additions & 9 deletions source/esm-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { fileURLToPath } from 'node:url'
import { readFile } from 'node:fs/promises'
import { type InitializeHook, type ResolveHook, type LoadHook, type ModuleSource, createRequire } from 'node:module'

const transformer = createRequire(import.meta.url)('./cjs-transform.cjs')
const { transform } = createRequire(import.meta.url)('./cjs-transform.cjs') as typeof import('./cjs-transform.cjs')

let self: string
export const initialize: InitializeHook<string> = data => {
Expand Down Expand Up @@ -36,13 +36,6 @@ export const resolve: ResolveHook = async (specifier, context, nextResolve) => {
return nextResolve(specifier, context)
}

function transpile(source: ModuleSource, format: NodeJS.ModuleType, filePath: string) {
const { code, sourceMap } = transformer.transform(source.toString(), format, filePath)
return code
+ '\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,'
+ Buffer.from(JSON.stringify(sourceMap)).toString('base64')
}

const unknownType = Symbol()
const pkgTypeCache = new Map<string, NodeJS.ModuleType | Symbol>()
async function nearestPackageType(file: string): Promise<NodeJS.ModuleType> {
Expand Down Expand Up @@ -98,7 +91,7 @@ export const load: LoadHook = async (url, context, nextLoad) => {
// - if null is returned for `source`, the CJS loader will be used instead
let { source } = await nextLoad(url, { ...context, format })
if (source)
source = transpile(source, format, filePath)
source = transform(source.toString(), format, path.basename(filePath))

return {
source,
Expand Down

0 comments on commit 8a3f1ac

Please sign in to comment.