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

Commit 715e13b

Browse files
refactor: Refactor moduleId: __filename to moduleId: module.id, implement compat loader for webpack (#337)
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 3967d79 commit 715e13b

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-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

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
this.callback(null, noModuleIdsSource, map);
16+
};

templates/webpack.angular.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ module.exports = env => {
6868
{ test: /\.scss$/, exclude: /^.\/app\.scss$/, use: ["raw-loader", "resolve-url-loader", "sass-loader"] },
6969

7070
// Compile TypeScript files with ahead-of-time compiler.
71-
{ test: /.ts$/, loader: "@ngtools/webpack" },
71+
{ test: /.ts$/, use: ["nativescript-dev-webpack/moduleid-compat-loader", "@ngtools/webpack"] },
7272
],
7373
},
7474
plugins: [

0 commit comments

Comments
 (0)