Skip to content

Commit 44926da

Browse files
authored
增加 import 选项解析 (#7)
* 增加 import 选项解析 * import 特性补充测试用例
1 parent 20ab48a commit 44926da

File tree

6 files changed

+34
-3
lines changed

6 files changed

+34
-3
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ Change the `webpack.config.js` file.
5555
+ color: '#008000'
5656
+ },
5757
+ yellow: {
58+
+ import: [
59+
+ '~thirdpartylibrary/styles/yellow.less'
60+
+ ],
5861
+ color: '#ffff00'
5962
+ }
6063
+ },

README_zh.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ module.exports = {
5555
+ color: '#008000'
5656
+ },
5757
+ yellow: {
58+
+ import: [
59+
+ '~thirdpartylibrary/styles/yellow.less'
60+
+ ],
5861
+ color: '#ffff00'
5962
+ }
6063
+ },

src/generate-themes.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@ const write = function(pathLike, content) {
1010
fs.writeFileSync(pathLike, content, 'utf-8');
1111
};
1212

13+
const getImportLines = config => {
14+
const imports = config.import || [];
15+
return _.map(imports, path => `@import '${path}';`);
16+
};
17+
1318
const getModifyVariablesContent = variableConfig => {
14-
const declaredContent = _.map(_.entries(variableConfig), ([key, value]) => `@${key}:${value};`);
19+
const declaredContent = _.map(_.entries(variableConfig).filter(([key]) => key !== 'import'), ([key, value]) => `@${key}:${value};`);
1520
return declaredContent.join('\r');
1621
};
1722

@@ -27,8 +32,9 @@ const generateThemes = (themesConfig = {}, configs = {}) => {
2732
_.forEach(_.entries(themesConfig), ([theme, config]) => {
2833
const lessFileName = `${theme}.less`;
2934
const jsFileName = `${theme}.js`;
35+
const importLines = getImportLines(config);
3036
const modifyVariablesContent = getModifyVariablesContent(config);
31-
const content = `${typeof lessContent === 'function' ? lessContent(theme, config) : lessContent}
37+
const content = `${typeof lessContent === 'function' ? lessContent(theme, config) : [...importLines, lessContent].join('\n')}
3238
3339
${preHeader}
3440
${modifyVariablesContent}`;

test/generate-themes.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ describe('Default generate.', () => {
88
generateThemes({
99
red: {
1010
'base-color': '#ff0000'
11+
},
12+
dark: {
13+
import: [
14+
'~rsuite/lib/styles/themes/dark/index'
15+
],
16+
color: '#e5e5e5'
1117
}
1218
});
1319
const outPutFilePath = path.resolve(process.cwd(), './src/src/less/themes');
@@ -34,6 +40,16 @@ import './red.less';`
3440
@base-color:#ff0000;`
3541
);
3642
});
43+
44+
test('dark.less import validate', () => {
45+
expect(readFile(resolvePath('dark.less'))).toEqual(
46+
`@import '~rsuite/lib/styles/themes/dark/index';
47+
@import "../index";
48+
49+
// Generate by Script.
50+
@color:#e5e5e5;`
51+
);
52+
})
3753
});
3854

3955
describe('Multiple themes generate.', () => {

test/index.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ test('Output Name test', done => {
6060
});
6161
runCompiler(compiler).then(() => {
6262
const dist = fs.readdirSync(`${outputPath}/css`);
63-
expect(dist.length).toEqual(3);
63+
expect(dist.length).toEqual(4);
6464
expect(dist).toContain('theme-green.css');
6565
expect(dist).toContain('theme-yellow.css');
6666
expect(dist).toContain('theme-red.css');

test/themes.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@ module.exports = {
77
},
88
red: {
99
color: '#ff0000'
10+
},
11+
dark: {
12+
color: '#e5e5e5'
1013
}
1114
};

0 commit comments

Comments
 (0)