Skip to content

Commit

Permalink
Merge branch 'main' into renovate/all-minor-patch
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Sep 18, 2023
2 parents d3b96e0 + c3e4791 commit b8df079
Show file tree
Hide file tree
Showing 15 changed files with 97 additions and 93 deletions.
2 changes: 1 addition & 1 deletion docs/config/build-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ Build as a library. `entry` is required since the library cannot use HTML as ent
- **Default:** `false`
- **Related:** [Backend Integration](/guide/backend-integration)
When set to `true`, the build will also generate a `manifest.json` file that contains a mapping of non-hashed asset filenames to their hashed versions, which can then be used by a server framework to render the correct asset links. When the value is a string, it will be used as the manifest file name.
When set to `true`, the build will also generate a `.vite/manifest.json` file that contains a mapping of non-hashed asset filenames to their hashed versions, which can then be used by a server framework to render the correct asset links. When the value is a string, it will be used as the manifest file name.
## build.ssrManifest
Expand Down
4 changes: 2 additions & 2 deletions docs/guide/backend-integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ If you need a custom integration, you can follow the steps in this guide to conf
// vite.config.js
export default defineConfig({
build: {
// generate manifest.json in outDir
// generate .vite/manifest.json in outDir
manifest: true,
rollupOptions: {
// overwrite default .html entry
Expand Down Expand Up @@ -56,7 +56,7 @@ If you need a custom integration, you can follow the steps in this guide to conf
</script>
```

3. For production: after running `vite build`, a `manifest.json` file will be generated alongside other asset files. An example manifest file looks like this:
3. For production: after running `vite build`, a `.vite/manifest.json` file will be generated alongside other asset files. An example manifest file looks like this:

```json
{
Expand Down
4 changes: 2 additions & 2 deletions docs/guide/ssr.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,14 @@ Refer to the [Vue](https://github.com/vitejs/vite-plugin-vue/tree/main/playgroun
## Generating Preload Directives
`vite build` supports the `--ssrManifest` flag which will generate `ssr-manifest.json` in build output directory:
`vite build` supports the `--ssrManifest` flag which will generate `.vite/ssr-manifest.json` in build output directory:
```diff
- "build:client": "vite build --outDir dist/client",
+ "build:client": "vite build --outDir dist/client --ssrManifest",
```
The above script will now generate `dist/client/ssr-manifest.json` for the client build (Yes, the SSR manifest is generated from the client build because we want to map module IDs to client files). The manifest contains mappings of module IDs to their associated chunks and asset files.
The above script will now generate `dist/client/.vite/ssr-manifest.json` for the client build (Yes, the SSR manifest is generated from the client build because we want to map module IDs to client files). The manifest contains mappings of module IDs to their associated chunks and asset files.
To leverage the manifest, frameworks need to provide a way to collect the module IDs of the components that were used during a server render call.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"playwright-chromium": "^1.38.0",
"prettier": "3.0.3",
"rimraf": "^5.0.1",
"rollup": "^3.28.0",
"rollup": "^3.29.0",
"simple-git-hooks": "^2.9.0",
"tslib": "^2.6.2",
"tsx": "^3.12.10",
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"dependencies": {
"esbuild": "^0.18.10",
"postcss": "^8.4.29",
"rollup": "^3.28.0"
"rollup": "^3.29.0"
},
"optionalDependencies": {
"fsevents": "~2.3.3"
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export interface BuildOptions {
*/
copyPublicDir?: boolean
/**
* Whether to emit a manifest.json under assets dir to map hash-less filenames
* Whether to emit a .vite/manifest.json under assets dir to map hash-less filenames
* to their hashed versions. Useful when you want to generate your own HTML
* instead of using the one generated by Vite.
*
Expand Down
9 changes: 6 additions & 3 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
dynamicImport,
isBuiltin,
isExternalUrl,
isNodeBuiltin,
isObject,
lookupFile,
mergeAlias,
Expand Down Expand Up @@ -1083,13 +1084,15 @@ async function bundleConfigFile(
if (
kind === 'entry-point' ||
path.isAbsolute(id) ||
isBuiltin(id)
isNodeBuiltin(id)
) {
return
}

// partial deno support as `npm:` does not work with esbuild
if (id.startsWith('npm:')) {
// With the `isNodeBuiltin` check above, this check captures if the builtin is a
// non-node built-in, which esbuild doesn't know how to handle. In that case, we
// externalize it so the non-node runtime handles it instead.
if (isBuiltin(id)) {
return { external: true }
}

Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import type { PreviewServerHook } from './preview'
* If a plugin should be applied only for server or build, a function format
* config file can be used to conditional determine the plugins to use.
*/
export interface Plugin extends RollupPlugin {
export interface Plugin<A = any> extends RollupPlugin<A> {
/**
* Enforce plugin invocation tier similar to webpack loaders.
*
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export function manifestPlugin(config: ResolvedConfig): Plugin {
fileName:
typeof config.build.manifest === 'string'
? config.build.manifest
: 'manifest.json',
: '.vite/manifest.json',
type: 'asset',
source: jsonStableStringify(manifest, { space: 2 }),
})
Expand Down
8 changes: 6 additions & 2 deletions packages/vite/src/node/server/searchRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ function hasWorkspacePackageJSON(root: string): boolean {
if (!isFileReadable(path)) {
return false
}
const content = JSON.parse(fs.readFileSync(path, 'utf-8')) || {}
return !!content.workspaces
try {
const content = JSON.parse(fs.readFileSync(path, 'utf-8')) || {}
return !!content.workspaces
} catch {
return false
}
}

function hasRootFile(root: string): boolean {
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/ssr/ssrManifestPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export function ssrManifestPlugin(config: ResolvedConfig): Plugin {
fileName:
typeof config.build.ssrManifest === 'string'
? config.build.ssrManifest
: 'ssr-manifest.json',
: '.vite/ssr-manifest.json',
type: 'asset',
source: jsonStableStringify(ssrManifest, { space: 2 }),
})
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/ssr/ssrModuleLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ async function nodeImport(
resolveOptions: InternalResolveOptionsWithOverrideConditions,
) {
let url: string
if (id.startsWith('node:') || id.startsWith('data:') || isBuiltin(id)) {
if (id.startsWith('data:') || isBuiltin(id)) {
url = id
} else {
const resolved = tryNodeResolve(
Expand Down
40 changes: 17 additions & 23 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,31 +88,25 @@ export const flattenId = (id: string): string =>
export const normalizeId = (id: string): string =>
id.replace(replaceNestedIdRE, ' > ')

//TODO: revisit later to see if the edge case that "compiling using node v12 code to be run in node v16 in the server" is what we intend to support.
const builtins = new Set([
...builtinModules,
'assert/strict',
'diagnostics_channel',
'dns/promises',
'fs/promises',
'path/posix',
'path/win32',
'readline/promises',
'stream/consumers',
'stream/promises',
'stream/web',
'timers/promises',
'util/types',
'wasi',
])

// Supported by Node, Deno, Bun
const NODE_BUILTIN_NAMESPACE = 'node:'
// Supported by Deno
const NPM_BUILTIN_NAMESPACE = 'npm:'
// Supported by Bun
const BUN_BUILTIN_NAMESPACE = 'bun:'
// Some runtimes like Bun injects namespaced modules here, which is not a node builtin
const nodeBuiltins = builtinModules.filter((id) => !id.includes(':'))

// TODO: Use `isBuiltin` from `node:module`, but Deno doesn't support it
export function isBuiltin(id: string): boolean {
return builtins.has(
id.startsWith(NODE_BUILTIN_NAMESPACE)
? id.slice(NODE_BUILTIN_NAMESPACE.length)
: id,
)
if (process.versions.deno && id.startsWith(NPM_BUILTIN_NAMESPACE)) return true
if (process.versions.bun && id.startsWith(BUN_BUILTIN_NAMESPACE)) return true
return isNodeBuiltin(id)
}

export function isNodeBuiltin(id: string): boolean {
if (id.startsWith(NODE_BUILTIN_NAMESPACE)) return true
return nodeBuiltins.includes(id)
}

export function isInNodeModules(id: string): boolean {
Expand Down
5 changes: 4 additions & 1 deletion playground/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,10 @@ export function findAssetFile(

export function readManifest(base = ''): Manifest {
return JSON.parse(
fs.readFileSync(path.join(testDir, 'dist', base, 'manifest.json'), 'utf-8'),
fs.readFileSync(
path.join(testDir, 'dist', base, '.vite/manifest.json'),
'utf-8',
),
)
}

Expand Down
Loading

0 comments on commit b8df079

Please sign in to comment.