feat: 升级vite5->rolldown-vite

This commit is contained in:
harrywan 2025-08-08 16:26:11 +08:00
parent effd1378b4
commit 73ffe7e112
6 changed files with 2528 additions and 1230 deletions

View File

@ -32,10 +32,10 @@
"@babel/core": "^7.23.3", "@babel/core": "^7.23.3",
"@fesjs/utils": "^3.0.3", "@fesjs/utils": "^3.0.3",
"@rollup/pluginutils": "^5.1.0", "@rollup/pluginutils": "^5.1.0",
"@vitejs/plugin-basic-ssl": "^1.0.2", "@vitejs/plugin-basic-ssl": "^2.1.0",
"@vitejs/plugin-legacy": "^5.2.0", "@vitejs/plugin-legacy": "^7.0.0",
"@vitejs/plugin-vue": "^4.5.0", "@vitejs/plugin-vue": "^6.0.1",
"@vitejs/plugin-vue-jsx": "^3.1.0", "@vitejs/plugin-vue-jsx": "^5.0.1",
"autoprefixer": "^10.4.4", "autoprefixer": "^10.4.4",
"colorette": "^2.0.16", "colorette": "^2.0.16",
"connect-history-api-fallback": "^2.0.0", "connect-history-api-fallback": "^2.0.0",
@ -51,9 +51,12 @@
"pathe": "^0.2.0", "pathe": "^0.2.0",
"postcss-flexbugs-fixes": "^5.0.2", "postcss-flexbugs-fixes": "^5.0.2",
"postcss-safe-parser": "^6.0.0", "postcss-safe-parser": "^6.0.0",
"rolldown-vite": "^7.1.0",
"rollup-plugin-visualizer": "^5.9.3", "rollup-plugin-visualizer": "^5.9.3",
"terser": "^5.24.0", "terser": "^5.24.0"
"vite": "^5.0.3" },
"devDependencies": {
"@types/node": "^22.12.0"
}, },
"typings": "./types.d.ts" "typings": "./types.d.ts"
} }

View File

@ -1,4 +1,3 @@
import { splitVendorChunkPlugin } from 'vite';
import legacy from '@vitejs/plugin-legacy'; import legacy from '@vitejs/plugin-legacy';
import { getInnerCommonConfig } from '../../common/getConfig'; import { getInnerCommonConfig } from '../../common/getConfig';
@ -39,7 +38,6 @@ export default async (api) => {
targets, targets,
...api.config.viteLegacy, ...api.config.viteLegacy,
}), }),
splitVendorChunkPlugin(),
], ],
build: { build: {
...build, ...build,

View File

@ -1,5 +1,5 @@
import { existsSync } from 'node:fs';
import { build } from 'vite'; import { build } from 'vite';
import { existsSync } from 'fs';
import getBuildConfig from './getBuildConfig'; import getBuildConfig from './getBuildConfig';
export default function (api) { export default function (api) {
@ -33,7 +33,8 @@ export default function (api) {
if (process.env.RM_TMPDIR !== 'none') { if (process.env.RM_TMPDIR !== 'none') {
rimraf.sync(paths.absTmpPath); rimraf.sync(paths.absTmpPath);
} }
} catch (err) { }
catch (err) {
// throw build error // throw build error
throw err; throw err;
} }

View File

@ -1,32 +1,35 @@
import process from 'node:process'; import process from 'node:process';
import { render } from 'ejs';
import { expand } from 'dotenv-expand';
import dotenv from 'dotenv';
import path, { dirname, join } from 'pathe';
import fse from 'fs-extra';
import { normalizePath } from 'vite';
import { parse } from 'node-html-parser';
import fg from 'fast-glob';
import consola from 'consola';
import { dim } from 'colorette';
import { minify } from 'html-minifier-terser';
import { createFilter } from '@rollup/pluginutils'; import { createFilter } from '@rollup/pluginutils';
import { dim } from 'colorette';
import consola from 'consola';
import dotenv from 'dotenv';
import { expand } from 'dotenv-expand';
import { render } from 'ejs';
import fg from 'fast-glob';
import fse from 'fs-extra';
import { minify } from 'html-minifier-terser';
import { parse } from 'node-html-parser';
import path, { dirname, join } from 'pathe';
import { normalizePath } from 'vite';
import history from './connectHistoryMiddleware'; import history from './connectHistoryMiddleware';
function lookupFile(dir, formats, pathOnly = false) { function lookupFile(dir, formats, pathOnly = false) {
for (const format of formats) { for (const format of formats) {
const fullPath = join(dir, format); const fullPath = join(dir, format);
if (fse.pathExistsSync(fullPath) && fse.statSync(fullPath).isFile()) if (fse.pathExistsSync(fullPath) && fse.statSync(fullPath).isFile()) {
return pathOnly ? fullPath : fse.readFileSync(fullPath, 'utf-8'); return pathOnly ? fullPath : fse.readFileSync(fullPath, 'utf-8');
} }
}
const parentDir = dirname(dir); const parentDir = dirname(dir);
if (parentDir !== dir) if (parentDir !== dir) {
return lookupFile(parentDir, formats, pathOnly); return lookupFile(parentDir, formats, pathOnly);
}
} }
function loadEnv(mode, envDir, prefix = '') { function loadEnv(mode, envDir, prefix = '') {
if (mode === 'local') if (mode === 'local') {
throw new Error(`"local" cannot be used as a mode name because it conflicts with the .local postfix for .env files.`); throw new Error(`"local" cannot be used as a mode name because it conflicts with the .local postfix for .env files.`);
}
const env = {}; const env = {};
const envFiles = [`.env.${mode}.local`, `.env.${mode}`, `.env.local`, `.env`]; const envFiles = [`.env.${mode}.local`, `.env.${mode}`, `.env.local`, `.env`];
@ -39,14 +42,16 @@ function loadEnv(mode, envDir, prefix = '') {
ignoreProcessEnv: true, ignoreProcessEnv: true,
}); });
for (const [key, value] of Object.entries(parsed)) { for (const [key, value] of Object.entries(parsed)) {
if (key.startsWith(prefix) && env[key] === undefined) if (key.startsWith(prefix) && env[key] === undefined) {
env[key] = value; env[key] = value;
}
else if (key === 'NODE_ENV') else if (key === 'NODE_ENV') {
process.env.VITE_USER_NODE_ENV = value; process.env.VITE_USER_NODE_ENV = value;
} }
} }
} }
}
return env; return env;
} }
@ -103,14 +108,15 @@ function createPlugin(userOptions = {}) {
const keys = Object.keys(proxy); const keys = Object.keys(proxy);
let indexPage = null; let indexPage = null;
for (const page of _pages) { for (const page of _pages) {
if (page.filename !== 'index.html') if (page.filename !== 'index.html') {
rewrites.push(createRewire(page.template, page, baseUrl, keys)); rewrites.push(createRewire(page.template, page, baseUrl, keys));
else
indexPage = page;
} }
if (indexPage)
else { indexPage = page; }
}
if (indexPage) {
rewrites.push(createRewire('', indexPage, baseUrl, keys)); rewrites.push(createRewire('', indexPage, baseUrl, keys));
}
server.middlewares.use( server.middlewares.use(
history(viteConfig, { history(viteConfig, {
@ -148,15 +154,17 @@ function createPlugin(userOptions = {}) {
if (isMpa(viteConfig) || pages.length) { if (isMpa(viteConfig) || pages.length) {
for (const page of pages) { for (const page of pages) {
const dir = path.dirname(page.template); const dir = path.dirname(page.template);
if (!ignoreDirs.includes(dir)) if (!ignoreDirs.includes(dir)) {
outputDirs.push(dir); outputDirs.push(dir);
} }
} }
}
else { else {
const dir = path.dirname(template); const dir = path.dirname(template);
if (!ignoreDirs.includes(dir)) if (!ignoreDirs.includes(dir)) {
outputDirs.push(dir); outputDirs.push(dir);
} }
}
const cwd = path.resolve(viteConfig.root, viteConfig.build.outDir); const cwd = path.resolve(viteConfig.root, viteConfig.build.outDir);
const htmlFiles = await fg( const htmlFiles = await fg(
outputDirs.map(dir => `${dir}/*.html`), outputDirs.map(dir => `${dir}/*.html`),
@ -176,8 +184,9 @@ function createPlugin(userOptions = {}) {
await Promise.all( await Promise.all(
htmlDirs.map(async (item) => { htmlDirs.map(async (item) => {
const isEmpty = await isDirEmpty(item); const isEmpty = await isDirEmpty(item);
if (isEmpty) if (isEmpty) {
return fse.remove(item); return fse.remove(item);
}
}), }),
); );
}, },
@ -198,8 +207,9 @@ function createInput({ pages = [], template = DEFAULT_TEMPLATE }, viteConfig) {
return input; return input;
} }
const dir = path.dirname(template); const dir = path.dirname(template);
if (ignoreDirs.includes(dir)) if (ignoreDirs.includes(dir)) {
return undefined; return undefined;
}
const file = path.basename(template); const file = path.basename(template);
const key = file.replace(/\.html/, ''); const key = file.replace(/\.html/, '');
@ -227,11 +237,11 @@ async function renderHtml(html, config) {
function getPage({ pages = [], entry, template = DEFAULT_TEMPLATE, inject = {} }, name, viteConfig) { function getPage({ pages = [], entry, template = DEFAULT_TEMPLATE, inject = {} }, name, viteConfig) {
let page; let page;
if (isMpa(viteConfig) || pages?.length) if (isMpa(viteConfig) || pages?.length) {
page = getPageConfig(name, pages, DEFAULT_TEMPLATE); page = getPageConfig(name, pages, DEFAULT_TEMPLATE);
}
else else { page = createSpaPage(entry, template, inject); }
page = createSpaPage(entry, template, inject);
return page; return page;
} }
@ -242,8 +252,9 @@ function isMpa(viteConfig) {
} }
function removeEntryScript(html, verbose = false) { function removeEntryScript(html, verbose = false) {
if (!html) if (!html) {
return html; return html;
}
const root = parse(html); const root = parse(html);
const scriptNodes = root.querySelectorAll('script[type=module]') || []; const scriptNodes = root.querySelectorAll('script[type=module]') || [];
@ -286,8 +297,9 @@ function createRewire(reg, page, baseUrl, proxyUrlKeys) {
const pathname = parsedUrl.pathname; const pathname = parsedUrl.pathname;
const excludeBaseUrl = pathname.replace(baseUrl, '/'); const excludeBaseUrl = pathname.replace(baseUrl, '/');
const template = path.resolve(baseUrl, page.template); const template = path.resolve(baseUrl, page.template);
if (excludeBaseUrl === '/') if (excludeBaseUrl === '/') {
return template; return template;
}
const isApiUrl = proxyUrlKeys.some(item => pathname.startsWith(path.resolve(baseUrl, item))); const isApiUrl = proxyUrlKeys.some(item => pathname.startsWith(path.resolve(baseUrl, item)));
return isApiUrl ? parsedUrl.path : template; return isApiUrl ? parsedUrl.path : template;
@ -311,12 +323,14 @@ function getOptions(_minify) {
} }
async function minifyHtml(html, minify$1) { async function minifyHtml(html, minify$1) {
if (typeof minify$1 === 'boolean' && !minify$1) if (typeof minify$1 === 'boolean' && !minify$1) {
return html; return html;
}
let minifyOptions = minify$1; let minifyOptions = minify$1;
if (typeof minify$1 === 'boolean' && minify$1) if (typeof minify$1 === 'boolean' && minify$1) {
minifyOptions = getOptions(minify$1); minifyOptions = getOptions(minify$1);
}
const res = await minify(html, minifyOptions); const res = await minify(html, minifyOptions);
return res; return res;
@ -329,10 +343,11 @@ function createMinifyHtmlPlugin({ _minify = true } = {}) {
async generateBundle(_, outBundle) { async generateBundle(_, outBundle) {
if (_minify) { if (_minify) {
for (const bundle of Object.values(outBundle)) { for (const bundle of Object.values(outBundle)) {
if (bundle.type === 'asset' && htmlFilter(bundle.fileName) && typeof bundle.source === 'string') if (bundle.type === 'asset' && htmlFilter(bundle.fileName) && typeof bundle.source === 'string') {
bundle.source = await minifyHtml(bundle.source, _minify); bundle.source = await minifyHtml(bundle.source, _minify);
} }
} }
}
}, },
}; };
} }

View File

@ -3,8 +3,10 @@ export default (api) => {
key: 'targets', key: 'targets',
config: { config: {
default: { default: {
chrome: '64', chrome: '107',
ios: '11', ios: '16',
firefox: '104',
edge: '107',
}, },
schema(joi) { schema(joi) {
return joi.object(); return joi.object();

3647
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff