-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1650 from embroider-build/esbuild-resolver
Add Esbuild resolver
- Loading branch information
Showing
14 changed files
with
468 additions
and
17 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
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
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
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,44 @@ | ||
import { type ModuleRequest, cleanUrl } from '@embroider/core'; | ||
|
||
export const virtualNamespace = 'embroider'; | ||
|
||
export class EsBuildModuleRequest implements ModuleRequest { | ||
static from( | ||
source: string, | ||
importer: string | undefined, | ||
pluginData: Record<string, any> | undefined | ||
): EsBuildModuleRequest | undefined { | ||
if (!(pluginData?.embroider?.enableCustomResolver ?? true)) { | ||
return; | ||
} | ||
|
||
if (source && importer && source[0] !== '\0') { | ||
let fromFile = cleanUrl(importer); | ||
return new EsBuildModuleRequest(source, fromFile, pluginData?.embroider?.meta, false); | ||
} | ||
} | ||
|
||
private constructor( | ||
readonly specifier: string, | ||
readonly fromFile: string, | ||
readonly meta: Record<string, any> | undefined, | ||
readonly isVirtual: boolean | ||
) {} | ||
|
||
alias(newSpecifier: string) { | ||
return new EsBuildModuleRequest(newSpecifier, this.fromFile, this.meta, this.isVirtual) as this; | ||
} | ||
rehome(newFromFile: string) { | ||
if (this.fromFile === newFromFile) { | ||
return this; | ||
} else { | ||
return new EsBuildModuleRequest(this.specifier, newFromFile, this.meta, this.isVirtual) as this; | ||
} | ||
} | ||
virtualize(filename: string) { | ||
return new EsBuildModuleRequest(filename, this.fromFile, this.meta, true) as this; | ||
} | ||
withMeta(meta: Record<string, any> | undefined): this { | ||
return new EsBuildModuleRequest(this.specifier, this.fromFile, meta, this.isVirtual) as this; | ||
} | ||
} |
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,132 @@ | ||
import type { Plugin as EsBuildPlugin, ImportKind, OnResolveResult, PluginBuild } from 'esbuild'; | ||
import { type PluginItem, transform } from '@babel/core'; | ||
import { | ||
type Resolution, | ||
type ResolverFunction, | ||
ResolverLoader, | ||
virtualContent, | ||
locateEmbroiderWorkingDir, | ||
} from '@embroider/core'; | ||
import { readFileSync, readJSONSync } from 'fs-extra'; | ||
import { EsBuildModuleRequest } from './esbuild-request'; | ||
import assertNever from 'assert-never'; | ||
import { dirname, resolve } from 'path'; | ||
import { hbsToJS } from '@embroider/core'; | ||
import { Preprocessor } from 'content-tag'; | ||
|
||
export function esBuildResolver(root = process.cwd()): EsBuildPlugin { | ||
let resolverLoader = new ResolverLoader(process.cwd()); | ||
let macrosConfig: PluginItem | undefined; | ||
let preprocessor = new Preprocessor(); | ||
|
||
return { | ||
name: 'embroider-esbuild-resolver', | ||
setup(build) { | ||
build.onResolve({ filter: /./ }, async ({ path, importer, pluginData, kind }) => { | ||
let request = EsBuildModuleRequest.from(path, importer, pluginData); | ||
if (!request) { | ||
return null; | ||
} | ||
let resolution = await resolverLoader.resolver.resolve(request, defaultResolve(build, kind)); | ||
switch (resolution.type) { | ||
case 'found': | ||
return resolution.result; | ||
case 'not_found': | ||
return resolution.err; | ||
default: | ||
throw assertNever(resolution); | ||
} | ||
}); | ||
|
||
build.onLoad({ namespace: 'embroider', filter: /./ }, ({ path }) => { | ||
let src = virtualContent(path, resolverLoader.resolver); | ||
if (!macrosConfig) { | ||
macrosConfig = readJSONSync( | ||
resolve(locateEmbroiderWorkingDir(root), 'rewritten-app', 'macros-config.json') | ||
) as PluginItem; | ||
} | ||
return { contents: runMacros(src, path, macrosConfig) }; | ||
}); | ||
|
||
build.onLoad({ filter: /\.gjs$/ }, async ({ path: filename }) => { | ||
const code = readFileSync(filename, 'utf8'); | ||
|
||
const result = transform(preprocessor.process(code, filename), { configFile: 'babel.config.js', filename }); | ||
|
||
if (!result || !result.code) { | ||
throw new Error(`Failed to load file ${filename} in esbuild-hbs-loader`); | ||
} | ||
|
||
const contents = result.code; | ||
|
||
return { contents }; | ||
}); | ||
|
||
build.onLoad({ filter: /\.hbs$/ }, async ({ path: filename }) => { | ||
const code = readFileSync(filename, 'utf8'); | ||
|
||
const result = transform(hbsToJS(code), { configFile: 'babel.config.js', filename }); | ||
|
||
if (!result || !result.code) { | ||
throw new Error(`Failed to load file ${filename} in esbuild-hbs-loader`); | ||
} | ||
|
||
const contents = result.code; | ||
|
||
return { contents }; | ||
}); | ||
|
||
build.onLoad({ filter: /\.js$/ }, ({ path, namespace }) => { | ||
let src: string; | ||
if (namespace === 'embroider') { | ||
src = virtualContent(path, resolverLoader.resolver); | ||
} else { | ||
src = readFileSync(path, 'utf8'); | ||
} | ||
if (!macrosConfig) { | ||
macrosConfig = readJSONSync( | ||
resolve(locateEmbroiderWorkingDir(root), 'rewritten-app', 'macros-config.json') | ||
) as PluginItem; | ||
} | ||
return { contents: runMacros(src, path, macrosConfig) }; | ||
}); | ||
}, | ||
}; | ||
} | ||
|
||
function runMacros(src: string, filename: string, macrosConfig: PluginItem): string { | ||
return transform(src, { | ||
filename, | ||
plugins: [macrosConfig], | ||
})!.code!; | ||
} | ||
|
||
function defaultResolve( | ||
context: PluginBuild, | ||
kind: ImportKind | ||
): ResolverFunction<EsBuildModuleRequest, Resolution<OnResolveResult, OnResolveResult>> { | ||
return async (request: EsBuildModuleRequest) => { | ||
if (request.isVirtual) { | ||
return { | ||
type: 'found', | ||
result: { path: request.specifier, namespace: 'embroider' }, | ||
}; | ||
} | ||
let result = await context.resolve(request.specifier, { | ||
importer: request.fromFile, | ||
resolveDir: dirname(request.fromFile), | ||
kind, | ||
pluginData: { | ||
embroider: { | ||
enableCustomResolver: false, | ||
meta: request.meta, | ||
}, | ||
}, | ||
}); | ||
if (result.errors.length > 0) { | ||
return { type: 'not_found', err: result }; | ||
} else { | ||
return { type: 'found', result }; | ||
} | ||
}; | ||
} |
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
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
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
Oops, something went wrong.