From 25852fa8f7087ed50764a4a955a9397b930c1f87 Mon Sep 17 00:00:00 2001 From: csr632 <632882184@qq.com> Date: Thu, 21 May 2020 02:09:03 +0800 Subject: [PATCH] feat: allow user to configure known named exports (#206) also fix resolve optimized cache dir --- src/node/build/index.ts | 6 +++--- src/node/config.ts | 8 ++++++++ src/node/depOptimizer.ts | 19 ++++++++++++++++++- src/node/resolver.ts | 6 ++++-- 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/node/build/index.ts b/src/node/build/index.ts index 0adb6df9a28064..0f393070148f4f 100644 --- a/src/node/build/index.ts +++ b/src/node/build/index.ts @@ -76,9 +76,9 @@ export async function createBaseRollupPlugins( ): Promise { const { rollupInputOptions = {}, transforms = [] } = options - // TODO allow user to configure known named exports - // or upgrade @rollup/plugin-commonjs when the new version is out - const knownNamedExports: Record = {} + const knownNamedExports: Record = { + ...options.rollupPluginCommonJSNamedExports + } for (const id of PACKAGES_TO_AUTO_DETECT_EXPORTS) { knownNamedExports[id] = knownNamedExports[id] || detectExports(root, id) || [] diff --git a/src/node/config.ts b/src/node/config.ts index 0e1e5d44846a06..7ae7c31dbaac1f 100644 --- a/src/node/config.ts +++ b/src/node/config.ts @@ -169,6 +169,14 @@ export interface BuildConfig extends SharedConfig { * https://rollupjs.org/guide/en/#big-list-of-options */ rollupInputOptions?: RollupInputOptions + /** + * Will be passed to @rollup/plugin-commonjs + * https://github.com/rollup/plugins/tree/commonjs-v11.1.0/packages/commonjs#namedexports + * This config can be removed after master branch is released. + * But there are some issues blocking it: + * https://github.com/rollup/plugins/issues/392 + */ + rollupPluginCommonJSNamedExports?: Record /** * Will be passed to bundle.generate() * diff --git a/src/node/depOptimizer.ts b/src/node/depOptimizer.ts index d8d93e938f3b63..ad48fbb5a48c98 100644 --- a/src/node/depOptimizer.ts +++ b/src/node/depOptimizer.ts @@ -67,7 +67,7 @@ export async function optimizeDeps( return } - const cacheDir = path.join(path.dirname(pkgPath), OPTIMIZE_CACHE_DIR) + const cacheDir = resolveOptimizedCacheDir(root, pkgPath)! const hashPath = path.join(cacheDir, 'hash') const depHash = getDepHash(root, config.__path) @@ -294,3 +294,20 @@ export function getDepHash( } return createHash('sha1').update(content).digest('base64') } + +const cacheDirCache = new Map() + +export function resolveOptimizedCacheDir( + root: string, + pkgPath?: string +): string | null { + const cached = cacheDirCache.get(root) + if (cached !== undefined) return cached + pkgPath = pkgPath || lookupFile(root, [`package.json`], true /* pathOnly */) + if (!pkgPath) { + return null + } + const cacheDir = path.join(path.dirname(pkgPath), OPTIMIZE_CACHE_DIR) + cacheDirCache.set(root, cacheDir) + return cacheDir +} diff --git a/src/node/resolver.ts b/src/node/resolver.ts index f4b54a0f3e1b3f..5a095351d80502 100644 --- a/src/node/resolver.ts +++ b/src/node/resolver.ts @@ -7,7 +7,7 @@ import { moduleRE, fileToRequestMap } from './server/serverPluginModuleResolve' -import { OPTIMIZE_CACHE_DIR } from './depOptimizer' +import { resolveOptimizedCacheDir } from './depOptimizer' import chalk from 'chalk' export interface Resolver { @@ -155,7 +155,9 @@ export function resolveOptimizedModule( } if (!id.endsWith('.js')) id += '.js' - const file = path.join(root, OPTIMIZE_CACHE_DIR, id) + const cacheDir = resolveOptimizedCacheDir(root) + if (!cacheDir) return + const file = path.join(cacheDir, id) if (fs.existsSync(file)) { viteOptimizedMap.set(id, file) return file