@embroider/vite 0.2.2-unstable.6caeed4 → 0.2.2-unstable.78dd1bb
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +5 -5
- package/src/esbuild-request.js +6 -1
- package/src/esbuild-request.js.map +1 -1
- package/src/resolver.js +18 -2
- package/src/resolver.js.map +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@embroider/vite",
|
|
3
|
-
"version": "0.2.2-unstable.
|
|
3
|
+
"version": "0.2.2-unstable.78dd1bb",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
},
|
|
11
11
|
"peerDependencies": {
|
|
12
12
|
"vite": "^5.2.0",
|
|
13
|
-
"@embroider/core": "^3.4.20-unstable.
|
|
13
|
+
"@embroider/core": "^3.4.20-unstable.78dd1bb"
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
16
|
"@babel/core": "^7.22.9",
|
|
@@ -27,8 +27,8 @@
|
|
|
27
27
|
"send": "^0.18.0",
|
|
28
28
|
"source-map-url": "^0.4.1",
|
|
29
29
|
"terser": "^5.7.0",
|
|
30
|
-
"@embroider/macros": "1.16.10-unstable.
|
|
31
|
-
"@embroider/reverse-exports": "0.1.1-unstable.
|
|
30
|
+
"@embroider/macros": "1.16.10-unstable.78dd1bb",
|
|
31
|
+
"@embroider/reverse-exports": "0.1.1-unstable.78dd1bb"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"@types/babel__core": "^7.20.1",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"@types/send": "^0.17.4",
|
|
39
39
|
"rollup": "^4.18.0",
|
|
40
40
|
"vite": "^5.3.3",
|
|
41
|
-
"@embroider/core": "^3.4.20-unstable.
|
|
41
|
+
"@embroider/core": "^3.4.20-unstable.78dd1bb"
|
|
42
42
|
},
|
|
43
43
|
"files": [
|
|
44
44
|
"index.mjs",
|
package/src/esbuild-request.js
CHANGED
|
@@ -74,7 +74,12 @@ export class EsBuildRequestAdapter {
|
|
|
74
74
|
// and non-strict handlebars (which resolves
|
|
75
75
|
// components/helpers/modifiers against the app's global pool).
|
|
76
76
|
let pkg = this.packageCache.ownerOfFile(result.path);
|
|
77
|
-
if ((pkg === null || pkg === void 0 ? void 0 : pkg.root) === this.packageCache.appRoot
|
|
77
|
+
if ((pkg === null || pkg === void 0 ? void 0 : pkg.root) === this.packageCache.appRoot &&
|
|
78
|
+
// vite provides node built-in polyfills under a custom namespace and we dont
|
|
79
|
+
// want to interrupt that. We'd prefer they get bundled in the dep optimizer normally,
|
|
80
|
+
// rather than getting deferred to the app build (which also works, but means they didn't
|
|
81
|
+
// get pre-optimized).
|
|
82
|
+
(result.namespace === 'file' || result.namespace.startsWith('embroider-'))) {
|
|
78
83
|
let externalizedName = request.specifier;
|
|
79
84
|
if (!packageName(externalizedName)) {
|
|
80
85
|
// the request was a relative path. This won't remain valid once
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"esbuild-request.js","sourceRoot":"","sources":["esbuild-request.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,iBAAiB,CAAC;AACnC,MAAM,EAAE,QAAQ,EAAE,WAAW,EAAE,GAAG,IAAI,CAAC;AAEvC,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAG/B,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAK1D,MAAM,OAAO,qBAAqB;IAChC,MAAM,CAAC,MAAM,CAAC,EACZ,YAAY,EACZ,KAAK,EACL,KAAK,EACL,IAAI,EACJ,IAAI,EACJ,QAAQ,EACR,UAAU,GASX;;QACC,IAAI,CAAC,CAAC,MAAA,MAAA,UAAU,aAAV,UAAU,uBAAV,UAAU,CAAE,SAAS,0CAAE,oBAAoB,mCAAI,IAAI,CAAC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AAE,CAAC;IAClC,IAAI,MAAM,GAAG,MAAA,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,mCAAI,SAAS,CAAC;IAC1C,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;IACnB,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import core from '@embroider/core';\nconst { cleanUrl, packageName } = core;\nimport type { ImportKind, OnResolveResult, PluginBuild } from 'esbuild';\nimport { dirname } from 'path';\n\nimport type { PackageCache as _PackageCache, Resolution, ModuleRequest, RequestAdapter } from '@embroider/core';\nimport { externalName } from '@embroider/reverse-exports';\n\ntype PublicAPI<T> = { [K in keyof T]: T[K] };\ntype PackageCache = PublicAPI<_PackageCache>;\n\nexport class EsBuildRequestAdapter implements RequestAdapter<Resolution<OnResolveResult, OnResolveResult>> {\n static create({\n packageCache,\n phase,\n build,\n kind,\n path,\n importer,\n pluginData,\n }: {\n packageCache: PackageCache;\n phase: 'bundling' | 'other';\n build: PluginBuild;\n kind: ImportKind;\n path: string;\n importer: string | undefined;\n pluginData: Record<string, any> | undefined;\n }) {\n if (!(pluginData?.embroider?.enableCustomResolver ?? true)) {\n return;\n }\n\n if (path && importer && path[0] !== '\\0' && !path.startsWith('virtual-module:')) {\n let fromFile = cleanUrl(importer);\n return {\n initialState: {\n specifier: path,\n fromFile,\n meta: pluginData?.embroider?.meta,\n },\n adapter: new EsBuildRequestAdapter(packageCache, phase, build, kind),\n };\n }\n }\n\n private constructor(\n private packageCache: PackageCache,\n private phase: 'bundling' | 'other',\n private context: PluginBuild,\n private kind: ImportKind\n ) {}\n\n get debugType() {\n return 'esbuild';\n }\n\n notFoundResponse(\n request: core.ModuleRequest<core.Resolution<OnResolveResult, OnResolveResult>>\n ): core.Resolution<OnResolveResult, OnResolveResult> {\n return {\n type: 'not_found',\n err: {\n errors: [{ text: `module not found ${request.specifier}` }],\n },\n };\n }\n\n virtualResponse(\n _request: core.ModuleRequest<core.Resolution<OnResolveResult, OnResolveResult>>,\n virtualFileName: string\n ): core.Resolution<OnResolveResult, OnResolveResult> {\n return {\n type: 'found',\n filename: virtualFileName,\n result: { path: virtualFileName, namespace: 'embroider-virtual' },\n isVirtual: true,\n };\n }\n\n async resolve(\n request: ModuleRequest<Resolution<OnResolveResult, OnResolveResult>>\n ): Promise<Resolution<OnResolveResult, OnResolveResult>> {\n requestStatus(request.specifier);\n\n let result = await this.context.resolve(request.specifier, {\n importer: request.fromFile,\n resolveDir: dirname(request.fromFile),\n kind: this.kind,\n pluginData: {\n embroider: {\n enableCustomResolver: false,\n meta: request.meta,\n },\n },\n });\n\n let status = readStatus(request.specifier);\n\n if (result.errors.length > 0 || status === 'not_found') {\n return { type: 'not_found', err: result };\n } else if (result.external) {\n return { type: 'ignored', result };\n } else {\n if (this.phase === 'bundling') {\n // we need to ensure that we don't traverse back into the app while\n // doing dependency pre-bundling. There are multiple ways an addon can\n // resolve things from the app, due to the existince of both app-js\n // (modules in addons that are logically part of the app's namespace)\n // and non-strict handlebars (which resolves\n // components/helpers/modifiers against the app's global pool).\n let pkg = this.packageCache.ownerOfFile(result.path);\n if (pkg?.root === this.packageCache.appRoot) {\n let externalizedName = request.specifier;\n if (!packageName(externalizedName)) {\n // the request was a relative path. This won't remain valid once\n // it has been bundled into vite/deps. But we know it targets the\n // app, so we can always convert it into a non-relative import\n // from the app's namespace\n //\n // IMPORTANT: whenever an addon resolves a relative path to the\n // app, it does so because our code in the core resolver has\n // rewritten the request to be relative to the app's root. So here\n // we will only ever encounter relative paths that are already\n // relative to the app's root directory.\n externalizedName = externalName(pkg.packageJSON, externalizedName) || externalizedName;\n }\n return {\n type: 'ignored',\n result: {\n path: externalizedName,\n external: true,\n },\n };\n }\n }\n return { type: 'found', filename: result.path, result, isVirtual: false };\n }\n }\n}\n\n/*\n This is an unfortunate necessity. During depscan, vite deliberately hides\n information from esbuild. Specifically, it treats \"not found\" and \"this is an\n external dependency\" as both \"external: true\". But we really care about the\n difference, since we have fallback behaviors for the \"not found\" case. Using\n this global state, our rollup resolver plugin can observe what vite is\n actually doing and communicate that knowledge outward to our esbuild resolver\n plugin.\n */\nfunction sharedGlobalState() {\n let channel = (globalThis as any).__embroider_vite_resolver_channel__ as\n | undefined\n | Map<string, 'pending' | 'found' | 'not_found'>;\n if (!channel) {\n channel = new Map();\n (globalThis as any).__embroider_vite_resolver_channel__ = channel;\n }\n return channel;\n}\n\nfunction requestStatus(id: string): void {\n sharedGlobalState().set(id, 'pending');\n}\n\nexport function writeStatus(id: string, status: 'found' | 'not_found'): void {\n let channel = sharedGlobalState();\n if (channel.get(id) === 'pending') {\n channel.set(id, status);\n }\n}\n\nfunction readStatus(id: string): 'pending' | 'not_found' | 'found' {\n let channel = sharedGlobalState();\n let result = channel.get(id) ?? 'pending';\n channel.delete(id);\n return result;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"esbuild-request.js","sourceRoot":"","sources":["esbuild-request.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,iBAAiB,CAAC;AACnC,MAAM,EAAE,QAAQ,EAAE,WAAW,EAAE,GAAG,IAAI,CAAC;AAEvC,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAG/B,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAK1D,MAAM,OAAO,qBAAqB;IAChC,MAAM,CAAC,MAAM,CAAC,EACZ,YAAY,EACZ,KAAK,EACL,KAAK,EACL,IAAI,EACJ,IAAI,EACJ,QAAQ,EACR,UAAU,GASX;;QACC,IAAI,CAAC,CAAC,MAAA,MAAA,UAAU,aAAV,UAAU,uBAAV,UAAU,CAAE,SAAS,0CAAE,oBAAoB,mCAAI,IAAI,CAAC,EAAE,CAAC;YAC3D,OAAO;QACT,CAAC;QAED,IAAI,IAAI,IAAI,QAAQ,IAAI,IAAI,CAAC,CAAC,CAAC,KAAK,IAAI,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,iBAAiB,CAAC,EAAE,CAAC;YAChF,IAAI,QAAQ,GAAG,QAA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AAA,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,mCAAI,SAAS,CAAC;IAC1C,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;IACnB,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import core from '@embroider/core';\nconst { cleanUrl, packageName } = core;\nimport type { ImportKind, OnResolveResult, PluginBuild } from 'esbuild';\nimport { dirname } from 'path';\n\nimport type { PackageCache as _PackageCache, Resolution, ModuleRequest, RequestAdapter } from '@embroider/core';\nimport { externalName } from '@embroider/reverse-exports';\n\ntype PublicAPI<T> = { [K in keyof T]: T[K] };\ntype PackageCache = PublicAPI<_PackageCache>;\n\nexport class EsBuildRequestAdapter implements RequestAdapter<Resolution<OnResolveResult, OnResolveResult>> {\n static create({\n packageCache,\n phase,\n build,\n kind,\n path,\n importer,\n pluginData,\n }: {\n packageCache: PackageCache;\n phase: 'bundling' | 'other';\n build: PluginBuild;\n kind: ImportKind;\n path: string;\n importer: string | undefined;\n pluginData: Record<string, any> | undefined;\n }) {\n if (!(pluginData?.embroider?.enableCustomResolver ?? true)) {\n return;\n }\n\n if (path && importer && path[0] !== '\\0' && !path.startsWith('virtual-module:')) {\n let fromFile = cleanUrl(importer);\n return {\n initialState: {\n specifier: path,\n fromFile,\n meta: pluginData?.embroider?.meta,\n },\n adapter: new EsBuildRequestAdapter(packageCache, phase, build, kind),\n };\n }\n }\n\n private constructor(\n private packageCache: PackageCache,\n private phase: 'bundling' | 'other',\n private context: PluginBuild,\n private kind: ImportKind\n ) {}\n\n get debugType() {\n return 'esbuild';\n }\n\n notFoundResponse(\n request: core.ModuleRequest<core.Resolution<OnResolveResult, OnResolveResult>>\n ): core.Resolution<OnResolveResult, OnResolveResult> {\n return {\n type: 'not_found',\n err: {\n errors: [{ text: `module not found ${request.specifier}` }],\n },\n };\n }\n\n virtualResponse(\n _request: core.ModuleRequest<core.Resolution<OnResolveResult, OnResolveResult>>,\n virtualFileName: string\n ): core.Resolution<OnResolveResult, OnResolveResult> {\n return {\n type: 'found',\n filename: virtualFileName,\n result: { path: virtualFileName, namespace: 'embroider-virtual' },\n isVirtual: true,\n };\n }\n\n async resolve(\n request: ModuleRequest<Resolution<OnResolveResult, OnResolveResult>>\n ): Promise<Resolution<OnResolveResult, OnResolveResult>> {\n requestStatus(request.specifier);\n\n let result = await this.context.resolve(request.specifier, {\n importer: request.fromFile,\n resolveDir: dirname(request.fromFile),\n kind: this.kind,\n pluginData: {\n embroider: {\n enableCustomResolver: false,\n meta: request.meta,\n },\n },\n });\n\n let status = readStatus(request.specifier);\n\n if (result.errors.length > 0 || status === 'not_found') {\n return { type: 'not_found', err: result };\n } else if (result.external) {\n return { type: 'ignored', result };\n } else {\n if (this.phase === 'bundling') {\n // we need to ensure that we don't traverse back into the app while\n // doing dependency pre-bundling. There are multiple ways an addon can\n // resolve things from the app, due to the existince of both app-js\n // (modules in addons that are logically part of the app's namespace)\n // and non-strict handlebars (which resolves\n // components/helpers/modifiers against the app's global pool).\n let pkg = this.packageCache.ownerOfFile(result.path);\n if (\n pkg?.root === this.packageCache.appRoot &&\n // vite provides node built-in polyfills under a custom namespace and we dont \n // want to interrupt that. We'd prefer they get bundled in the dep optimizer normally,\n // rather than getting deferred to the app build (which also works, but means they didn't\n // get pre-optimized).\n (result.namespace === 'file' || result.namespace.startsWith('embroider-'))\n ) {\n let externalizedName = request.specifier;\n if (!packageName(externalizedName)) {\n // the request was a relative path. This won't remain valid once\n // it has been bundled into vite/deps. But we know it targets the\n // app, so we can always convert it into a non-relative import\n // from the app's namespace\n //\n // IMPORTANT: whenever an addon resolves a relative path to the\n // app, it does so because our code in the core resolver has\n // rewritten the request to be relative to the app's root. So here\n // we will only ever encounter relative paths that are already\n // relative to the app's root directory.\n externalizedName = externalName(pkg.packageJSON, externalizedName) || externalizedName;\n }\n return {\n type: 'ignored',\n result: {\n path: externalizedName,\n external: true,\n },\n };\n }\n }\n return { type: 'found', filename: result.path, result, isVirtual: false };\n }\n }\n}\n\n/*\n This is an unfortunate necessity. During depscan, vite deliberately hides\n information from esbuild. Specifically, it treats \"not found\" and \"this is an\n external dependency\" as both \"external: true\". But we really care about the\n difference, since we have fallback behaviors for the \"not found\" case. Using\n this global state, our rollup resolver plugin can observe what vite is\n actually doing and communicate that knowledge outward to our esbuild resolver\n plugin.\n */\nfunction sharedGlobalState() {\n let channel = (globalThis as any).__embroider_vite_resolver_channel__ as\n | undefined\n | Map<string, 'pending' | 'found' | 'not_found'>;\n if (!channel) {\n channel = new Map();\n (globalThis as any).__embroider_vite_resolver_channel__ = channel;\n }\n return channel;\n}\n\nfunction requestStatus(id: string): void {\n sharedGlobalState().set(id, 'pending');\n}\n\nexport function writeStatus(id: string, status: 'found' | 'not_found'): void {\n let channel = sharedGlobalState();\n if (channel.get(id) === 'pending') {\n channel.set(id, status);\n }\n}\n\nfunction readStatus(id: string): 'pending' | 'not_found' | 'found' {\n let channel = sharedGlobalState();\n let result = channel.get(id) ?? 'pending';\n channel.delete(id);\n return result;\n}\n"]}
|
package/src/resolver.js
CHANGED
|
@@ -15,9 +15,13 @@ export function resolver() {
|
|
|
15
15
|
let server;
|
|
16
16
|
const virtualDeps = new Map();
|
|
17
17
|
const notViteDeps = new Set();
|
|
18
|
+
let mode = '';
|
|
18
19
|
return {
|
|
19
20
|
name: 'embroider-resolver',
|
|
20
21
|
enforce: 'pre',
|
|
22
|
+
configResolved(config) {
|
|
23
|
+
mode = config.mode;
|
|
24
|
+
},
|
|
21
25
|
configureServer(s) {
|
|
22
26
|
server = s;
|
|
23
27
|
server.watcher.on('all', (_eventName, path) => {
|
|
@@ -98,9 +102,21 @@ export function resolver() {
|
|
|
98
102
|
});
|
|
99
103
|
this.emitFile({
|
|
100
104
|
type: 'asset',
|
|
101
|
-
fileName: '@embroider/virtual/
|
|
102
|
-
source: virtualContent(resolve(resolverLoader.resolver.options.engines[0].root, '-embroider-
|
|
105
|
+
fileName: '@embroider/virtual/vendor.css',
|
|
106
|
+
source: virtualContent(resolve(resolverLoader.resolver.options.engines[0].root, '-embroider-vendor-styles.css'), resolverLoader.resolver).src,
|
|
103
107
|
});
|
|
108
|
+
if (mode !== 'production') {
|
|
109
|
+
this.emitFile({
|
|
110
|
+
type: 'asset',
|
|
111
|
+
fileName: '@embroider/virtual/test-support.js',
|
|
112
|
+
source: virtualContent(resolve(resolverLoader.resolver.options.engines[0].root, '-embroider-test-support.js'), resolverLoader.resolver).src,
|
|
113
|
+
});
|
|
114
|
+
this.emitFile({
|
|
115
|
+
type: 'asset',
|
|
116
|
+
fileName: '@embroider/virtual/test-support.css',
|
|
117
|
+
source: virtualContent(resolve(resolverLoader.resolver.options.engines[0].root, '-embroider-test-support-styles.css'), resolverLoader.resolver).src,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
104
120
|
},
|
|
105
121
|
};
|
|
106
122
|
}
|
package/src/resolver.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resolver.js","sourceRoot":"","sources":["resolver.ts"],"names":[],"mappings":"AACA,OAAO,IAAI,EAAE,EAAE,aAAa,EAAiB,MAAM,iBAAiB,CAAC;AACrE,MAAM,EAAE,cAAc,EAAE,cAAc,EAAE,gBAAgB,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;AACpF,OAAO,EAAE,oBAAoB,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AACnE,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,SAAS,MAAM,OAAO,CAAC;AAC9B,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AACrC,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAEnD,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAC1D,OAAO,EAAE,MAAM,UAAU,CAAC;AAC1B,OAAO,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AAEpC,MAAM,EAAE,iBAAiB,EAAE,cAAc,EAAE,GAA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,CAAC,CAAC,CAAC;IACrE,IAAI,QAAQ,GAAG,IAAI,CAAC,QAAQ,EAAE,cAAc,CAAC,CAAC;IAC9C,cAAc,CAAC,QAAQ,EAAE;QACvB,IAAI,EAAE,WAAW;KAClB,CAAC,CAAC;IACH,iBAAiB,CAAC,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,QAAQ,EAAE,cAAc,EAAE,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC;IACtE,IAAI,SAAS,GAAG,MAAM,OAAO,CAAC,OAAO,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;IACxD,IAAI,SAAS,EAAE,CAAC;QACd,IAAI,YAAY,CAAC,SAAS,CAAC,KAAK,SAAS,EAAE,CAAC;YAC1C,0DAA0D;YAC1D,yEAAyE;YACzE,sEAAsE;YACtE,yDAAyD;YACzD,EAAE;YACF,gDAAgD;YAChD,KAAK,CAAC,iEAAiE,EAAE,SAAS,CAAC,CAAC;YACpF,WAAW,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QAC7B,CAAC;QAED,OAAO,SAAS,CAAC;IACnB,CAAC;SAAM,CAAC;QACN,OAAO,MAAM,CAAC;IAChB,CAAC;AACH,CAAC","sourcesContent":["import type { Plugin, ViteDevServer } from 'vite';\nimport core, { ModuleRequest, type Resolver } from '@embroider/core';\nconst { virtualContent, ResolverLoader, explicitRelative, cleanUrl, tmpdir } = core;\nimport { RollupRequestAdapter, virtualPrefix } from './request.js';\nimport { assertNever } from 'assert-never';\nimport makeDebug from 'debug';\nimport { resolve, join } from 'path';\nimport { writeStatus } from './esbuild-request.js';\nimport type { PluginContext, ResolveIdResult } from 'rollup';\nimport { externalName } from '@embroider/reverse-exports';\nimport fs from 'fs-extra';\nimport { createHash } from 'crypto';\n\nconst { ensureSymlinkSync, outputJSONSync } = fs;\n\nconst debug = makeDebug('embroider:vite');\n\nexport function resolver(): Plugin {\n const resolverLoader = new ResolverLoader(process.cwd());\n let server: ViteDevServer;\n const virtualDeps: Map<string, string[]> = new Map();\n const notViteDeps = new Set<string>();\n\n return {\n name: 'embroider-resolver',\n enforce: 'pre',\n\n configureServer(s) {\n server = s;\n server.watcher.on('all', (_eventName, path) => {\n for (let [id, watches] of virtualDeps) {\n for (let watch of watches) {\n if (path.startsWith(watch)) {\n debug('Invalidate %s because %s', id, path);\n server.moduleGraph.onFileChange(id);\n let m = server.moduleGraph.getModuleById(id);\n if (m) {\n server.reloadModule(m);\n }\n }\n }\n }\n });\n return () => {\n server.middlewares.use((req, _res, next) => {\n const base = server.config.base || '/';\n const originalUrl = req.originalUrl!.slice(base.length - 1);\n if (originalUrl && originalUrl.length > 1) {\n if (originalUrl?.match(/^\\/tests($|\\?)/)) {\n req.originalUrl = `${base}tests/index.html`;\n (req as any).url = `${base}tests/index.html`;\n return next();\n }\n }\n return next();\n });\n };\n },\n\n async resolveId(source, importer, options) {\n if (options.custom?.depScan) {\n return await observeDepScan(this, source, importer, options);\n }\n\n let request = ModuleRequest.create(RollupRequestAdapter.create, {\n context: this,\n source,\n importer,\n custom: options.custom,\n });\n if (!request) {\n // fallthrough to other rollup plugins\n return null;\n }\n let resolution = await resolverLoader.resolver.resolve(request);\n switch (resolution.type) {\n case 'found':\n if (resolution.isVirtual) {\n return resolution.result;\n } else {\n return await maybeCaptureNewOptimizedDep(this, resolverLoader.resolver, resolution.result, notViteDeps);\n }\n case 'ignored':\n return resolution.result;\n case 'not_found':\n return null;\n default:\n throw assertNever(resolution);\n }\n },\n load(id) {\n if (id.startsWith(virtualPrefix)) {\n let { pathname } = new URL(id, 'http://example.com');\n let { src, watches } = virtualContent(pathname.slice(virtualPrefix.length + 1), resolverLoader.resolver);\n virtualDeps.set(id, watches);\n server?.watcher.add(watches);\n return src;\n }\n },\n buildEnd() {\n this.emitFile({\n type: 'asset',\n fileName: '@embroider/virtual/vendor.js',\n source: virtualContent(\n resolve(resolverLoader.resolver.options.engines[0].root, '-embroider-vendor.js'),\n resolverLoader.resolver\n ).src,\n });\n this.emitFile({\n type: 'asset',\n fileName: '@embroider/virtual/test-support.js',\n source: virtualContent(\n resolve(resolverLoader.resolver.options.engines[0].root, '-embroider-test-support.js'),\n resolverLoader.resolver\n ).src,\n });\n },\n };\n}\n\n// During depscan, we have a wildly different job than during normal\n// usage. Embroider's esbuild resolver plugin replaces this rollup\n// resolver plugin for actually doing resolving, so we don't do any of\n// that. But we are still well-positioned to observe what vite's rollup\n// resolver plugin is doing, and that is important because vite's\n// esbuild depscan plugin will always obscure the results before\n// embroider's esbuild resolver plugin can see them. It obscures the\n// results by marking *both* \"not found\" and \"this is a third-party\n// package\" as \"external: true\". We really care about the difference\n// between the two, since we have fallback behaviors that should apply\n// to \"not found\" that should not apply to successfully discovered\n// third-party packages.\nasync function observeDepScan(context: PluginContext, source: string, importer: string | undefined, options: any) {\n let result = await context.resolve(source, importer, {\n ...options,\n skipSelf: true,\n });\n writeStatus(source, result ? 'found' : 'not_found');\n return result;\n}\n\nfunction idFromResult(result: ResolveIdResult): string | undefined {\n if (!result) {\n return undefined;\n }\n if (typeof result === 'string') {\n return cleanUrl(result);\n }\n return cleanUrl(result.id);\n}\n\nfunction hashed(path: string): string {\n let h = createHash('sha1');\n return h.update(path).digest('hex').slice(0, 8);\n}\n\nasync function maybeCaptureNewOptimizedDep(\n context: PluginContext,\n resolver: Resolver,\n result: ResolveIdResult,\n notViteDeps: Set<string>\n): Promise<ResolveIdResult> {\n let foundFile = idFromResult(result);\n if (!foundFile) {\n return result;\n }\n if (foundFile.startsWith(join(resolver.packageCache.appRoot, 'node_modules', '.vite'))) {\n debug('maybeCaptureNewOptimizedDep: %s already in vite deps', foundFile);\n return result;\n }\n let pkg = resolver.packageCache.ownerOfFile(foundFile);\n if (!pkg?.isV2Addon()) {\n debug('maybeCaptureNewOptimizedDep: %s not in v2 addon', foundFile);\n return result;\n }\n let target = externalName(pkg.packageJSON, explicitRelative(pkg.root, foundFile));\n if (!target) {\n debug('maybeCaptureNewOptimizedDep: %s is not exported', foundFile);\n return result;\n }\n\n if (notViteDeps.has(foundFile)) {\n debug('maybeCaptureNewOptimizedDep: already attmpted %s', foundFile);\n return result;\n }\n\n debug('maybeCaptureNewOptimizedDep: doing re-resolve for %s ', foundFile);\n\n let jumpRoot = join(tmpdir, 'embroider-vite-jump', hashed(pkg.root));\n let fromFile = join(jumpRoot, 'package.json');\n outputJSONSync(fromFile, {\n name: 'jump-root',\n });\n ensureSymlinkSync(pkg.root, join(jumpRoot, 'node_modules', pkg.name));\n let newResult = await context.resolve(target, fromFile);\n if (newResult) {\n if (idFromResult(newResult) === foundFile) {\n // This case is normal. For example, people could be using\n // `optimizeDeps.exclude` or they might be working in a monorepo where an\n // addon is not in node_modules. In both cases vite will decide not to\n // optimize the file, even though we gave it a chance to.\n //\n // We cache that result so we don't keep trying.\n debug('maybeCaptureNewOptimizedDep: %s did not become an optimized dep', foundFile);\n notViteDeps.add(foundFile);\n }\n\n return newResult;\n } else {\n return result;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"resolver.js","sourceRoot":"","sources":["resolver.ts"],"names":[],"mappings":"AACA,OAAO,IAAI,EAAE,EAAE,aAAa,EAAiB,MAAM,iBAAiB,CAAC;AACrE,MAAM,EAAE,cAAc,EAAE,cAAc,EAAE,gBAAgB,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;AACpF,OAAO,EAAE,oBAAoB,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AACnE,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,SAAS,MAAM,OAAO,CAAC;AAC9B,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AACrC,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAEnD,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAC1D,OAAO,EAAE,MAAM,UAAU,CAAC;AAC1B,OAAO,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AAEpC,MAAM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,0DAA0D;YAC1D,yEAAyE;YACzE,sEAAsE;YACtE,yDAAyD;YACzD,EAAE;YACF,gDAAgD;YAChD,KAAK,CAAC,iEAAiE,EAAE,SAAS,CAAC,CAAC;YACpF,WAAW,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QAC7B,CAAC;QAED,OAAO,SAAS,CAAC;IACnB,CAAC;SAAM,CAAC;QACN,OAAO,MAAM,CAAC;IAChB,CAAC;AACH,CAAC","sourcesContent":["import type { Plugin, ViteDevServer } from 'vite';\nimport core, { ModuleRequest, type Resolver } from '@embroider/core';\nconst { virtualContent, ResolverLoader, explicitRelative, cleanUrl, tmpdir } = core;\nimport { RollupRequestAdapter, virtualPrefix } from './request.js';\nimport { assertNever } from 'assert-never';\nimport makeDebug from 'debug';\nimport { resolve, join } from 'path';\nimport { writeStatus } from './esbuild-request.js';\nimport type { PluginContext, ResolveIdResult } from 'rollup';\nimport { externalName } from '@embroider/reverse-exports';\nimport fs from 'fs-extra';\nimport { createHash } from 'crypto';\n\nconst { ensureSymlinkSync, outputJSONSync } = fs;\n\nconst debug = makeDebug('embroider:vite');\n\nexport function resolver(): Plugin {\n const resolverLoader = new ResolverLoader(process.cwd());\n let server: ViteDevServer;\n const virtualDeps: Map<string, string[]> = new Map();\n const notViteDeps = new Set<string>();\n\n let mode = '';\n\n return {\n name: 'embroider-resolver',\n enforce: 'pre',\n\n configResolved(config) {\n mode = config.mode;\n },\n\n configureServer(s) {\n server = s;\n server.watcher.on('all', (_eventName, path) => {\n for (let [id, watches] of virtualDeps) {\n for (let watch of watches) {\n if (path.startsWith(watch)) {\n debug('Invalidate %s because %s', id, path);\n server.moduleGraph.onFileChange(id);\n let m = server.moduleGraph.getModuleById(id);\n if (m) {\n server.reloadModule(m);\n }\n }\n }\n }\n });\n return () => {\n server.middlewares.use((req, _res, next) => {\n const base = server.config.base || '/';\n const originalUrl = req.originalUrl!.slice(base.length - 1);\n if (originalUrl && originalUrl.length > 1) {\n if (originalUrl?.match(/^\\/tests($|\\?)/)) {\n req.originalUrl = `${base}tests/index.html`;\n (req as any).url = `${base}tests/index.html`;\n return next();\n }\n }\n return next();\n });\n };\n },\n\n async resolveId(source, importer, options) {\n if (options.custom?.depScan) {\n return await observeDepScan(this, source, importer, options);\n }\n\n let request = ModuleRequest.create(RollupRequestAdapter.create, {\n context: this,\n source,\n importer,\n custom: options.custom,\n });\n if (!request) {\n // fallthrough to other rollup plugins\n return null;\n }\n let resolution = await resolverLoader.resolver.resolve(request);\n switch (resolution.type) {\n case 'found':\n if (resolution.isVirtual) {\n return resolution.result;\n } else {\n return await maybeCaptureNewOptimizedDep(this, resolverLoader.resolver, resolution.result, notViteDeps);\n }\n case 'ignored':\n return resolution.result;\n case 'not_found':\n return null;\n default:\n throw assertNever(resolution);\n }\n },\n load(id) {\n if (id.startsWith(virtualPrefix)) {\n let { pathname } = new URL(id, 'http://example.com');\n let { src, watches } = virtualContent(pathname.slice(virtualPrefix.length + 1), resolverLoader.resolver);\n virtualDeps.set(id, watches);\n server?.watcher.add(watches);\n return src;\n }\n },\n buildEnd() {\n this.emitFile({\n type: 'asset',\n fileName: '@embroider/virtual/vendor.js',\n source: virtualContent(\n resolve(resolverLoader.resolver.options.engines[0].root, '-embroider-vendor.js'),\n resolverLoader.resolver\n ).src,\n });\n this.emitFile({\n type: 'asset',\n fileName: '@embroider/virtual/vendor.css',\n source: virtualContent(\n resolve(resolverLoader.resolver.options.engines[0].root, '-embroider-vendor-styles.css'),\n resolverLoader.resolver\n ).src,\n });\n if (mode !== 'production') {\n this.emitFile({\n type: 'asset',\n fileName: '@embroider/virtual/test-support.js',\n source: virtualContent(\n resolve(resolverLoader.resolver.options.engines[0].root, '-embroider-test-support.js'),\n resolverLoader.resolver\n ).src,\n });\n this.emitFile({\n type: 'asset',\n fileName: '@embroider/virtual/test-support.css',\n source: virtualContent(\n resolve(resolverLoader.resolver.options.engines[0].root, '-embroider-test-support-styles.css'),\n resolverLoader.resolver\n ).src,\n });\n }\n },\n };\n}\n\n// During depscan, we have a wildly different job than during normal\n// usage. Embroider's esbuild resolver plugin replaces this rollup\n// resolver plugin for actually doing resolving, so we don't do any of\n// that. But we are still well-positioned to observe what vite's rollup\n// resolver plugin is doing, and that is important because vite's\n// esbuild depscan plugin will always obscure the results before\n// embroider's esbuild resolver plugin can see them. It obscures the\n// results by marking *both* \"not found\" and \"this is a third-party\n// package\" as \"external: true\". We really care about the difference\n// between the two, since we have fallback behaviors that should apply\n// to \"not found\" that should not apply to successfully discovered\n// third-party packages.\nasync function observeDepScan(context: PluginContext, source: string, importer: string | undefined, options: any) {\n let result = await context.resolve(source, importer, {\n ...options,\n skipSelf: true,\n });\n writeStatus(source, result ? 'found' : 'not_found');\n return result;\n}\n\nfunction idFromResult(result: ResolveIdResult): string | undefined {\n if (!result) {\n return undefined;\n }\n if (typeof result === 'string') {\n return cleanUrl(result);\n }\n return cleanUrl(result.id);\n}\n\nfunction hashed(path: string): string {\n let h = createHash('sha1');\n return h.update(path).digest('hex').slice(0, 8);\n}\n\nasync function maybeCaptureNewOptimizedDep(\n context: PluginContext,\n resolver: Resolver,\n result: ResolveIdResult,\n notViteDeps: Set<string>\n): Promise<ResolveIdResult> {\n let foundFile = idFromResult(result);\n if (!foundFile) {\n return result;\n }\n if (foundFile.startsWith(join(resolver.packageCache.appRoot, 'node_modules', '.vite'))) {\n debug('maybeCaptureNewOptimizedDep: %s already in vite deps', foundFile);\n return result;\n }\n let pkg = resolver.packageCache.ownerOfFile(foundFile);\n if (!pkg?.isV2Addon()) {\n debug('maybeCaptureNewOptimizedDep: %s not in v2 addon', foundFile);\n return result;\n }\n let target = externalName(pkg.packageJSON, explicitRelative(pkg.root, foundFile));\n if (!target) {\n debug('maybeCaptureNewOptimizedDep: %s is not exported', foundFile);\n return result;\n }\n\n if (notViteDeps.has(foundFile)) {\n debug('maybeCaptureNewOptimizedDep: already attmpted %s', foundFile);\n return result;\n }\n\n debug('maybeCaptureNewOptimizedDep: doing re-resolve for %s ', foundFile);\n\n let jumpRoot = join(tmpdir, 'embroider-vite-jump', hashed(pkg.root));\n let fromFile = join(jumpRoot, 'package.json');\n outputJSONSync(fromFile, {\n name: 'jump-root',\n });\n ensureSymlinkSync(pkg.root, join(jumpRoot, 'node_modules', pkg.name));\n let newResult = await context.resolve(target, fromFile);\n if (newResult) {\n if (idFromResult(newResult) === foundFile) {\n // This case is normal. For example, people could be using\n // `optimizeDeps.exclude` or they might be working in a monorepo where an\n // addon is not in node_modules. In both cases vite will decide not to\n // optimize the file, even though we gave it a chance to.\n //\n // We cache that result so we don't keep trying.\n debug('maybeCaptureNewOptimizedDep: %s did not become an optimized dep', foundFile);\n notViteDeps.add(foundFile);\n }\n\n return newResult;\n } else {\n return result;\n }\n}\n"]}
|