@embroider/vite 0.2.1-unstable.72d2f54 → 0.2.1-unstable.72ee15d
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/resolver.js +77 -5
- 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.1-unstable.
|
|
3
|
+
"version": "0.2.1-unstable.72ee15d",
|
|
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.16-unstable.
|
|
13
|
+
"@embroider/core": "^3.4.16-unstable.72ee15d"
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
16
|
"@babel/core": "^7.22.9",
|
|
@@ -25,8 +25,8 @@
|
|
|
25
25
|
"send": "^0.18.0",
|
|
26
26
|
"source-map-url": "^0.4.1",
|
|
27
27
|
"terser": "^5.7.0",
|
|
28
|
-
"@embroider/
|
|
29
|
-
"@embroider/
|
|
28
|
+
"@embroider/reverse-exports": "0.1.1-unstable.72ee15d",
|
|
29
|
+
"@embroider/macros": "1.16.7-unstable.72ee15d"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@types/babel__core": "^7.20.1",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"@types/send": "^0.17.4",
|
|
37
37
|
"rollup": "^4.18.0",
|
|
38
38
|
"vite": "^5.3.3",
|
|
39
|
-
"@embroider/core": "^3.4.16-unstable.
|
|
39
|
+
"@embroider/core": "^3.4.16-unstable.72ee15d"
|
|
40
40
|
},
|
|
41
41
|
"files": [
|
|
42
42
|
"index.mjs",
|
package/src/resolver.js
CHANGED
|
@@ -1,15 +1,20 @@
|
|
|
1
1
|
import core from '@embroider/core';
|
|
2
|
-
const { virtualContent, ResolverLoader } = core;
|
|
2
|
+
const { virtualContent, ResolverLoader, explicitRelative, cleanUrl, tmpdir } = core;
|
|
3
3
|
import { RollupModuleRequest, virtualPrefix } from './request.js';
|
|
4
4
|
import { assertNever } from 'assert-never';
|
|
5
5
|
import makeDebug from 'debug';
|
|
6
|
-
import { resolve } from 'path';
|
|
6
|
+
import { resolve, join } from 'path';
|
|
7
7
|
import { writeStatus } from './esbuild-request.js';
|
|
8
|
+
import { externalName } from '@embroider/reverse-exports';
|
|
9
|
+
import fs from 'fs-extra';
|
|
10
|
+
import { createHash } from 'crypto';
|
|
11
|
+
const { ensureSymlinkSync, outputJSONSync } = fs;
|
|
8
12
|
const debug = makeDebug('embroider:vite');
|
|
9
13
|
export function resolver() {
|
|
10
|
-
|
|
14
|
+
const resolverLoader = new ResolverLoader(process.cwd());
|
|
11
15
|
let server;
|
|
12
|
-
|
|
16
|
+
const virtualDeps = new Map();
|
|
17
|
+
const notViteDeps = new Set();
|
|
13
18
|
return {
|
|
14
19
|
name: 'embroider-resolver',
|
|
15
20
|
enforce: 'pre',
|
|
@@ -43,6 +48,12 @@ export function resolver() {
|
|
|
43
48
|
let resolution = await resolverLoader.resolver.resolve(request);
|
|
44
49
|
switch (resolution.type) {
|
|
45
50
|
case 'found':
|
|
51
|
+
if (resolution.isVirtual) {
|
|
52
|
+
return resolution.result;
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
return await maybeCaptureNewOptimizedDep(this, resolverLoader.resolver, resolution.result, notViteDeps);
|
|
56
|
+
}
|
|
46
57
|
case 'ignored':
|
|
47
58
|
return resolution.result;
|
|
48
59
|
case 'not_found':
|
|
@@ -63,7 +74,7 @@ export function resolver() {
|
|
|
63
74
|
buildEnd() {
|
|
64
75
|
this.emitFile({
|
|
65
76
|
type: 'asset',
|
|
66
|
-
fileName: '@embroider/
|
|
77
|
+
fileName: '@embroider/virtual/vendor.js',
|
|
67
78
|
source: virtualContent(resolve(resolverLoader.resolver.options.engines[0].root, '-embroider-vendor.js'), resolverLoader.resolver).src,
|
|
68
79
|
});
|
|
69
80
|
this.emitFile({
|
|
@@ -94,4 +105,65 @@ async function observeDepScan(context, source, importer, options) {
|
|
|
94
105
|
writeStatus(source, result ? 'found' : 'not_found');
|
|
95
106
|
return result;
|
|
96
107
|
}
|
|
108
|
+
function idFromResult(result) {
|
|
109
|
+
if (!result) {
|
|
110
|
+
return undefined;
|
|
111
|
+
}
|
|
112
|
+
if (typeof result === 'string') {
|
|
113
|
+
return cleanUrl(result);
|
|
114
|
+
}
|
|
115
|
+
return cleanUrl(result.id);
|
|
116
|
+
}
|
|
117
|
+
function hashed(path) {
|
|
118
|
+
let h = createHash('sha1');
|
|
119
|
+
return h.update(path).digest('hex').slice(0, 8);
|
|
120
|
+
}
|
|
121
|
+
async function maybeCaptureNewOptimizedDep(context, resolver, result, notViteDeps) {
|
|
122
|
+
let foundFile = idFromResult(result);
|
|
123
|
+
if (!foundFile) {
|
|
124
|
+
return result;
|
|
125
|
+
}
|
|
126
|
+
if (foundFile.startsWith(join(resolver.packageCache.appRoot, 'node_modules', '.vite'))) {
|
|
127
|
+
debug('maybeCaptureNewOptimizedDep: %s already in vite deps', foundFile);
|
|
128
|
+
return result;
|
|
129
|
+
}
|
|
130
|
+
let pkg = resolver.packageCache.ownerOfFile(foundFile);
|
|
131
|
+
if (!(pkg === null || pkg === void 0 ? void 0 : pkg.isV2Addon())) {
|
|
132
|
+
debug('maybeCaptureNewOptimizedDep: %s not in v2 addon', foundFile);
|
|
133
|
+
return result;
|
|
134
|
+
}
|
|
135
|
+
let target = externalName(pkg.packageJSON, explicitRelative(pkg.root, foundFile));
|
|
136
|
+
if (!target) {
|
|
137
|
+
debug('maybeCaptureNewOptimizedDep: %s is not exported', foundFile);
|
|
138
|
+
return result;
|
|
139
|
+
}
|
|
140
|
+
if (notViteDeps.has(foundFile)) {
|
|
141
|
+
debug('maybeCaptureNewOptimizedDep: already attmpted %s', foundFile);
|
|
142
|
+
return result;
|
|
143
|
+
}
|
|
144
|
+
debug('maybeCaptureNewOptimizedDep: doing re-resolve for %s ', foundFile);
|
|
145
|
+
let jumpRoot = join(tmpdir, 'embroider-vite-jump', hashed(pkg.root));
|
|
146
|
+
let fromFile = join(jumpRoot, 'package.json');
|
|
147
|
+
outputJSONSync(fromFile, {
|
|
148
|
+
name: 'jump-root',
|
|
149
|
+
});
|
|
150
|
+
ensureSymlinkSync(pkg.root, join(jumpRoot, 'node_modules', pkg.name));
|
|
151
|
+
let newResult = await context.resolve(target, fromFile);
|
|
152
|
+
if (newResult) {
|
|
153
|
+
if (idFromResult(newResult) === foundFile) {
|
|
154
|
+
// This case is normal. For example, people could be using
|
|
155
|
+
// `optimizeDeps.exclude` or they might be working in a monorepo where an
|
|
156
|
+
// addon is not in node_modules. In both cases vite will decide not to
|
|
157
|
+
// optimize the file, even though we gave it a chance to.
|
|
158
|
+
//
|
|
159
|
+
// We cache that result so we don't keep trying.
|
|
160
|
+
debug('maybeCaptureNewOptimizedDep: %s did not become an optimized dep', foundFile);
|
|
161
|
+
notViteDeps.add(foundFile);
|
|
162
|
+
}
|
|
163
|
+
return newResult;
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
return result;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
97
169
|
//# sourceMappingURL=resolver.js.map
|
package/src/resolver.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resolver.js","sourceRoot":"","sources":["resolver.ts"],"names":[],"mappings":"AACA,OAAO,IAAI,MAAM,iBAAiB,CAAC;AACnC,MAAM,EAAE,cAAc,EAAE,cAAc,EAAE,GAAG,IAAI,CAAC;AAChD,OAAO,EAAE,mBAAmB,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAClE,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,SAAS,MAAM,OAAO,CA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,EAAE,2BAA2B;gBACrC,MAAM,EAAE,cAAc,CACpB,OAAO,CAAC,cAAc,CAAC,QAAQ,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,sBAAsB,CAAC,EAChF,cAAc,CAAC,QAAQ,CACxB,CAAC,GAAG;aACN,CAAC,CAAC;YACH,IAAI,CAAC,QAAQ,CAAC;gBACZ,IAAI,EAAE,OAAO;gBACb,QAAQ,EAAE,iCAAiC;gBAC3C,MAAM,EAAE,cAAc,CACpB,OAAO,CAAC,cAAc,CAAC,QAAQ,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,4BAA4B,CAAC,EACtF,cAAc,CAAC,QAAQ,CACxB,CAAC,GAAG;aACN,CAAC,CAAC;QACL,CAAC;KACF,CAAC;AACJ,CAAC;AAED,oEAAoE;AACpE,kEAAkE;AAClE,sEAAsE;AACtE,uEAAuE;AACvE,iEAAiE;AACjE,gEAAgE;AAChE,oEAAoE;AACpE,mEAAmE;AACnE,oEAAoE;AACpE,sEAAsE;AACtE,kEAAkE;AAClE,wBAAwB;AACxB,KAAK,UAAU,cAAc,CAAC,OAAsB,EAAE,MAAc,EAAE,QAA4B,EAAE,OAAY;IAC9G,IAAI,MAAM,GAAG,MAAM,OAAO,CAAC,OAAO,CAAC,MAAM,EAAE,QAAQ,EAAE;QACnD,GAAG,OAAO;QACV,QAAQ,EAAE,IAAI;KACf,CAAC,CAAC;IACH,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC;IACpD,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import type { Plugin, ViteDevServer } from 'vite';\nimport core from '@embroider/core';\nconst { virtualContent, ResolverLoader } = core;\nimport { RollupModuleRequest, virtualPrefix } from './request.js';\nimport { assertNever } from 'assert-never';\nimport makeDebug from 'debug';\nimport { resolve } from 'path';\nimport { writeStatus } from './esbuild-request.js';\nimport type { PluginContext } from 'rollup';\n\nconst debug = makeDebug('embroider:vite');\n\nexport function resolver(): Plugin {\n let resolverLoader = new ResolverLoader(process.cwd());\n let server: ViteDevServer;\n let virtualDeps: Map<string, string[]> = new Map();\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 },\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 = RollupModuleRequest.from(this, source, importer, options.custom);\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 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/core/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/core/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"]}
|
|
1
|
+
{"version":3,"file":"resolver.js","sourceRoot":"","sources":["resolver.ts"],"names":[],"mappings":"AACA,OAAO,IAAuB,MAAM,iBAAiB,CAAC;AACtD,MAAM,EAAE,cAAc,EAAE,cAAc,EAAE,gBAAgB,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;AACpF,OAAO,EAAE,mBAAmB,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAClE,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,GAAG,EAAE,CAAC;AAEjD,MAAM,KAAK,GAAG,SAAS,CAAC,gBAAgB,CAAC,CAAC;AAE1C,MAAM,UAAU,QAAQ;IACtB,MAAM,cAAc,GAAG,IAAI,cAAc,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC;IACzD,IAAI,MAAqB,CAAC;IAC1B,MAAM,WAAW,GA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S,CAAC,EAAE,CAAC;QAC/B,KAAK,CAAC,kDAAkD,EAAE,SAAS,CAAC,CAAC;QACrE,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,KAAK,CAAC,uDAAuD,EAAE,SAAS,CAAC,CAAC;IAE1E,IAAI,QAAQ,GAAG,IAAI,CAAC,MAAM,EAAE,qBAAqB,EAAE,MAAM,CAAC,GAAG,CAAC,IAAI,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, { type Resolver } from '@embroider/core';\nconst { virtualContent, ResolverLoader, explicitRelative, cleanUrl, tmpdir } = core;\nimport { RollupModuleRequest, 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 },\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 = RollupModuleRequest.from(this, source, importer, options.custom);\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/core/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"]}
|