-
-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4b1f03c
commit d82d7f0
Showing
8 changed files
with
114 additions
and
104 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { register } from './register.js'; |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import type { GlobalPreloadHook, InitializeHook } from 'module'; | ||
|
||
export const initialize: InitializeHook = async (data) => { | ||
if (!data) { | ||
throw new Error('tsx must be loaded with --import instead of --loader\nThe --loader flag was deprecated in Node v20.6.0 and v18.19.0'); | ||
} | ||
}; | ||
|
||
/** | ||
* Technically globalPreload is deprecated so it should be in loaders-deprecated | ||
* but it shares a closure with the new load hook | ||
*/ | ||
export const globalPreload: GlobalPreloadHook = () => ` | ||
const require = getBuiltin('module').createRequire("${import.meta.url}"); | ||
require('../source-map.cjs').installSourceMapSupport(); | ||
`; | ||
|
||
export { load } from './load.js'; | ||
export { resolve } from './resolve.js'; |
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,84 @@ | ||
import { fileURLToPath } from 'url'; | ||
import type { LoadHook } from 'module'; | ||
import type { TransformOptions } from 'esbuild'; | ||
import { transform } from '../../utils/transform/index.js'; | ||
import { transformDynamicImport } from '../../utils/transform/transform-dynamic-import.js'; | ||
import { installSourceMapSupport } from '../../source-map.js'; | ||
import { isFeatureSupported, importAttributes } from '../../utils/node-features.js'; | ||
import { parent } from '../../utils/ipc/client.js'; | ||
import { | ||
fileMatcher, | ||
tsExtensionsPattern, | ||
isJsonPattern, | ||
} from './utils.js'; | ||
|
||
const applySourceMap = installSourceMapSupport(); | ||
|
||
const contextAttributesProperty = ( | ||
isFeatureSupported(importAttributes) | ||
? 'importAttributes' | ||
: 'importAssertions' | ||
); | ||
|
||
export const load: LoadHook = async ( | ||
url, | ||
context, | ||
defaultLoad, | ||
) => { | ||
/* | ||
Filter out node:* | ||
Maybe only handle files that start with file:// | ||
*/ | ||
if (parent.send) { | ||
parent.send({ | ||
type: 'dependency', | ||
path: url, | ||
}); | ||
} | ||
|
||
if (isJsonPattern.test(url)) { | ||
if (!context[contextAttributesProperty]) { | ||
context[contextAttributesProperty] = {}; | ||
} | ||
|
||
context[contextAttributesProperty]!.type = 'json'; | ||
} | ||
|
||
const loaded = await defaultLoad(url, context); | ||
|
||
// CommonJS and Internal modules (e.g. node:*) | ||
if (!loaded.source) { | ||
return loaded; | ||
} | ||
|
||
const filePath = url.startsWith('file://') ? fileURLToPath(url) : url; | ||
const code = loaded.source.toString(); | ||
|
||
if ( | ||
// Support named imports in JSON modules | ||
loaded.format === 'json' | ||
|| tsExtensionsPattern.test(url) | ||
) { | ||
const transformed = await transform( | ||
code, | ||
filePath, | ||
{ | ||
tsconfigRaw: fileMatcher?.(filePath) as TransformOptions['tsconfigRaw'], | ||
}, | ||
); | ||
|
||
return { | ||
format: 'module', | ||
source: applySourceMap(transformed), | ||
}; | ||
} | ||
|
||
if (loaded.format === 'module') { | ||
const dynamicImportTransformed = transformDynamicImport(filePath, code); | ||
if (dynamicImportTransformed) { | ||
loaded.source = applySourceMap(dynamicImportTransformed); | ||
} | ||
} | ||
|
||
return loaded; | ||
}; |
File renamed without changes.
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
File renamed without changes.
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,13 +1,13 @@ | ||
import { isMainThread } from 'node:worker_threads'; | ||
import { isFeatureSupported, moduleRegister } from '../utils/node-features.js'; | ||
import { registerLoader } from './register.js'; | ||
import { register } from './api/index.js'; | ||
|
||
// Loaded via --import flag | ||
if ( | ||
isFeatureSupported(moduleRegister) | ||
&& isMainThread | ||
) { | ||
registerLoader(); | ||
register(); | ||
} | ||
|
||
export * from './loaders.js'; | ||
export * from './hook/index.js'; |