fix: import theme icon (#484)

* fix: import theme icon

* fix: match entry
This commit is contained in:
ClarkXia 2022-08-29 11:04:21 +08:00
parent 11a58ed9ae
commit 52c2314ee9
1 changed files with 32 additions and 9 deletions

View File

@ -27,6 +27,29 @@ function getVariablesPath({
return filePath; return filePath;
} }
function importIcon(iconPath: string, cssPrefix: string) {
let entryFile = '';
return {
name: 'transform-import-icon',
enforce: 'pre',
async transform(code: string, id: string, options: { isServer: boolean }) {
const { isServer } = options;
// Only import icon scss in client
if (!isServer) {
// Icon just import once.
if (!entryFile && !id.match(/node_modules/) && id.match(/[js|jsx|ts|tsx]$/)) {
entryFile = id;
}
if (id === entryFile) {
return `import '${iconPath}';\n${code}`;
} else if (id === iconPath) {
return `$css-prefix: '${cssPrefix}';\n${code}`;
}
}
},
};
}
const plugin: Plugin<PluginOptions> = (options = {}) => ({ const plugin: Plugin<PluginOptions> = (options = {}) => ({
name: '@ice/plugin-fusion', name: '@ice/plugin-fusion',
setup: ({ onGetConfig }) => { setup: ({ onGetConfig }) => {
@ -41,6 +64,15 @@ const plugin: Plugin<PluginOptions> = (options = {}) => ({
} }
if (theme || themePackage) { if (theme || themePackage) {
onGetConfig((config) => { onGetConfig((config) => {
// Try to get icon.scss if exists.
const iconFile = getVariablesPath({
packageName: themePackage,
filename: 'icons.scss',
silent: true,
});
if (iconFile) {
config.transformPlugins = [...(config.transformPlugins || []), importIcon(iconFile, theme['css-prefix'] || 'next-')];
}
// Modify webpack config of scss rule for fusion theme. // Modify webpack config of scss rule for fusion theme.
config.configureWebpack ??= []; config.configureWebpack ??= [];
config.configureWebpack.push((webpackConfig) => { config.configureWebpack.push((webpackConfig) => {
@ -65,15 +97,6 @@ const plugin: Plugin<PluginOptions> = (options = {}) => ({
if (themeFile) { if (themeFile) {
additionalContent.push(`@import '${themePackage}/variables.scss';`); additionalContent.push(`@import '${themePackage}/variables.scss';`);
} }
// Try to get icon.scss if exists.
const iconFile = getVariablesPath({
packageName: themePackage,
filename: 'icons.scss',
silent: true,
});
if (iconFile) {
additionalContent.push(`@import '${themePackage}/icons.scss';`);
}
} }
let themeConfig = []; let themeConfig = [];
Object.keys(theme || {}).forEach((key) => { Object.keys(theme || {}).forEach((key) => {