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

Option for esModule to export named exports instead of default export #1029

Closed
billyjanitsch opened this issue Dec 20, 2019 · 6 comments · Fixed by #1108
Closed

Option for esModule to export named exports instead of default export #1029

billyjanitsch opened this issue Dec 20, 2019 · 6 comments · Fixed by #1108

Comments

@billyjanitsch
Copy link

  • Operating System: macOS
  • Node Version: 12.13.1
  • NPM Version: 6.13.4
  • webpack Version: 4.41.4
  • css-loader Version: 3.4.0

Expected Behavior / Situation

When the new esModule option is enabled, I hoped that each CSS class name would be exported as a named export:

.foo {color: red}
.bar {color: blue}
export const foo = 'abc'
export const bar = 'def'

Actual Behavior / Situation

Instead, an object of class names to hashes is exported as a default export:

export default {foo: 'abc', bar: 'def'}

This means that tree-shaking + inlining is very limited. For example, this is able to tree-shake:

import css from './styles.css'
console.log(css.foo)
function(e,t,r){"use strict";r.r(t);console.log("foo-3TUTh")}

But this is not able to tree-shake or inline:

import css from './styles.css'
function fn() {
  return css.foo
}
console.log(fn)
function(e,r,t){"use strict";t.r(r);var n={foo:"foo-3TUTh",bar:"bar-1JkxR"};console.log((function(){return n.foo}))}

Modification Proposal

If esModule used named exports, tree-shaking + inlining would always work as expected.

This would significantly reduce bundle size for apps using CSS modules.

@alexander-akait
Copy link
Member

hm, i think we should open issue in webpack, we should concat modules in this case too

@GeorgeTaveras1231
Copy link

hm, i think we should open issue in webpack, we should concat modules in this case too

@evilebottnawi Sorry I'm missing a lot of context but am interested in learning. Can you elaborate on your comment? Why should concatenating modules on webpack be necessary?

@alexander-akait
Copy link
Member

@GeorgeTaveras1231 We should improve concatenated module plugin, unfortunately I did not look deep into this problem, so I can not give information where it is better to do.

Also esModule generated module syntax, we need the exportNamed option for that, and maybe switch on that behavior in future, also it is require to patch style-loader/mini-css-extract-plugin and other, it is very big work.

@jdb8
Copy link

jdb8 commented Mar 14, 2020

@GeorgeTaveras1231 I just tried running your example using the same version of webpack (4.41.4) and in my output bundle I actually do see successful tree-shaking: this is with esModule: true in both mini-css-extract-plugin and css-loader.

Files I used: https://gist.github.com/jdb8/8049b6952898795be68dd6290e57bdfa

I wonder if this has been fixed somewhere, or if I'm missing something? Did you run your original example in production mode with the two esModule option flags set?

@alexander-akait
Copy link
Member

alexander-akait commented Jul 16, 2020

Implemented on css-loader side, but we should improve it on style-loader and mini-css-extract-plugin side

@alexander-akait
Copy link
Member

Implemented in mini-css-extract-plugin,
issue for style-loader webpack-contrib/style-loader#483

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants