Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade Vite to v5 #1657

Merged
merged 3 commits into from
Nov 17, 2023
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
13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@
"@testing-library/react": "~12.1.5",
"@testing-library/react-hooks": "~7.0.2",
"@testing-library/user-event": "~14.4.3",
"@vitejs/plugin-legacy": "~4.1.1",
"@vitejs/plugin-react-swc": "^3.4.0",
"@vitejs/plugin-legacy": "~5.0.0",
"@vitejs/plugin-react-swc": "^3.5.0",
"cross-env": "~7.0.3",
"esbuild": "~0.19.4",
"eslint": "~8.51.0",
Expand Down Expand Up @@ -97,10 +97,9 @@
"unexpected-react": "~6.0.2",
"unexpected-sinon": "~11.1.0",
"url": "~0.11.3",
"vite": "~4.4.11",
"vite": "~5.0.0",
"vite-plugin-compression": "~0.5.1",
"vite-plugin-generate-file": "~0.0.4",
"vite-plugin-inject-preload": "~1.3.3",
"vite-plugin-generate-file": "~0.1.1",
"vitest": "~0.34.6"
},
"resolutions": {
Expand All @@ -110,8 +109,8 @@
},
"scripts": {
"start": "vite",
"build-prod": "vite build && vite -c vite.bookmarklet.config.js build",
"build-modern": "cross-env MODERN=1 vite build && vite -c vite.bookmarklet.config.js build",
"build-prod": "vite build && vite -c vite.bookmarklet.config.mjs build",
"build-modern": "cross-env MODERN=1 vite build && vite -c vite.bookmarklet.config.mjs build",
"preview": "vite preview",
"test": "cross-env TZ=utc vitest run",
"lint": "eslint --ext .js --ext .jsx .",
Expand Down
33 changes: 33 additions & 0 deletions src/vite/inject-preload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export function injectPreload({ files = [] } = {}) {
let basePath;
return {
name: 'inject-preload',
apply: 'build',
configResolved(config) {
basePath = config.base;
},
/** @type import("vite").IndexHtmlTransformHook */
async transformIndexHtml(html, context) {
if (!context.bundle) {
return html;
}

const tags = [];

for (const key of Object.keys(context.bundle)) {
const conf = files.find((f) => f.match.test(key));
if (!conf) {
continue;
}

tags.push({
tag: 'link',
attrs: { rel: 'preload', ...conf.attributes, href: basePath + key },
injectTo: 'head-prepend',
});
}

return tags;
},
};
}
File renamed without changes.
14 changes: 10 additions & 4 deletions vite.config.js → vite.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import legacy from '@vitejs/plugin-legacy';
import react from '@vitejs/plugin-react-swc';
import { defineConfig } from 'vite';
import generateFile from 'vite-plugin-generate-file';
import injectPreload from 'vite-plugin-inject-preload';
import pkg from './package.json';
import { injectInlineResources } from './src/vite/inject-inline-resources';
import { injectPreload } from './src/vite/inject-preload';

// Move the listed node modules into separate named chunks. Format: module name
// - chunk name.
Expand All @@ -31,9 +31,15 @@ export default defineConfig(({ mode }) => ({
react(),
injectPreload({
files: [
{ match: /\/app-\w+\.js$/, attributes: { rel: 'modulepreload' } },
{ match: /\/vendor-.+?\.js$/, attributes: { rel: 'modulepreload' } },
{ match: /\/app-\w+\.css$/ },
{
match: /\/app-\w+\.js$/,
attributes: { rel: 'modulepreload', type: 'application/javascript', as: 'script' },
},
{
match: /\/vendor-.+?\.js$/,
attributes: { rel: 'modulepreload', type: 'application/javascript', as: 'script' },
},
{ match: /\/app-\w+\.css$/, attributes: { type: 'text/css', as: 'style' } },
],
}),
injectInlineResources(),
Expand Down
Loading