如何使用 webpack 从 node_modules 加载静态 CSS 文件的示例?
Example of how to load static CSS files from node_modules using webpack?
我不知道如何使用 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 .
对于遇到类似问题的用户,我已经完成了一些步骤让它工作,我不确定这种平衡方式。
npm install file-loader --save
- 在主app.js
中添加import 'leaflet/dist/leaflet.css';
- 通过以下方式更改 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,也许有人知道并且可以进行代码审查?但我的解决方案有效,我可以进一步工作。
我必须做的:
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.js
或 main.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' ] },
],
},
我不知道如何使用 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 .
对于遇到类似问题的用户,我已经完成了一些步骤让它工作,我不确定这种平衡方式。
npm install file-loader --save
- 在主app.js 中添加
- 通过以下方式更改 webpack.config.js:
import 'leaflet/dist/leaflet.css';
添加 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,也许有人知道并且可以进行代码审查?但我的解决方案有效,我可以进一步工作。
我必须做的:
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.js
或 main.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' ] },
],
},