-
Notifications
You must be signed in to change notification settings - Fork 142
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
Showing
11 changed files
with
263 additions
and
249 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,101 @@ | ||
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'; | ||
|
||
export function esBuildResolver(root = process.cwd()): EsBuildPlugin { | ||
let resolverLoader = new ResolverLoader(process.cwd()); | ||
let macrosConfig: PluginItem | undefined; | ||
|
||
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: /\.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
import { esBuildResolver } from './esbuild-resolver'; | ||
|
||
export interface OptimizeDeps { | ||
exclude?: string[]; | ||
[key: string]: unknown; | ||
} | ||
|
||
export function optimizeDeps(): OptimizeDeps { | ||
return { | ||
exclude: ['@embroider/macros'], | ||
include: ['ember-welcome-page'], | ||
esbuildOptions: { | ||
plugins: [esBuildResolver()], | ||
}, | ||
}; | ||
} |
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.