question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

πŸ› Loading local fonts via CSS returns error

See original GitHub issue

πŸ› bug report

πŸŽ› Configuration (.babelrc, package.json, cli command)

{
  "presets": [
    "env",
    "vue"
  ]
}
{
  ...
  "dependencies": {
    "axios": "^0.17.1",
    "vue": "^2.5.13",
    "vue-router": "^3.0.1",
    "vuex": "^3.0.1"
  },
  "devDependencies": {
    "@storybook/addon-actions": "^3.3.10",
    "@storybook/addon-links": "^3.3.10",
    "@storybook/addons": "^3.3.10",
    "@storybook/vue": "^3.3.10",
    "babel-core": "^6.26.0",
    "babel-preset-env": "^1.6.1",
    "babel-preset-vue": "^2.0.0",
    "css-loader": "^0.28.9",
    "eslint": "^4.16.0",
    "eslint-plugin-vue": "^4.2.0",
    "node-sass": "^4.7.2",
    "parcel-bundler": "^1.5.0",
    "parcel-plugin-eslint": "^1.0.3",
    "parcel-plugin-vue": "^1.5.0"
  }
}

πŸ€” Expected Behavior

I have a css file that references some local fonts:

@font-face{
font-family:example;
src:url(..\fonts\example.eot?6kxrs1);
src:url(..\fonts\example.eot?#iefix6kxrs1) format("embedded-opentype"),
url(..\fonts\example.ttf?6kxrs1) format("truetype"),
url(..\fonts\example.woff?6kxrs1) format("woff"),
url(..\fonts\example.svg?6kxrs1#example) format("svg");
font-weight:400;
font-style:normal}

I import this file in my main.js file so it is bundled in the app.

😯 Current Behavior

When trying to import the css file in my main.js I get the following error:

Cannot read property 'add' of undefined
    at JSPackager.addAsset (...\node_modules\parcel-bundler\src\packagers\JSPackager.js:56:28)
    at Bundle._addDeps (...\node_modules\parcel-bundler\src\Bundle.js:146:20)
    at <anonymous>

πŸ’ Possible Solution

Looking at the file with the error JSPackager I put it a validation before the this.bundleLoaders.add(mod.type);:

if(!this.bundleLoaders){
    this.bundleLoaders = new Set();
}
this.bundleLoaders.add(mod.type);

πŸ”¦ Context

πŸ’» Code Sample

🌍 Your Environment

Software Version(s)
Parcel 1.5.0
Node 8.9.3
npm/Yarn 5.5.1
Operating System Windows 10

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:5
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

9reactions
darita92commented, Jan 29, 2018

hey @FalkoJoseph, I went to the actual code of parcel to do this. On the file \node_modules\parcel-bundler\src\packagers\JSPackager.js on line 56 you will see this line:

this.bundleLoaders.add(mod.type);

So what I did to get rid of the error is put a validation before that line. Something like this:

if(!this.bundleLoaders){
    this.bundleLoaders = new Set();
}
this.bundleLoaders.add(mod.type);
3reactions
tunjioyecommented, Mar 7, 2018

I recommend you change the async addAsset(asset) function located in node_modules\parcel-bundler\src\packagers\JSPackager.js:

async addAsset(asset) {
    if (this.dedupe.has(asset.generated.js)) {
      return;
    }

    // Don't dedupe when HMR is turned on since it messes with the asset ids
    if (!this.options.hmr) {
      this.dedupe.set(asset.generated.js, asset.id);
    }

    let deps = {};
    for (let [dep, mod] of asset.depAssets) {
      // For dynamic dependencies, list the child bundles to load along with the module id
      if (dep.dynamic && this.bundle.childBundles.has(mod.parentBundle)) {
        let bundles = [this.getBundleSpecifier(mod.parentBundle)];
        for (let child of mod.parentBundle.siblingBundles) {
          if (!child.isEmpty) {
            bundles.push(this.getBundleSpecifier(child));
            this.bundleLoaders.add(child.type);
          }
        }

        bundles.push(mod.id);
        deps[dep.name] = bundles;
        // this.bundleLoaders.add(mod.type);
      } else {
        deps[dep.name] = this.dedupe.get(mod.generated.js) || mod.id;

        // If the dep isn't in this bundle, add it to the list of external modules to preload.
        // Only do this if this is the root JS bundle, otherwise they will have already been
        // loaded in parallel with this bundle as part of a dynamic import.
        // if (
        //   !this.bundle.assets.has(mod) &&
        //   (!this.bundle.parentBundle || this.bundle.parentBundle.type !== 'js')
        // ) {
        //   this.externalModules.add(mod);
        //   this.bundleLoaders.add(mod.type);
        // }
      }
    }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Problems when loading local fonts in css with font-face
I'm working with html and CSS, and I need to load a font which is in my local storage, but it gives me...
Read more >
How To Load and Use Custom Fonts with CSS | DigitalOcean
The browser will sequentially attempt to load the local fonts in the font stack until it is successful. For this font stack, the...
Read more >
Forums - CSS - @font-face problem
I've written a css with @font-face {font-family: taller_evolution_rev; src: url(taller_evolution_rev.otf);}. and locally it shows perfectly.
Read more >
css - Custom font not loading - Webmasters Stack Exchange
I downloaded it online. I added this to my head and still no change: <link rel="stylesheet" href="shadtv.onthewifi.com:8080/fonttest/css/custom ...
Read more >
How to load web fonts to avoid performance issues and speed ...
If you're using a CDN like Google Fonts, be sure that the font files you're preloading match the ones in the CSS. Fonts...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found