Skip to content

Commit

Permalink
build(packaging): support entryComponents from demos (#921)
Browse files Browse the repository at this point in the history
  • Loading branch information
wilsoncook authored and 执衡 committed Jan 18, 2018
1 parent 46797a6 commit 88d6816
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
3 changes: 3 additions & 0 deletions scripts/template/demo-module.template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import { NgZorroAntdModule } from 'ng-zorro-antd';
],
declarations: [
{{declarations}}
],
entryComponents: [
{{entryComponents}}
]
})
export class NzDemo{{component}}Module {
Expand Down
21 changes: 17 additions & 4 deletions scripts/utils/generate-demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,20 @@ function generateDemoModule(content) {
const demoMap = content.demoMap;
let imports = '';
let declarations = '';
let entryComponents = [];
for (const key in demoMap) {
imports += `import { NzDemo${componentName(component)}${componentName(key)}Component } from './${key}';\n`;
declarations += `\t\tNzDemo${componentName(component)}${componentName(key)}Component,\n`;
const declareComponents = [ `NzDemo${componentName(component)}${componentName(key)}Component` ];
const entries = retrieveEntryComponents(demoMap[key] && demoMap[key].ts);
entryComponents.push(...entries);
declareComponents.push(...entries);
imports += `import { ${declareComponents.join(', ')} } from './${key}';\n`;
declarations += `\t\t${declareComponents.join(',\n\t')},\n`;
}
imports += `import { NzDemo${componentName(component)}ZhComponent } from './zh.component';\n`;
imports += `import { NzDemo${componentName(component)}EnComponent } from './en.component';\n`;
declarations += `\t\tNzDemo${componentName(component)}ZhComponent,\n`;
declarations += `\t\tNzDemo${componentName(component)}EnComponent,\n`;
return demoModuleTemplate.replace(/{{imports}}/g, imports).replace(/{{declarations}}/g, declarations).replace(/{{component}}/g, componentName(component));
return demoModuleTemplate.replace(/{{imports}}/g, imports).replace(/{{declarations}}/g, declarations).replace(/{{component}}/g, componentName(component)).replace(/{{entryComponents}}/g, entryComponents.join(',\n'));
}

function componentName(component) {
Expand Down Expand Up @@ -166,4 +171,12 @@ function generateExample(result) {
zh: isZhIcon ? '' : (isZhUnion ? templateUnion.replace(/{{content}}/g, zhPart) : templateSplit.replace(/{{first}}/g, firstZhPart).replace(/{{second}}/g, secondZhPart)),
en: isEnIcon ? '' : (isEnUnion ? templateUnion.replace(/{{content}}/g, enPart) : templateSplit.replace(/{{first}}/g, firstEnPart).replace(/{{second}}/g, secondEnPart))
}
}
}

function retrieveEntryComponents(plainCode) {
var matches = (plainCode + '').match(/^\/\*\s*?entryComponents:\s*([^\n]+?)\*\//) || [];
if (matches[1]) {
return matches[1].split(',').map(className => className.trim()).filter((value, index, self) => value && self.indexOf(value) === index);
}
return [];
}

0 comments on commit 88d6816

Please sign in to comment.