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.

Dynamic import builds `.js` and '.css' file. Don't want seperate `.css` file.

See original GitHub issue

What problem does this feature solve?

I have lots of dynamic import in my peoject. Each one builds two files. one is js one is css. However most of css files are too small which results in too many http request.

What does the proposed API look like?

How to config vue.config.js can solve this problem?

Better understanding

image

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

10reactions
fangbinweicommented, Dec 25, 2020

If you are using webpack 4, you can extracting-css-based-on-entry according to doc of mini-css-extract-plugin

vue.config.js

/** @type  {import('@vue/cli-service').ProjectOptions} */
module.exports = {
  configureWebpack () {
    function recursiveIssuer (m, c) {
      const issuer = m.issuer

      if (issuer) {
        return recursiveIssuer(issuer, c)
      }

      const chunks = m._chunks
      // For webpack@4 chunks = m._chunks

      for (const chunk of chunks) {
        return chunk.name
      }

      return false
    }
    return {
      optimization: {
        splitChunks: {
          cacheGroups: {
            appStyle: {
              name: 'app_style',
              test: (m, c, entry = 'app') => {
                return m.constructor.name === 'CssModule' &&
                recursiveIssuer(m, c) === entry
              },
              chunks: 'all',
              enforce: true
            }
          }
        }
      }
    }
  }
}


or you can group all css in .vue ,

          cacheGroups: {
            vueStyle: {
              name: 'css-in-vue',
              test: function (module) {
                if (module.constructor.name !== 'CssModule') return false

                if (module.nameForCondition && /.vue$/.test(module.nameForCondition())) {
                  return true
                }
                return false
              },
              chunks: 'all',
              enforce: true
            }
          }
0reactions
Edge00commented, Dec 27, 2020

@fangbinwei 好的,感谢!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Lazy load CSS with the help of dynamic import ... - Medium
The goal of the solution is loading CSS on demand. To achieve such objective, we can take advantage of the JavaScript dynamic import()...
Read more >
Dynamically load and unload content of css file in javascript ...
Ideally, what I want is the ability to conditionally render/import the css files. I've tried a few things most of them don't work...
Read more >
Dynamically import CSS · Issue #261 · ef4/ember-auto-import
I want them to be separate files, same as the JS chunk files created. And then, perhaps, have an easy way of importing...
Read more >
Using CSS Module Scripts to import stylesheets - web.dev
Learn how to use CSS module scripts to import CSS stylesheets using the same syntax as JavaScript modules.
Read more >
Component-Scoped Styles with CSS Modules - Gatsby
CSS Modules let you write styles in CSS files but consume them as JavaScript objects for additional processing and safety. CSS Modules are...
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