js中import less文件后使用webpack打包时报错
想用webpack搭个小项目,没想到在less这里就卡住了,运行npm run build打包时提示以下错误
ERROR in ./src/style/index.less
Module parse failed: C:\webpackApp\src\style\index.less Unexpected character '@' (1:0)
You may need an appropriate loader to handle this file type.
| @mainColor:#fc9032;
|
| html,body{
@ ./src/m1.js 1:0-28
不引入less文件后一切正常.但是又苦于找不出问题在哪里,想请高手帮忙看看.
package.json:
{
"name": "y",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-polyfill": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-3": "^6.24.1",
"css-emitter": "^0.0.1",
"css-loader": "^0.28.7",
"emitter": "^0.0.5",
"file-loader": "^0.11.2",
"less": "^2.7.2",
"less-loader": "^4.0.5",
"lodash": "^4.17.4",
"style-loader": "^0.18.2",
"webpack": "^3.5.5"
}
}
webpack.config.js:
const path = require('path') //引入path模块
module.exports = {
entry: './src/m1.js',
output: {
filename: 'main.js',
path: path.resolve(__dirname, 'app')
},
module: {
rules: [
{
test: /\.(less|css)$/,
use: ['style-loader','css-loader','less-loader']
}
],
rules: [
{ test: /\.js$/, exclude: /node_modules/, loader: "babel-loader" }
]
}
}
m1.js:
import './style/index.less'; // 引入less,但是打包期间报错
import _ from 'lodash';
import {plane} from './m2'
let container = document.getElementById('app')
let dict = [{name:'mike', age:18}, {name:'jack', age:22}, {name:'tim', age:23}]
let target = _.findIndex(dict, {name:'tim'})
window.alert(dict[target].age)
index.less
@mainColor:#fc9032;
html,body{
width : 100%;
height : 100%;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
https://segmentfault.com/a/11...
先参考下 是1.x的写法.
我之前跟你差不多类似的问题,应该是配置的问题
你可以按我这样试试
把你的webpack.config.js中的module模块修改成如下配置即可。
config文件的rules可以合并
建议你按官方教程再走一遍