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

need to support ES6 module #210

Closed
fisherspy opened this issue Jan 5, 2016 · 7 comments
Closed

need to support ES6 module #210

fisherspy opened this issue Jan 5, 2016 · 7 comments

Comments

@fisherspy
Copy link

I'm using webpack 2.0 beta, if I want to " import xx from 'x.css' ", then pass xx string to the style config(like styles: xx), running will go wrong cause the build will be: "styles: xx.default", however cssloader will be : "module.exports=xx", not "module['default']=xx".

@hawkrives
Copy link

Looking at my project, css-loader is exporting all of the class names both under default and as top-level exports, for compatibility with both es6 and commonjs modules.

If I have

:local(.subtitle) {
    font-size: 0.75em;
}

:local(.title) {
    font-feature-settings: 'onum';
}

its exporting them such that the exported module looks like

module.exports = {
  subtitle: '<hash>',
  title: '<hash>',
  default: {
    subtitle: '<hash>',
    title: '<hash>',
  },
}

I will note that if you're wanting to import a specific class name, you'll need to use the brace style of import: import { specificThing } from './x.css', rather than import specific from './x.css'.

I've been doing import styles from './x.css', personally, so that I can just pass styles[class name] around to whatever needs it.

@fisherspy
Copy link
Author

Hi hawkrives,

I'm not sure I get what you mean, In my example, I write .bi-tab {xx}, then import {xx} from './x.css' or import xx from './x.css', I get this compiled javascript showing no default. Pls correct me:)

function(module, exports, webpack_require) {

exports = module.exports = __webpack_require__(338)();
// imports


// module
exports.push([module.id, ".bi-tab {\n  background-color: red; }\n", ""]);

// exports

@hawkrives
Copy link

Ok, hang on a second.

First things first: are you using the ?modules query with your css-loader? If you're not, then you'll need to use the :local style of defining classnames, otherwise they're global and not exported.

Anyway, I just tested this set of files, and they worked. When I open this index.html, the text has a red background.

Does this config work for you? I built it with webpack --config webpack.config.js.

package.json:

{
  "main": "index.js",
  "devDependencies": {
    "webpack": "^2.0.7-beta"
  }
}

styles.css: (with ?modules)

.className {
  background-color: red;
}

styles.css: (without ?modules)

:local(.className) {
  background-color: red;
}

index.js:

import {className} from './styles.css'

let div = document.createElement('div')
div.className = className
div.textContent = 'Words!'
document.body.appendChild(div)

webpack.config.js:

module.exports = {
  entry: ['./index.js'],
  output: {
    path: './',
    filename: 'bundle.js',
  },
  module: {
    loaders: [
      { test: /\.js$/, loader: 'babel-loader' },
      { test: /\.css$/, loader: 'style-loader!css-loader?modules' },  // important! you have to either have `?modules` or use the `:local` thingy in your css files.
    ],
  },
}

index.html:

<body></body>
<script src='./bundle.js'></script>

My bundle.js now includes this for my css file:

exports = module.exports = __webpack_require__(1)();
// imports

// module
exports.push([module.id, "._163b4mm1fitxanXK3GM0_t {\n  background-color: red;\n}\n", ""]);

// exports
exports.locals = {
    "className": "_163b4mm1fitxanXK3GM0_t"
};

@fisherspy
Copy link
Author

@hawkrives thank you for your guide! I totally get what you mean, however it's not in my case, I'm sorry I didn't describe it correctly.

I'm trying Angular2 currently, if I want to develop a component called bi-tabs,

// bi-tabs.css file,
.bi-tabs {
background-color: red;
}
...
.xx {

}

// in bi-tabs.ts file,
import BiTabsCss from "./bi-tabs.css";
@component {
styles:[BitabsCss]
}
Bi-Tabs.css is only used in Bi-Tabs compnent, it's not global(view-encapsulation), I don't need style-loader or add :local. I just need string in Bi-Tabs Css file and pass it to styles.

I think the bundle may be like: export.default = {whole content string in css file}

Thanks!

@fisherspy
Copy link
Author

@hawkrives, I think show you the code is more clear:
Source:

import TabsCss from './bi-tabs.css';

@Component({
  selector: 'bi-tabs',
  template: `
    <div class="bi-tabs">
      <a 
        class="bi-tabs-item"
        [style.width.px]="320/biTabsConfigs.length"
        *ngFor="#tabConfig of biTabsConfigs"
        [routerLink]="[tabConfig.routerLink]"
      >
        <span class="iconfont "></span>
        <p class="bi-tabs-item-text">{{tabConfig.text}}</p>
      </a>
    </div>
  `,
  styles: TabsCss, //  need to be identical with string below
  // styles: [".bi-tabs{position: fixed;bottom: 0;left: 0;width: 100%;z-index: 199;height: 50px;background: #f2f2f2;border-top: 1px solid #dbdbdb;}.bi-tabs-item{display: inline-block;overflow: hidden;height: 100%;color: #999;text-decoration: none;text-overflow: ellipsis;white-space: nowrap;}
  //.bi-tabs-item.router-link-active{color: #F60!important;} .bi-tabs>.bi-tabs-item>.bi-tabs-item-text {font-size: 10px;height: 10px;line-height: 10px;width: 100%;margin: 5px auto;text-align: center;}"],
  directives: [ROUTER_DIRECTIVES],

Build:

var bi_tabs_css_1 = __webpack_require__(354);
    var BiTabs = (function () {
        function BiTabs() {
        }
        __decorate([
            core_1.Input(), 
            __metadata('design:type', Array)
        ], BiTabs.prototype, "biTabsConfigs", void 0);
        BiTabs = __decorate([
            core_1.Component({
                selector: 'bi-tabs',
                template: "\n    <div class=\"bi-tabs\">\n      <a \n        class=\"bi-tabs-item\"\n        [style.width.px]=\"320/biTabsConfigs.length\"\n        *ngFor=\"#tabConfig of biTabsConfigs\"\n        [routerLink]=\"[tabConfig.routerLink]\"\n      >\n        <span class=\"iconfont \"></span>\n        <p class=\"bi-tabs-item-text\">{{tabConfig.text}}</p>\n      </a>\n    </div>\n  ",
                styles: bi_tabs_css_1.default,
                directives: [router_1.ROUTER_DIRECTIVES],
            }), 
            __metadata('design:paramtypes', [])
        ], BiTabs);
        return BiTabs;
    }());
    exports.BiTabs = BiTabs;

/* 354 */
/***/ function(module, exports, __webpack_require__) {

    exports = module.e = __webpack_require__(355)();
    // imports
    // module
    exports.push([module.i, ".bi-tabs{position: fixed;bottom: 0;left: 0;width: 100%;z-index: 199;height: 50px;background: #f2f2f2;border-top: 1px solid #dbdbdb;}\n.bi-tabs-item{display: inline-block;overflow: hidden;height: 100%;color: #999;text-decoration: none;text-overflow: ellipsis;white-space: nowrap;}\n.bi-tabs-item.router-link-active{color: #F60!important;}\n.bi-tabs>.bi-tabs-item>.bi-tabs-item-text {font-size: 10px;height: 10px;line-height: 10px;width: 100%;margin: 5px auto;text-align: center;}", ""]);

    // exports
/***/ },

@michael-ciniawsky
Copy link
Member

Will be fixed by #402

@tjpalmer
Copy link

tjpalmer commented Sep 4, 2017

Is this the kind of problem I should expect to see in the current state?

ModuleConcatenation bailout: Cannot concat with ./src/index.css (<- Module is not an ECMAScript module)

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

No branches or pull requests

5 participants