build-plugin-fusion
Version:
plugin for build scripts while use fusion component
69 lines • 2.8 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
var fs = require("fs");
var app_helpers_1 = require("@builder/app-helpers");
var sassLangs = '\\.(sass|scss)($|\\?)';
var sassLangRE = new RegExp(sassLangs);
var isSASSRequest = function (request) { return sassLangRE.test(request); };
// delete empty lines
function deleteEmptyLine(str) {
var filterLines = str.split('\n').filter(function (line) {
return line !== '';
});
filterLines.push('');
return filterLines.join('\n');
}
function vitePluginTheme(options) {
var themeFile = options.themeFile, themeConfig = options.themeConfig, iconFile = options.iconFile;
var mainSassFile = '';
var themeFileVars = '';
var themeConfigVars = '';
if (themeFile) {
themeFileVars = deleteEmptyLine(fs.readFileSync(themeFile).toString());
}
if (themeConfig) {
themeConfigVars = Object.keys(themeConfig).map(function (themeKey) {
if (['nextPrefix', 'theme'].includes(themeKey)) {
return false;
}
else {
var value = themeConfig[themeKey];
return "$".concat(themeKey, ": ").concat(value, ";");
}
}).filter(Boolean).join('\n');
}
return {
name: 'vite:fusion-plugin-theme',
enforce: 'pre',
transform: function (code, id) {
if (!code || !isSASSRequest(id)) {
return null;
}
var prefixVars = '';
if (themeConfig.nextPrefix && /@alifd[\\/]next[\\/](lib|es)[\\/](.+).scss$/.test(id)) {
// @deprecated
// 将 next 1.x 的 prefix 从 next- 改为自定义前缀,解决 0.x&1.x 混用的问题
prefixVars = "$css-prefix: \"".concat(themeConfig.nextPrefix, "\";");
}
var importVarsCode = '';
if (!/^node_modules[\\/]/.test(id)) {
importVarsCode = '@import \'@alifd/next/variables.scss\';';
}
var iconImport = '';
// theme icon only inject once
var needInjectIcon = !mainSassFile || mainSassFile === id;
if (iconFile && needInjectIcon) {
iconImport = "@import '".concat((0, app_helpers_1.formatPath)(iconFile), "';");
mainSassFile = id;
}
if (!prefixVars && !themeConfigVars && !themeFileVars && !importVarsCode) {
return null;
}
else {
return "".concat(themeFileVars, "\n").concat(themeConfigVars, "\n").concat(prefixVars, "\n").concat(importVarsCode, "\n").concat(iconImport, "\n").concat(code);
}
}
};
}
exports.default = vitePluginTheme;
//# sourceMappingURL=vitePluginTheme.js.map
;