如何在.vue文件中将选项传递给SASS加载器以获取样式?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在.vue文件中将选项传递给SASS加载器以获取样式?相关的知识,希望对你有一定的参考价值。
我正在尝试配置vue-loader以使其在@import
语句中包含node_modules。 scss文件的加载器配置工作正常,如下所示:
{
test: /.(sass|scss)$/,
use: [
'style-loader',
'css-loader',
{
loader: 'sass-loader',
options: {
includePaths: [path.resolve(__dirname, 'node_modules')],
},
},
],
},
所以现在我正试图在.vue文件中使用它。我想到了这样的事情:
{
test: /.vue$/,
loader: 'vue-loader',
options: {
loaders: {
// ?{"includePaths":["C:/Users/samuel/Code/school/tinf/sem03/proj01/node_modules"]}
sass: 'vue-style-loader!css-loader!sass-loader?' +
`includePaths[]=${path.resolve(__dirname, 'node_modules').replace(/\/g, '/')}`,
},
},
},
我收到一条错误消息:
错误在./~/css-loader!./~/vue-loader/lib/style-rewriter.js?id=data-v-1ec85c08!./~/sass-loader?includePaths[]=C:/Users /samuel/Code/school/tinf/sem03/proj01/node_modules!./~/vue-loader/lib/selector.js?type=styles&index=0!./src/components/PageHeader.vue
我试过从堆栈中删除vue-style-loader
和css-loader
,但我仍然收到错误。
但是当我直接将选项传递给样式标记时,它可以正常工作:
<style lang="sass?{"includePaths":["C:/Users/samuel/Code/school/tinf/sem03/proj01/node_modules"]}">
如何修改加载器以使其工作?
您可以将它作为includePaths
传递给sass option,如下所示:
module.exports = {
...
module: {
loaders: [
{
test: /.scss$/,
loaders: ["sass-loader"]
}
]
},
sassLoader: {
includePaths: [path.resolve(__dirname, "./node_modules")]
}
};
糟糕!
给定的代码工作正常。
我收到错误File to import not found or unreadable: @material/typography
因为该文件不存在。我应该进口@material/typography/mdc-typography
。
那是一个愚蠢的问题,我很抱歉。
无论如何,这是我的固定webpack配置:
const path = require('path');
const webpack = require('webpack');
const htmlWebpackPlugin = require('html-webpack-plugin');
const nodeModules = path.resolve(__dirname, 'node_modules');
module.exports = {
devtool: 'source-map',
entry: path.resolve(__dirname, './src/index.js'),
module: {
rules: [
{
test: /.js$/,
loader: 'babel-loader',
exclude: /node_modules/,
},
{
test: /.vue$/,
loader: 'vue-loader',
options: {
loaders: {
// IMPORTANT for scss (lang="sass") in .vue files
sass: 'style-loader!css-loader!sass-loader?' +
`includePaths[]=${nodeModules}`,
},
},
},
{
test: /.(sass|scss)$/,
use: [
'style-loader',
'css-loader',
// IMPORTANT for scss files
{ loader: 'sass-loader', options: { includePaths: [nodeModules] } },
],
},
{
test: /.css$/,
use: ['style-loader', 'css-loader'],
},
],
},
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},
resolve: {
extensions: ['.js', '.vue'],
alias: {
vue: 'vue/dist/vue.js',
},
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new HtmlWebpackPlugin({
filename: 'index.html',
template: 'src/index.html',
}),
],
};
{
test: /.vue$/,
loader: 'vue-loader',
options: {
loaders: {
scss: [
{
loader: 'sass-loader', options: {
// here
}
]
}
}
}
https://vue-loader.vuejs.org/en/options.html#loaders
以上是关于如何在.vue文件中将选项传递给SASS加载器以获取样式?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 JAVA 中将 JSON 和文件传递给 REST API?
如何在`collection`字段Symfony 2.1中将选项传递给CustomType?