gpt4 book ai didi

css - Webpack - 导出 SASS (.scss) 文件

转载 作者:行者123 更新时间:2023-11-28 08:50:58 25 4
gpt4 key购买 nike

我有一个包,我想将我的 SASS 变量导出到其他包使用它。目前我所有的 .scss 文件都被编译并放入/dist/main.css 文件中。我的 webpack 配置:

var webpack = require('webpack');
var ExtractTextPlugin = require("extract-text-webpack-plugin");

module.exports = {
entry: ['./src/index.js'],
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel'
},
{
test: /\.(scss|sass|css)$/,
loader: ExtractTextPlugin.extract("style", "css!sass")
},
{
test: /\.(png|woff|woff2|eot|ttf|svg)$/,
loader: 'url-loader?limit=10000&name=fonts/[hash].[ext]'
},
{
test: /\.scss$/, loader: 'style!css!sass!sass-resources'
}
]
},
resolve: {
extensions: ['', '.js', '.jsx']
},
output: {
path: __dirname + '/build',
publicPath: '/',
filename: 'index.js',
library: 'Supernova',
libraryTarget: 'umd'
},
externals: {
'react': 'react',
'react-dom': 'react-dom'
},
plugins: [
new ExtractTextPlugin("[name].css")
]
};

我的目标是创建一个类似 bootstrap-sass 的包。

最佳答案

如果你想让你在 sass 文件中使用的变量对你发布的包的消费者可用,那么你需要查看 node-sass 的一些特殊配置。

目前(以及在您发布这篇文章时)node-sass 支持在 javascript 中编写您自己的自定义 sass 函数:https://github.com/sass/node-sass#functions--v300---experimental

这是未经测试的,但我们不久前在我工作过的一家公司做过这个......要做你想做的事,你需要类似的东西:

src/
your-package.js
your-styles.scss

tools/
constants/
colours.js

webpack/
...
base.sass.js
base.js
development.js
production.js

sass/
functions/
colours.js

# tools/webpack/base.sass.js

const Config = require('webpack-config').default

import {
signature as ColourSignature,
handler as ColourHandler
} from '@tools/sass/functions/colours

module.exports = new Config()
.merge({
module: {
rules: [
{
test: /\.scss$/,
use: [
...
{ loader: 'sass-loader',
options: {
sourceMap: true,
functions: {
[ColourSignature]: ColourHandler
}
}
},
]
}
]
}
})

# src/your-package.js

import Colours from '@tools/constants/colours'
import "./your-styles.scss"

export default YourAwesomeComponent {
static Colours = Colours
}

export const colours = Colours

# src/your-styles.scss

.your-awesome-component {
background-color: ColourGet(veganvomit, sobrightithurts);
}
# tools/sass/functions/colour.js

import Colours from '@tools/constants/colours'

export signature = 'ColourGet($name, $shade: default)'
export handler = function(name, shade) {
const colour = Colours[name]

if (!colour) return

if (typeof colour === 'string') return colour

return colour[shade]
}
# tools/sass/constants/colours.js

export default {

veganvomit: {
sobrightithurts: "darkkhaki",
light: "#D2691E",
default: "#8B4513",
somethingsomethingsomethingdarkside: "#000"
}

}

现在当你发布你的包时,他们可以从你的默认导出 YourAwesomeClass.Colours 访问 sass 变量,或者他们可以直接导入它 `import { Colors } from 'your-awesome-package'

关于css - Webpack - 导出 SASS (.scss) 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38796993/

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