To use external templates & stylesheets, you can:
- process your
scssfiles (if you have them) - use an external library (or a custom function) to inline the
html/cssfiles
Following are the steps to adapt the library to the use of external templates and stylesheets (thanks to Paul Ryan @paullryan for his suggestion):
-
Install the following packages:
npm i node-sass --save-devnpm i angular2-inline-template-style --save-devNote that you could use different packages.
-
build.jsfile:Add the
BUILD_DIRconstant:const BUILD_DIR = `build`;Add to the start:
shell.rm(`-Rf`, `${BUILD_DIR}/**`); shell.mkdir(`-p`, `./${BUILD_DIR}`);Add before the Aot Compilation:
/* Process scss files */ shell.echo(`Process scss files`); shell.cp(`-Rf`, [`src`, `*.ts`, `*.json`], `${BUILD_DIR}`); shell.exec(`node-sass -r ${BUILD_DIR} -o ${BUILD_DIR}`); shell.rm(`-Rf`, `${BUILD_DIR}/**/*.scss`); shell.ls(`${BUILD_DIR}/**/*.css`).forEach(function (file) { shell.mv(file, file.replace('.css', '.scss')); }); /* Inline templates & stylesheets */ shell.echo(`Inline templates & stylesheets`); shell.exec(`ng2-inline -b ${BUILD_DIR} --silent --relative --compress -o . \"${BUILD_DIR}/**/*.ts\"`);Note that you could skip the processing of
scssfiles if you don't have them.Then change all
ngc -p tsconfig-build.jsonwithngc -p ${BUILD_DIR}/tsconfig-build.jsonand finally remove thebuildfolder:shell.rm(`-Rf`, `${BUILD_DIR}`); -
tsconfig-build.jsfile:Change:
"node_modules/@angular/*"=>"../node_modules/@angular/*""dist"=>"../dist""node_modules/zone.js/dist/zone.js.d.ts"=>"../node_modules/zone.js/dist/zone.js.d.ts"
After bundling, make sure that your bundles &
metadata.jsoncontain the inline templates & styles.
If you want test your components that use external templates & stylesheets, you must also update the webpack configuration in karma.conf.js file.