我不知道如何从node_modules库中加载leaflet/dist/leaflet.css,例如我安装了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 .发布于 2015-12-17 07:18:32
对于遇到类似问题的用户,我已经做了一些步骤来让它工作,我不确定这种平衡的方式。
通过以下方式更改main app.js
npm install file-loader --saveimport '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来加载scs,以及与css-loader有什么关系,也许为了更连贯,我应该在这部分也使用ExtractTextPlugin,也许有人知道,可以审查代码吗?但我的解决方案是有效的,我可以进一步工作。
发布于 2018-09-09 14:18:13
我不得不这样做:
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文件导入到您的 main.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' ] },
],
},https://stackoverflow.com/questions/34311656
复制相似问题