-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathng-add-pug-loader.js
65 lines (54 loc) · 2.08 KB
/
ng-add-pug-loader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* 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/webpack/configs/common.js';
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" },';
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(pugRules) > -1) {
return;
}
// Insert the pug webpack rule
const position = configText.indexOf('rules: [') + 8;
const output = [configText.slice(0, position), pugRules, configText.slice(position)].join('');
const file = fs.openSync(commonCliConfig, 'r+');
fs.writeFile(file, output, (error) => {
if (error) console.error("An error occurred while overwriting Angular CLI's Webpack config");
fs.close(file, () => {});
});
});
/**
* Set's directTemplateLoading: false to allow custom pug template loader to work
* @see https://github.com/angular/angular-cli/issues/14534
*/
const typescriptCliConfig =
'node_modules/@angular-devkit/build-angular/src/webpack/configs/typescript.js';
fs.readFile(typescriptCliConfig, (err, data) => {
if (err) {
throw err;
}
const typescriptText = data.toString();
// check if needed to be set or already set
if (
typescriptText.indexOf('directTemplateLoading') === -1 ||
typescriptText.indexOf('directTemplateLoading: false,') > -1
) {
return;
}
// update the setting
const output = typescriptText.replace(
'directTemplateLoading: true,',
'directTemplateLoading: false,',
);
// rewrite the file
const file2 = fs.openSync(typescriptCliConfig, 'r+');
fs.writeFile(file2, output, (error) => {
if (error) console.error("An error occurred while overwriting Angular CLI's Webpack config");
fs.close(file2, () => {});
});
});