-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathangular.webpack.js
More file actions
86 lines (79 loc) · 2.92 KB
/
angular.webpack.js
File metadata and controls
86 lines (79 loc) · 2.92 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const { AngularCompilerPlugin } = require("@ngtools/webpack");
/**
* Custom Angular Webpack Configuration
*/
module.exports = (config, options) => {
return addPugSupport(config);
};
/**
* Add support for pug and pug partials, without requiring `ng add ng-cli-pug-loader`,
* which does deep string manipulation of node_modules methods and is thus more brittle.
*
* Note this requires creating a new AngularCompilerPlugin with modified options to
* set directTemplateLoading to false.
*
* If this plugin modification becomes undesirable in the future, the next best option is to
* still push rules for pug and pug partials, but remove the HTML raw-loader rule and the
* AngularCompilerPlugin modification. So in module.exports, simply `return addSimplePugSupport(config)`
* instead. Also see documentation for the addSimplePugSupport function.
*
* @param config
* @returns {*}
*/
const addPugSupport = (config) => {
config.module.rules.push({
test: /\.(pug|jade)$/,
exclude: /\.(include|partial)\.(pug|jade)$/,
use: [{ loader: "apply-loader" }, { loader: "pug-loader" }],
});
config.module.rules.push({
test: /\.(include|partial)\.(pug|jade)$/,
loader: "pug-loader",
});
config.module.rules.push({ test: /.html$/, use: [{ loader: "raw-loader" }] });
let pluginIndex = -1;
// find existing AngularCompilerPlugin
for (let iterator = 0; iterator < config.plugins.length; iterator++) {
if (
config.plugins[iterator].constructor &&
"AngularCompilerPlugin" === config.plugins[iterator].constructor.name
) {
pluginIndex = iterator;
break;
}
}
if (pluginIndex < 0) {
console.error(
"Could not find the configuration plugin AngularCompilerPlugin to set directTemplateLoading to false. Note Pug templating support may be broken."
);
} else {
// swap out existing AngularCompilerPlugin for a new AngularCompilerPlugin instance with modified options
const configuredOptions = config.plugins[pluginIndex]._options;
configuredOptions.directTemplateLoading = false;
config.plugins[pluginIndex] = new AngularCompilerPlugin(configuredOptions);
}
return config;
};
/**
* Add support for pug and pug partials, without requiring `ng add ng-cli-pug-loader`,
* which does deep string manipulation of node_modules methods and is thus more brittle.
*
* This method is simpler because it does not create a new AngularCompilerPlugin,
* but does require that when a pug file is used, you wrap the templateUrl in require,
* for example: `templateUrl: require("./example.component.pug")`.
*
* @param config
* @returns {*}
*/
const addSimplePugSupport = (config) => {
config.module.rules.push({
test: /\.(pug|jade)$/,
exclude: /\.(include|partial)\.(pug|jade)$/,
use: [{ loader: "apply-loader" }, { loader: "pug-loader" }],
});
config.module.rules.push({
test: /\.(include|partial)\.(pug|jade)$/,
loader: "pug-loader",
});
return config;
};