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

Commit c7f2230

Browse files
committed
refactor: Refactor moduleId: __filename to moduleId: module.id, implement compat loader for webpack
The angular 5 framework will typecheck the moduleId is a string, while in webpack scenarios the moduleId is ignored and relative paths are handled by the angular compiler and the webpack loaders, module.id is number and fails the typeschecks. Implemented a loader that will strip the moduleId: module.id when compiled with webpack, the moduleId: module.id is still necessary for development without webpack. As a reminder if we manage to setup webpack for watch and debug, we can recommend removing all moduleId: module.id occurences and delete the loader.
1 parent 028089b commit c7f2230

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

demo/AngularApp/app/item/item-detail.component.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { ItemService } from "./item.service";
66

77
@Component({
88
selector: "ns-details",
9-
moduleId: __filename,
9+
moduleId: module.id,
1010
templateUrl: "./item-detail.component.html",
1111
})
1212
export class ItemDetailComponent implements OnInit {

demo/AngularApp/app/item/items.component.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { ItemService } from "./item.service";
55

66
@Component({
77
selector: "ns-items",
8-
moduleId: __filename,
8+
moduleId: module.id,
99
styleUrls: ["./items.component.scss"],
1010
templateUrl: "./items.component.html",
1111
})
@@ -19,4 +19,4 @@ export class ItemsComponent implements OnInit {
1919
ngOnInit(): void {
2020
this.items = this.itemService.getItems();
2121
}
22-
}
22+
}

moduleid-compat-loader.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* When building NativeScript angular apps without webpack (`tns run android`) the moduleId: module.id is necessary.
3+
* When building with webpack the angular compiler and webpack handle relative paths in the modules and no longer need moduleId
4+
* to be set, however webpack emits numbers for module.id and angular has typecheck for moduleId to be a string.
5+
*/
6+
module.exports = function (source, map) {
7+
this.cacheable();
8+
9+
// Strips occurences of `moduleId: module.id,`, since it is no longer needed for webpack builds
10+
const noModuleIdsSource = source.replace(/moduleId\:\s*module\.id\s*(\,)?/g, result =>
11+
// Try to preserve char count so sourcemaps may remain intact
12+
"/*" + result.substring(2, result.length - 2) + "*/"
13+
);
14+
15+
if (source != noModuleIdsSource) {
16+
console.log("Before:\n\n" + source + "\n\nAfter:\n\n" + noModuleIdsSource + "\n\n");
17+
}
18+
19+
this.callback(null, noModuleIdsSource, map);
20+
};

templates/webpack.angular.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ module.exports = env => {
6666
// SASS support
6767
{ test: /\.scss$/, use: ["raw-loader", "resolve-url-loader", "sass-loader"] },
6868
// Compile TypeScript files with ahead-of-time compiler.
69-
{ test: /.ts$/, loader: "@ngtools/webpack" },
69+
{ test: /.ts$/, use: ["nativescript-dev-webpack/moduleid-compat-loader", "@ngtools/webpack"] },
7070
],
7171
},
7272
plugins: [

0 commit comments

Comments
 (0)