2 * Adds the pug-loader inside Angular CLI's webpack config, if not there yet.
3 * @see https://github.com/danguilherme/ng-cli-pug-loader
5 const fs = require('fs');
6 const commonCliConfig = 'node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/common.js';
7 const pugRules = ` { test: /\.(pug|jade)$/, exclude: /\.(include|partial)\.(pug|jade)$/, use: [ { loader: 'apply-loader' }, { loader: 'pug-loader' } ] }, { test: /\.(include|partial)\.(pug|jade)$/, loader: 'pug-loader' },`;
9 fs.readFile(commonCliConfig, (err, data) => {
12 const configText = data.toString();
13 // make sure we don't add the rule if it already exists
14 if (configText.indexOf(pugRules) > -1) { return; }
16 // Insert the pug webpack rule
17 const position = configText.indexOf('rules: [') + 8;
18 const output = [configText.slice(0, position), pugRules, configText.slice(position)].join('');
19 const file = fs.openSync(commonCliConfig, 'r+');
20 fs.writeFile(file, output, error => {
22 console.error("An error occurred while overwriting Angular CLI's Webpack config");
24 fs.close(file, () => {});