Skip to content

Commit

Permalink
fix: provide build load fallback for arbitrary request with queries
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jul 23, 2021
1 parent 41a00df commit f097aa1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { isCSSRequest } from './plugins/css'
import { DepOptimizationMetadata } from './optimizer'
import { scanImports } from './optimizer/scan'
import { assetImportMetaUrlPlugin } from './plugins/assetImportMetaUrl'
import { loadFallbackPlugin } from './plugins/loadFallback'

export interface BuildOptions {
/**
Expand Down Expand Up @@ -289,7 +290,8 @@ export function resolveBuildPlugins(config: ResolvedConfig): {
: []),
...(options.manifest ? [manifestPlugin(config)] : []),
...(options.ssrManifest ? [ssrManifestPlugin(config)] : []),
buildReporterPlugin(config)
buildReporterPlugin(config),
loadFallbackPlugin()
]
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
map: { mappings: '' },
// avoid the css module from being tree-shaken so that we can retrieve
// it in renderChunk()
moduleSideEffects: 'no-treeshake'
moduleSideEffects: inlined ? false : 'no-treeshake'
}
},

Expand Down
16 changes: 16 additions & 0 deletions packages/vite/src/node/plugins/loadFallback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { promises as fs } from 'fs'
import { Plugin } from '..'
import { cleanUrl } from '../utils'

export function loadFallbackPlugin(): Plugin {
return {
name: 'load-fallback',
async load(id) {
try {
return fs.readFile(cleanUrl(id), 'utf-8')
} catch (e) {
return fs.readFile(id, 'utf-8')

This comment has been minimized.

Copy link
@aleclarson

aleclarson Oct 12, 2021

Member

In what situation would this catch clause be necessary?

}
}
}
}

0 comments on commit f097aa1

Please sign in to comment.