From 6387b4aacefab6a91554c8e9dda525f949731683 Mon Sep 17 00:00:00 2001 From: csr632 <632882184@qq.com> Date: Thu, 4 Jun 2020 00:57:09 +0800 Subject: [PATCH 1/9] test: add test rewrite-unoptimized-test-package --- playground/App.vue | 5 ++++- playground/package.json | 3 ++- .../rewrite-unoptimized/TestRewriteUnoptimized.vue | 12 ++++++++++++ .../rewrite-unoptimized/test-package/es/index.js | 3 +++ .../test-package/es/nested/index.js | 1 + .../test-package/es/nested/test.js | 1 + .../rewrite-unoptimized/test-package/package.json | 6 ++++++ playground/vite.config.ts | 2 +- playground/yarn.lock | 8 ++++++++ test/test.js | 4 ++++ 10 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 playground/rewrite-unoptimized/TestRewriteUnoptimized.vue create mode 100644 playground/rewrite-unoptimized/test-package/es/index.js create mode 100644 playground/rewrite-unoptimized/test-package/es/nested/index.js create mode 100644 playground/rewrite-unoptimized/test-package/es/nested/test.js create mode 100644 playground/rewrite-unoptimized/test-package/package.json diff --git a/playground/App.vue b/playground/App.vue index c9ea18f1aee0fa..af37727ff091b4 100644 --- a/playground/App.vue +++ b/playground/App.vue @@ -26,6 +26,7 @@ + diff --git a/playground/package.json b/playground/package.json index 6f4edbf9c3b64c..081aa32f541df1 100644 --- a/playground/package.json +++ b/playground/package.json @@ -11,6 +11,7 @@ "normalize.css": "link:../node_modules/normalize.css", "resolve-browser-field-test-package": "link:./resolve-browser-field", "rewrite-optimized-test-package": "link:./rewrite-optimized/test-package", - "optimize-linked": "link:./optimize-linked" + "optimize-linked": "link:./optimize-linked", + "rewrite-unoptimized-test-package": "link:./rewrite-unoptimized/test-package" } } diff --git a/playground/rewrite-unoptimized/TestRewriteUnoptimized.vue b/playground/rewrite-unoptimized/TestRewriteUnoptimized.vue new file mode 100644 index 00000000000000..3838856ce44943 --- /dev/null +++ b/playground/rewrite-unoptimized/TestRewriteUnoptimized.vue @@ -0,0 +1,12 @@ + + + diff --git a/playground/rewrite-unoptimized/test-package/es/index.js b/playground/rewrite-unoptimized/test-package/es/index.js new file mode 100644 index 00000000000000..ba1501c042ee11 --- /dev/null +++ b/playground/rewrite-unoptimized/test-package/es/index.js @@ -0,0 +1,3 @@ +import { Item } from './nested' + +export default Item diff --git a/playground/rewrite-unoptimized/test-package/es/nested/index.js b/playground/rewrite-unoptimized/test-package/es/nested/index.js new file mode 100644 index 00000000000000..d6a1baece48df8 --- /dev/null +++ b/playground/rewrite-unoptimized/test-package/es/nested/index.js @@ -0,0 +1 @@ +export { default as Item } from './test' diff --git a/playground/rewrite-unoptimized/test-package/es/nested/test.js b/playground/rewrite-unoptimized/test-package/es/nested/test.js new file mode 100644 index 00000000000000..fb5230a12964fa --- /dev/null +++ b/playground/rewrite-unoptimized/test-package/es/nested/test.js @@ -0,0 +1 @@ +export default 123 diff --git a/playground/rewrite-unoptimized/test-package/package.json b/playground/rewrite-unoptimized/test-package/package.json new file mode 100644 index 00000000000000..264b7f760881f5 --- /dev/null +++ b/playground/rewrite-unoptimized/test-package/package.json @@ -0,0 +1,6 @@ +{ + "name": "rewrite-unoptimized-test-package", + "version": "1.0.0", + "main": "es/index.js", + "license": "MIT" +} diff --git a/playground/vite.config.ts b/playground/vite.config.ts index f467b0073aa41a..f24c6f42d0e6ae 100644 --- a/playground/vite.config.ts +++ b/playground/vite.config.ts @@ -13,7 +13,7 @@ const config: UserConfig = { plugins: [jsPlugin], vueCustomBlockTransforms: { i18n: i18nTransform }, optimizeDeps: { - exclude: ['bootstrap'], + exclude: ['bootstrap', 'rewrite-unoptimized-test-package'], link: ['optimize-linked'] } } diff --git a/playground/yarn.lock b/playground/yarn.lock index 4f72d5f94f633f..7e188a0d25be30 100644 --- a/playground/yarn.lock +++ b/playground/yarn.lock @@ -6,6 +6,10 @@ version "0.0.0" uid "" +"linked-dep@link:./optimize-linked/linked-dep": + version "0.0.0" + uid "" + "lodash-es@link:../node_modules/lodash-es": version "0.0.0" uid "" @@ -29,3 +33,7 @@ "rewrite-optimized-test-package@link:./rewrite-optimized/test-package": version "0.0.0" uid "" + +"rewrite-unoptimized-test-package@link:./rewrite-unoptimized/test-package": + version "0.0.0" + uid "" diff --git a/test/test.js b/test/test.js index 42f676ae03cae0..66dbaf3921f601 100644 --- a/test/test.js +++ b/test/test.js @@ -491,6 +491,10 @@ describe('vite', () => { ) }) + test('rewrite import in unoptimized deps', async () => { + expect(await getText('.test-rewrite-in-unoptimized')).toMatch('123') + }) + test('monorepo support', async () => { // linked dep + optimizing linked dep expect(await getText(`.optimize-linked`)).toMatch(`ok`) From 8e01932ae45f8f3b2307b8edb40cfb68d94c6c40 Mon Sep 17 00:00:00 2001 From: csr632 <632882184@qq.com> Date: Thu, 4 Jun 2020 01:01:30 +0800 Subject: [PATCH 2/9] fix: module rewrite in unoptimized dep --- src/node/resolver.ts | 123 +++++++++++++------ src/node/server/serverPluginModuleRewrite.ts | 7 +- src/node/utils/pathUtils.ts | 15 +++ 3 files changed, 101 insertions(+), 44 deletions(-) diff --git a/src/node/resolver.ts b/src/node/resolver.ts index 59011a7eb2a893..e9caaaefdd2c9a 100644 --- a/src/node/resolver.ts +++ b/src/node/resolver.ts @@ -1,7 +1,13 @@ import fs from 'fs-extra' import path from 'path' import slash from 'slash' -import { cleanUrl, resolveFrom, queryRE } from './utils' +import { + cleanUrl, + resolveFrom, + queryRE, + lookupFile, + parseNodeModuleId +} from './utils' import { moduleRE, moduleIdToFileMap, @@ -21,8 +27,8 @@ export interface Resolver { export interface InternalResolver { requestToFile(publicPath: string): string fileToRequest(filePath: string): string + normalizePublicPath(publicPath: string): string alias(id: string): string | undefined - resolveExt(publicPath: string): string | undefined } export const supportedExts = ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json'] @@ -70,26 +76,34 @@ const isFile = (file: string): boolean => { } } -const resolveExt = (id: string): string | undefined => { - const cleanId = cleanUrl(id) - if (!isFile(cleanId)) { - let inferredExt = '' +/** + * this function resolve fuzzy file path. examples: + * /path/file is a fuzzy file path for /path/file.tsx + * /path/dir is a fuzzy file path for /path/dir/index.js + * + * returning undefined indicates the filePath is not fuzzy: + * it is already an exact file path, or it can't match any file + */ +const resolveFilePathPostfix = (filePath: string): string | undefined => { + const cleanPath = cleanUrl(filePath) + if (!isFile(cleanPath)) { + let postfix = '' for (const ext of supportedExts) { - if (isFile(cleanId + ext)) { - inferredExt = ext + if (isFile(cleanPath + ext)) { + postfix = ext break } - if (isFile(path.join(cleanId, '/index' + ext))) { - inferredExt = '/index' + ext + if (isFile(path.join(cleanPath, '/index' + ext))) { + postfix = '/index' + ext break } } - const queryMatch = id.match(/\?.*$/) + const queryMatch = filePath.match(/\?.*$/) const query = queryMatch ? queryMatch[0] : '' - const resolved = cleanId + inferredExt + query - if (resolved !== id) { - debug(`(extension) ${id} -> ${resolved}`) - return inferredExt + const resolved = cleanPath + postfix + query + if (resolved !== filePath) { + debug(`(postfix) ${filePath} -> ${resolved}`) + return postfix } } } @@ -141,17 +155,12 @@ export function createResolver( }) resolveAlias(userAlias) - const requestToFileCache = new Map() - const fileToRequestCache = new Map() + const requestToFileCache = new Map() + const fileToRequestCache = new Map() - const resolveRequest = ( - publicPath: string - ): { - filePath: string - ext: string | undefined - } => { + const resolveRequest = (publicPath: string): string => { if (requestToFileCache.has(publicPath)) { - return requestToFileCache.get(publicPath) + return requestToFileCache.get(publicPath)! } let resolved: string | undefined @@ -165,27 +174,21 @@ export function createResolver( if (!resolved) { resolved = defaultRequestToFile(publicPath, root) } - const ext = resolveExt(resolved) - const result = { - filePath: ext ? resolved + ext : resolved, - ext: ext || path.extname(resolved) - } + const postfix = resolveFilePathPostfix(resolved) + // TODO resolved may contain query + const result = postfix ? resolved + postfix : resolved requestToFileCache.set(publicPath, result) return result } return { requestToFile(publicPath) { - return resolveRequest(publicPath).filePath - }, - - resolveExt(publicPath) { - return resolveRequest(publicPath).ext + return resolveRequest(publicPath) }, fileToRequest(filePath) { if (fileToRequestCache.has(filePath)) { - return fileToRequestCache.get(filePath) + return fileToRequestCache.get(filePath)! } for (const r of resolvers) { const request = r.fileToRequest && r.fileToRequest(filePath, root) @@ -196,6 +199,33 @@ export function createResolver( return res }, + normalizePublicPath(publicPath) { + if (!moduleRE.test(publicPath)) + return this.fileToRequest(this.requestToFile(publicPath)) + + const filePath = this.requestToFile(publicPath) + const optimizedPublicPath = recognizeOptimizedFilePath(root, filePath) + if (optimizedPublicPath) return optimizedPublicPath + + // fileToRequest doesn't work with files in node_modules + // because of edge cases like symlinks or yarn-aliased-install + // or even aliased-symlinks + + // /@modules/@scope/test-package -> /@modules/@scope/test-package + const id = publicPath.replace(moduleRE, '') + const { scope, name, inPkgPath } = parseNodeModuleId(id) + if (!inPkgPath) return publicPath + // /@modules/test-package/dir -> /@modules/test-package/dir/index.js + const pkgPath = lookupFile(filePath, ['package.json'], true) + if (!pkgPath) + throw new Error( + `[vite] can't find package.json for a node_module file: ` + + `"${publicPath}". something is wrong.` + ) + const inLibPath = path.relative(path.dirname(pkgPath), filePath) + return ['/@modules', scope, name, inLibPath].filter(Boolean).join('/') + }, + alias(id) { let aliased: string | undefined = literalAlias[id] if (aliased) { @@ -305,6 +335,21 @@ export function resolveOptimizedModule( } } +/** + * if filePath is in the optimized cache dir, + * return the public path for it + */ +function recognizeOptimizedFilePath( + root: string, + filePath: string +): string | undefined { + const cacheDir = resolveOptimizedCacheDir(root) + if (!cacheDir) return + const relative = path.relative(cacheDir, filePath) + if (!relative.startsWith('../')) + return relative.replace(/^\.\.\//, '/@modules/') +} + interface NodeModuleInfo { entry: string | null entryFilePath: string | null @@ -371,10 +416,10 @@ export function resolveNodeModule( if (entryPoint) { // #284 some packages specify entry without extension... entryFilePath = path.join(path.dirname(pkgPath), entryPoint!) - const ext = resolveExt(entryFilePath) - if (ext) { - entryPoint += ext - entryFilePath += ext + const postfix = resolveFilePathPostfix(entryFilePath) + if (postfix) { + entryPoint += postfix + entryFilePath += postfix } entryPoint = path.posix.join(id, entryPoint!) // save the resolved file path now so we don't need to do it again in diff --git a/src/node/server/serverPluginModuleRewrite.ts b/src/node/server/serverPluginModuleRewrite.ts index 90c053b6f574df..bebaaaf102f23e 100644 --- a/src/node/server/serverPluginModuleRewrite.ts +++ b/src/node/server/serverPluginModuleRewrite.ts @@ -254,11 +254,8 @@ export const resolveImport = ( // ./foo -> /some/path/foo let { pathname, query } = resolveRelativeRequest(importer, id) - // 2. resolve extensions. - const ext = resolver.resolveExt(pathname) - if (ext && ext !== path.extname(pathname)) { - pathname = path.posix.normalize(pathname + ext) - } + // 2. resolve dir index and extensions. + pathname = resolver.normalizePublicPath(pathname) // 3. mark non-src imports if (!query && path.extname(pathname) && !jsSrcRE.test(pathname)) { diff --git a/src/node/utils/pathUtils.ts b/src/node/utils/pathUtils.ts index 1478586684dea8..27e8b154d5fdba 100644 --- a/src/node/utils/pathUtils.ts +++ b/src/node/utils/pathUtils.ts @@ -74,3 +74,18 @@ export const isStaticAsset = (file: string) => { export const isImportRequest = (ctx: Context): boolean => { return ctx.query.import != null } + +export function parseNodeModuleId(id: string) { + const parts = id.split('/') + let scope = '', + name = '', + inPkgPath = '' + if (id.startsWith('@')) scope = parts.shift()! + name = parts.shift()! + inPkgPath = parts.join('/') + return { + scope, + name, + inPkgPath + } +} From d532b04f4c07905fa556dd7dbd1f62927c6ca171 Mon Sep 17 00:00:00 2001 From: csr632 <632882184@qq.com> Date: Thu, 4 Jun 2020 01:10:51 +0800 Subject: [PATCH 3/9] fix: find another usecase for resolver.normalizePublicPath --- src/node/server/serverPluginModuleRewrite.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/node/server/serverPluginModuleRewrite.ts b/src/node/server/serverPluginModuleRewrite.ts index bebaaaf102f23e..7a5114195871cd 100644 --- a/src/node/server/serverPluginModuleRewrite.ts +++ b/src/node/server/serverPluginModuleRewrite.ts @@ -78,9 +78,7 @@ export const moduleRewritePlugin: ServerPlugin = ({ // before we perform hmr analysis. // on the other hand, static import is guaranteed to have extension // because they must all have gone through module rewrite. - const importer = resolver.fileToRequest( - resolver.requestToFile(ctx.path) - ) + const importer = resolver.normalizePublicPath(ctx.path) ctx.body = rewriteImports( root, content!, From 9bc5c2bb6093ad2b08666d9269ecf172c899455c Mon Sep 17 00:00:00 2001 From: csr632 <632882184@qq.com> Date: Thu, 4 Jun 2020 01:38:48 +0800 Subject: [PATCH 4/9] fix: preserve query and add double check --- src/node/resolver.ts | 59 +++++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 22 deletions(-) diff --git a/src/node/resolver.ts b/src/node/resolver.ts index e9caaaefdd2c9a..2ef64b56114134 100644 --- a/src/node/resolver.ts +++ b/src/node/resolver.ts @@ -200,30 +200,45 @@ export function createResolver( }, normalizePublicPath(publicPath) { - if (!moduleRE.test(publicPath)) - return this.fileToRequest(this.requestToFile(publicPath)) - - const filePath = this.requestToFile(publicPath) - const optimizedPublicPath = recognizeOptimizedFilePath(root, filePath) - if (optimizedPublicPath) return optimizedPublicPath - - // fileToRequest doesn't work with files in node_modules - // because of edge cases like symlinks or yarn-aliased-install - // or even aliased-symlinks - - // /@modules/@scope/test-package -> /@modules/@scope/test-package - const id = publicPath.replace(moduleRE, '') - const { scope, name, inPkgPath } = parseNodeModuleId(id) - if (!inPkgPath) return publicPath - // /@modules/test-package/dir -> /@modules/test-package/dir/index.js - const pkgPath = lookupFile(filePath, ['package.json'], true) - if (!pkgPath) + // preserve query + const queryMatch = publicPath.match(/\?.*$/) + const query = queryMatch ? queryMatch[0] : '' + const cleanPublicPath = cleanUrl(publicPath) + const result = main(cleanPublicPath, this) + query + // double check + if (this.requestToFile(result) !== this.requestToFile(publicPath)) { throw new Error( - `[vite] can't find package.json for a node_module file: ` + - `"${publicPath}". something is wrong.` + `[vite] normalizePublicPath check fail. please report to vite.` ) - const inLibPath = path.relative(path.dirname(pkgPath), filePath) - return ['/@modules', scope, name, inLibPath].filter(Boolean).join('/') + } + return result + + function main(publicPath: string, resolver: InternalResolver) { + if (!moduleRE.test(publicPath)) + return resolver.fileToRequest(resolver.requestToFile(publicPath)) + + const filePath = resolver.requestToFile(publicPath) + const optimizedPublicPath = recognizeOptimizedFilePath(root, filePath) + if (optimizedPublicPath) return optimizedPublicPath + + // fileToRequest doesn't work with files in node_modules + // because of edge cases like symlinks or yarn-aliased-install + // or even aliased-symlinks + + // /@modules/@scope/test-package -> /@modules/@scope/test-package + const id = publicPath.replace(moduleRE, '') + const { scope, name, inPkgPath } = parseNodeModuleId(id) + if (!inPkgPath) return publicPath + // /@modules/test-package/dir -> /@modules/test-package/dir/index.js + const pkgPath = lookupFile(filePath, ['package.json'], true) + if (!pkgPath) + throw new Error( + `[vite] can't find package.json for a node_module file: ` + + `"${publicPath}". something is wrong.` + ) + const inLibPath = path.relative(path.dirname(pkgPath), filePath) + return ['/@modules', scope, name, inLibPath].filter(Boolean).join('/') + } }, alias(id) { From 355ebb2162b114c6078f28f9a44b67777fd2cae1 Mon Sep 17 00:00:00 2001 From: csr632 <632882184@qq.com> Date: Thu, 4 Jun 2020 12:30:56 +0800 Subject: [PATCH 5/9] test: add test case for normalizePublicPath and watch it fail --- package.json | 1 + playground/App.vue | 5 ++++- playground/TestNormalizePublicPath.vue | 21 +++++++++++++++++++++ playground/package.json | 1 + playground/yarn.lock | 9 +++++++++ test/test.js | 6 ++++++ yarn.lock | 7 +++++++ 7 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 playground/TestNormalizePublicPath.vue diff --git a/package.json b/package.json index 6b053d1169df8d..ffb606ce92a670 100644 --- a/package.json +++ b/package.json @@ -97,6 +97,7 @@ "ws": "^7.2.3" }, "devDependencies": { + "@babel/runtime": "7.10.2", "@pika/react": "^16.13.1", "@pika/react-dom": "^16.13.1", "@types/es-module-lexer": "^0.3.0", diff --git a/playground/App.vue b/playground/App.vue index af37727ff091b4..792589a22dd9c5 100644 --- a/playground/App.vue +++ b/playground/App.vue @@ -27,6 +27,7 @@ + diff --git a/playground/TestNormalizePublicPath.vue b/playground/TestNormalizePublicPath.vue new file mode 100644 index 00000000000000..eb1157ed3764aa --- /dev/null +++ b/playground/TestNormalizePublicPath.vue @@ -0,0 +1,21 @@ + + + diff --git a/playground/package.json b/playground/package.json index 081aa32f541df1..6a32f580a0e581 100644 --- a/playground/package.json +++ b/playground/package.json @@ -8,6 +8,7 @@ "bootstrap": "link:../node_modules/bootstrap", "lodash-es": "link:../node_modules/lodash-es", "moment": "link:../node_modules/moment", + "@babel/runtime": "link:../node_modules/@babel/runtime", "normalize.css": "link:../node_modules/normalize.css", "resolve-browser-field-test-package": "link:./resolve-browser-field", "rewrite-optimized-test-package": "link:./rewrite-optimized/test-package", diff --git a/playground/yarn.lock b/playground/yarn.lock index 7e188a0d25be30..712cf9a2947365 100644 --- a/playground/yarn.lock +++ b/playground/yarn.lock @@ -2,6 +2,10 @@ # yarn lockfile v1 +"@babel/runtime@link:../node_modules/@babel/runtime": + version "0.0.0" + uid "" + "bootstrap@link:../node_modules/bootstrap": version "0.0.0" uid "" @@ -26,6 +30,11 @@ version "0.0.0" uid "" +regenerator-runtime@^0.13.4: + version "0.13.5" + resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.13.5.tgz#d878a1d094b4306d10b9096484b33ebd55e26697" + integrity sha512-ZS5w8CpKFinUzOwW3c83oPeVXoNsrLsaCoLtJvAClH135j/R77RuymhiSErhm2lKcwSCIpmvIWSbDkIfAqKQlA== + "resolve-browser-field-test-package@link:./resolve-browser-field": version "0.0.0" uid "" diff --git a/test/test.js b/test/test.js index 66dbaf3921f601..df768cd5cbbab4 100644 --- a/test/test.js +++ b/test/test.js @@ -520,6 +520,12 @@ describe('vite', () => { await expectByPolling(() => getText('.custom-block'), 'こんにちは') } }) + + test('normalize publicPath', async () => { + expect(await getText('.normalize-public-path')).toMatch( + JSON.stringify([2, 4]) + ) + }) } // test build first since we are going to edit the fixtures when testing dev diff --git a/yarn.lock b/yarn.lock index 46f1fdb5cf25b6..996da4a545e00e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -219,6 +219,13 @@ dependencies: "@babel/helper-plugin-utils" "^7.8.0" +"@babel/runtime@7.10.2": + version "7.10.2" + resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.10.2.tgz#d103f21f2602497d38348a32e008637d506db839" + integrity sha512-6sF3uQw2ivImfVIl62RZ7MXhO2tap69WeWK57vAaimT6AZbE4FbqjdEJIN1UqoD6wI6B+1n9UiagafH1sxjOtg== + dependencies: + regenerator-runtime "^0.13.4" + "@babel/runtime@^7.9.2": version "7.9.6" resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.9.6.tgz#a9102eb5cadedf3f31d08a9ecf294af7827ea29f" From a5ed6efee377280c0a6a7be733f78fb49ef7b44a Mon Sep 17 00:00:00 2001 From: csr632 <632882184@qq.com> Date: Thu, 4 Jun 2020 12:36:09 +0800 Subject: [PATCH 6/9] fix: normalizePublicPath bug and make playground works --- src/node/resolver.ts | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/node/resolver.ts b/src/node/resolver.ts index 2ef64b56114134..2308e53e1b34fc 100644 --- a/src/node/resolver.ts +++ b/src/node/resolver.ts @@ -225,19 +225,29 @@ export function createResolver( // because of edge cases like symlinks or yarn-aliased-install // or even aliased-symlinks - // /@modules/@scope/test-package -> /@modules/@scope/test-package + // example id: "@babel/runtime/helpers/esm/slicedToArray" + // see the test case: /playground/TestNormalizePublicPath.vue const id = publicPath.replace(moduleRE, '') const { scope, name, inPkgPath } = parseNodeModuleId(id) if (!inPkgPath) return publicPath - // /@modules/test-package/dir -> /@modules/test-package/dir/index.js - const pkgPath = lookupFile(filePath, ['package.json'], true) - if (!pkgPath) - throw new Error( - `[vite] can't find package.json for a node_module file: ` + - `"${publicPath}". something is wrong.` - ) - const inLibPath = path.relative(path.dirname(pkgPath), filePath) - return ['/@modules', scope, name, inLibPath].filter(Boolean).join('/') + let filePathPostFix = '' + let findPkgFrom = filePath + while (!filePathPostFix.startsWith(inPkgPath)) { + // some package contains multi package.json... + // for example: @babel/runtime@7.10.2/helpers/esm/package.json + const pkgPath = lookupFile(findPkgFrom, ['package.json'], true) + if (!pkgPath) { + throw new Error( + `[vite] can't find package.json for a node_module file: ` + + `"${publicPath}". something is wrong.` + ) + } + filePathPostFix = path.relative(path.dirname(pkgPath), filePath) + findPkgFrom = path.join(path.dirname(pkgPath), '../') + } + return ['/@modules', scope, name, filePathPostFix] + .filter(Boolean) + .join('/') } }, @@ -361,8 +371,7 @@ function recognizeOptimizedFilePath( const cacheDir = resolveOptimizedCacheDir(root) if (!cacheDir) return const relative = path.relative(cacheDir, filePath) - if (!relative.startsWith('../')) - return relative.replace(/^\.\.\//, '/@modules/') + if (!relative.startsWith('../')) return path.join('/@modules/', relative) } interface NodeModuleInfo { From 5e22598700dec131247524f7b8a49f45a85ea9c2 Mon Sep 17 00:00:00 2001 From: Evan You Date: Thu, 4 Jun 2020 15:24:37 -0400 Subject: [PATCH 7/9] test: exclude playground from jest test match --- jest.config.js | 1 + 1 file changed, 1 insertion(+) diff --git a/jest.config.js b/jest.config.js index 3fc6d20fa0dc3e..4e0a5fac509b8a 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,3 +1,4 @@ module.exports = { + testPathIgnorePatterns: ['/playground/', '/node_modules/'], watchPathIgnorePatterns: ['/temp'] } From adf8a8e5772a87b549b062f9935db51ac42a35dc Mon Sep 17 00:00:00 2001 From: Evan You Date: Thu, 4 Jun 2020 17:14:15 -0400 Subject: [PATCH 8/9] fix windows issues --- playground/TestHmr/TestHmr.vue | 3 +- src/node/resolver.ts | 57 ++++++++++++++++++---------------- 2 files changed, 32 insertions(+), 28 deletions(-) diff --git a/playground/TestHmr/TestHmr.vue b/playground/TestHmr/TestHmr.vue index a6c4f3575c8872..fbff720728e731 100644 --- a/playground/TestHmr/TestHmr.vue +++ b/playground/TestHmr/TestHmr.vue @@ -52,8 +52,7 @@ qux not loaded + >qux not loaded