Skip to content

Commit

Permalink
Wrap in experiment.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Jan 19, 2024
1 parent 3974773 commit 7a71420
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 8 deletions.
20 changes: 16 additions & 4 deletions docs/guide/monorepo-packages.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,19 @@ This method is based entirely around `package.json` entry points. For local pack

- Requires entry points within the package's `package.json`.
- Requires package (npm, pnpm, yarn) workspaces.
- Requires the package to exist in `node_modules` (symlinked via workspaces).
- Requires the package to be symlinked in `node_modules`.

Because this functionality is experimental, it must be enabled in your config.

```js
import { defineConfig } from 'vite'

export default defineConfig({
experimental: {
vitePackageEntryPoints: true,
},
})
```

### Implementation

Expand Down Expand Up @@ -44,11 +56,11 @@ For packages that are depended on (not symlinked in `node_modules`), the `vite`

### Caveats

- If using `vite` exports condition:
- If using `vite` export condition:
- To support deep imports, the `./*` entry point must be defined, which maps 1:1 to the file system.
- To support multiple source files with different extensions, use an array as the `vite` condition (example above).
- To support multiple source files with different extensions, use an array as the `vite` condition value (example above).
- If using `vite` main entry point:
- Only default imports are supported.
- Only default/index imports are supported.
- Deep imports are not supported.
- Packages that are not symlinked into `node_modules` will _not_ use the `vite` entry points. For reference, `workspace:`, `portal:`, and `link:` are supported, while `file:`, `git:`, etc are not.

Expand Down
11 changes: 10 additions & 1 deletion packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,15 @@ export interface ExperimentalOptions {
* @default false
*/
skipSsrTransform?: boolean
/**
* Resolves source files of local packages (in a monorepo workspace) using `package.json`
* entry points. If using `exports`, uses the `vite` condition. Otherwise, uses the `vite`
* main-like field.
*
* @experimental
* @default false
*/
vitePackageEntryPoints?: boolean
}

export interface LegacyOptions {
Expand Down Expand Up @@ -1257,7 +1266,7 @@ function optimizeDepsDisabledBackwardCompatibility(
}
resolved.logger.warn(
colors.yellow(`(!) Experimental ${optimizeDepsPath}optimizeDeps.disabled and deps pre-bundling during build were removed in Vite 5.1.
To disable the deps optimizer, set ${optimizeDepsPath}optimizeDeps.noDiscovery to true and ${optimizeDepsPath}optimizeDeps.include as undefined or empty.
To disable the deps optimizer, set ${optimizeDepsPath}optimizeDeps.noDiscovery to true and ${optimizeDepsPath}optimizeDeps.include as undefined or empty.
Please remove ${optimizeDepsPath}optimizeDeps.disabled from your config.
${
commonjsPluginDisabled
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/node/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export async function resolvePlugins(
isBuild && config.build.ssr
? (id, importer) => shouldExternalizeForSSR(id, importer, config)
: undefined,
vitePackageEntryPoints: config.experimental.vitePackageEntryPoints,
}),
htmlInlineProxyPlugin(config),
cssPlugin(config),
Expand Down
7 changes: 5 additions & 2 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ export interface InternalResolveOptions extends Required<ResolveOptions> {
* @internal
*/
idOnly?: boolean

// Maps to the experiment of the same name
vitePackageEntryPoints?: boolean
}

export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
Expand Down Expand Up @@ -980,7 +983,7 @@ export function resolvePackageEntry(
const mainFields = [...options.mainFields]

// Support `vite` entry point field for local packages
if (pkg.inWorkspace) {
if (pkg.inWorkspace && options.vitePackageEntryPoints) {
mainFields.unshift('vite')
}

Expand Down Expand Up @@ -1088,7 +1091,7 @@ function resolveExportsOrImports(
})

// Support `vite` condition for local packages
if (pkg.inWorkspace) {
if (pkg.inWorkspace && options.vitePackageEntryPoints) {
conditions.unshift('vite')
}

Expand Down
5 changes: 4 additions & 1 deletion playground/monorepo-vite-condition/exports/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
"module": "./build/index.js"
},
"./*": {
"vite": ["./src/*.ts", "./src/*.tsx"],
"vite": [
"./src/*.ts",
"./src/*.tsx"
],
"module": "./build/*.js"
}
}
Expand Down
3 changes: 3 additions & 0 deletions playground/monorepo-vite-condition/root/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ export default defineConfig({
// None!
alias: {},
},
experimental: {
vitePackageEntryPoints: true,
},
})

0 comments on commit 7a71420

Please sign in to comment.