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

Commit f48bd8c

Browse files
Dimitar Tachevdtopuzov
Dimitar Tachev
authored andcommitted
fix: handle the bundle-config-loader require.context change without breaking-changes for the end-users (#973)
1 parent 35d7385 commit f48bd8c

File tree

9 files changed

+63
-5
lines changed

9 files changed

+63
-5
lines changed

Diff for: bundle-config-loader.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
11
import * as unitTestingConfigLoader from "./unit-testing-config-loader";
22
import { loader } from "webpack";
33
import { getOptions } from "loader-utils";
4+
import * as escapeRegExp from "escape-string-regexp";
45

56
// Matches all source, markup and style files that are not in App_Resources
6-
const defaultMatch = /(?<!App_Resources.*)\.(xml|css|js|(?<!d\.)ts|scss)$/;
7+
const defaultMatch = "(?<!App_Resources.*)\.(xml|css|js|(?<!d\.)ts|scss)$";
78

89
const loader: loader.Loader = function (source, map) {
9-
const {
10+
let {
1011
angular = false,
1112
loadCss = true,
1213
unitTesting,
1314
projectRoot,
1415
appFullPath,
15-
registerModules = defaultMatch,
16+
registerModules,
17+
ignoredFiles = []
1618
} = getOptions(this);
1719

20+
if (!registerModules) {
21+
registerModules = defaultMatch;
22+
for (const key in ignoredFiles) {
23+
registerModules = `(?<!${escapeRegExp(ignoredFiles[key])})` + registerModules;
24+
}
25+
registerModules = new RegExp(registerModules);
26+
}
27+
1828
if (unitTesting) {
1929
source = unitTestingConfigLoader({ appFullPath, projectRoot, angular, rootPagesRegExp: registerModules });
2030
this.callback(null, source);

Diff for: demo/AngularApp/webpack.config.js

+2
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ module.exports = env => {
109109
itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "build", "configurations", "nativescript-android-snapshot")}`);
110110
}
111111

112+
nsWebpack.processAppComponents(appComponents, platform);
112113
const config = {
113114
mode: production ? "production" : "development",
114115
context: appFullPath,
@@ -213,6 +214,7 @@ module.exports = env => {
213214
unitTesting,
214215
appFullPath,
215216
projectRoot,
217+
ignoredFiles: nsWebpack.getUserDefinedEntries(entries, platform)
216218
}
217219
},
218220
].filter(loader => !!loader)

Diff for: demo/JavaScriptApp/webpack.config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ module.exports = env => {
6969
itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "build", "configurations", "nativescript-android-snapshot")}`);
7070
}
7171

72+
nsWebpack.processAppComponents(appComponents, platform);
7273
const config = {
7374
mode: production ? "production" : "development",
7475
context: appFullPath,
@@ -173,7 +174,7 @@ module.exports = env => {
173174
unitTesting,
174175
appFullPath,
175176
projectRoot,
176-
registerModules: /(?<!App_Resources.*)(?<!\.\/application)(?<!\.\/activity)\.(xml|css|js|(?<!d\.)ts|scss)$/
177+
ignoredFiles: nsWebpack.getUserDefinedEntries(entries, platform)
177178
}
178179
},
179180
].filter(loader => !!loader)

Diff for: demo/TypeScriptApp/webpack.config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ module.exports = env => {
7373
itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "build", "configurations", "nativescript-android-snapshot")}`);
7474
}
7575

76+
nsWebpack.processAppComponents(appComponents, platform);
7677
const config = {
7778
mode: production ? "production" : "development",
7879
context: appFullPath,
@@ -179,7 +180,7 @@ module.exports = env => {
179180
unitTesting,
180181
appFullPath,
181182
projectRoot,
182-
registerModules: /(?<!App_Resources.*)(?<!\.\/application)(?<!\.\/activity)\.(xml|css|js|(?<!d\.)ts|scss)$/
183+
ignoredFiles: nsWebpack.getUserDefinedEntries(entries, platform)
183184
}
184185
},
185186
].filter(loader => !!loader)

Diff for: index.js

+35
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,41 @@ exports.getConvertedExternals = (externals) => {
9898
return modifiedExternals;
9999
};
100100

101+
102+
/**
103+
* The `require.context` call in `bundle-config-loader` will ask the FS for files and
104+
* the PlatformFSPlugin will return files without `.${platform}`. The SplitChunksPlugin will
105+
* compare the `appComponents` with the files returned from the `PlatformFSPlugin` and when they
106+
* do not match because of the platform extension, it will duplicate the custom components
107+
* in `bundle` (activity.js - included by the `require.context` call in `bundle-config-loader`)
108+
* and `vendor` (activity.android.js - included by `android-app-components-loader` and `SplitChunksPlugin`).
109+
* We are post-processing the `appComponents` in order to unify the file names and avoid getting
110+
* a build-time SBG exception for duplicate native class definition.
111+
*/
112+
exports.processAppComponents = (appComponents, platform) => {
113+
for (const key in appComponents) {
114+
appComponents[key] = appComponents[key].replace(`.${platform}`, "");
115+
}
116+
};
117+
118+
/**
119+
* The `bundle-config-loader` needs this in order to skip the custom entries in its `require.context` call.
120+
* If we don't skip them, custom entries like custom Android application will be included in both `application.js`
121+
* (because its defined as an entry) and `bundle.js` (included by the `require.context` call in `bundle-config-loader`)
122+
* causing a build-time SBG exception for duplicate native class definition.
123+
* We are removing the extension in order to unify the file names with the `PlatformFSPlugin`.
124+
*/
125+
exports.getUserDefinedEntries = (entries, platform) => {
126+
const userDefinedEntries = [];
127+
for (const entry in entries) {
128+
if (entry !== "bundle" && entry !== "tns_modules/tns-core-modules/inspector_modules") {
129+
userDefinedEntries.push(entries[entry].replace(`.${platform}`, ""));
130+
}
131+
}
132+
133+
return userDefinedEntries;
134+
};
135+
101136
const sanitize = name => name
102137
.split("")
103138
.filter(char => /[a-zA-Z0-9]/.test(char))

Diff for: templates/webpack.angular.js

+2
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ module.exports = env => {
108108
itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "build", "configurations", "nativescript-android-snapshot")}`);
109109
}
110110

111+
nsWebpack.processAppComponents(appComponents, platform);
111112
const config = {
112113
mode: production ? "production" : "development",
113114
context: appFullPath,
@@ -212,6 +213,7 @@ module.exports = env => {
212213
unitTesting,
213214
appFullPath,
214215
projectRoot,
216+
ignoredFiles: nsWebpack.getUserDefinedEntries(entries, platform)
215217
}
216218
},
217219
].filter(loader => !!loader)

Diff for: templates/webpack.javascript.js

+3
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ module.exports = env => {
6868
itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "build", "configurations", "nativescript-android-snapshot")}`);
6969
}
7070

71+
72+
nsWebpack.processAppComponents(appComponents, platform);
7173
const config = {
7274
mode: production ? "production" : "development",
7375
context: appFullPath,
@@ -172,6 +174,7 @@ module.exports = env => {
172174
unitTesting,
173175
appFullPath,
174176
projectRoot,
177+
ignoredFiles: nsWebpack.getUserDefinedEntries(entries, platform)
175178
}
176179
},
177180
].filter(loader => !!loader)

Diff for: templates/webpack.typescript.js

+2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ module.exports = env => {
7272
itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "build", "configurations", "nativescript-android-snapshot")}`);
7373
}
7474

75+
nsWebpack.processAppComponents(appComponents, platform);
7576
const config = {
7677
mode: production ? "production" : "development",
7778
context: appFullPath,
@@ -178,6 +179,7 @@ module.exports = env => {
178179
unitTesting,
179180
appFullPath,
180181
projectRoot,
182+
ignoredFiles: nsWebpack.getUserDefinedEntries(entries, platform)
181183
}
182184
},
183185
].filter(loader => !!loader)

Diff for: templates/webpack.vue.js

+2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ module.exports = env => {
7575
itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "build", "configurations", "nativescript-android-snapshot")}`);
7676
}
7777

78+
nsWebpack.processAppComponents(appComponents, platform);
7879
const config = {
7980
mode: mode,
8081
context: appFullPath,
@@ -185,6 +186,7 @@ module.exports = env => {
185186
unitTesting,
186187
appFullPath,
187188
projectRoot,
189+
ignoredFiles: nsWebpack.getUserDefinedEntries(entries, platform)
188190
},
189191
},
190192
].filter(loader => Boolean(loader)),

0 commit comments

Comments
 (0)