如何使用webpack从node_modules加载静态CSS文件?

时间:2015-12-16 12:08:19

标签: javascript css webpack webpack-style-loader

我不知道如何使用webpack从node_modules libs加载任何CSS,例如我安装了传单,每次加载leaflet/dist/leaflet.css的尝试都失败。

您能举例说明如何从node_modules加载静态样式吗?

我目前的webpack配置如下。另外,我正在使用extract-text-webpack-pluginsass-loader我的项目scss文件运行良好,我还css-loader,让我解析静态css文件或向stylePathResolves添加内容?

//require('leaflet/dist/leaflet.css');

var ExtractTextPlugin = require("extract-text-webpack-plugin");
var webpack = require("webpack");
var path = require('path');

var stylePathResolves = (
  'includePaths[]=' + path.resolve('./') + '&' +
  'includePaths[]=' + path.resolve('./node_modules')
)

module.exports = {
  entry: ".js/app.js",
  output: {
    path: "./static/js",
    filename: "app.js"
  },
  module: {
    loaders: [{
        test: /\.jsx?$/,
        exclude: /node_modules/,
        loader: 'babel'
      }, {
        test: /\.scss$/,
        loader: ExtractTextPlugin.extract(
          'style',
          'css' + '!sass?outputStyle=expanded&' + stylePathResolves
        )
      }
    ]
  },
  plugins: [new ExtractTextPlugin("app.css")]
};

在哪里加载leaflet.css,注释掉require('leaflet/dist/leaflet.css')会出现以下错误:

home/myproj/node_modules/leaflet/dist/leaflet.css:3
.leaflet-map-pane,
^

SyntaxError: Unexpected token .

2 个答案:

答案 0 :(得分:12)

对于遇到类似问题的用户,我已经采取了一些步骤来实现它,我不确定这种均衡方式。

  1. npm install file-loader --save
  2. 在主app.js
  3. 中添加import 'leaflet/dist/leaflet.css';
  4. 按照以下方式更改webpack.config.js:
  5. 添加css-loader以摆脱SyntaxError: Unexpected token .,然后添加file-loader并匹配文件以摆脱Uncaught Error: Cannot find module "./images/layers.png"

    module.exports = {
      entry: "./web/static/js/app.js",
      output: {
        path: "./priv/static/js",
        filename: "app.js"
      },
      module: {
        loaders: [{
          test: /\.jsx?$/,
          exclude: /node_modules/,
          loader: 'babel'
        }, {
          test: /\.scss$/,
          loader: ExtractTextPlugin.extract(
            'style',
            'css' + '!sass?outputStyle=expanded&' + stylePathResolves
          )
        }, {
          test: /\.css$/,
          loader: "style-loader!css-loader"
        }, {
          test: /\.(png|jpg|jpeg|gif|svg|woff|woff2|ttf|eot)$/,
          loader: 'file-loader'
        }]
      },
      plugins: [new ExtractTextPlugin("app.css")]
    };
    

    一开始我从一些例子中得到了这个配置并且它不是100%清楚为什么我使用ExtractTextPlugin来加载scss以及与css-loader的相关性,也许是如果我在这部分也使用ExtractTextPlugin,可能会有人知道并且可以进行代码审查吗?但我的解决方案正在发挥作用,我可以继续努力。

答案 1 :(得分:1)

我必须做:

npm install --save-dev style-loader css-loader file-loader

如果我没有为图像配置文件加载器,则会得到:

ERROR in ./node_modules/leaflet/dist/images/layers-2x.png 1:0
Module parse failed: Unexpected character '�' (1:0)
You may need an appropriate loader to handle this file type.
(Source code omitted for this binary file)
 @ ./node_modules/leaflet/dist/leaflet.css (./node_modules/css-loader!./node_modules/leaflet/dist/leaflet.css) 7:8888-8921
 @ ./node_modules/leaflet/dist/leaflet.css
 @ ./src/view/component/RouteMap.js
 @ ./src/index.js

ERROR in ./node_modules/leaflet/dist/images/layers.png 1:0
Module parse failed: Unexpected character '�' (1:0)
You may need an appropriate loader to handle this file type.
(Source code omitted for this binary file)
 @ ./node_modules/leaflet/dist/leaflet.css (./node_modules/css-loader!./node_modules/leaflet/dist/leaflet.css) 7:8716-8746
 @ ./node_modules/leaflet/dist/leaflet.css
 @ ./src/view/component/RouteMap.js
 @ ./src/index.js

ERROR in ./node_modules/leaflet/dist/images/marker-icon.png 1:0
Module parse failed: Unexpected character '�' (1:0)
You may need an appropriate loader to handle this file type.
(Source code omitted for this binary file)
 @ ./node_modules/leaflet/dist/leaflet.css (./node_modules/css-loader!./node_modules/leaflet/dist/leaflet.css) 7:9975-10010
 @ ./node_modules/leaflet/dist/leaflet.css
 @ ./src/view/component/RouteMap.js
 @ ./src/index.js

您还必须将CSS文件导入到index.jsmain.js(或任何称为主JS的文件)中。否则,您会收到一条错误消息,说module.exports is read-only

import 'leaflet/dist/leaflet.css';

更新后的webpack.config.js代码段是:

{
    ...
    module: {
        rules: [
            { test: /\.css$/, use: ["style-loader","css-loader"] },
            { test: /\.(png|svg|jpe?g|gif|woff2?|ttf|eot)$/, use: [ 'file-loader' ] },
        ],
    },