Skip to content
This repository has been archived by the owner on Aug 7, 2021. It is now read-only.

Commit

Permalink
fix(AoT): stop using require.context in Angular apps (#574)
Browse files Browse the repository at this point in the history
Calling `require.context` for the app directory adds a `ContextDependency` in webpack for it. That causes every change inside the app/ directory to emit a change event for the whole directory.
There's a logic in the TypeScript compiler host (from
[@ngtools/webpack](https://github.com/angular/angular-cli/blob/master/packages/ngtools/webpack/src/compiler_host.ts#L235))
that invalidates all files in the changed directory. The invalidation
removes all cached information for the virtual files produced by the
Angular AoT compilation (ngfactory files, etc.). Since these files are
not in the cache anymore, webpack tries to resolve them from the
filesystem and fails. The solution is to remove the `ContextDependency`
for the `app` dir, which should also make the rebuilds much faster.

fixes #566
  • Loading branch information
sis0k0 authored Jun 22, 2018
1 parent afe569d commit 23aaee9
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
6 changes: 5 additions & 1 deletion bundle-config-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ module.exports = function(source) {

if (loadCss) {
source = `
require("nativescript-dev-webpack/load-application-css")(${angular});
require("${
angular ?
'nativescript-dev-webpack/load-application-css-angular' :
'nativescript-dev-webpack/load-application-css-regular'
}")();
${source}
`;
}
Expand Down
7 changes: 7 additions & 0 deletions load-application-css-angular.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const loadCss = require("./load-application-css");

module.exports = function() {
loadCss(function() {
global.registerModule("./app.css", () => require("~/app"));
});
}
8 changes: 8 additions & 0 deletions load-application-css-regular.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const loadCss = require("./load-application-css");

module.exports = function() {
loadCss(function() {
const appCssContext = require.context("~/", false, /^\.\/app\.(css|scss|less|sass)$/);
global.registerWebpackModules(appCssContext);
});
}
10 changes: 2 additions & 8 deletions load-application-css.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
module.exports = function(isAngular) {
module.exports = function (loadModuleFn) {
const application = require("tns-core-modules/application");
require("tns-core-modules/ui/styling/style-scope");

if (isAngular) {
global.registerModule("./app.css", () => require("~/app"));
} else {
const appCssContext = require.context("~/", false, /^\.\/app\.(css|scss|less|sass)$/);
global.registerWebpackModules(appCssContext);
}
loadModuleFn();

application.loadAppCss();
}

8 changes: 7 additions & 1 deletion plugins/NativeScriptSnapshotPlugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@ exports.NativeScriptSnapshotPlugin = (function() {

let snapshotEntryContent = "";
if (includeApplicationCss) {
snapshotEntryContent += `require("nativescript-dev-webpack/load-application-css")(${options.angular});`;
snapshotEntryContent += `
require("${
options.angular ?
'nativescript-dev-webpack/load-application-css-angular' :
'nativescript-dev-webpack/load-application-css-regular'
}")();
`;
}
snapshotEntryContent += [ ...requireModules, ...internalRequireModules]
.map(mod => `require('${mod}')`).join(";");
Expand Down

0 comments on commit 23aaee9

Please sign in to comment.