Skip to content

Commit

Permalink
feat: environment api (#16129)
Browse files Browse the repository at this point in the history
Co-authored-by: Vladimir Sheremet <[email protected]>
Co-authored-by: Hiroshi Ogawa <[email protected]>
Co-authored-by: 翠 / green <[email protected]>
Co-authored-by: Jun Shindo <[email protected]>
Co-authored-by: Greg T. Wallace <[email protected]>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Caven <[email protected]>
Co-authored-by: sapphi-red <[email protected]>
Co-authored-by: Matyáš Racek <[email protected]>
Co-authored-by: bluwy <[email protected]>
  • Loading branch information
11 people authored Apr 19, 2024
1 parent b41895c commit f684d4c
Show file tree
Hide file tree
Showing 121 changed files with 5,730 additions and 2,542 deletions.
8 changes: 8 additions & 0 deletions docs/config/build-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,18 @@ When set to `true`, the build will also generate an SSR manifest for determining

Produce SSR-oriented build. The value can be a string to directly specify the SSR entry, or `true`, which requires specifying the SSR entry via `rollupOptions.input`.

## build.emitAssets

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

During non-client builds, static assets aren't emitted as it is assumed they would be emitted as part of the client build. This option allows frameworks to force emitting them in other environments build. It is responsibility of the framework to merge the assets with a post build step.

## build.ssrEmitAssets

- **Type:** `boolean`
- **Default:** `false`
- **Deprecated:** use `build.emitAssets`

During the SSR build, static assets aren't emitted as it is assumed they would be emitted as part of the client build. This option allows frameworks to force emitting them in both the client and SSR build. It is responsibility of the framework to merge the assets with a post build step.

Expand Down
1 change: 1 addition & 0 deletions docs/guide/api-javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ interface ViteDevServer {
/**
* Programmatically resolve, load and transform a URL and get the result
* without going through the http request pipeline.
* @deprecated use environment.transformRequest
*/
transformRequest(
url: string,
Expand Down
14 changes: 4 additions & 10 deletions docs/guide/api-vite-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,7 @@ export class ViteRuntime {
/**
* URL to execute. Accepts file path, server path, or id relative to the root.
*/
public async executeUrl<T = any>(url: string): Promise<T>
/**
* Entry point URL to execute. Accepts file path, server path or id relative to the root.
* In the case of a full reload triggered by HMR, this is the module that will be reloaded.
* If this method is called multiple times, all entry points will be reloaded one at a time.
*/
public async executeEntrypoint<T = any>(url: string): Promise<T>
public async import<T = any>(url: string): Promise<T>
/**
* Clear all caches including HMR listeners.
*/
Expand All @@ -57,7 +51,7 @@ The `ViteRuntime` class requires `root` and `fetchModule` options when initiated

Runner in `ViteRuntime` is responsible for executing the code. Vite exports `ESModulesRunner` out of the box, it uses `new AsyncFunction` to run the code. You can provide your own implementation if your JavaScript runtime doesn't support unsafe evaluation.

The two main methods that runtime exposes are `executeUrl` and `executeEntrypoint`. The only difference between them is that all modules executed by `executeEntrypoint` will be reexecuted if HMR triggers `full-reload` event. Be aware that Vite Runtime doesn't update `exports` object when this happens (it overrides it), you would need to run `executeUrl` or get the module from `moduleCache` again if you rely on having the latest `exports` object.
Module runner exposes `import` method. When Vite server triggers `full-reload` HMR event, all affected modules will be re-executed. Be aware that Module Runner doesn't update `exports` object when this happens (it overrides it), you would need to run `import` or get the module from `moduleCache` again if you rely on having the latest `exports` object.

**Example Usage:**

Expand All @@ -74,7 +68,7 @@ const runtime = new ViteRuntime(
new ESModulesRunner(),
)

await runtime.executeEntrypoint('/src/entry-point.js')
await runtime.import('/src/entry-point.js')
```

## `ViteRuntimeOptions`
Expand Down Expand Up @@ -209,7 +203,7 @@ const __dirname = fileURLToPath(new URL('.', import.meta.url))
await server.listen()
const runtime = await createViteRuntime(server)
await runtime.executeEntrypoint('/src/entry-point.js')
await runtime.import('/src/entry-point.js')
})()
```

Expand Down
123 changes: 123 additions & 0 deletions packages/vite/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,114 @@
## 6.0.0-alpha.2 (2024-04-09)

* chore: update ([46c8910](https://github.com/vitejs/vite/commit/46c8910))
* feat: environment aware define ([9f9a716](https://github.com/vitejs/vite/commit/9f9a716))
* feat: rework more ssr.target webworker branches ([1f644d0](https://github.com/vitejs/vite/commit/1f644d0))



## 6.0.0-alpha.1 (2024-04-08)

* fix: `fsp.rm` removing files does not take effect (#16032) ([b05c405](https://github.com/vitejs/vite/commit/b05c405)), closes [#16032](https://github.com/vitejs/vite/issues/16032)
* fix: csp nonce injection when no closing tag (#16281) (#16282) ([67a74f8](https://github.com/vitejs/vite/commit/67a74f8)), closes [#16281](https://github.com/vitejs/vite/issues/16281) [#16282](https://github.com/vitejs/vite/issues/16282)
* fix: do not access document in `/@vite/client` when not defined (#16318) ([6c5536b](https://github.com/vitejs/vite/commit/6c5536b)), closes [#16318](https://github.com/vitejs/vite/issues/16318)
* fix: fix sourcemap when using object as `define` value (#15805) ([9699ba3](https://github.com/vitejs/vite/commit/9699ba3)), closes [#15805](https://github.com/vitejs/vite/issues/15805)
* fix: package types ([bdf13bb](https://github.com/vitejs/vite/commit/bdf13bb))
* fix(deps): update all non-major dependencies (#16376) ([58a2938](https://github.com/vitejs/vite/commit/58a2938)), closes [#16376](https://github.com/vitejs/vite/issues/16376)
* fix(environment): use `environments.client.build.outDir` for preview (#16301) ([8621c3f](https://github.com/vitejs/vite/commit/8621c3f)), closes [#16301](https://github.com/vitejs/vite/issues/16301)
* feat: async createEnvironment ([d15a157](https://github.com/vitejs/vite/commit/d15a157))
* feat: dedupe/preserveSymlinks ([3ba9214](https://github.com/vitejs/vite/commit/3ba9214))
* refactor: environment.dev.recoverable ([ea1c7eb](https://github.com/vitejs/vite/commit/ea1c7eb))
* refactor: isFileServingAllowed load fallback for SSR ([d91714b](https://github.com/vitejs/vite/commit/d91714b))
* refactor: lib options ([70731ce](https://github.com/vitejs/vite/commit/70731ce))
* chore: merge ([bcac048](https://github.com/vitejs/vite/commit/bcac048))
* chore: merge ([833dabf](https://github.com/vitejs/vite/commit/833dabf))
* chore: remove ssr.target use ([0ea8be9](https://github.com/vitejs/vite/commit/0ea8be9))
* chore: remove ssrConfig ([27371dc](https://github.com/vitejs/vite/commit/27371dc))
* chore: update region comment (#16380) ([77562c3](https://github.com/vitejs/vite/commit/77562c3)), closes [#16380](https://github.com/vitejs/vite/issues/16380)
* chore(deps): update all non-major dependencies (#16325) ([c7efec4](https://github.com/vitejs/vite/commit/c7efec4)), closes [#16325](https://github.com/vitejs/vite/issues/16325)
* perf: reduce size of injected __vite__mapDeps code (#16184) ([a9bf430](https://github.com/vitejs/vite/commit/a9bf430)), closes [#16184](https://github.com/vitejs/vite/issues/16184)
* perf: reduce size of injected __vite__mapDeps code (#16184) ([c0ec6be](https://github.com/vitejs/vite/commit/c0ec6be)), closes [#16184](https://github.com/vitejs/vite/issues/16184)
* perf(css): only replace empty chunk if imported (#16349) ([f61d8b1](https://github.com/vitejs/vite/commit/f61d8b1)), closes [#16349](https://github.com/vitejs/vite/issues/16349)



## 6.0.0-alpha.0 (2024-04-05)

* feat: abstract moduleGraph into ModuleExecutionEnvironment ([5f5e0ec](https://github.com/vitejs/vite/commit/5f5e0ec))
* feat: add `hot` property to environments ([e966ba0](https://github.com/vitejs/vite/commit/e966ba0))
* feat: build.ssrEmitAssets -> build.emitAssets ([ef8c9b9](https://github.com/vitejs/vite/commit/ef8c9b9))
* feat: builder config, runBuildTasks option ([f4789a3](https://github.com/vitejs/vite/commit/f4789a3))
* feat: configureDevEnvironments + configureBuildEnvironments ([88fea3b](https://github.com/vitejs/vite/commit/88fea3b))
* feat: environment aware createIdResolver ([f1dcd2c](https://github.com/vitejs/vite/commit/f1dcd2c))
* feat: environment aware createResolver and resolvePlugin ([dd6332e](https://github.com/vitejs/vite/commit/dd6332e))
* feat: environment aware depsOptimizer ([a7e52aa](https://github.com/vitejs/vite/commit/a7e52aa))
* feat: environment id resolver for css plugin ([0bec1b9](https://github.com/vitejs/vite/commit/0bec1b9))
* feat: environment in hooks, context vs param (#16261) ([fbe6361](https://github.com/vitejs/vite/commit/fbe6361)), closes [#16261](https://github.com/vitejs/vite/issues/16261)
* feat: environment.transformRequest ([fcebb7d](https://github.com/vitejs/vite/commit/fcebb7d))
* feat: inject environment in build hooks ([cef1091](https://github.com/vitejs/vite/commit/cef1091))
* feat: separate module graphs per environment ([83068fe](https://github.com/vitejs/vite/commit/83068fe))
* feat: server.runHmrTasks ([7f94c03](https://github.com/vitejs/vite/commit/7f94c03))
* feat: ssr.external/noExternal -> resolve.external/noExternal ([2a0b524](https://github.com/vitejs/vite/commit/2a0b524))
* feat: ssr.target -> environment.webCompatible ([1a7d290](https://github.com/vitejs/vite/commit/1a7d290))
* feat: support transport options to communicate between the environment and the runner (#16209) ([dbcc375](https://github.com/vitejs/vite/commit/dbcc375)), closes [#16209](https://github.com/vitejs/vite/issues/16209)
* feat: vite runtime renamed to module runner (#16137) ([60f7f2b](https://github.com/vitejs/vite/commit/60f7f2b)), closes [#16137](https://github.com/vitejs/vite/issues/16137)
* feat(hmr): call `hotUpdate` hook with file create/delete (#16249) ([3d37ac1](https://github.com/vitejs/vite/commit/3d37ac1)), closes [#16249](https://github.com/vitejs/vite/issues/16249)
* refactor: allow custom connections in node module runner ([9005841](https://github.com/vitejs/vite/commit/9005841))
* refactor: base environment.config + environment.options ([c7e4da2](https://github.com/vitejs/vite/commit/c7e4da2))
* refactor: buildEnvironments + hmrEnvironments ([c1fc111](https://github.com/vitejs/vite/commit/c1fc111))
* refactor: clientEnvironment instead of browserEnvironment (#16194) ([ccf3de4](https://github.com/vitejs/vite/commit/ccf3de4)), closes [#16194](https://github.com/vitejs/vite/issues/16194)
* refactor: configEnvironment hook + enviroment config resolving ([fee54ea](https://github.com/vitejs/vite/commit/fee54ea))
* refactor: environment id,type -> name + fixes ([29f1b7b](https://github.com/vitejs/vite/commit/29f1b7b))
* refactor: environments array to plain object ([a7a06fe](https://github.com/vitejs/vite/commit/a7a06fe))
* refactor: environments as array instead of map (#16193) ([f1d660c](https://github.com/vitejs/vite/commit/f1d660c)), closes [#16193](https://github.com/vitejs/vite/issues/16193)
* refactor: hooks get an environment object instead of a string ([5e60d8a](https://github.com/vitejs/vite/commit/5e60d8a))
* refactor: hooks to config for creating environments ([3e6216c](https://github.com/vitejs/vite/commit/3e6216c))
* refactor: isolate back compat module graph in its own module ([8000e8e](https://github.com/vitejs/vite/commit/8000e8e))
* refactor: ModuleExecutionEnvironment -> DevEnvironment ([6e71b24](https://github.com/vitejs/vite/commit/6e71b24))
* refactor: move safeModulesPath set to server ([95ae29b](https://github.com/vitejs/vite/commit/95ae29b))
* refactor: move transport to properties ([9cfa916](https://github.com/vitejs/vite/commit/9cfa916))
* refactor: node -> ssr for default environment ([e03bac8](https://github.com/vitejs/vite/commit/e03bac8))
* refactor: options and environment are required when calling container.hook ([e30b858](https://github.com/vitejs/vite/commit/e30b858))
* refactor: pass down name to the environment factory ([52edfc9](https://github.com/vitejs/vite/commit/52edfc9))
* refactor: remove default nodeModuleRunner because it's not used anywhere ([f29e95a](https://github.com/vitejs/vite/commit/f29e95a))
* refactor: remove environment name from the hmr context ([a183a0f](https://github.com/vitejs/vite/commit/a183a0f))
* refactor: rename "hmrEnvironments" to "hotUpdateEnvironments" ([a0b7edb](https://github.com/vitejs/vite/commit/a0b7edb))
* refactor: rename createSsrEnvironment to createNodeEnvironment ([c9abcfc](https://github.com/vitejs/vite/commit/c9abcfc))
* refactor: rename ssrInvalidates to invalidates ([72fe84e](https://github.com/vitejs/vite/commit/72fe84e))
* refactor: rework resolveId in ModuleExecutionEnvironment constructor ([03d3889](https://github.com/vitejs/vite/commit/03d3889))
* refactor: ssrConfig.optimizeDeps.include/exclude ([5bd8e95](https://github.com/vitejs/vite/commit/5bd8e95))
* refactor: use ssr environment module graph in ssrFixStacktrace ([5477972](https://github.com/vitejs/vite/commit/5477972))
* fix: add auto complete to server.environments ([a160a1b](https://github.com/vitejs/vite/commit/a160a1b))
* fix: call updateModules for each environmnet ([281cf97](https://github.com/vitejs/vite/commit/281cf97))
* fix: fine-grained hmr ([31e1d3a](https://github.com/vitejs/vite/commit/31e1d3a))
* fix: HotContext only gets ModuleExecutionEnvironment ([30be775](https://github.com/vitejs/vite/commit/30be775))
* fix: injectEnvironmentInContext ([a1d385c](https://github.com/vitejs/vite/commit/a1d385c))
* fix: injectEnvironmentToHooks ([681ccd4](https://github.com/vitejs/vite/commit/681ccd4))
* fix: missing externalConditions back compat ([beb40ef](https://github.com/vitejs/vite/commit/beb40ef))
* fix: optimizeDeps backward compatibility layer ([3806fe6](https://github.com/vitejs/vite/commit/3806fe6))
* fix: partial backward compat for config.ssr ([85ada0d](https://github.com/vitejs/vite/commit/85ada0d))
* fix: resolve.externalConditions ([fb9365c](https://github.com/vitejs/vite/commit/fb9365c))
* fix: use "register" event for remote environment transport ([c4f4dfb](https://github.com/vitejs/vite/commit/c4f4dfb))
* fix(css): unknown file error happened with lightningcss (#16306) ([01af308](https://github.com/vitejs/vite/commit/01af308)), closes [#16306](https://github.com/vitejs/vite/issues/16306)
* fix(hmr): multiple updates happened when invalidate is called while multiple tabs open (#16307) ([21cc10b](https://github.com/vitejs/vite/commit/21cc10b)), closes [#16307](https://github.com/vitejs/vite/issues/16307)
* fix(scanner): duplicate modules for same id if glob is used in html-like types (#16305) ([eca68fa](https://github.com/vitejs/vite/commit/eca68fa)), closes [#16305](https://github.com/vitejs/vite/issues/16305)
* test: add test for worker transport ([a5ef42e](https://github.com/vitejs/vite/commit/a5ef42e))
* test: fix after merge ([d9ed857](https://github.com/vitejs/vite/commit/d9ed857))
* test(environment): add environment playground (#16299) ([a5c7e4f](https://github.com/vitejs/vite/commit/a5c7e4f)), closes [#16299](https://github.com/vitejs/vite/issues/16299)
* chore: fix lint ([b4e46fe](https://github.com/vitejs/vite/commit/b4e46fe))
* chore: fix lint ([6040ab3](https://github.com/vitejs/vite/commit/6040ab3))
* chore: lint ([8785f4f](https://github.com/vitejs/vite/commit/8785f4f))
* chore: lint ([92eccf9](https://github.com/vitejs/vite/commit/92eccf9))
* chore: lint ([f927702](https://github.com/vitejs/vite/commit/f927702))
* chore: rename module and error back to ssrModule and ssrError ([d8ff12a](https://github.com/vitejs/vite/commit/d8ff12a))
* chore: rename server environment to node environment ([4808b27](https://github.com/vitejs/vite/commit/4808b27))
* chore: run prettier on environment file ([1fe63b1](https://github.com/vitejs/vite/commit/1fe63b1))
* chore: update ([9a600fe](https://github.com/vitejs/vite/commit/9a600fe))
* chore: update environment.server.config ([2ddf28e](https://github.com/vitejs/vite/commit/2ddf28e))
* wip: environment config overrides ([81abf6e](https://github.com/vitejs/vite/commit/81abf6e))



## <small>5.2.9 (2024-04-15)</small>

* fix: `fsp.rm` removing files does not take effect (#16032) ([b05c405](https://github.com/vitejs/vite/commit/b05c405)), closes [#16032](https://github.com/vitejs/vite/issues/16032)
Expand All @@ -9,6 +120,18 @@



## <small>5.2.8 (2024-04-03)</small>

* release: v5.2.8 ([8b8d402](https://github.com/vitejs/vite/commit/8b8d402))
* fix: csp nonce injection when no closing tag (#16281) (#16282) ([3c85c6b](https://github.com/vitejs/vite/commit/3c85c6b)), closes [#16281](https://github.com/vitejs/vite/issues/16281) [#16282](https://github.com/vitejs/vite/issues/16282)
* fix: do not access document in `/@vite/client` when not defined (#16318) ([646319c](https://github.com/vitejs/vite/commit/646319c)), closes [#16318](https://github.com/vitejs/vite/issues/16318)
* fix: fix sourcemap when using object as `define` value (#15805) ([445c4f2](https://github.com/vitejs/vite/commit/445c4f2)), closes [#15805](https://github.com/vitejs/vite/issues/15805)
* chore(deps): update all non-major dependencies (#16325) ([a78e265](https://github.com/vitejs/vite/commit/a78e265)), closes [#16325](https://github.com/vitejs/vite/issues/16325)
* refactor: use types from sass instead of @types/sass (#16340) ([4581e83](https://github.com/vitejs/vite/commit/4581e83)), closes [#16340](https://github.com/vitejs/vite/issues/16340)




## <small>5.2.8 (2024-04-03)</small>

* fix: csp nonce injection when no closing tag (#16281) (#16282) ([3c85c6b](https://github.com/vitejs/vite/commit/3c85c6b)), closes [#16281](https://github.com/vitejs/vite/issues/16281) [#16282](https://github.com/vitejs/vite/issues/16282)
Expand Down
12 changes: 6 additions & 6 deletions packages/vite/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vite",
"version": "5.2.9",
"version": "6.0.0-alpha.2",
"type": "module",
"license": "MIT",
"author": "Evan You",
Expand Down Expand Up @@ -32,9 +32,9 @@
"./client": {
"types": "./client.d.ts"
},
"./runtime": {
"types": "./dist/node/runtime.d.ts",
"import": "./dist/node/runtime.js"
"./module-runner": {
"types": "./dist/node/module-runner.d.ts",
"import": "./dist/node/module-runner.js"
},
"./dist/client/*": "./dist/client/*",
"./types/*": {
Expand All @@ -44,8 +44,8 @@
},
"typesVersions": {
"*": {
"runtime": [
"dist/node/runtime.d.ts"
"module-runner": [
"dist/node/module-runner.d.ts"
]
}
},
Expand Down
12 changes: 6 additions & 6 deletions packages/vite/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,11 @@ function createNodeConfig(isProduction: boolean) {
})
}

function createRuntimeConfig(isProduction: boolean) {
function createModuleRunnerConfig(isProduction: boolean) {
return defineConfig({
...sharedNodeOptions,
input: {
runtime: path.resolve(__dirname, 'src/runtime/index.ts'),
'module-runner': path.resolve(__dirname, 'src/module-runner/index.ts'),
},
output: {
...sharedNodeOptions.output,
Expand All @@ -202,7 +202,7 @@ function createRuntimeConfig(isProduction: boolean) {
isProduction ? false : './dist/node',
),
esbuildMinifyPlugin({ minify: false, minifySyntax: true }),
bundleSizeLimit(45),
bundleSizeLimit(47),
],
})
}
Expand Down Expand Up @@ -240,7 +240,7 @@ export default (commandLineArgs: any): RollupOptions[] => {
envConfig,
clientConfig,
createNodeConfig(isProduction),
createRuntimeConfig(isProduction),
createModuleRunnerConfig(isProduction),
createCjsConfig(isProduction),
])
}
Expand Down Expand Up @@ -332,10 +332,10 @@ const __require = require;
name: 'cjs-chunk-patch',
renderChunk(code, chunk) {
if (!chunk.fileName.includes('chunks/dep-')) return
// don't patch runtime utils chunk because it should stay lightweight and we know it doesn't use require
// don't patch runner utils chunk because it should stay lightweight and we know it doesn't use require
if (
chunk.name === 'utils' &&
chunk.moduleIds.some((id) => id.endsWith('/ssr/runtime/utils.ts'))
chunk.moduleIds.some((id) => id.endsWith('/ssr/module-runner/utils.ts'))
)
return
const match = code.match(/^(?:import[\s\S]*?;\s*)+/)
Expand Down
Loading

0 comments on commit f684d4c

Please sign in to comment.