18

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

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

我当前的 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")]
};

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

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

SyntaxError: Unexpected token .
4

2 回答 2

13

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

  1. npm install file-loader --save
  2. 添加import 'leaflet/dist/leaflet.css';主app.js
  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 更加连贯,也许有人知道并且可以进行代码审查吗?但我的解决方案正在工作,我可以进一步工作。

于 2015-12-16T23:18:32.070 回答
4

我必须做:

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' ] },
        ],
    },
于 2018-09-09T06:18:12.627 回答