出身背景

考虑以下_variables.scss个文件:

/* Define all colours */
$white:    #fff;
$black:    #000;
$grey:     #ccc;
// etc...

// Export the color palette to make it accessible to JS
:export {
    white: $white;
    black: $black;
    grey: $grey;
    // etc...
}

上述代码的目的是通过如下方式导入,使Javascript可以使用SCSS变量:

import variables from 'variables.scss';

请参阅更详细的说明here.

问题

现在考虑下面的模板(我用Vue.js模板作为例子,但这与许多框架相关):

<!-- Template here... -->

<style lang="scss" scoped>

    // Import Partials
    @import "~core-styles/brand/_variables.scss";
    @import "~core-styles/brand/_mixins.scss";

    // Styles here...

</style>

In the above example I have used the 100 attribute as this demonstrates the worst case scenario for the upcoming problem, but even without 100 the problem is still relevant.

上述SCS将编译成以下内容:

[data-v-9a6487c0]:export {
    white: #fff;
    black: #000;
    grey: #ccc;
    // etc...
}

此外,对于scoped属性,当_variables.scss被导入模板时,这将重复every次,can可能会导致额外的冗余代码.在某些情况下,对于大型应用程序(许多组件和大型调色板),这实际上可以添加000行完全冗余的代码.

我的问题

有没有办法将SCSS变量导出到Javascript without并将其导出到CSS?

潜在(肮脏)解决方案

理想情况下,我尽量避免使用一个名为_export.scss的单独文件的解决方案,其目的只是将所有SCSS变量导出到JS,但它被排除在所有CSS构建之外...

为了扩展上述dirty个解决方案,我目前正在使用这一解决方案(在我的情况下,在一个标准大小的网站上,到目前为止,它为我节省了约600行冗余CSS代码):

100

/*
 |--------------------------------------------------------------------------
 | SASS Export
 |--------------------------------------------------------------------------
 |
 | Define any variables that should be exported to Javascript. This file
 | is excluded from the CSS builds in order to prevent the variables from
 | being exported to CSS.
 |
 */

@import "variables";

:export {

    // Export the color palette to make it accessible to JS
    white: #fff;
    black: #000;
    grey: #ccc;
    // etc...

}

然后,在我的Javascript中,不是从_variables.scss导入,而是从_export.scss导入,如下所示:

import styles from 'core-styles/brand/_export.scss';

最后,export语句现在可以从_variables.scss文件中删除,从而防止编译CSS export代码.

Note:_export.scss文件must从SCSS编译中排除!

推荐答案

Note:我发布这个答案是因为似乎没有better解决方案,但是,如果有人随后提供了更好的解决方案,我将非常乐意接受它.

似乎唯一真正的解决方案是从_variables.scss文件中提取export语句,并将其放入自己的_export.scss文件中,该文件随后将包含在SCSS合规中.

这看起来像这样:

_variables.scss-包含在SCSS汇编中

/* Define all colours */
$white:    #fff;
$black:    #000;
$grey:     #ccc;
// etc...

_export.scss-not包括在SCSS汇编中

@import "variables";

:export {

    // Export the color palette to make it accessible to JS
    white: #fff;
    black: #000;
    grey: #ccc;
    // etc...

}

然后你的app.scss(我使用brand.scss)文件将看起来像这样(注意没有@include "export";):

@import "variables";
@import "mixins";
@import "core";
@import "animations";
// etc...

然后,_export.scss在JavaScript中只是引用only,就像这样(注意core-styles只是我在项目中使用的别名):

import styles from 'core-styles/brand/_export.scss';

Vue.js相关问答推荐

Vuetify/Vue3 插槽模板之间的阴影

如何防止Nuxt3重新加载时滚动跳到网页顶部

在 Vuejs 中更新 Chartjs 图表数据集

Vuetify 复选框未正确显示值

vue 计算的 ant watch 不会在嵌套属性更改时触发

如何使用 vue 或 javascript 向服务器发送密码?

VueJS:如何在组件数据属性中引用 this.$store

我是否必须在 Vue 3 中使用 Composition API,还是仍然可以使用Vue 2的方式工作?

即使响应为 200,也会调用 Axios Catch

Vue 和 Bootstrap Vue - 动态使用插槽

Webpack 你可能需要一个合适的加载器来处理这个文件类型

VueJS:向左,元素的顶部位置?

如何获取 Vue 路由历史记录?

将数据传递给另一条路由上的组件

Vuetify - 如何在 v-data-table 中单击时突出显示行

如何使用 Vuejs 和 Laravel 获取当前经过身份验证的用户 ID?

如何在 Vue.js 应用程序中正确下载 Excel 文件?

Vuejs获取事件正在调用的元素?

居中 v-toolbar-title

隐藏特定标题及其在 vuetify 数据表中的对应列