gpt4 book ai didi

webpack - 使用 webpack 和 twig 覆盖 Vuetify SASS 变量

转载 作者:行者123 更新时间:2023-12-03 14:47:58 25 4
gpt4 key购买 nike

我有 Vuetify 2.2.11,我正在尝试覆盖他们的 SASS 变量。我在一个 Symfony 3.x 项目中,所以我没有使用 vue-cli 安装 Vuetify。我关注了Webpack install guide但我似乎无法让它发挥作用。 Vuetify 样式被转储到一个 css 文件(以我的 js 命名: app.js -> app.css ),但我的覆盖没有考虑在内。至于我的项目样式( company.scss ),它们被注入(inject)到 <style type="text/css"> 中。 html中的标签。还有一大堆空的<style type="text/css"></style>标签,我猜它们来自每个 Vuetify 组件,但我不知道为什么它们是空的。

这就是我的代码的样子:

// /assets/js/app.js

import Vue from 'vue';
import vuetify from './plugins/Vuetify';
import FooComponent from './components/FooComponent;

import '../styles/company.scss';

const vmConfig = {
el: '#app',
vuetify,
components: {
FooComponent
},
};

new Vue(vmConfig);

// /assets/js/plugins/Vuetify

import Vue from 'vue';

// We import from "lib" to enable the "a-la-carte" installation.
// But even tough we use vuetify-loader we still need to manually specify VApp because it's used in a twig template and the loader doesn't read it.
import Vuetify, { VApp } from 'vuetify/lib';

Vue.use(Vuetify, {
components: {
VApp
}
});

export default new Vuetify({
icons: { iconfont: 'md' }
});

/* /assets/styles/variables.scss */

$font-size-root: 30px;
$body-font-family: 'Times New Roman';

@import '~vuetify/src/styles/styles.sass';
/*@import '~vuetify/src/styles/settings/variables'; // I tried this one instead and it didn't work either*/

/* /assets/styles/company.scss */

#foo-component {
background-color: pink;
}

{# /app/Resources/views/app.html.twig #}

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" media="all" href="{{ asset("build/app.css") }}" />
</head>
<body>
<div id="app">
<v-app>
{% block main %}
<h1>My page</h1>
<foo-component></foo-component>
{% endblock %}
</v-app>
</div>

{% block footer_js %}
<script type="text/javascript" src="{{ asset("build/app.js") }}"></script>
{% endblock %}
</body>
</html>

// webpack.config.js

const Encore = require('@symfony/webpack-encore');
const VuetifyLoaderPlugin = require('vuetify-loader/lib/plugin');
const webpack = require('webpack');
let path = require('path');

Encore
.setOutputPath('web/build/')
.setPublicPath('/build')
.addEntry('app', './assets/js/app.js')
.disableSingleRuntimeChunk()
.enableVueLoader()
.addPlugin(new VuetifyLoaderPlugin())
.enableSassLoader()
.addAliases({
'vue$': 'vue/dist/vue.esm.js'
})
.configureBabel(null, {
includeNodeModules: ['debug', 'vuetify'] // to make it work in IE11
})
.configureLoaderRule('sass', loaderRule => {
loaderRule.test = /\.sass$/;
loaderRule.use = [
'vue-style-loader',
'css-loader',
{
loader: 'sass-loader',
options: {
data: "@import '" + path.resolve(__dirname, 'assets/styles/variables.scss') + "'",
implementation: require('sass'),
fiber: require('fibers'),
indentedSyntax: true
}
}
];
})
;

let config = Encore.getWebpackConfig();
config.module.rules.push({
test: /\.scss$/,
use: [
'vue-style-loader',
'css-loader',
{
loader: 'sass-loader',
options: {
data: "@import '" + path.resolve(__dirname, 'assets/styles/variables.scss') + "';",
implementation: require('sass'),
fiber: require('fibers'),
indentedSyntax: false
},
},
]
});

module.exports = config;

最佳答案

好吧,我终于让它工作了。方法如下:webpack.config.js :
您需要在这里做的就是.enableSassLoader() . config options described here实际加载但变量声明无效的地方。app.js只需包括您的样式:

import '../styles/company.scss';
variables.scss我已经创建了这个文件并将所有 vuetify 特定变量移到那里。您可以使用 the official example . company.scss这是诀窍:
@import './assets/css/variables.scss';
@import '~vuetify/src/styles/main.sass'; // include this after your variables
就是这样。
我注意到热重新加载不适用于变量的更改。但只要这些变量有效,那就没问题了。我从 this page about the color pack 那里得到了提示

关于webpack - 使用 webpack 和 twig 覆盖 Vuetify SASS 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60228807/

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