Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 0 additions & 72 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -143,78 +143,6 @@ jobs:
steps:
- run: echo "Build & Test Failed"

test-js-plugins:
needs: changed
if: needs.changed.outputs.should_skip != 'true'
timeout-minutes: 20
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
node_version: [22]
fail-fast: false

name: "Build&Test: node-${{ matrix.node_version }}, ${{ matrix.os }} (js plugins)"
steps:
- name: Checkout
uses: actions/checkout@v6

- name: Install pnpm
uses: pnpm/action-setup@41ff72655975bd51cab0327fa583b6e92b6d3061 # v4.2.0

- name: Set node version to ${{ matrix.node_version }}
uses: actions/setup-node@v6
with:
node-version: ${{ matrix.node_version }}
cache: "pnpm"

- name: Install deps
run: pnpm install

# Install playwright's binary under custom directory to cache
- name: (non-windows) Set Playwright path and Get playwright version
if: runner.os != 'Windows'
run: |
echo "PLAYWRIGHT_BROWSERS_PATH=$HOME/.cache/playwright-bin" >> $GITHUB_ENV
PLAYWRIGHT_VERSION="$(pnpm ls --depth 0 --json -w playwright-chromium | jq --raw-output '.[0].devDependencies["playwright-chromium"].version')"
echo "PLAYWRIGHT_VERSION=$PLAYWRIGHT_VERSION" >> $GITHUB_ENV
- name: (windows) Set Playwright path and Get playwright version
if: runner.os == 'Windows'
run: |
echo "PLAYWRIGHT_BROWSERS_PATH=$HOME\.cache\playwright-bin" >> $env:GITHUB_ENV
$env:PLAYWRIGHT_VERSION="$(pnpm ls --depth 0 --json -w playwright-chromium | jq --raw-output '.[0].devDependencies["playwright-chromium"].version')"
echo "PLAYWRIGHT_VERSION=$env:PLAYWRIGHT_VERSION" >> $env:GITHUB_ENV

- name: Cache Playwright's binary
uses: actions/cache@v5
with:
key: ${{ runner.os }}-playwright-bin-v1-${{ env.PLAYWRIGHT_VERSION }}
path: ${{ env.PLAYWRIGHT_BROWSERS_PATH }}
restore-keys: |
${{ runner.os }}-playwright-bin-v1-

- name: Install Playwright
# does not need to explicitly set chromium after https://github.com/microsoft/playwright/issues/14862 is solved
run: pnpm playwright install chromium

- name: Build
run: pnpm run build

- name: Test unit
run: pnpm run test-unit
env:
_VITE_TEST_JS_PLUGIN: 1

- name: Test serve
run: pnpm run test-serve
env:
_VITE_TEST_JS_PLUGIN: 1

- name: Test build
run: pnpm run test-build
env:
_VITE_TEST_JS_PLUGIN: 1

lint:
timeout-minutes: 10
runs-on: ubuntu-latest
Expand Down
130 changes: 14 additions & 116 deletions packages/vite/src/node/__tests__/plugins/define.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ import { PartialEnvironment } from '../../baseEnvironment'

async function createDefinePluginTransform(
define: Record<string, any> = {},
build = true,
ssr = false,
isSsrDev = false,
) {
const ssr = isSsrDev
const build = !isSsrDev
const config = await resolveConfig(
{ configFile: false, define },
{ configFile: false, define, environments: { ssr: {} } },
build ? 'build' : 'serve',
)
const instance = definePlugin(config)
const environment = new PartialEnvironment(ssr ? 'ssr' : 'client', config)

return async (code: string) => {
if (process.env._VITE_TEST_JS_PLUGIN) {
if (isSsrDev) {
// @ts-expect-error transform.handler should exist
const result = await instance.transform.handler.call(
{ environment },
Expand Down Expand Up @@ -58,9 +59,12 @@ async function createDefinePluginTransform(
}
}

describe.skipIf(!process.env._VITE_TEST_JS_PLUGIN)('definePlugin', () => {
describe('definePlugin (SSR dev)', () => {
const createJsDefinePluginTransform = (define: Record<string, any> = {}) =>
createDefinePluginTransform(define, true)

test('replaces custom define', async () => {
const transform = await createDefinePluginTransform({
const transform = await createJsDefinePluginTransform({
__APP_VERSION__: JSON.stringify('1.0'),
})
expect(await transform('export const version = __APP_VERSION__ ;')).toBe(
Expand All @@ -72,7 +76,7 @@ describe.skipIf(!process.env._VITE_TEST_JS_PLUGIN)('definePlugin', () => {
})

test('should not replace if not defined', async () => {
const transform = await createDefinePluginTransform({
const transform = await createJsDefinePluginTransform({
__APP_VERSION__: JSON.stringify('1.0'),
})
expect(await transform('export const version = "1.0";')).toBe(undefined)
Expand All @@ -81,55 +85,8 @@ describe.skipIf(!process.env._VITE_TEST_JS_PLUGIN)('definePlugin', () => {
).toBe(undefined)
})

test('replaces import.meta.env.SSR with false', async () => {
const transform = await createDefinePluginTransform()
expect(await transform('export const isSSR = import.meta.env.SSR;')).toBe(
'export const isSSR = false;\n',
)
})

test('preserve import.meta.hot with override', async () => {
// assert that the default behavior is to replace import.meta.hot with undefined
const transform = await createDefinePluginTransform()
expect(await transform('export const hot = import.meta.hot;')).toBe(
'export const hot = undefined;\n',
)
// assert that we can specify a user define to preserve import.meta.hot
const overrideTransform = await createDefinePluginTransform({
'import.meta.hot': 'import.meta.hot',
})
expect(await overrideTransform('export const hot = import.meta.hot;')).toBe(
'export const hot = import.meta.hot;\n',
)
})

test('replace import.meta.env.UNKNOWN with undefined', async () => {
const transform = await createDefinePluginTransform()
expect(await transform('export const foo = import.meta.env.UNKNOWN;')).toBe(
'export const foo = undefined ;\n',
)
})

test('leave import.meta.env["UNKNOWN"] to runtime', async () => {
const transform = await createDefinePluginTransform()
expect(
await transform('export const foo = import.meta.env["UNKNOWN"];'),
).toMatch(
/const __vite_import_meta_env__ = .*;\nexport const foo = __vite_import_meta_env__\["UNKNOWN"\];/,
)
})

test('preserve import.meta.env.UNKNOWN with override', async () => {
const transform = await createDefinePluginTransform({
'import.meta.env.UNKNOWN': 'import.meta.env.UNKNOWN',
})
expect(await transform('export const foo = import.meta.env.UNKNOWN;')).toBe(
'export const foo = import.meta.env.UNKNOWN;\n',
)
})

test('replace import.meta.env when it is a invalid json', async () => {
const transform = await createDefinePluginTransform({
const transform = await createJsDefinePluginTransform({
'import.meta.env.LEGACY': '__VITE_IS_LEGACY__',
})

Expand All @@ -139,47 +96,13 @@ describe.skipIf(!process.env._VITE_TEST_JS_PLUGIN)('definePlugin', () => {
),
).toMatchInlineSnapshot(`
"export const isLegacy = __VITE_IS_LEGACY__;
undefined && console.log(undefined );
import.meta.env.UNDEFINED && console.log(import.meta.env.UNDEFINED);
"
`)
})

test('replace bare import.meta.env', async () => {
const transform = await createDefinePluginTransform()
expect(await transform('export const env = import.meta.env;')).toMatch(
/const __vite_import_meta_env__ = .*;\nexport const env = __vite_import_meta_env__;/,
)
})

test('already has marker', async () => {
const transform = await createDefinePluginTransform()
expect(
await transform(
'console.log(__vite_import_meta_env__);\nexport const env = import.meta.env;',
),
).toMatch(
/const __vite_import_meta_env__1 = .*;\nconsole.log\(__vite_import_meta_env__\);\nexport const env = __vite_import_meta_env__1;/,
)

expect(
await transform(
'console.log(__vite_import_meta_env__, __vite_import_meta_env__1);\n export const env = import.meta.env;',
),
).toMatch(
/const __vite_import_meta_env__2 = .*;\nconsole.log\(__vite_import_meta_env__, __vite_import_meta_env__1\);\nexport const env = __vite_import_meta_env__2;/,
)

expect(
await transform(
'console.log(__vite_import_meta_env__);\nexport const env = import.meta.env;\nconsole.log(import.meta.env.UNDEFINED);',
),
).toMatch(
/const __vite_import_meta_env__1 = .*;\nconsole.log\(__vite_import_meta_env__\);\nexport const env = __vite_import_meta_env__1;\nconsole.log\(undefined {26}\);/,
)
})
})

describe.skipIf(process.env._VITE_TEST_JS_PLUGIN)('native definePlugin', () => {
describe('native definePlugin', () => {
test('replaces custom define', async () => {
const transform = await createDefinePluginTransform({
__APP_VERSION__: JSON.stringify('1.0'),
Expand Down Expand Up @@ -269,29 +192,4 @@ describe.skipIf(process.env._VITE_TEST_JS_PLUGIN)('native definePlugin', () => {
/const env = .*;\n\nexport \{ env \};/s,
)
})

test('already has marker', async () => {
const transform = await createDefinePluginTransform()
expect(
await transform(
'console.log(__vite_import_meta_env__);\nexport const env = import.meta.env;',
),
).toMatch(/console.log\(__vite_import_meta_env__\);\nconst env = .*/)

expect(
await transform(
'console.log(__vite_import_meta_env__, __vite_import_meta_env__1);\n export const env = import.meta.env;',
),
).toMatch(
/console.log\(__vite_import_meta_env__, __vite_import_meta_env__1\);\nconst env = .*/,
)

expect(
await transform(
'console.log(__vite_import_meta_env__);\nexport const env = import.meta.env;\nconsole.log(import.meta.env.UNDEFINED);',
),
).toMatch(
/console.log\(__vite_import_meta_env__\);\nconst env = .*;\nconsole.log\(void 0\);/s,
)
})
})
2 changes: 1 addition & 1 deletion packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ export async function resolveBuildPlugins(config: ResolvedConfig): Promise<{
...(isBuild && !config.isWorker
? [
licensePlugin(),
manifestPlugin(config),
manifestPlugin(),
ssrManifestPlugin(),
buildReporterPlugin(config),
]
Expand Down
37 changes: 0 additions & 37 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,17 +565,6 @@ export interface ExperimentalOptions {
* @default false
*/
hmrPartialAccept?: boolean
/**
* Enable builtin plugin that written by rust, which is faster than js plugin.
*
* - 'v1' (will be deprecated, will be removed in v8 stable): Enable the first stable set of native plugins.
* - 'v2' (will be deprecated, will be removed in v8 stable): Enable the improved dynamicImportVarsPlugin and importGlobPlugin.
* - true: Enable all native plugins (currently an alias of 'v2', it will map to a newer one in the future versions).
*
* @experimental
* @default 'v2'
*/
enableNativePlugin?: boolean | 'v1' | 'v2'
/**
* Enable full bundle mode.
*
Expand Down Expand Up @@ -733,8 +722,6 @@ export interface ResolvedConfig extends Readonly<
/** @internal */
safeModulePaths: Set<string>
/** @internal */
nativePluginEnabledLevel: number
/** @internal */
[SYMBOL_RESOLVED_CONFIG]: true
} & PluginHookUtils
> {}
Expand Down Expand Up @@ -833,7 +820,6 @@ const configDefaults = Object.freeze({
importGlobRestoreExtension: false,
renderBuiltUrl: undefined,
hmrPartialAccept: false,
enableNativePlugin: process.env._VITE_TEST_JS_PLUGIN ? false : 'v2',
bundledDev: false,
},
future: {
Expand Down Expand Up @@ -2005,9 +1991,6 @@ export async function resolveConfig(
},
),
safeModulePaths: new Set<string>(),
nativePluginEnabledLevel: resolveNativePluginEnabledLevel(
experimental.enableNativePlugin,
),
[SYMBOL_RESOLVED_CONFIG]: true,
}
resolved = {
Expand Down Expand Up @@ -2140,26 +2123,6 @@ assetFileNames isn't equal for every build.rollupOptions.output. A single patter
return resolved
}

function resolveNativePluginEnabledLevel(
enableNativePlugin: Exclude<
ExperimentalOptions['enableNativePlugin'],
undefined
>,
) {
switch (enableNativePlugin) {
case 'v1':
return 1
case 'v2':
case true:
return 2
case false:
return -1
default:
enableNativePlugin satisfies never
return -1
}
}

/**
* Resolve base url. Note that some users use Vite to build for non-web targets like
* electron or expects to deploy
Expand Down
Loading