From 1433644f39acaf716c18246ea3210be50e837add Mon Sep 17 00:00:00 2001 From: Trotyl Yu Date: Sat, 30 Dec 2017 14:02:04 +0800 Subject: [PATCH] build: add inline step for templateUrl (#831) closes #684 --- build.sh | 1 + inline-template.js | 30 +++++++++++++++++++ package.json | 1 + .../button/nz-button.component.html | 2 ++ src/components/button/nz-button.component.ts | 5 +--- 5 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 inline-template.js create mode 100644 src/components/button/nz-button.component.html diff --git a/build.sh b/build.sh index ac0100a65f3..beaaeed54b5 100755 --- a/build.sh +++ b/build.sh @@ -4,6 +4,7 @@ readonly currentDir=$(cd $(dirname $0); pwd) cd ${currentDir} rm -rf publish cp -r src/components src/__gen_components +node ./inline-template.js node ./less.convert.js echo 'Compiling to es2015 via Angular compiler' diff --git a/inline-template.js b/inline-template.js new file mode 100644 index 00000000000..f476b98bbd8 --- /dev/null +++ b/inline-template.js @@ -0,0 +1,30 @@ +const fs = require('fs') +const path = require('path') +const glob = require('glob').sync + +function inlineResourcesForDirectory(folderPath) { + glob(path.join(folderPath, '**/*.ts')).forEach(filePath => inlineResources(filePath)) +} + +function inlineResources(filePath) { + let fileContent = fs.readFileSync(filePath, 'utf-8') + + fileContent = inlineTemplate(fileContent, filePath) + + fs.writeFileSync(filePath, fileContent, 'utf-8') +} + +function inlineTemplate(fileContent, filePath) { + return fileContent.replace(/templateUrl\s*:\s*'([^']+?\.html)'/g, (_match, templateUrl) => { + const templatePath = path.join(path.dirname(filePath), templateUrl) + const templateContent = loadResourceFile(templatePath) + return `template: \`${templateContent}\`` + }) +} + +function loadResourceFile(filePath) { + return fs.readFileSync(filePath, 'utf-8') + .replace(/([\n\r]\s*)+/gm, ' ') +} + +inlineResourcesForDirectory('./src/__gen_components') diff --git a/package.json b/package.json index fb719ecbdf3..7dca3b60553 100644 --- a/package.json +++ b/package.json @@ -64,6 +64,7 @@ "codelyzer": "~4.0.1", "core-js": "^2.4.1", "coveralls": "^2.13.1", + "glob": "^7.1.2", "highlight.js": "^9.12.0", "intl": "^1.2.5", "jasmine-core": "~2.6.2", diff --git a/src/components/button/nz-button.component.html b/src/components/button/nz-button.component.html new file mode 100644 index 00000000000..c0c067591b7 --- /dev/null +++ b/src/components/button/nz-button.component.html @@ -0,0 +1,2 @@ + + diff --git a/src/components/button/nz-button.component.ts b/src/components/button/nz-button.component.ts index 332e4a688b7..b4b94c0d6ee 100644 --- a/src/components/button/nz-button.component.ts +++ b/src/components/button/nz-button.component.ts @@ -16,10 +16,7 @@ export type NzButtonSize = 'small' | 'large' | 'default' ; @Component({ selector : '[nz-button]', encapsulation: ViewEncapsulation.None, - template : ` - - - `, + templateUrl : './nz-button.component.html', styleUrls : [ './style/index.less' ]