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

Cannot load font files referenced in a node_modules css file #1618

Closed
2 of 3 tasks
scambier opened this issue Jan 21, 2021 · 6 comments
Closed
2 of 3 tasks

Cannot load font files referenced in a node_modules css file #1618

scambier opened this issue Jan 21, 2021 · 6 comments

Comments

@scambier
Copy link

  • Read the docs.
  • Use Vite >=2.0. (1.x is no longer supported)
  • If the issue is related to 1.x -> 2.0 upgrade, read the Migration Guide first.

Describe the bug

I'm working on a new project at work, and we're using a node_modules dependency containing all the company css. The css file itself loads Font Awesome files with relative paths:

@font-face {
    font-family: Font Awesome\ 5 Free;
    font-style: normal;
    font-weight: 400;
    font-display: auto;
    src: url(../fonts/fa/fa-regular-400.eot);
    src: url(../fonts/fa/fa-regular-400.eot?#iefix) format("embedded-opentype"), url(../fonts/fa/fa-regular-400.woff2) format("woff2"), url(../fonts/fa/fa-regular-400.woff) format("woff"), url(../fonts/fa/fa-regular-400.ttf) format("truetype"), url(../fonts/fa/fa-regular-400.svg#fontawesome) format("svg")
}

I import the dependency like this in App.vue

<style lang="scss">
@import "@company/css-library/assets/css/styles.css";
</style>

The issue is that the FA fonts are then loaded relatively to the ./src folder, which leads to this 404 error in the browser:
GET http://localhost:3000/fonts/fa/fa-solid-900.woff2 net::ERR_ABORTED 404 (Not Found)

How can I solve this?

Reproduction

I cannot share the source, but I guess that the issue will arise with any css in a node_modules dep, that will load a font file with a relative path.

image

System Info

  • vite version: ^2.0.0-beta.34
  • Operating System: Windows 7
  • Node version: 12.16.3
  • Package manager (npm/yarn/pnpm) and version: npm 6.14.4

Logs (Optional if provided reproduction)

vite:config bundled config file loaded in 376ms +0ms
  vite:config using resolved config: {
  vite:config   plugins: [
  vite:config     'alias',
  vite:config     'vite:dynamic-import-polyfill',
  vite:config     'vite:resolve',
  vite:config     'vite:html',
  vite:config     'vite:css',
  vite:config     'vite:esbuild',
  vite:config     'vite:json',
  vite:config     'vite:wasm',
  vite:config     'vite:worker',
  vite:config     'vite:asset',
  vite:config     'vite:vue',
  vite:config     'vite:css-post',
  vite:config     'vite:build-html',
  vite:config     'commonjs',
  vite:config     'vite:data-uri',
  vite:config     'vite:define',
  vite:config     'rollup-plugin-dynamic-import-variables',
  vite:config     'vite:import-analysis',
  vite:config     'vite:esbuild-transpile',
  vite:config     'vite:terser',
  vite:config     'vite:size'
  vite:config   ],
  vite:config   alias: [
  vite:config     {
  vite:config       find: /^\/@vite\//,
  vite:config       replacement: 'C:\\Users\\[redacted]\\node_modules\\vite\\dist\\client/'
  vite:config     },
  vite:config     {
  vite:config       find: '@',
  vite:config       replacement: 'C:\\Users\\[redacted]\\src'
  vite:config     }
  vite:config   ],
  vite:config   build: {
  vite:config     base: '/',
  vite:config     target: [ 'es2019', 'edge16', 'firefox60', 'chrome61', 'safari11' ],
  vite:config     polyfillDynamicImport: true,
  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     commonjsOptions: { include: [Array], extensions: [Array] },
  vite:config     minify: 'terser',
  vite:config     terserOptions: {},
  vite:config     cleanCssOptions: {},
  vite:config     write: true,
  vite:config     emptyOutDir: null,
  vite:config     manifest: false,
  vite:config     lib: false,
  vite:config     ssr: false,
  vite:config     ssrManifest: false
  vite:config   },
  vite:config   define: { __VUE_OPTIONS_API__: true, __VUE_PROD_DEVTOOLS__: false },
  vite:config   ssr: { external: [ 'vue', '@vue/server-renderer' ] },
  vite:config   configFile: '/Users/[redacted]/vite.config.ts',
  vite:config   inlineConfig: {
  vite:config     root: undefined,
  vite:config     mode: undefined,
  vite:config     configFile: undefined,
  vite:config     logLevel: undefined,
  vite:config     clearScreen: undefined,
  vite:config     build: {}
  vite:config   },
  vite:config   root: '/Users/[redacted]',
  vite:config   command: 'build',
  vite:config   mode: 'production',
  vite:config   isProduction: true,
  vite:config   optimizeCacheDir: '\\Users\\[redacted]\\node_modules\\.vite',
  vite:config   server: {},
  vite:config   env: { BASE_URL: '/', MODE: 'production', DEV: false, PROD: true },
  vite:config   assetsInclude: [Function: assetsInclude],
  vite:config   logger: {
  vite:config     info: [Function: info],
  vite:config     warn: [Function: warn],
  vite:config     error: [Function: error],
  vite:config     clearScreen: [Function: clearScreen]
  vite:config   }
  vite:config } +12ms
building for production...
[vite:css] ENOENT: no such file or directory, open 'C:\Users\[redacted]\fonts\fa\fa-regular-400.eot'
file: /Users/[redacted]/src/App.vue?vue&type=style&index=0&lang.scss
error during build:
Error: ENOENT: no such file or directory, open 'C:\Users\[redacted]\fonts\fa\fa-regular-400.eot'
@scambier
Copy link
Author

scambier commented Jan 21, 2021

Update : I did a dirty workaround with rollup-plugin-copy:

import type { UserConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
import copy from 'rollup-plugin-copy'

const config: UserConfig = {
  plugins: [
    vue(),
    copy({
      targets: [
        { src: './node_modules/@company/css-library/assets/fonts/**/*', dest: 'public/fonts' },
        { src: './node_modules/@company/css-library/assets/img/**/*', dest: 'public/img' }
      ]
    })
  ],
  alias: {
    '@': resolve(__dirname, 'src')
  }
}

export default config

But npm run build doesn't work unless the folders fonts and img are at the project's root (and not in ./public), so the files are now copied in two different locations ._.

@yyx990803
Copy link
Member

This should work as expected, can't reproduce. No actionables until a valid reproduction is provided.

@scambier
Copy link
Author

scambier commented Jan 21, 2021

@yyx990803 Then I guess there's something wrong either on my project, or on its dependency. I got a small reproduction repo at https://github.com/scambier/test-vite-app

There are two folders: test-vite-app is the main project (a default vue 3 app from the cli), and test-vite-css-dep is the local dependency. It contains css and font files with everything needed to run font-awesome and nothing else. The css code itself is directly copied from our work repo.

  • Clone the repo
  • cd in the test-vite-app
  • npm install then npm run dev
  • You should get this, with 404 errors for the font files:
    image

Thank you for your time.

@germsb
Copy link

germsb commented Jan 22, 2021

You can solve it with postcss

//postcss.config.js
const path = require('path');
module.exports = {
    plugins: {
        'postcss-import': {},
        'postcss-url': [
            {
                filter: t => {
                    //console.log(t, /([a-zA-Z0-9\s_\\.\-\(\):])+(.woff|.woff2)$/i.test(t.url));
                    return /([a-zA-Z0-9\s_\\.\-\(\):])+(.woff|.woff2)$/i.test(t.url);
                },
                url: 'copy',
                assetsPath:
                    process.env.NODE_ENV === 'production'
                        ? './'
                        : path.resolve(__dirname, '.vite_temp/fonts'),
                useHash: process.env.NODE_ENV !== 'production',
                hashOptions: {append: true},
            },
        ],

        tailwindcss: {},
        autoprefixer: {},
    },
};

@scambier
Copy link
Author

It works for npm run dev but fails for npm run build with a weird error:

postcss.config.js:8:26 - error 7006: Parameter 't' implicitly has an 'any' type.
 5 |         'postcss-url': [
 6 |             {
 7 |                 filter: t => {
   |                               ~
 8 |                     return /([a-zA-Z0-9\s_\\.\-\(\):])+(.woff|.woff2)$/i.test(t.url);
 9 |                 },

And if I add an any type to t, it obviously tells me that typings are for .ts files...

I'll continue with my workaround in the meantime, thank you though :)

@yyx990803
Copy link
Member

It's the same issue as #1629 and is fixed in beta.37.

dididy added a commit to Im-D/your-voice-front that referenced this issue Feb 22, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Jul 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants