Created
October 30, 2018 04:52
-
-
Save mvanderlee/6dcd56e01abc7e4ecd8530ce0ddb6ead to your computer and use it in GitHub Desktop.
ng-add-pug-loader with pug-plugin-ng
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Adds the pug-loader inside Angular CLI's webpack config, if not there yet. | |
* @see https://github.com/danguilherme/ng-cli-pug-loader | |
*/ | |
const fs = require('fs'); | |
const commonCliConfig = 'node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/common.js'; | |
const pugImport = 'const PugNgPlugin = require("pug-plugin-ng");'; | |
const pugRule = '{ test: /.pug$/, use: [ { loader: "html-loader" }, { loader: "pug-html-loader", options: {doctype: "html", plugins: PugNgPlugin} } ] },'; | |
fs.readFile(commonCliConfig, (err, data) => { | |
if (err) { throw err; } | |
const configText = data.toString(); | |
// make sure we don't add the rule if it already exists | |
if (configText.indexOf(pugRule) > -1) { return; } | |
// Insert the pug-plugin-ng import | |
let position = configText.indexOf('const '); | |
let output = [configText.slice(0, position), pugImport, configText.slice(position)].join(''); | |
// Insert the pug webpack rule | |
position = output.indexOf('rules: [') + 8; | |
output = [output.slice(0, position), pugRule, output.slice(position)].join(''); | |
const file = fs.openSync(commonCliConfig, 'r+'); | |
fs.writeFile(file, output); | |
fs.close(file); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment