-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: esbuild based dep pre-bundling
- Loading branch information
Showing
13 changed files
with
352 additions
and
327 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import path from 'path' | ||
import { Plugin } from 'esbuild' | ||
import { knownAssetTypes } from '../constants' | ||
import builtins from 'builtin-modules' | ||
import { ResolvedConfig } from '..' | ||
import chalk from 'chalk' | ||
import { deepImportRE } from '../utils' | ||
|
||
const externalTypes = ['css', 'vue', 'svelte', ...knownAssetTypes] | ||
|
||
export function esbuildDepPlugin( | ||
qualified: Record<string, string>, | ||
config: ResolvedConfig, | ||
transitiveOptimized: Record<string, true> | ||
): Plugin { | ||
return { | ||
name: 'vite:dep-pre-bundle', | ||
setup(build) { | ||
// externalize assets and commonly known non-js file types | ||
build.onResolve( | ||
{ | ||
filter: new RegExp(`\\.(` + externalTypes.join('|') + `)(\\?.*)?$`) | ||
}, | ||
({ path: _path, importer }) => { | ||
if (_path.startsWith('.')) { | ||
const dir = path.dirname(importer) | ||
return { | ||
path: path.resolve(dir, _path), | ||
external: true | ||
} | ||
} | ||
} | ||
) | ||
|
||
// record transitive deps | ||
build.onResolve({ filter: /^[\w@]/ }, ({ path: id }) => { | ||
if (!(id in qualified) && !/:\/\//.test(id)) { | ||
const deepMatch = id.match(deepImportRE) | ||
const pkgId = deepMatch ? deepMatch[1] || deepMatch[2] : id | ||
transitiveOptimized[pkgId] = true | ||
} | ||
return null | ||
}) | ||
|
||
// redirect node-builtins to empty module for browser | ||
build.onResolve( | ||
{ | ||
filter: new RegExp(`^(${builtins.join('|')})$`) | ||
}, | ||
({ path: id, importer }) => { | ||
config.logger.warn( | ||
chalk.yellow( | ||
`externalized node built-in "${id}" to empty module. ` + | ||
`(imported by: ${chalk.white.dim(importer)})` | ||
) | ||
) | ||
return { | ||
path: id, | ||
namespace: 'browser-external' | ||
} | ||
} | ||
) | ||
|
||
build.onLoad( | ||
{ filter: /.*/, namespace: 'browser-external' }, | ||
({ path: id }) => { | ||
return { | ||
contents: | ||
`export default new Proxy({}, { | ||
get() { | ||
throw new Error('Module "${id}" has been externalized for ` + | ||
`browser compatibility and cannot be accessed in client code.') | ||
} | ||
})` | ||
} | ||
} | ||
) | ||
|
||
if (config.dedupe) { | ||
build.onResolve( | ||
{ | ||
filter: new RegExp(`^(${config.dedupe.join('|')})$`) | ||
}, | ||
({ path: id }) => { | ||
if (id in qualified) { | ||
return { | ||
path: path.resolve(qualified[id]) | ||
} | ||
} | ||
} | ||
) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.