@node-loaders/esbuild
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -0,2 +1,6 @@ | ||
import EsbuildLoader from './esbuild-loader.js'; | ||
declare const loader: EsbuildLoader; | ||
export default loader; | ||
export declare const resolve: (specifier: string, context: import("@node-loaders/core").ResolveContext, nextResolve?: import("@node-loaders/core").NextResolve | undefined) => Promise<import("@node-loaders/core").ResolvedModule>; | ||
export declare const load: (url: string, context: import("@node-loaders/core").LoadContext, nextLoad?: import("@node-loaders/core").NextLoad | undefined) => Promise<import("@node-loaders/core").LoadedModule>; | ||
export { default as EsbuildLoader } from './esbuild-loader.js'; |
import EsbuildLoader from './esbuild-loader.js'; | ||
const routerLoader = new EsbuildLoader({ allowDefaults: true }); | ||
export const resolve = routerLoader.exportResolve(); | ||
export const load = routerLoader.exportLoad(); | ||
const loader = new EsbuildLoader({ allowDefaults: true }); | ||
export default loader; | ||
export const resolve = loader.exportResolve(); | ||
export const load = loader.exportLoad(); | ||
export { default as EsbuildLoader } from './esbuild-loader.js'; | ||
//# sourceMappingURL=compat.js.map |
@@ -10,8 +10,8 @@ import { type TransformOptions } from 'esbuild'; | ||
constructor(options?: EsbuildLoaderOptions); | ||
protected lookForExistingEsbuildFilePath(filePath: string): Promise<string | undefined>; | ||
protected _matchesEspecifier(specifier: string, context?: ResolveContext | undefined): boolean; | ||
protected _resolve(specifier: string, context: ResolveContext, nextResolve: NextResolve): Promise<ResolvedModule>; | ||
protected _load(url: string, context: LoadContext, nextLoad: NextLoad): Promise<LoadedModule>; | ||
protected transform(url: string, context: LoadContext): Promise<LoadedModule>; | ||
protected getOptions(options?: TransformOptions): TransformOptions; | ||
lookForExistingEsbuildFilePath(filePath: string): Promise<string | undefined>; | ||
_handlesEspecifier(specifier: string, context?: ResolveContext | undefined): boolean; | ||
_resolve(specifier: string, context: ResolveContext, nextResolve: NextResolve): Promise<ResolvedModule>; | ||
_load(url: string, context: LoadContext, nextLoad: NextLoad): Promise<LoadedModule>; | ||
transform(filePath: string, format: string): Promise<string>; | ||
getOptions(options: TransformOptions & Required<Pick<TransformOptions, 'sourcefile'>>): TransformOptions; | ||
} |
import { readFile } from 'node:fs/promises'; | ||
import { dirname } from 'node:path'; | ||
import process from 'node:process'; | ||
import { fileURLToPath, pathToFileURL } from 'node:url'; | ||
import process from 'node:process'; | ||
import { transform } from 'esbuild'; | ||
import { getTsconfig } from 'get-tsconfig'; | ||
import LoaderBase, { isFileSpecifier, isPackageJsonImportSpecifier, } from '@node-loaders/core'; | ||
import { existingFile, lookForDefaultModule, specifierToFilePath } from '@node-loaders/resolve'; | ||
import { detectFormatForEsbuildFileExtension, detectFormatForEsbuildFilePath, isEsbuildExtensionSupported, lookForEsbuildReplacementFile, } from './esbuild-module.js'; | ||
import { existingFile, lookForDefaultModule, specifierToFilePath, detectPackageJsonType, resolvePackageJsonImports, } from '@node-loaders/resolve'; | ||
import { detectFormatForEsbuildFileExtension, isEsbuildExtensionSupported, lookForEsbuildReplacementFile } from './esbuild-module.js'; | ||
export default class EsbuildLoader extends LoaderBase { | ||
@@ -13,3 +15,3 @@ allowDefaults; | ||
// We want builtin modules and package import to pass through | ||
super({ matchBuiltinSpecifier: false, matchPackageSpecifier: false, ...options }); | ||
super('esbuild', { forwardBuiltinSpecifiers: true, forwardPackageSpecifiers: true, ...options }); | ||
const { allowDefaults = false } = options; | ||
@@ -23,9 +25,9 @@ this.allowDefaults = allowDefaults; | ||
// Optionally look for js directory imports and imports without extension | ||
(this.allowDefaults ? lookForDefaultModule(filePath) : undefined) ?? | ||
(this.allowDefaults ? await lookForDefaultModule(filePath) : undefined) ?? | ||
// Optionally look for ts directory imports and imports without extension | ||
(this.allowDefaults ? lookForDefaultModule(filePath, '.ts') : undefined) ?? | ||
(this.allowDefaults ? await lookForDefaultModule(filePath, '.ts') : undefined) ?? | ||
// Look for replacements files | ||
lookForEsbuildReplacementFile(filePath)); | ||
} | ||
_matchesEspecifier(specifier, context) { | ||
_handlesEspecifier(specifier, context) { | ||
return isFileSpecifier(specifier); | ||
@@ -35,5 +37,3 @@ } | ||
if (isPackageJsonImportSpecifier(specifier)) { | ||
// Delegate package.json imports mapping to the chain. | ||
const resolved = await nextResolve(specifier, context); | ||
specifier = resolved.url; | ||
specifier = await resolvePackageJsonImports(specifier, context.parentURL); | ||
} | ||
@@ -53,2 +53,3 @@ const filePath = specifierToFilePath(specifier, context.parentURL); | ||
} | ||
this.log(`Forwarding ${specifier}`); | ||
return nextResolve(specifier, context); | ||
@@ -58,4 +59,7 @@ } | ||
if (isEsbuildExtensionSupported(url)) { | ||
const filePath = fileURLToPath(url); | ||
const format = context.format ?? (await detectPackageJsonType(filePath)); | ||
return { | ||
...(await this.transform(url, context)), | ||
format, | ||
source: await this.transform(filePath, format), | ||
shortCircuit: true, | ||
@@ -66,6 +70,5 @@ }; | ||
} | ||
async transform(url, context) { | ||
const sourcefile = fileURLToPath(url); | ||
const code = await readFile(sourcefile); | ||
const format = context.format ?? (await detectFormatForEsbuildFilePath(sourcefile)); | ||
async transform(filePath, format) { | ||
this.log(`Transforming ${filePath}`); | ||
const code = await readFile(filePath); | ||
const esbuildFormat = format === 'module' ? 'esm' : 'cjs'; | ||
@@ -81,14 +84,16 @@ // We are transpiling, enable sourcemap is available | ||
const { code: source } = await transform(code.toString(), this.getOptions({ | ||
sourcefile, | ||
sourcefile: filePath, | ||
format: esbuildFormat, | ||
})); | ||
return { format, source }; | ||
return source; | ||
} | ||
getOptions(options) { | ||
const tsconfigRaw = getTsconfig(dirname(options.sourcefile))?.config; | ||
return { | ||
loader: 'default', | ||
target: `node14`, | ||
minifyWhitespace: true, | ||
keepNames: true, | ||
sourcemap: 'inline', | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
tsconfigRaw: tsconfigRaw, | ||
...options, | ||
@@ -98,1 +103,2 @@ }; | ||
} | ||
//# sourceMappingURL=esbuild-loader.js.map |
import { type Format } from '@node-loaders/core'; | ||
export declare function detectFormatForEsbuildFileExtension(filePath: string): Format; | ||
export declare function detectFormatForEsbuildFilePath(filePath: string): Promise<Format>; | ||
export declare function isEsbuildExtensionSupported(filePath: string): boolean; | ||
export declare function lookForEsbuildReplacementFile(filePath: string): Promise<string | undefined>; |
/* eslint-disable @typescript-eslint/naming-convention */ | ||
import { dirname, extname } from 'node:path'; | ||
import { readPackageUp } from 'read-pkg-up'; | ||
import { lookForAlternativeFiles } from '@node-loaders/resolve'; | ||
import { extname } from 'node:path'; | ||
import { resolveAlternativeFile } from '@node-loaders/resolve'; | ||
const esbuildExtensions = new Set(['.ts', '.cts', '.mts', '.tsx']); | ||
@@ -19,6 +18,2 @@ const formatForExtension = { | ||
} | ||
export async function detectFormatForEsbuildFilePath(filePath) { | ||
const read = await readPackageUp({ cwd: dirname(filePath) }); | ||
return read?.packageJson?.type ?? 'commonjs'; | ||
} | ||
export function isEsbuildExtensionSupported(filePath) { | ||
@@ -30,7 +25,6 @@ return esbuildExtensions.has(extname(filePath)); | ||
if (replacementExtensions) { | ||
const alternativeFiles = await lookForAlternativeFiles(filePath); | ||
const compatibleFiles = alternativeFiles.filter(file => replacementExtensions.includes(extname(file))); | ||
return compatibleFiles.length > 0 ? compatibleFiles[0] : undefined; | ||
return resolveAlternativeFile(filePath, replacementExtensions); | ||
} | ||
return undefined; | ||
} | ||
//# sourceMappingURL=esbuild-module.js.map |
@@ -0,2 +1,6 @@ | ||
import EsbuildLoader from './esbuild-loader.js'; | ||
declare const loader: EsbuildLoader; | ||
export default loader; | ||
export declare const resolve: (specifier: string, context: import("@node-loaders/core").ResolveContext, nextResolve?: import("@node-loaders/core").NextResolve | undefined) => Promise<import("@node-loaders/core").ResolvedModule>; | ||
export declare const load: (url: string, context: import("@node-loaders/core").LoadContext, nextLoad?: import("@node-loaders/core").NextLoad | undefined) => Promise<import("@node-loaders/core").LoadedModule>; | ||
export { default as EsbuildLoader } from './esbuild-loader.js'; |
import EsbuildLoader from './esbuild-loader.js'; | ||
const routerLoader = new EsbuildLoader(); | ||
export const resolve = routerLoader.exportResolve(); | ||
export const load = routerLoader.exportLoad(); | ||
const loader = new EsbuildLoader(); | ||
export default loader; | ||
export const resolve = loader.exportResolve(); | ||
export const load = loader.exportLoad(); | ||
export { default as EsbuildLoader } from './esbuild-loader.js'; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@node-loaders/esbuild", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"private": false, | ||
"keywords": [ | ||
"esm", | ||
"import", | ||
"loader", | ||
"node", | ||
"esbuild", | ||
"typescript", | ||
"esm-loader" | ||
], | ||
"bugs": "https://github.com/node-loaders/loaders/issues", | ||
@@ -12,5 +21,10 @@ "repository": { | ||
"type": "module", | ||
"imports": { | ||
"#local": "./test/fixtures/imports/default-fallback/index.js" | ||
}, | ||
"exports": { | ||
".": "./dist/index.js", | ||
"./compat": "./dist/compat.js" | ||
"./compat": "./dist/compat.js", | ||
"./node14": "./dist/node14.js", | ||
"./compat-node14": "./dist/compat-node14.js" | ||
}, | ||
@@ -25,9 +39,12 @@ "files": [ | ||
"fix": "xo --fix", | ||
"test": "npm run build && mocha" | ||
"pretest": "xo", | ||
"test": "mocha", | ||
"test-coverage": "c8 mocha", | ||
"test-node14": "mocha --config .mocharc.node14.cjs test" | ||
}, | ||
"dependencies": { | ||
"@node-loaders/core": "^0.1.0", | ||
"@node-loaders/resolve": "^0.1.0", | ||
"@node-loaders/core": "^0.2.0", | ||
"@node-loaders/resolve": "^0.2.0", | ||
"esbuild": "^0.15.16", | ||
"read-pkg-up": "^9.1.0" | ||
"get-tsconfig": "^4.2.0" | ||
}, | ||
@@ -40,3 +57,3 @@ "engines": { | ||
}, | ||
"gitHead": "862d4ae439701d82cd170cc1c0d764284bc82105" | ||
"gitHead": "ccadd1fe4e68b4b8c47e0eac438df092a45f6541" | ||
} |
@@ -7,6 +7,20 @@ # @node-loaders/esbuild | ||
See [usage](../../README.md) | ||
For configuration tools, refer to [usage](https://github.com/node-loaders/loaders#usage) | ||
When using in combination with others @node-loaders modules make sure to use [@node-loaders/auto](https://github.com/node-loaders/loaders/tree/main/workspaces/auto#node-loadersauto) for better interoperability. | ||
### Node 14 | ||
For Node 14 compatibility use the node14 exported path `@node-loaders/esbuild/node14`. | ||
See [Node 14](https://github.com/node-loaders/loaders#node_14) | ||
### Directory and extension-less imports | ||
ESM is strict on requiring extension. | ||
If there is a need to allow directory and extension-less imports use the `compat` exports. | ||
`@node-loaders/esbuild/compat` and `@node-loaders/esbuild/compat-node14` | ||
## License | ||
MIT |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27722
21
363
26
1
+ Addedget-tsconfig@^4.2.0
+ Added@node-loaders/core@0.2.0(transitive)
+ Added@node-loaders/resolve@0.2.0(transitive)
+ Addedget-tsconfig@4.8.1(transitive)
+ Addedresolve-pkg-maps@1.0.0(transitive)
- Removedread-pkg-up@^9.1.0
- Removed@node-loaders/core@0.1.0(transitive)
- Removed@node-loaders/resolve@0.1.0(transitive)
Updated@node-loaders/core@^0.2.0
Updated@node-loaders/resolve@^0.2.0