Skip to content

Commit

Permalink
feat: allow user to configure known named exports (#206)
Browse files Browse the repository at this point in the history
also fix resolve optimized cache dir
  • Loading branch information
csr632 authored May 20, 2020
1 parent ba614ef commit 25852fa
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/node/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ export async function createBaseRollupPlugins(
): Promise<Plugin[]> {
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<string, string[]> = {}
const knownNamedExports: Record<string, string[]> = {
...options.rollupPluginCommonJSNamedExports
}
for (const id of PACKAGES_TO_AUTO_DETECT_EXPORTS) {
knownNamedExports[id] =
knownNamedExports[id] || detectExports(root, id) || []
Expand Down
8 changes: 8 additions & 0 deletions src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string[]>
/**
* Will be passed to bundle.generate()
*
Expand Down
19 changes: 18 additions & 1 deletion src/node/depOptimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -294,3 +294,20 @@ export function getDepHash(
}
return createHash('sha1').update(content).digest('base64')
}

const cacheDirCache = new Map<string, string | null>()

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
}
6 changes: 4 additions & 2 deletions src/node/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 25852fa

Please sign in to comment.