gpt4 book ai didi

node.js - Webpack-simple + vue-cli with SASS - 无法编译?

转载 作者:搜寻专家 更新时间:2023-10-31 23:45:09 26 4
gpt4 key购买 nike

我使用默认的 CLI 设置使用 vue-cli 和 webpack-simple 设置了一个简单的项目,并确保在询问我是否要使用 SASS 时选择了“Y”。

一、文件夹结构图:

enter image description here

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)
});

这是我的文件夹结构

enter image description here

您现在可以将 style.scss 中面向对象的 css 代码编译为 CSS

在组件级范围内使用 scss lang="scss"必须在 <style> 中指定标签

App.vue
<style lang="scss">
$override-color : green;

div {
color: $override-color;
}
</style>

引用this manual有关此主题的更多见解。期待您的反馈。

关于node.js - Webpack-simple + vue-cli with SASS - 无法编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48490969/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com