Webpack-simple + vue-cli with SASS - 无法编译?
Webpack-simple + vue-cli with SASS - Can't compile?
我使用默认的 CLI 设置使用 vue-cli 和 webpack-simple 设置了一个简单的项目,并确保在询问我是否要使用 SASS 时选择了 'Y'。
一、文件夹结构图:
I created a main.scss
file in the ./src/scss
directory and
added the following simple scss syntax to the file:
$primary-color: rgb(173, 16, 16);
Then, in my App.vue template, I have it like so:
<style lang="scss" scoped>
h1, h2 {
font-weight: normal;
color: $primary-color;
}
</style>
In my main.js file, I made sure to import the main.scss file like
so:
import './scss/main.scss'
当运行 npm run dev
时,我得到以下编译错误:
编译失败
./node_modules/css-loader!./node_modules/vue-loader/lib/style-compiler?{"vue":true,"id":"data-v-04c2046b","scoped":true,"hasInlineConfig":false}!./node_modules/sass-loader/lib/loader.js!./node_modules/vue-loader/lib/selector.js?type=styles&index=0!./src/App.vue
Module build failed:
undefined
^
Undefined variable: "$primary-color".
in D:\Users\medranns\Desktop\sasswebpack\src\App.vue (line 46, column 10)
@ ./node_modules/vue-style-loader!./node_modules/css-loader!./node_modules/vue-loader/lib/style-compiler?{"vue":true,"id":"data-v-04c2046b","scoped":true,"hasInlineConfig":false}!./node_modules/sass-loader/lib/loader.js!./node_modules/vue-loader/lib/selector.js?type=styles&index=0!./src/App.vue 4:14-316 13:3-17:5 14:22-324
@ ./src/App.vue
@ ./src/main.js
@ multi (webpack)-dev-server/client?http://localhost:8080 webpack/hot/dev-server ./src/main.js
Here's what my webpack.config.js file looks like:
var path = require('path')
var webpack = require('webpack')
module.exports = {
entry: './src/main.js',
output: {
path: path.resolve(__dirname, './dist'),
publicPath: '/dist/',
filename: 'build.js'
},
module: {
rules: [
{
test: /\.css$/,
use: [
'vue-style-loader',
'css-loader'
],
},
{
test: /\.scss$/,
use: [
'vue-style-loader',
'css-loader',
'sass-loader'
],
},
{
test: /\.sass$/,
use: [
'vue-style-loader',
'css-loader',
'sass-loader?indentedSyntax'
],
},
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
loaders: {
// Since sass-loader (weirdly) has SCSS as its default parse mode, we map
// the "scss" and "sass" values for the lang attribute to the right configs here.
// other preprocessors should work out of the box, no loader config like this necessary.
'scss': [
'vue-style-loader',
'css-loader',
'sass-loader'
],
'sass': [
'vue-style-loader',
'css-loader',
'sass-loader?indentedSyntax'
]
}
// other vue-loader options go here
}
},
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.(png|jpg|gif|svg)$/,
loader: 'file-loader',
options: {
name: '[name].[ext]?[hash]'
}
}
]
},
resolve: {
alias: {
'vue$': 'vue/dist/vue.esm.js'
},
extensions: ['*', '.js', '.vue', '.json']
},
devServer: {
historyApiFallback: true,
noInfo: true,
overlay: true
},
performance: {
hints: false
},
devtool: '#eval-source-map'
}
if (process.env.NODE_ENV === 'production') {
module.exports.devtool = '#source-map'
// http://vue-loader.vuejs.org/en/workflow/production.html
module.exports.plugins = (module.exports.plugins || []).concat([
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"'
}
}),
new webpack.optimize.UglifyJsPlugin({
sourceMap: true,
compress: {
warnings: false
}
}),
new webpack.LoaderOptionsPlugin({
minimize: true
})
])
}
And package.json snippet:
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-env": "^1.6.0",
"babel-preset-stage-3": "^6.24.1",
"cross-env": "^5.0.5",
"css-loader": "^0.28.7",
"file-loader": "^1.1.4",
"node-sass": "^4.7.2",
"sass-loader": "^6.0.6",
"vue-loader": "^13.0.5",
"vue-template-compiler": "^2.4.4",
"webpack": "^3.6.0",
"webpack-dev-server": "^2.9.1"
}
我已经按照文档进行了操作,仍然进行了一些搜索,但我仍然不确定如何修复。感谢任何线索!
我在一个从 webpack-simple
样板
发展而来的 vue 项目中使用 scss
vue-init webpack-simple test-scss
尝试以下步骤在 vue 项目中设置 scss
使用上面的命令安装基本设置后,安装下面的 npm 包
npm install sass-loader node-sass style-loader --save-dev
在 webpack.config.js
规则数组下包括下面的加载程序
rules:[
...,
...,
{
test: /\.scss$/,
use: [{
loader: "style-loader" // creates style nodes from JS strings
},
{
loader: "css-loader" // translates CSS into CommonJS
},
{
loader: "sass-loader" // compiles Sass to CSS
}]
}
]
到目前为止做了什么? 使编译器能够将 scss 转换为 css 的配置已经完成。
现在将 .scss
导入 main.js
import Vue from 'vue';
import App from './App.vue';
import './style.scss';
new Vue({
el: '#app',
render: h => h(App)
});
这是我的文件夹结构
您现在可以将style.scss中的面向对象css代码编译为CSS
要在组件级别范围中使用 scss lang="scss"
必须在 <style>
标记中指定
App.vue
<style lang="scss">
$override-color : green;
div {
color: $override-color;
}
</style>
参考 this manual 以获得关于此主题的更多见解。期待您的反馈。
我遇到了同样的问题。尝试删除 'vue-loader':
中的 ?indentedSyntax
test: /\.vue$/,
loader: 'vue-loader',
options: {
loaders: {
'scss': [
'vue-style-loader',
'css-loader',
'sass-loader'
],
'sass': [
'vue-style-loader',
'css-loader',
'sass-loader?indentedSyntax'
]
}
// other vue-loader options go here
}
结果应该是这样的:
test: /\.vue$/,
loader: 'vue-loader',
options: {
loaders: {
// Since sass-loader (weirdly) has SCSS as its default parse mode, we map
// the "scss" and "sass" values for the lang attribute to the right configs here.
// other preprocessors should work out of the box, no loader config like this necessary.
'scss': [
'vue-style-loader',
'css-loader',
'sass-loader'
],
'sass': [
'vue-style-loader',
'css-loader',
'sass-loader'
]
}
// other vue-loader options go here
我使用默认的 CLI 设置使用 vue-cli 和 webpack-simple 设置了一个简单的项目,并确保在询问我是否要使用 SASS 时选择了 'Y'。
一、文件夹结构图:
I created a
main.scss
file in the./src/scss
directory and added the following simple scss syntax to the file:
$primary-color: rgb(173, 16, 16);
Then, in my App.vue template, I have it like so:
<style lang="scss" scoped>
h1, h2 {
font-weight: normal;
color: $primary-color;
}
</style>
In my main.js file, I made sure to import the main.scss file like so:
import './scss/main.scss'
当运行 npm run dev
时,我得到以下编译错误:
编译失败
./node_modules/css-loader!./node_modules/vue-loader/lib/style-compiler?{"vue":true,"id":"data-v-04c2046b","scoped":true,"hasInlineConfig":false}!./node_modules/sass-loader/lib/loader.js!./node_modules/vue-loader/lib/selector.js?type=styles&index=0!./src/App.vue
Module build failed:
undefined
^
Undefined variable: "$primary-color".
in D:\Users\medranns\Desktop\sasswebpack\src\App.vue (line 46, column 10)
@ ./node_modules/vue-style-loader!./node_modules/css-loader!./node_modules/vue-loader/lib/style-compiler?{"vue":true,"id":"data-v-04c2046b","scoped":true,"hasInlineConfig":false}!./node_modules/sass-loader/lib/loader.js!./node_modules/vue-loader/lib/selector.js?type=styles&index=0!./src/App.vue 4:14-316 13:3-17:5 14:22-324
@ ./src/App.vue
@ ./src/main.js
@ multi (webpack)-dev-server/client?http://localhost:8080 webpack/hot/dev-server ./src/main.js
Here's what my webpack.config.js file looks like:
var path = require('path')
var webpack = require('webpack')
module.exports = {
entry: './src/main.js',
output: {
path: path.resolve(__dirname, './dist'),
publicPath: '/dist/',
filename: 'build.js'
},
module: {
rules: [
{
test: /\.css$/,
use: [
'vue-style-loader',
'css-loader'
],
},
{
test: /\.scss$/,
use: [
'vue-style-loader',
'css-loader',
'sass-loader'
],
},
{
test: /\.sass$/,
use: [
'vue-style-loader',
'css-loader',
'sass-loader?indentedSyntax'
],
},
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
loaders: {
// Since sass-loader (weirdly) has SCSS as its default parse mode, we map
// the "scss" and "sass" values for the lang attribute to the right configs here.
// other preprocessors should work out of the box, no loader config like this necessary.
'scss': [
'vue-style-loader',
'css-loader',
'sass-loader'
],
'sass': [
'vue-style-loader',
'css-loader',
'sass-loader?indentedSyntax'
]
}
// other vue-loader options go here
}
},
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.(png|jpg|gif|svg)$/,
loader: 'file-loader',
options: {
name: '[name].[ext]?[hash]'
}
}
]
},
resolve: {
alias: {
'vue$': 'vue/dist/vue.esm.js'
},
extensions: ['*', '.js', '.vue', '.json']
},
devServer: {
historyApiFallback: true,
noInfo: true,
overlay: true
},
performance: {
hints: false
},
devtool: '#eval-source-map'
}
if (process.env.NODE_ENV === 'production') {
module.exports.devtool = '#source-map'
// http://vue-loader.vuejs.org/en/workflow/production.html
module.exports.plugins = (module.exports.plugins || []).concat([
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"'
}
}),
new webpack.optimize.UglifyJsPlugin({
sourceMap: true,
compress: {
warnings: false
}
}),
new webpack.LoaderOptionsPlugin({
minimize: true
})
])
}
And package.json snippet:
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-env": "^1.6.0",
"babel-preset-stage-3": "^6.24.1",
"cross-env": "^5.0.5",
"css-loader": "^0.28.7",
"file-loader": "^1.1.4",
"node-sass": "^4.7.2",
"sass-loader": "^6.0.6",
"vue-loader": "^13.0.5",
"vue-template-compiler": "^2.4.4",
"webpack": "^3.6.0",
"webpack-dev-server": "^2.9.1"
}
我已经按照文档进行了操作,仍然进行了一些搜索,但我仍然不确定如何修复。感谢任何线索!
我在一个从 webpack-simple
样板
vue-init webpack-simple test-scss
尝试以下步骤在 vue 项目中设置 scss
使用上面的命令安装基本设置后,安装下面的 npm 包
npm install sass-loader node-sass style-loader --save-dev
在 webpack.config.js
规则数组下包括下面的加载程序
rules:[
...,
...,
{
test: /\.scss$/,
use: [{
loader: "style-loader" // creates style nodes from JS strings
},
{
loader: "css-loader" // translates CSS into CommonJS
},
{
loader: "sass-loader" // compiles Sass to CSS
}]
}
]
到目前为止做了什么? 使编译器能够将 scss 转换为 css 的配置已经完成。
现在将 .scss
导入 main.js
import Vue from 'vue';
import App from './App.vue';
import './style.scss';
new Vue({
el: '#app',
render: h => h(App)
});
这是我的文件夹结构
您现在可以将style.scss中的面向对象css代码编译为CSS
要在组件级别范围中使用 scss lang="scss"
必须在 <style>
标记中指定
App.vue
<style lang="scss">
$override-color : green;
div {
color: $override-color;
}
</style>
参考 this manual 以获得关于此主题的更多见解。期待您的反馈。
我遇到了同样的问题。尝试删除 'vue-loader':
中的 ?indentedSyntax test: /\.vue$/,
loader: 'vue-loader',
options: {
loaders: {
'scss': [
'vue-style-loader',
'css-loader',
'sass-loader'
],
'sass': [
'vue-style-loader',
'css-loader',
'sass-loader?indentedSyntax'
]
}
// other vue-loader options go here
}
结果应该是这样的:
test: /\.vue$/,
loader: 'vue-loader',
options: {
loaders: {
// Since sass-loader (weirdly) has SCSS as its default parse mode, we map
// the "scss" and "sass" values for the lang attribute to the right configs here.
// other preprocessors should work out of the box, no loader config like this necessary.
'scss': [
'vue-style-loader',
'css-loader',
'sass-loader'
],
'sass': [
'vue-style-loader',
'css-loader',
'sass-loader'
]
}
// other vue-loader options go here