【问题标题】:Example of how to load static CSS files from node_modules using webpack?如何使用 webpack 从 node_modules 加载静态 CSS 文件的示例?
【发布时间】:2016-03-22 13:37:33
【问题描述】:

我不知道如何使用 webpack 从 node_modules 库中加载任何 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 .

【问题讨论】:

    标签: javascript css webpack webpack-style-loader


    【解决方案1】:

    对于遇到类似问题的用户,我已经采取了一些步骤来使其正常工作,我不确定这种平衡方式。

    1. npm install file-loader --save
    2. 在主app.js中添加import 'leaflet/dist/leaflet.css';
    3. 通过以下方式更改 webpack.config.js:

    添加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 可能会更连贯在这部分,也许有人知道并且可以进行代码审查?但我的解决方案是有效的,我可以进一步工作。

    【讨论】:

    • 谢谢@luzny!如果你愿意,你可以接受你自己的答案;)
    • 我已经为这个问题苦苦挣扎了几天,尽管我永远无法导入我的 bootstrap.css 文件。添加图像和字体测试规则后,它消除了我遇到的错误。我想说的是你救了我兄弟!
    • 如果上帝是我们中的一员,他的名字将是“Iuzny”——这不过是事实。我已经尝试了好几天了。愿神与你同在,祝你一生健康!
    【解决方案2】:

    我必须这样做:

    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.jssn-p 是:

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

    【讨论】:

    • 这是更好的解决方案。加载器应该只添加到 dev 中,它使用新的配置格式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-19
    • 1970-01-01
    • 1970-01-01
    • 2016-07-26
    • 2017-09-29
    • 2016-08-07
    相关资源
    最近更新 更多