Skip to content

Commit c8cc30b

Browse files
authored
perf: find parent component when only secondary component used in demo (vueComponent#1680)
* perf: set demo id with component name when run dev * perf: find parent component when only secondary component used in demo
1 parent ae50539 commit c8cc30b

File tree

3 files changed

+41
-21
lines changed

3 files changed

+41
-21
lines changed

build/config.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module.exports = {
2-
devComponent: 'menu',
2+
dev: {
3+
componentName: 'tree', // dev components
4+
},
35
};

build/dev.js

+31-19
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,19 @@ const devWebpack = require('./webpack.dev.conf');
1111

1212
const configPath = path.join(__dirname, './config.js');
1313

14-
let { devComponent } = require('./config');
14+
/**
15+
* a-bc-d --> aBcD
16+
* @param {string} s
17+
*/
18+
const camelize = s => s.replace(/-(\w)/g, ($, $1) => $1.toUpperCase());
19+
20+
/**
21+
* radio-group --> radio
22+
* @param {string} s
23+
*/
24+
const getUpper = s => s.replace(/(-[a-z]*)/g, '');
25+
26+
let { componentName } = require('./config').dev;
1527

1628
const componentsInPrototype = ['Modal', 'message', 'notification'];
1729

@@ -116,6 +128,7 @@ const generateInstall = components =>
116128
const renderTemplate = name => {
117129
const components = {
118130
Tooltip: 'tooltip', // for DemoBox
131+
Icon: 'icon', // Basic
119132
};
120133

121134
const demoPaths = fs
@@ -128,16 +141,20 @@ const renderTemplate = name => {
128141
const demo = fs.readFileSync(path.join(__dirname, demoPath)).toString();
129142

130143
const componentsInDemo = demo.match(/a-(\w+(-\w+)*)/g) || [];
131-
componentsInDemo.forEach(n => {
132-
const componentName = n.replace(/-(\w)/g, ($, $1) => $1.toUpperCase()).replace(/^a/, '');
133-
134-
if (componentsInPrototype.includes(componentName)) {
135-
return;
136-
}
144+
componentsInDemo.forEach(name => {
145+
const dirName = name.replace(/^a-/, '');
146+
const componentName = camelize(name).replace(/^a/, '');
147+
const upperComponentDir = getUpper(dirName);
148+
const upperComponentName = upperComponentDir.replace(/^[a-z]/, $ => $.toUpperCase());
137149

138-
const componentPath = path.join(__dirname, `../components/${n.replace(/^a-/, '')}`);
150+
const componentPath = path.join(__dirname, `../components/${dirName}`);
139151
if (fs.existsSync(componentPath)) {
140-
components[componentName] = n.replace(/^a-/, '');
152+
if (componentsInPrototype.includes(componentName)) {
153+
return;
154+
}
155+
components[componentName] = dirName;
156+
} else if (fs.existsSync(path.join(__dirname, `../components/${upperComponentDir}`))) {
157+
components[upperComponentName] = upperComponentDir;
141158
}
142159
});
143160
});
@@ -173,24 +190,19 @@ if (!fsExistsSync(path.join(__dirname, '../components/test/index.vue'))) {
173190
let demoWatcher;
174191

175192
chokidar.watch(configPath, { ignoreInitial: true }).on('change', async () => {
176-
devComponent = importFresh(configPath).devComponent;
193+
({ componentName } = importFresh(configPath).dev);
177194

178195
demoWatcher && (await demoWatcher.close());
179196

180-
demoWatcher = chokidar.watch(path.join(__dirname, `../components/${devComponent}/demo`));
197+
demoWatcher = chokidar.watch(path.join(__dirname, `../components/${componentName}/demo`));
181198
demoWatcher.on('change', () => {
182-
renderTemplate(devComponent);
199+
renderTemplate(componentName);
183200
});
184201

185-
renderTemplate(devComponent);
186-
});
187-
188-
testWatcher = chokidar.watch(path.join(__dirname, `../components/test`));
189-
testWatcher.on('change', () => {
190-
renderTemplate(devComponent);
202+
renderTemplate(componentName);
191203
});
192204

193-
renderTemplate(devComponent);
205+
renderTemplate(componentName);
194206

195207
const compiler = webpack(devWebpack);
196208

site/components/demoBox.vue

+7-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import animate from 'antd/_util/openAnimation';
5656
import BaseMixin from 'antd/_util/BaseMixin';
5757
import { isZhCN } from '../util';
58+
import { dev } from '../../build/config';
5859
export default {
5960
name: 'DemoBox',
6061
mixins: [BaseMixin],
@@ -81,7 +82,12 @@ export default {
8182
.split(' ')
8283
.join('-')
8384
.toLowerCase();
84-
const id = ['components', name.replace(/-cn\/?$/, ''), 'demo', ...usTitle.split(' ')]
85+
const id = [
86+
'components',
87+
name.replace(/-cn\/?$/, '') || dev.componentName,
88+
'demo',
89+
...usTitle.split(' '),
90+
]
8591
.join('-')
8692
.toLowerCase();
8793

0 commit comments

Comments
 (0)