Skip to content

Is there a proper way to import vendor styles from a CSS module? #253

Closed
@dbazile

Description

@dbazile

Are any of the following intended to work?

@import "leaflet/dist/leaflet.css";
    // throws `Error: Cannot resolve module 'images/layers.png'`

:global { @import "leaflet/dist/leaflet.css"; }
    // throws `Error: Cannot resolve module 'images/layers.png'`

:global { @import '../../node_modules/leaflet/dist/leaflet.css'; }
    // throws `Error: Cannot resolve module 'images/layers.png'`

:global { @import '!style!css?-module!leaflet/dist/leaflet.css'; }
    // throws `CSSSyntaxError
    //   @ ./~/css-loader?module&localIdentName=[name]__[local]!./~/less-loader!./app/components/MapWidget.less 3:10-138`

Background

I'm trying to load the vendor CSS from Leaflet and running into errors because it does relative imports for image assets. What I'd like to have happen is the CSS is loaded and everything in it treated as a global.

I've been scouring the web for a few hours now and couldn't find an answer to this.

Temporary Workaround

I've disabled modules on plain .css files and added import 'leaflet/dist/leaflet.css'; to the top of the .jsx file. Is this the happy path? It's certainly the path of least resistance, but it feels odd to have this thing dangling outside of CSS.

My guess as to what's happening

My guess is the CSS loader is preempting the LESS loader and doesn't recognize the :global {} nesting.

Relevant parts of webpack.config.js:

module.exports = {
  context: path.join(__dirname, 'app'),
  entry: './index.js',
  devtool: 'source-map',

  resolve: {
    extensions: ['', '.js', '.jsx']
  },

  output: {
    path: path.join(__dirname, 'dist'),
    filename: 'bundle.js'
  },

  module: {
    preLoaders: [
      {test: /\.jsx?$/, loader: 'eslint', exclude: /node_modules/}
    ],
    loaders: [
      {test: /\.jsx?$/, loader: 'babel', exclude: /node_modules/},
      {test: /\.css$/, loader: 'style!css'},
      {test: /\.less$/, loader: 'style!css?module&localIdentName=[name]__[local]!less'},
      {test: /\.(png|jpg|gif)$/, loader: 'file'}
    ]
  },

  plugins: [
    new webpack.EnvironmentPlugin(['NODE_ENV']),
    new HtmlWebpackPlugin({
      title: `${pkg.name} v${pkg.version}`,
      hash: true,
      xhtml: true
    }),

    // Polyfill just in case -- unsure what cutoff is for "modern browsers"
    new webpack.ProvidePlugin({fetch:'isomorphic-fetch'})
  ]
};

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions