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

Commit de10041

Browse files
committed
fix: unify the entry points handling and enable custom applications in android
1 parent 5f7f70a commit de10041

13 files changed

+128
-150
lines changed

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

+1-11
Original file line numberDiff line numberDiff line change
@@ -264,17 +264,7 @@ module.exports = env => {
264264
{ from: { glob: "**/*.jpg" } },
265265
{ from: { glob: "**/*.png" } },
266266
], { ignore: [`${relative(appPath, appResourcesFullPath)}/**`] }),
267-
// Generate a bundle starter script and activate it in package.json
268-
new nsWebpack.GenerateBundleStarterPlugin(
269-
// Don't include `runtime.js` when creating a snapshot. The plugin
270-
// configures the WebPack runtime to be generated inside the snapshot
271-
// module and no `runtime.js` module exist.
272-
(snapshot ? [] : ["./runtime"])
273-
.concat([
274-
"./vendor",
275-
"./bundle",
276-
])
277-
),
267+
new nsWebpack.GenerateNativeScriptEntryPointsPlugin("bundle"),
278268
// For instructions on how to set up workers with webpack
279269
// check out https://github.com/nativescript/worker-loader
280270
new NativeScriptWorkerPlugin(),

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

+2-11
Original file line numberDiff line numberDiff line change
@@ -215,17 +215,8 @@ module.exports = env => {
215215
{ from: { glob: "**/*.jpg" } },
216216
{ from: { glob: "**/*.png" } },
217217
], { ignore: [`${relative(appPath, appResourcesFullPath)}/**`] }),
218-
// Generate a bundle starter script and activate it in package.json
219-
new nsWebpack.GenerateBundleStarterPlugin(
220-
// Don't include `runtime.js` when creating a snapshot. The plugin
221-
// configures the WebPack runtime to be generated inside the snapshot
222-
// module and no `runtime.js` module exist.
223-
(snapshot ? [] : ["./runtime"])
224-
.concat([
225-
"./vendor",
226-
"./bundle",
227-
])
228-
),
218+
new nsWebpack.GenerateNativeScriptEntryPointsPlugin("bundle"),
219+
229220
// For instructions on how to set up workers with webpack
230221
// check out https://github.com/nativescript/worker-loader
231222
new NativeScriptWorkerPlugin(),

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

+13-12
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const nsWebpack = require("nativescript-dev-webpack");
55
const nativescriptTarget = require("nativescript-dev-webpack/nativescript-target");
66
const CleanWebpackPlugin = require("clean-webpack-plugin");
77
const CopyWebpackPlugin = require("copy-webpack-plugin");
8+
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
89
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
910
const { NativeScriptWorkerPlugin } = require("nativescript-worker-loader/NativeScriptWorkerPlugin");
1011
const TerserPlugin = require("terser-webpack-plugin");
@@ -55,6 +56,9 @@ module.exports = env => {
5556
const entryModule = nsWebpack.getEntryModule(appFullPath, platform);
5657
const entryPath = `.${sep}${entryModule}.ts`;
5758
const entries = { bundle: entryPath, application: "./application.android" };
59+
60+
const tsConfigPath = resolve(projectRoot, "tsconfig.tns.json");
61+
5862
if (platform === "ios") {
5963
entries["tns_modules/tns-core-modules/inspector_modules"] = "inspector_modules.js";
6064
};
@@ -207,7 +211,8 @@ module.exports = env => {
207211
use: {
208212
loader: "ts-loader",
209213
options: {
210-
configFile: "tsconfig.tns.json",
214+
configFile: tsConfigPath,
215+
transpileOnly: !!hmr,
211216
allowTsInNodeModules: true,
212217
compilerOptions: {
213218
sourceMap: isAnySourceMapEnabled
@@ -231,17 +236,7 @@ module.exports = env => {
231236
{ from: { glob: "**/*.jpg" } },
232237
{ from: { glob: "**/*.png" } },
233238
], { ignore: [`${relative(appPath, appResourcesFullPath)}/**`] }),
234-
// Generate a bundle starter script and activate it in package.json
235-
new nsWebpack.GenerateBundleStarterPlugin(
236-
// Don't include `runtime.js` when creating a snapshot. The plugin
237-
// configures the WebPack runtime to be generated inside the snapshot
238-
// module and no `runtime.js` module exist.
239-
(snapshot ? [] : ["./runtime"])
240-
.concat([
241-
"./vendor",
242-
"./bundle",
243-
])
244-
),
239+
new nsWebpack.GenerateNativeScriptEntryPointsPlugin("bundle"),
245240
// For instructions on how to set up workers with webpack
246241
// check out https://github.com/nativescript/worker-loader
247242
new NativeScriptWorkerPlugin(),
@@ -290,6 +285,12 @@ module.exports = env => {
290285

291286
if (hmr) {
292287
config.plugins.push(new webpack.HotModuleReplacementPlugin());
288+
289+
// With HMR ts-loader should run in `transpileOnly` mode,
290+
// so assure type-checking with fork-ts-checker-webpack-plugin
291+
config.plugins.push(new ForkTsCheckerWebpackPlugin({
292+
tsconfig: tsConfigPath
293+
}));
293294
}
294295

295296

Diff for: dependencyManager.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ You can now bundle your project by passing --bundle flag to NativeScript CLI com
1717
- tns build ios --bundle
1818
- tns run android --bundle
1919
- tns run ios --bundle
20-
You can also pass the "--env.uglify" flag to use UglifyJS for minification.
20+
You can also pass the "--env.uglify" flag to use Terser for minification.
2121
For more information check out https://docs.nativescript.org/tooling/bundling-with-webpack#bundling.
2222
`;
2323

Diff for: plugins/GenerateBundleStarterPlugin.js

+7-47
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,13 @@
1-
const { RawSource } = require("webpack-sources");
2-
const { getPackageJson } = require("../projectHelpers");
1+
const { GenerateNativeScriptEntryPointsPlugin } = require("./GenerateNativeScriptEntryPointsPlugin");
32

4-
exports.GenerateBundleStarterPlugin = (function() {
5-
function GenerateBundleStarterPlugin(bundles) {
6-
this.bundles = bundles;
7-
this.files = {};
3+
// backwards compatibility for <= 0.22 configs
4+
exports.GenerateBundleStarterPlugin = (function () {
5+
function GenerateBundleStarterPlugin() {
6+
this.entryPointsPlugin = new GenerateNativeScriptEntryPointsPlugin("bundle");
87
};
98

10-
GenerateBundleStarterPlugin.prototype.apply = function(compiler) {
11-
this.webpackContext = compiler.options.context;
12-
13-
compiler.hooks.emit.tapAsync("GenerateBundleStarterPlugin", (compilation, cb) => {
14-
this.addAsset(compilation, "package.json", this.generatePackageJson());
15-
this.addAsset(compilation, "starter.js", this.generateStarterModule());
16-
this.generateTnsJavaClasses(compilation);
17-
18-
cb();
19-
});
20-
}
21-
22-
GenerateBundleStarterPlugin.prototype.generateTnsJavaClasses = function (compilation) {
23-
const path = compilation.compiler.outputPath;
24-
const isAndroid = path.indexOf("android") > -1;
25-
26-
if (isAndroid && !compilation.assets["tns-java-classes.js"]) {
27-
this.addAsset(compilation, "tns-java-classes.js", "");
28-
}
29-
}
30-
31-
GenerateBundleStarterPlugin.prototype.generatePackageJson = function () {
32-
const packageJson = getPackageJson(this.webpackContext);
33-
packageJson.main = "starter";
34-
35-
return JSON.stringify(packageJson, null, 4);
36-
}
37-
38-
GenerateBundleStarterPlugin.prototype.generateStarterModule = function () {
39-
const moduleSource = this.bundles
40-
.map(bundle => `require("${bundle}")`)
41-
.join("\n");
42-
43-
return moduleSource;
44-
}
45-
46-
GenerateBundleStarterPlugin.prototype.addAsset = function(compilation, name, content) {
47-
if (this.files[name] !== content) {
48-
this.files[name] = content;
49-
compilation.assets[name] = new RawSource(content);
50-
}
9+
GenerateBundleStarterPlugin.prototype.apply = function (compiler) {
10+
this.entryPointsPlugin.apply(compiler);
5111
}
5212

5313
return GenerateBundleStarterPlugin;

Diff for: plugins/GenerateNativeScriptEntryPointsPlugin.js

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
const { RawSource } = require("webpack-sources");
2+
const { getPackageJson } = require("../projectHelpers");
3+
const { SNAPSHOT_ENTRY_MODULE } = require("./NativeScriptSnapshotPlugin");
4+
5+
exports.GenerateNativeScriptEntryPointsPlugin = (function () {
6+
function GenerateNativeScriptEntryPointsPlugin(appEntryName) {
7+
this.appEntryName = appEntryName;
8+
this.files = {};
9+
};
10+
11+
GenerateNativeScriptEntryPointsPlugin.prototype.apply = function (compiler) {
12+
this.webpackContext = compiler.options.context;
13+
14+
compiler.hooks.emit.tapAsync("GenerateNativeScriptEntryPointsPlugin", (compilation, cb) => {
15+
compilation.entrypoints.forEach(entryPoint => {
16+
this.generateEntryFile(compilation, entryPoint);
17+
});
18+
this.addAsset(compilation, "package.json", this.generatePackageJson());
19+
this.generateTnsJavaClasses(compilation);
20+
21+
cb();
22+
});
23+
}
24+
25+
GenerateNativeScriptEntryPointsPlugin.prototype.generateTnsJavaClasses = function (compilation) {
26+
const path = compilation.compiler.outputPath;
27+
const isAndroid = path.indexOf("android") > -1;
28+
29+
if (isAndroid && !compilation.assets["tns-java-classes.js"]) {
30+
this.addAsset(compilation, "tns-java-classes.js", ""); 0
31+
}
32+
}
33+
34+
GenerateNativeScriptEntryPointsPlugin.prototype.generatePackageJson = function () {
35+
const packageJson = getPackageJson(this.webpackContext);
36+
packageJson.main = this.appEntryName;
37+
38+
return JSON.stringify(packageJson, null, 4);
39+
}
40+
41+
GenerateNativeScriptEntryPointsPlugin.prototype.generateEntryFile = function (compilation, entryPoint) {
42+
const entryPointFileName = `${entryPoint.options.name}.js`;
43+
if (entryPointFileName === SNAPSHOT_ENTRY_MODULE) {
44+
// Do not require the snapshot entry dependencies as the snapshot will fail.
45+
return;
46+
}
47+
48+
const requireDeps =
49+
entryPoint.chunks.map(chunk => {
50+
let requireChunkFiles = "";
51+
chunk.files.forEach(fileName => {
52+
if (fileName !== entryPointFileName) {
53+
requireChunkFiles += `require("./${fileName}");`;
54+
}
55+
});
56+
57+
return requireChunkFiles;
58+
}).join("\n");
59+
60+
const currentEntryPointContent = compilation.assets[entryPointFileName].source();
61+
compilation.assets[entryPointFileName] = new RawSource(`${requireDeps}${currentEntryPointContent}`);
62+
}
63+
64+
GenerateNativeScriptEntryPointsPlugin.prototype.addAsset = function (compilation, name, content) {
65+
if (this.files[name] !== content) {
66+
this.files[name] = content;
67+
compilation.assets[name] = new RawSource(content);
68+
}
69+
}
70+
71+
return GenerateNativeScriptEntryPointsPlugin;
72+
})();

Diff for: plugins/NativeScriptSnapshotPlugin/index.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ const schema = require("./options.json");
1212

1313
const SNAPSHOT_ENTRY_NAME = "snapshot-entry";
1414
const SNAPSHOT_ENTRY_MODULE = `${SNAPSHOT_ENTRY_NAME}.js`;
15+
exports.SNAPSHOT_ENTRY_MODULE = SNAPSHOT_ENTRY_MODULE;
1516

16-
exports.NativeScriptSnapshotPlugin = (function() {
17+
exports.NativeScriptSnapshotPlugin = (function () {
1718
function NativeScriptSnapshotPlugin(options) {
1819
NativeScriptSnapshotPlugin.validateSchema(options);
1920

@@ -30,14 +31,14 @@ exports.NativeScriptSnapshotPlugin = (function() {
3031
NativeScriptSnapshotPlugin.ensureSnapshotModuleEntry(this.options);
3132
}
3233

33-
NativeScriptSnapshotPlugin.removeLibraryTarget = function(webpackConfig) {
34+
NativeScriptSnapshotPlugin.removeLibraryTarget = function (webpackConfig) {
3435
const { output } = webpackConfig;
3536
if (output) {
3637
output.libraryTarget = undefined;
3738
}
3839
}
3940

40-
NativeScriptSnapshotPlugin.ensureSnapshotModuleEntry = function(options) {
41+
NativeScriptSnapshotPlugin.ensureSnapshotModuleEntry = function (options) {
4142
const { webpackConfig, requireModules, chunks, includeApplicationCss } = options;
4243
const internalRequireModules = this.getInternalRequireModules(webpackConfig.context);
4344

@@ -50,10 +51,10 @@ exports.NativeScriptSnapshotPlugin = (function() {
5051
options.angular ?
5152
'nativescript-dev-webpack/load-application-css-angular' :
5253
'nativescript-dev-webpack/load-application-css-regular'
53-
}")();
54+
}")();
5455
`;
5556
}
56-
snapshotEntryContent += [ ...requireModules, ...internalRequireModules]
57+
snapshotEntryContent += [...requireModules, ...internalRequireModules]
5758
.map(mod => `require('${mod}')`).join(";");
5859

5960
writeFileSync(snapshotEntryPath, snapshotEntryContent, { encoding: "utf8" });
@@ -68,12 +69,12 @@ exports.NativeScriptSnapshotPlugin = (function() {
6869
webpackConfig.optimization.runtimeChunk = { name: SNAPSHOT_ENTRY_NAME };
6970
}
7071

71-
NativeScriptSnapshotPlugin.getInternalRequireModules = function(webpackContext) {
72+
NativeScriptSnapshotPlugin.getInternalRequireModules = function (webpackContext) {
7273
const packageJson = getPackageJson(webpackContext);
7374
return (packageJson && packageJson["android"] && packageJson["android"]["requireModules"]) || [];
7475
}
7576

76-
NativeScriptSnapshotPlugin.validateSchema = function(options) {
77+
NativeScriptSnapshotPlugin.validateSchema = function (options) {
7778
if (!options.chunk && !options.chunks) {
7879
const error = NativeScriptSnapshotPlugin.extendError({ message: `No chunks specified!` });
7980
throw error;
@@ -87,7 +88,7 @@ exports.NativeScriptSnapshotPlugin = (function() {
8788
options.chunks.push(options.chunk);
8889
}
8990
} catch (error) {
90-
throw new Error(error.message);
91+
throw new Error(error.message);
9192
}
9293
}
9394

Diff for: plugins/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module.exports = Object.assign({},
22
require("./GenerateBundleStarterPlugin"),
3+
require("./GenerateNativeScriptEntryPointsPlugin"),
34
require("./NativeScriptSnapshotPlugin"),
45
require("./PlatformSuffixPlugin"),
56
require("./PlatformFSPlugin"),

Diff for: templates/webpack.angular.js

+1-11
Original file line numberDiff line numberDiff line change
@@ -263,17 +263,7 @@ module.exports = env => {
263263
{ from: { glob: "**/*.jpg" } },
264264
{ from: { glob: "**/*.png" } },
265265
], { ignore: [`${relative(appPath, appResourcesFullPath)}/**`] }),
266-
// Generate a bundle starter script and activate it in package.json
267-
new nsWebpack.GenerateBundleStarterPlugin(
268-
// Don't include `runtime.js` when creating a snapshot. The plugin
269-
// configures the WebPack runtime to be generated inside the snapshot
270-
// module and no `runtime.js` module exist.
271-
(snapshot ? [] : ["./runtime"])
272-
.concat([
273-
"./vendor",
274-
"./bundle",
275-
])
276-
),
266+
new nsWebpack.GenerateNativeScriptEntryPointsPlugin("bundle"),
277267
// For instructions on how to set up workers with webpack
278268
// check out https://github.com/nativescript/worker-loader
279269
new NativeScriptWorkerPlugin(),

0 commit comments

Comments
 (0)