如何使用 webpack 从 node_modules 加载静态 CSS 文件的示例? [英] Example of how to load static CSS files from node_modules using webpack?

查看:47
本文介绍了如何使用 webpack 从 node_modules 加载静态 CSS 文件的示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.css fails.

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

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

我当前的 webpack 配置如下.另外我正在使用 extract-text-webpack-pluginsass-loader 我的项目 scss 文件运行良好,我也有 css-loader,我需要解析静态 css 文件还是在 stylePathResolves 中添加一些东西?

My current webpack config below. Additionaly I'm using extract-text-webpack-plugin and sass-loader my project scss files are working well, I have also css-loader, have I to resolve static css files or add something to 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') 给我以下错误:

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

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

SyntaxError: Unexpected token .

推荐答案

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

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. 在主 app.js 中添加 import 'leaflet/dist/leaflet.css';
  3. 通过以下方式更改 webpack.config.js:

添加 css-loader 以摆脱 SyntaxError: Unexpected token . 然后添加 file-loader 并匹配文件以摆脱未捕获的错误:找不到模块./images/layers.png":

add css-loader to get rid of SyntaxError: Unexpected token . and next add file-loader and match files to get rid of 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,也许有人知道并且可以进行代码审查?但我的解决方案是有效的,我可以进一步工作.

At the beginning I got this config from some example and it's not 100% clear why I've used ExtractTextPlugin to 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.

这篇关于如何使用 webpack 从 node_modules 加载静态 CSS 文件的示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆