javascript 如何使用 webpack 从 node_modules 加载静态 CSS 文件的示例?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34311656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 17:32:08  来源:igfitidea点击:

Example of how to load static CSS files from node_modules using webpack?

javascriptcsswebpackwebpack-style-loader

提问by luzny

I don't know how to load with webpack any CSS from node_modules libs, for example I've installed leaflet and each attempt of load leaflet/dist/leaflet.cssfails.

我不知道如何使用 webpack 加载来自 node_modules 库的任何 CSS,例如我已经安装了传单并且每次加载尝试都leaflet/dist/leaflet.css失败了。

Could you provide example how to load static styles from node_modules?

您能否提供如何从 node_modules 加载静态样式的示例?

My current webpack config below. Additionaly I'm using extract-text-webpack-pluginand sass-loadermy project scss files are working well, I have also css-loader, have I to resolve static css files or add something to stylePathResolves?

我当前的 webpack 配置如下。另外我正在使用extract-text-webpack-plugin并且sass-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")]
};

Where to load leaflet.css, commented out require('leaflet/dist/leaflet.css')gives me following error:

在哪里加载传单.css,注释掉require('leaflet/dist/leaflet.css')给了我以下错误:

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

SyntaxError: Unexpected token .

采纳答案by luzny

For users who have encountered a similar problem, there are steps that I've done to get it working, I'm not sure that this equilibrium way.

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

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

add css-loaderto get rid of SyntaxError: Unexpected token .and next add file-loaderand match files to get rid of Uncaught Error: Cannot find module "./images/layers.png":

添加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")]
};

At the beginning I got this config from some example and it's not 100% clear why I've used ExtractTextPluginto load scss and what the correlation is with css-loader, maybe to be more coherent should I use ExtractTextPlugin also in this part, maybe someone knows and can code review? But my solution is working and I can work further.

一开始我从一些例子中得到了这个配置,并不是 100% 清楚为什么我习惯ExtractTextPlugin加载 scss 以及与 css-loader 的相关性,也许我应该在这部分也使用 ExtractTextPlugin 更加连贯,也许有人知道并且可以进行代码吗?但我的解决方案有效,我可以进一步工作。

回答by Daniel Gray

I had to do:

我必须做:

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

If I didn't configure the file loader for the images, I got:

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

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

You also MUST import the CSS file in your index.jsor main.js(or whatever your main JS file is called). Otherwise, you get an error saying that module.exports is read-only.

您还必须在您的index.jsmain.js(或任何您的主要 JS 文件调用)中导入 CSS 文件。否则,您会收到一个错误提示module.exports is read-only

import 'leaflet/dist/leaflet.css';

A more updated webpack.config.jssnippet is:

更新的webpack.config.js片段是:

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