Skip to content

Commit 05302b0

Browse files
authored
feat(config): warn features incompatible with native loader in bundle loader (#22850)
1 parent 65d3604 commit 05302b0

40 files changed

Lines changed: 579 additions & 32 deletions

‎docs/config/index.md‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export default {
1414
}
1515
```
1616

17-
Note Vite supports using ES modules syntax in the config file even if the project is not using native Node ESM, e.g. `"type": "module"` in `package.json`. In this case, the config file is auto pre-processed before load.
17+
Note that to use ES modules syntax in the config file, it should be in a file detected as ESM by Node.js, e.g. `.mjs` or `.js` with `"type": "module"` in closest `package.json`.
1818

1919
You can also explicitly specify a config file to use with the `--config` CLI option (resolved relative to `cwd`):
2020

@@ -25,9 +25,7 @@ vite --config my-config.js
2525
<ScrimbaLink href="https://scrimba.com/intro-to-vite-c03p6pbbdq/~05jg?via=vite" title="Configuring Vite">Watch an interactive lesson on Scrimba</ScrimbaLink>
2626

2727
::: tip CONFIG LOADING
28-
By default, Vite uses [Rolldown](https://rolldown.rs/) to bundle the config into a temporary file and load it. This may cause issues when importing TypeScript files in a monorepo. If you encounter any issues with this approach, you can specify `--configLoader runner` to use the [module runner](/guide/api-environment-runtimes.html#modulerunner) instead, which will not create a temporary config and will transform any files on the fly. Note that module runner doesn't support CJS in config files, but external CJS packages should work as usual.
29-
30-
Alternatively, if you're using an environment that supports TypeScript (e.g. `node --experimental-strip-types`), or if you're only writing plain JavaScript, you can specify `--configLoader native` to use the environment's native runtime to load the config file. Note that updates to modules imported by the config file are not detected and hence would not auto-restart the Vite server.
28+
By default, Vite uses [Rolldown](https://rolldown.rs/) to bundle the config into a temporary file and load it. If you're using an environment that supports TypeScript (e.g. Node 22.18+), or if you're only writing plain JavaScript, you can specify `--configLoader native` to use the environment's native runtime to load the config file. `configLoader: 'native'` is planned to become the default in a future major version.
3129
:::
3230

3331
## Config Intellisense

‎packages/vite/LICENSE.md‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,6 +1219,13 @@ Repository: https://github.com/micromatch/is-glob
12191219
12201220
---------------------------------------
12211221

1222+
## is-reference
1223+
License: MIT
1224+
By: Rich Harris
1225+
Repository: https://github.com/Rich-Harris/is-reference
1226+
1227+
---------------------------------------
1228+
12221229
## isexe, which
12231230
License: ISC
12241231
By: Isaac Z. Schlueter
@@ -2285,3 +2292,31 @@ Repository: https://github.com/websockets/ws
22852292
> COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
22862293
> IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22872294
> CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2295+
2296+
---------------------------------------
2297+
2298+
## zimmerframe
2299+
License: MIT
2300+
Repository: https://github.com/sveltejs/zimmerframe
2301+
2302+
> MIT License
2303+
>
2304+
> Copyright (c) 2023 [these people](https://github.com/Rich-Harris/zimmerframe/graphs/contributors)
2305+
>
2306+
> Permission is hereby granted, free of charge, to any person obtaining a copy
2307+
> of this software and associated documentation files (the "Software"), to deal
2308+
> in the Software without restriction, including without limitation the rights
2309+
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
2310+
> copies of the Software, and to permit persons to whom the Software is
2311+
> furnished to do so, subject to the following conditions:
2312+
>
2313+
> The above copyright notice and this permission notice shall be included in all
2314+
> copies or substantial portions of the Software.
2315+
>
2316+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2317+
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2318+
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2319+
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2320+
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2321+
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2322+
> SOFTWARE.

‎packages/vite/src/node/__tests__/config.spec.ts‎

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import http from 'node:http'
22
import os from 'node:os'
33
import path from 'node:path'
44
import fs from 'node:fs'
5+
import { stripVTControlCharacters } from 'node:util'
56
import { afterEach, assert, describe, expect, test, vi } from 'vitest'
67
import type { InlineConfig, PluginOption } from '..'
78
import type { UserConfig, UserConfigExport } from '../config'
@@ -1371,6 +1372,144 @@ test('preTransformRequests', async () => {
13711372
describe('loadConfigFromFile', () => {
13721373
const fixtures = path.resolve(import.meta.dirname, './fixtures/config')
13731374

1375+
describe('native-incompatibility warnings', () => {
1376+
const compatRoot = path.resolve(fixtures, './native-compat')
1377+
1378+
// eslint-disable-next-line n/no-unsupported-features/node-builtins
1379+
const supportsTypeStripping = !!process.features.typescript
1380+
1381+
const loadWithWarnings = async (
1382+
dir: string,
1383+
configFile = 'vite.config.js',
1384+
) => {
1385+
const logger = createLogger('info')
1386+
const warn = vi.spyOn(logger, 'warn').mockImplementation(() => {})
1387+
const root = path.resolve(compatRoot, dir)
1388+
await loadConfigFromFile(
1389+
{} as any,
1390+
path.resolve(root, configFile),
1391+
root,
1392+
undefined,
1393+
logger,
1394+
'bundle',
1395+
)
1396+
const messages = warn.mock.calls.map((c) =>
1397+
stripVTControlCharacters(c[0]),
1398+
)
1399+
warn.mockRestore()
1400+
return messages
1401+
}
1402+
1403+
test('warns on __dirname / __filename', async () => {
1404+
const messages = await loadWithWarnings('dirname')
1405+
expect(messages).toMatchInlineSnapshot(`
1406+
[
1407+
"(!) Your Vite config uses features that are unsupported by \`configLoader: 'native'\`, which is planned to become the default in a future major version of Vite:
1408+
- \`__dirname\` (vite.config.js:3). Use \`import.meta.dirname\` instead
1409+
- \`__filename\` (vite.config.js:4). Use \`import.meta.filename\` instead
1410+
Set \`VITE_CONFIG_NATIVE_IGNORE_WARNING=true\` to suppress this warning.",
1411+
]
1412+
`)
1413+
})
1414+
1415+
// uses a `.ts` config with TypeScript-only syntax; skipped on Node
1416+
// versions without type stripping support
1417+
test.skipIf(!supportsTypeStripping)(
1418+
'warns on __dirname in a config using TypeScript syntax',
1419+
async () => {
1420+
const messages = await loadWithWarnings('ts-syntax', 'vite.config.ts')
1421+
expect(messages).toMatchInlineSnapshot(`
1422+
[
1423+
"(!) Your Vite config uses features that are unsupported by \`configLoader: 'native'\`, which is planned to become the default in a future major version of Vite:
1424+
- \`__dirname\` (vite.config.ts:10). Use \`import.meta.dirname\` instead
1425+
Set \`VITE_CONFIG_NATIVE_IGNORE_WARNING=true\` to suppress this warning.",
1426+
]
1427+
`)
1428+
},
1429+
)
1430+
1431+
test('warns on extension-less import', async () => {
1432+
const messages = await loadWithWarnings('extensionless')
1433+
expect(messages).toMatchInlineSnapshot(`
1434+
[
1435+
"(!) Your Vite config uses features that are unsupported by \`configLoader: 'native'\`, which is planned to become the default in a future major version of Vite:
1436+
- import "./helper" without a file extension (vite.config.js:1). Add the file extension
1437+
Set \`VITE_CONFIG_NATIVE_IGNORE_WARNING=true\` to suppress this warning.",
1438+
]
1439+
`)
1440+
})
1441+
1442+
test('warns on directory-index import', async () => {
1443+
const messages = await loadWithWarnings('directory-index')
1444+
expect(messages).toMatchInlineSnapshot(`
1445+
[
1446+
"(!) Your Vite config uses features that are unsupported by \`configLoader: 'native'\`, which is planned to become the default in a future major version of Vite:
1447+
- import "./plugins" resolves to a directory index (vite.config.js:1). Import the index file directly
1448+
Set \`VITE_CONFIG_NATIVE_IGNORE_WARNING=true\` to suppress this warning.",
1449+
]
1450+
`)
1451+
})
1452+
1453+
test('warns on JSON import without attributes', async () => {
1454+
const messages = await loadWithWarnings('json')
1455+
expect(messages).toMatchInlineSnapshot(`
1456+
[
1457+
"(!) Your Vite config uses features that are unsupported by \`configLoader: 'native'\`, which is planned to become the default in a future major version of Vite:
1458+
- JSON import "./data.json" without import attributes (vite.config.js:1). Add \`with { type: 'json' }\`
1459+
Set \`VITE_CONFIG_NATIVE_IGNORE_WARNING=true\` to suppress this warning.",
1460+
]
1461+
`)
1462+
})
1463+
1464+
test('does not warn on JSON import with attributes', async () => {
1465+
expect(await loadWithWarnings('json-ok')).toHaveLength(0)
1466+
})
1467+
1468+
test('warns on an extension-less import that resolves to JSON', async () => {
1469+
const messages = await loadWithWarnings('json-extensionless')
1470+
expect(messages).toMatchInlineSnapshot(`
1471+
[
1472+
"(!) Your Vite config uses features that are unsupported by \`configLoader: 'native'\`, which is planned to become the default in a future major version of Vite:
1473+
- import "./foo" resolves to a JSON file (vite.config.js:1). Import it with a \`.json\` extension and \`with { type: 'json' }\`
1474+
Set \`VITE_CONFIG_NATIVE_IGNORE_WARNING=true\` to suppress this warning.",
1475+
]
1476+
`)
1477+
})
1478+
1479+
test('does not warn on a native-compatible config', async () => {
1480+
expect(await loadWithWarnings('clean')).toHaveLength(0)
1481+
})
1482+
1483+
test('does not warn for a CJS config using __dirname', async () => {
1484+
expect(await loadWithWarnings('cjs', 'vite.config.cjs')).toHaveLength(0)
1485+
})
1486+
1487+
test('warns for an ESM file imported by a CJS config', async () => {
1488+
const messages = await loadWithWarnings(
1489+
'cjs-imports-esm',
1490+
'vite.config.cjs',
1491+
)
1492+
expect(messages).toMatchInlineSnapshot(`
1493+
[
1494+
"(!) Your Vite config uses features that are unsupported by \`configLoader: 'native'\`, which is planned to become the default in a future major version of Vite:
1495+
- \`__dirname\` (esm-helper.mjs:1). Use \`import.meta.dirname\` instead
1496+
Set \`VITE_CONFIG_NATIVE_IGNORE_WARNING=true\` to suppress this warning.",
1497+
]
1498+
`)
1499+
})
1500+
1501+
test('warns on ESM syntax in a config under a `type: commonjs` package', async () => {
1502+
const messages = await loadWithWarnings('esm-in-commonjs-pkg')
1503+
expect(messages).toMatchInlineSnapshot(`
1504+
[
1505+
"(!) Your Vite config uses features that are unsupported by \`configLoader: 'native'\`, which is planned to become the default in a future major version of Vite:
1506+
- ESM syntax in a file loaded as CommonJS (vite.config.js:1). Use a \`.mjs\` extension or set \`"type": "module"\` in the closest package.json
1507+
Set \`VITE_CONFIG_NATIVE_IGNORE_WARNING=true\` to suppress this warning.",
1508+
]
1509+
`)
1510+
})
1511+
})
1512+
13741513
describe('load default files', () => {
13751514
const root = path.resolve(fixtures, './loadConfigFromFile')
13761515

‎packages/vite/src/node/__tests__/fixtures/config/entry/vite.config.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import moduleCondition from '@vite/test-config-plugin-module-condition'
2-
import { array } from '../siblings/foo'
2+
import { array } from '../siblings/foo.js'
33
import importsField from '#imports-field'
44

55
export default {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const dir = JSON.stringify(__dirname)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const { dir } = require('./esm-helper.mjs')
2+
3+
module.exports = { define: { DIR: dir } }
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
define: {
3+
__DIR__: JSON.stringify(__dirname),
4+
__FILE__: JSON.stringify(__filename),
5+
},
6+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const value = 1
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { value } from './helper.js'
2+
3+
export default {
4+
define: {
5+
DIR: JSON.stringify(import.meta.dirname),
6+
VALUE: value,
7+
},
8+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const plugins = []

0 commit comments

Comments
 (0)