vite icon indicating copy to clipboard operation
vite copied to clipboard

Using a full url as the base path generates bad parameters for @font-face url

Open Codex- opened this issue 2 years ago • 1 comments
trafficstars

Describe the bug

If you use a full URL as the base path in your vite config:

export default defineConfig({
  plugins: [react()],
  base: "https://assets.site.com/",
});

And reference a font URL like below in your html:

<style>
  @font-face {
    font-family: "Arial";
    src: local("Arial"), url("/fonts/Arial.ttf") format("truetype");
  }
</style>

After performing a build, the parameter that is generated during a build for the url() call is invalid:

<style>
  @font-face {
    font-family: "Arial";
    src: local("Arial"), url("https:/assets.site.com/fonts/Arial.ttf") format("truetype");
  }
</style>

It has the incorrect amount of /s after http:

Reproduction

https://github.com/Codex-/vite-font-url-bug

Steps to reproduce

  • Clone the reproduction repository
  • npm i
  • npm run build
  • Inspect the generated html

System Info

System:
  OS: macOS 13.1
  CPU: (8) arm64 Apple M1
  Memory: 45.17 MB / 16.00 GB
  Shell: 5.8.1 - /bin/zsh
Binaries:
  Node: 16.19.0 - ~/.asdf/installs/nodejs/lts-gallium/bin/node
  Yarn: 1.22.17 - ~/.asdf/shims/yarn
  npm: 8.19.3 - ~/.asdf/plugins/nodejs/shims/npm
Browsers:
  Chrome: 109.0.5414.119
  Firefox Developer Edition: 109.0
  Safari: 16.2
npmPackages:
  @vitejs/plugin-react: ^3.0.0 => 3.0.1 
  vite: ^4.0.0 => 4.0.4

Used Package Manager

npm

Logs

Click to expand!
➜  vite-sandbox git:(main) npm run build -- --debug

> [email protected] build
> tsc && vite build --mode development --debug

  vite:config bundled config file loaded in 290.03ms +0ms
  vite:esbuild init tsconfck (root: /Users/alexmiller/dev/vite-sandbox) +0ms
  vite:esbuild init tsconfck (root: /Users/alexmiller/dev/vite-sandbox) +0ms
  vite:esbuild init tsconfck (root: /Users/alexmiller/dev/vite-sandbox) +1ms
  vite:esbuild init tsconfck (root: /Users/alexmiller/dev/vite-sandbox) +0ms
  vite:esbuild init tsconfck end +6ms
  vite:esbuild init tsconfck end +0ms
  vite:esbuild init tsconfck end +0ms
  vite:esbuild init tsconfck end +0ms
  vite:config using resolved config: {
  vite:config   plugins: [
  vite:config     'vite:build-metadata',
  vite:config     'vite:pre-alias',
  vite:config     'alias',
  vite:config     'vite:react-babel',
  vite:config     'vite:react-refresh',
  vite:config     'vite:react-jsx',
  vite:config     'vite:modulepreload-polyfill',
  vite:config     'vite:resolve',
  vite:config     'vite:html-inline-proxy',
  vite:config     'vite:css',
  vite:config     'vite:esbuild',
  vite:config     'vite:json',
  vite:config     'vite:wasm-helper',
  vite:config     'vite:worker',
  vite:config     'vite:asset',
  vite:config     'vite:wasm-fallback',
  vite:config     'vite:define',
  vite:config     'vite:css-post',
  vite:config     'vite:build-html',
  vite:config     'vite:worker-import-meta-url',
  vite:config     'vite:asset-import-meta-url',
  vite:config     'vite:force-systemjs-wrap-complete',
  vite:config     'vite:watch-package-data',
  vite:config     'commonjs',
  vite:config     'vite:data-uri',
  vite:config     'vite:dynamic-import-vars',
  vite:config     'vite:import-glob',
  vite:config     'vite:build-import-analysis',
  vite:config     'vite:esbuild-transpile',
  vite:config     'vite:terser',
  vite:config     'vite:reporter',
  vite:config     'vite:load-fallback'
  vite:config   ],
  vite:config   base: 'http://assets.site.com/',
  vite:config   mode: 'development',
  vite:config   optimizeDeps: {
  vite:config     disabled: 'build',
  vite:config     force: undefined,
  vite:config     include: [ 'react/jsx-runtime', 'react/jsx-dev-runtime', 'react' ],
  vite:config     esbuildOptions: { preserveSymlinks: false }
  vite:config   },
  vite:config   build: {
  vite:config     target: [ 'es2020', 'edge88', 'firefox78', 'chrome87', 'safari14' ],
  vite:config     cssTarget: [ 'es2020', 'edge88', 'firefox78', 'chrome87', 'safari14' ],
  vite:config     outDir: 'dist',
  vite:config     assetsDir: 'assets',
  vite:config     assetsInlineLimit: 4096,
  vite:config     cssCodeSplit: true,
  vite:config     sourcemap: false,
  vite:config     rollupOptions: {},
  vite:config     minify: 'esbuild',
  vite:config     terserOptions: {},
  vite:config     write: true,
  vite:config     emptyOutDir: null,
  vite:config     copyPublicDir: true,
  vite:config     manifest: false,
  vite:config     lib: false,
  vite:config     ssr: false,
  vite:config     ssrManifest: false,
  vite:config     reportCompressedSize: true,
  vite:config     chunkSizeWarningLimit: 500,
  vite:config     watch: null,
  vite:config     commonjsOptions: { include: [Array], extensions: [Array] },
  vite:config     dynamicImportVarsOptions: { warnOnError: true, exclude: [Array] },
  vite:config     modulePreload: { polyfill: true }
  vite:config   },
  vite:config   esbuild: {
  vite:config     jsxDev: false,
  vite:config     jsx: 'automatic',
  vite:config     jsxImportSource: undefined,
  vite:config     jsxSideEffects: false
  vite:config   },
  vite:config   resolve: {
  vite:config     mainFields: [ 'module', 'jsnext:main', 'jsnext' ],
  vite:config     browserField: true,
  vite:config     conditions: [],
  vite:config     extensions: [
  vite:config       '.mjs',  '.js',
  vite:config       '.mts',  '.ts',
  vite:config       '.jsx',  '.tsx',
  vite:config       '.json'
  vite:config     ],
  vite:config     dedupe: [ 'react', 'react-dom' ],
  vite:config     preserveSymlinks: false,
  vite:config     alias: [ [Object], [Object] ]
  vite:config   },
  vite:config   configFile: '/Users/alexmiller/dev/vite-sandbox/vite.config.ts',
  vite:config   configFileDependencies: [ '/Users/alexmiller/dev/vite-sandbox/vite.config.ts' ],
  vite:config   inlineConfig: {
  vite:config     root: undefined,
  vite:config     base: undefined,
  vite:config     mode: 'development',
  vite:config     configFile: undefined,
  vite:config     logLevel: undefined,
  vite:config     clearScreen: undefined,
  vite:config     optimizeDeps: { force: undefined },
  vite:config     build: {}
  vite:config   },
  vite:config   root: '/Users/alexmiller/dev/vite-sandbox',
  vite:config   rawBase: 'http://assets.site.com/',
  vite:config   publicDir: '/Users/alexmiller/dev/vite-sandbox/public',
  vite:config   cacheDir: '/Users/alexmiller/dev/vite-sandbox/node_modules/.vite',
  vite:config   command: 'build',
  vite:config   ssr: {
  vite:config     format: 'esm',
  vite:config     target: 'node',
  vite:config     optimizeDeps: { disabled: true, esbuildOptions: [Object] }
  vite:config   },
  vite:config   isWorker: false,
  vite:config   mainConfig: null,
  vite:config   isProduction: true,
  vite:config   server: {
  vite:config     preTransformRequests: true,
  vite:config     middlewareMode: false,
  vite:config     fs: { strict: true, allow: [Array], deny: [Array] }
  vite:config   },
  vite:config   preview: {
  vite:config     port: undefined,
  vite:config     strictPort: undefined,
  vite:config     host: undefined,
  vite:config     https: undefined,
  vite:config     open: undefined,
  vite:config     proxy: undefined,
  vite:config     cors: undefined,
  vite:config     headers: undefined
  vite:config   },
  vite:config   env: {
  vite:config     BASE_URL: 'http://assets.site.com/',
  vite:config     MODE: 'development',
  vite:config     DEV: false,
  vite:config     PROD: true
  vite:config   },
  vite:config   assetsInclude: [Function: assetsInclude],
  vite:config   logger: {
  vite:config     hasWarned: false,
  vite:config     info: [Function: info],
  vite:config     warn: [Function: warn],
  vite:config     warnOnce: [Function: warnOnce],
  vite:config     error: [Function: error],
  vite:config     clearScreen: [Function: clearScreen],
  vite:config     hasErrorLogged: [Function: hasErrorLogged]
  vite:config   },
  vite:config   packageCache: Map(0) { set: [Function (anonymous)] },
  vite:config   createResolver: [Function: createResolver],
  vite:config   worker: {
  vite:config     format: 'iife',
  vite:config     plugins: [
  vite:config       'vite:build-metadata',
  vite:config       'vite:pre-alias',
  vite:config       'alias',
  vite:config       'vite:modulepreload-polyfill',
  vite:config       'vite:resolve',
  vite:config       'vite:html-inline-proxy',
  vite:config       'vite:css',
  vite:config       'vite:esbuild',
  vite:config       'vite:json',
  vite:config       'vite:wasm-helper',
  vite:config       'vite:worker',
  vite:config       'vite:asset',
  vite:config       'vite:wasm-fallback',
  vite:config       'vite:define',
  vite:config       'vite:css-post',
  vite:config       'vite:build-html',
  vite:config       'vite:worker-import-meta-url',
  vite:config       'vite:asset-import-meta-url',
  vite:config       'vite:force-systemjs-wrap-complete',
  vite:config       'vite:watch-package-data',
  vite:config       'commonjs',
  vite:config       'vite:data-uri',
  vite:config       'vite:dynamic-import-vars',
  vite:config       'vite:import-glob',
  vite:config       'vite:build-import-analysis',
  vite:config       'vite:esbuild-transpile',
  vite:config       'vite:terser',
  vite:config       'vite:load-fallback'
  vite:config     ],
  vite:config     rollupOptions: {},
  vite:config     getSortedPlugins: [Function: getSortedPlugins],
  vite:config     getSortedPluginHooks: [Function: getSortedPluginHooks]
  vite:config   },
  vite:config   appType: 'spa',
  vite:config   experimental: { importGlobRestoreExtension: false, hmrPartialAccept: false },
  vite:config   getSortedPlugins: [Function: getSortedPlugins],
  vite:config   getSortedPluginHooks: [Function: getSortedPluginHooks]
  vite:config } +12ms
vite v4.0.4 building for development...
✓ 35 modules transformed.
dist/index.html                   0.70 kB
dist/assets/react-35ef61ed.svg    4.13 kB
dist/assets/index-3fce1f81.css    1.41 kB │ gzip:  0.73 kB
dist/assets/index-d97d1fdf.js   143.57 kB │ gzip: 46.19 kB

Validations

Codex- avatar Jan 31 '23 00:01 Codex-

I've done some debugging and found where this is caused: during the asset resolution, public assets are always treated like a file path and not possibly as a URL, so URL's get normalised and have the double / removed by normalizePath.

During transformation when vite attempts to resolve the public asset paths from __VITE_PUBLIC_ASSET__{hash}__ to their full path to be included it performs this normalization without validating whether or not something is a URL first: https://github.com/vitejs/vite/blob/main/packages/vite/src/node/plugins/html.ts#L808

Codex- avatar Jan 31 '23 21:01 Codex-