Skip to content

Commit

Permalink
feat: add preload switch config
Browse files Browse the repository at this point in the history
  • Loading branch information
ygj6 committed Jul 2, 2021
1 parent bd34203 commit d2abc41
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
7 changes: 7 additions & 0 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,13 @@ createServer()

Note: the polyfill does **not** apply to [Library Mode](/guide/build#library-mode). If you need to support browsers without native dynamic import, you should probably avoid using it in your library.

### build.autoPreload

- **Type:** `boolean`
- **Default:** `false`

Specify the output directory (relative to [project root](/guide/#index-html-and-project-root)).

### build.outDir

- **Type:** `string`
Expand Down
7 changes: 7 additions & 0 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ export interface BuildOptions {
* @default false
*/
polyfillDynamicImport?: boolean
/**
* whether to inject `__vitePreload` method.
* Note: does not apply to library mode.
* @default true
*/
autoPreload?: boolean
/**
* Directory relative from `root` where build output will be placed. If the
* directory exists, it will be removed before the build.
Expand Down Expand Up @@ -209,6 +215,7 @@ export function resolveBuildOptions(raw?: BuildOptions): ResolvedBuildOptions {
const resolved: ResolvedBuildOptions = {
target: 'modules',
polyfillDynamicImport: false,
autoPreload: true,
outDir: 'dist',
assetsDir: 'assets',
assetsInlineLimit: 4096,
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/importGlob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export async function transformImportGlob(
importIndex: number,
root: string,
normalizeUrl?: (url: string, pos: number) => Promise<[string, string]>,
ssr = false
preload = true
): Promise<{
importsString: string
imports: string[]
Expand Down Expand Up @@ -87,7 +87,7 @@ export async function transformImportGlob(
entries += ` ${JSON.stringify(file)}: ${identifier},`
} else {
let imp = `import(${JSON.stringify(importee)})`
if (!normalizeUrl && !ssr) {
if (!normalizeUrl && preload) {
imp =
`(${isModernFlag}` +
`? ${preloadMethod}(()=>${imp},"${preloadMarker}")` +
Expand Down
6 changes: 3 additions & 3 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,17 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
index,
config.root,
undefined,
ssr
!ssr && config.build.autoPreload
)
str().prepend(importsString)
str().overwrite(expStart, endIndex, exp)
if (!isEager) {
if (!isEager && config.build.autoPreload) {
needPreloadHelper = true
}
continue
}

if (dynamicIndex > -1 && !ssr) {
if (dynamicIndex > -1 && !ssr && config.build.autoPreload) {
needPreloadHelper = true
const dynamicEnd = source.indexOf(`)`, end) + 1
const original = source.slice(dynamicIndex, dynamicEnd)
Expand Down

0 comments on commit d2abc41

Please sign in to comment.