Skip to content
This repository was archived by the owner on Aug 7, 2021. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2de4c68

Browse files
Fatmerosen-vladimirov
authored andcommittedJun 19, 2019
feat: introduce webpack only workflow (#882)
* fix: don't provide fake paths to the {N} CLI - relative to the `app` folder * feat: remove not needed hooks * feat: remove webpack compiler logic * fix: don't copy app_resources to the platforms folder * fix: respect --env.verbose * feat: respect production mode based on release option Implements: #911 * fix: watch platform specific files from node_modules Rel to: NativeScript/nativescript-cli#4480 * fix: don't emit absolute webpack's runtime files Previously we needed to emit files with full paths as {N} CLI relies on this and expected them in a such format. With the changes for "webpack-only" mode, {N} CLI expects only relative paths. So we need to fix this in order to ensure that runtime.js file will not be transferred on device on change in hmr mode. * fix: don't process runtime.js files We needed a special processing for `runtime.js` files as we excluded them when transferring the files on device. As the CLI is filtering and emit only hot-update files we don't need this logic anymore. * fix: emit runtime files and entry point files * fix: update webpack config files of demo apps * fix: don't use short imports in demo apps * fix: update dependencies of demo apps so they are compatible with 6.0 release
1 parent fc4e415 commit 2de4c68

32 files changed

+163
-622
lines changed
 

‎demo/AngularApp/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@
4242
"mocha": "~5.2.0",
4343
"mochawesome": "~3.1.2",
4444
"nativescript-dev-appium": "next",
45-
"nativescript-dev-sass": "next",
46-
"nativescript-dev-typescript": "next",
4745
"nativescript-dev-webpack": "next",
46+
"node-sass": "^4.12.0",
4847
"typescript": "~3.4.5"
4948
},
5049
"scripts": {

‎demo/AngularApp/webpack.config.js

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,15 @@ module.exports = env => {
4444

4545
// You can provide the following flags when running 'tns run android|ios'
4646
aot, // --env.aot
47-
snapshot, // --env.snapshot
47+
snapshot, // --env.snapshot,
48+
production, // --env.production
4849
uglify, // --env.uglify
4950
report, // --env.report
5051
sourceMap, // --env.sourceMap
5152
hiddenSourceMap, // --env.hiddenSourceMap
5253
hmr, // --env.hmr,
5354
unitTesting, // --env.unitTesting
55+
verbose, // --env.verbose
5456
} = env;
5557

5658
const isAnySourceMapEnabled = !!sourceMap || !!hiddenSourceMap;
@@ -60,8 +62,9 @@ module.exports = env => {
6062
const tsConfigName = "tsconfig.tns.json";
6163
const entryModule = `${nsWebpack.getEntryModule(appFullPath, platform)}.ts`;
6264
const entryPath = `.${sep}${entryModule}`;
63-
const entries = { bundle: entryPath, application: "./application.android" };
64-
if (platform === "ios") {
65+
const entries = { bundle: entryPath };
66+
const areCoreModulesExternal = Array.isArray(env.externals) && env.externals.some(e => e.indexOf("tns-core-modules") > -1);
67+
if (platform === "ios" && !areCoreModulesExternal) {
6568
entries["tns_modules/tns-core-modules/inspector_modules"] = "inspector_modules";
6669
};
6770

@@ -101,8 +104,14 @@ module.exports = env => {
101104

102105
let sourceMapFilename = nsWebpack.getSourceMapFilename(hiddenSourceMap, __dirname, dist);
103106

107+
const itemsToClean = [`${dist}/**/*`];
108+
if (platform === "android") {
109+
itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "src", "main", "assets", "snapshots")}`);
110+
itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "build", "configurations", "nativescript-android-snapshot")}`);
111+
}
112+
104113
const config = {
105-
mode: uglify ? "production" : "development",
114+
mode: production ? "production" : "development",
106115
context: appFullPath,
107116
externals,
108117
watchOptions: {
@@ -257,7 +266,7 @@ module.exports = env => {
257266
"process": undefined,
258267
}),
259268
// Remove all files from the out dir.
260-
new CleanWebpackPlugin([`${dist}/**/*`]),
269+
new CleanWebpackPlugin(itemsToClean, { verbose: !!verbose }),
261270
// Copy assets to out dir. Add your own globs as needed.
262271
new CopyWebpackPlugin([
263272
{ from: { glob: "fonts/**" } },
@@ -274,19 +283,6 @@ module.exports = env => {
274283
],
275284
};
276285

277-
// Copy the native app resources to the out dir
278-
// only if doing a full build (tns run/build) and not previewing (tns preview)
279-
if (!externals || externals.length === 0) {
280-
config.plugins.push(new CopyWebpackPlugin([
281-
{
282-
from: `${appResourcesFullPath}/${appResourcesPlatformDir}`,
283-
to: `${dist}/App_Resources/${appResourcesPlatformDir}`,
284-
context: projectRoot
285-
},
286-
]));
287-
}
288-
289-
290286
if (report) {
291287
// Generate report files for bundles content
292288
config.plugins.push(new BundleAnalyzerPlugin({

‎demo/JavaScriptApp/app/activity.android.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const frame = require("ui/frame");
1+
const frame = require("tns-core-modules/ui/frame");
22

33
const superProto = androidx.appcompat.app.AppCompatActivity.prototype;
44
androidx.appcompat.app.AppCompatActivity.extend("org.myApp.MainActivity", {

‎demo/JavaScriptApp/app/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ You can use this file to perform app-level initialization, but the primary
44
purpose of the file is to pass control to the app’s first module.
55
*/
66

7-
var application = require("application");
7+
var application = require("tns-core-modules/application");
88

99
application.start({ moduleName: "main-page" });
1010

‎demo/JavaScriptApp/app/main-page.android.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var frameModule = require("ui/frame");
1+
var frameModule = require("tns-core-modules/ui/frame");
22
var createViewModel = require("./main-view-model").createViewModel;
33

44
function onNavigatingTo(args) {

‎demo/JavaScriptApp/app/main-page.ios.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var frameModule = require("ui/frame");
1+
var frameModule = require("tns-core-modules/ui/frame");
22
var createViewModel = require("./main-view-model").createViewModel;
33

44
function onNavigatingTo(args) {

‎demo/JavaScriptApp/app/main-view-model.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var Observable = require("data/observable").Observable;
1+
var Observable = require("tns-core-modules/data/observable").Observable;
22

33
function getMessage(counter) {
44
if (counter <= 0) {

‎demo/JavaScriptApp/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@
2727
"mocha": "~5.2.0",
2828
"mochawesome": "~3.1.2",
2929
"nativescript-dev-appium": "next",
30-
"nativescript-dev-sass": "next",
3130
"nativescript-dev-webpack": "next",
32-
"node-sass": "^4.7.1"
31+
"node-sass": "4.12.0"
3332
},
3433
"scripts": {
3534
"setup": "npm pack ../../ && npm i -D nativescript-dev-webpack*.tgz",

‎demo/JavaScriptApp/webpack.config.js

Lines changed: 17 additions & 16 deletions
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 ExtraWatchWebpackPlugin = require('extra-watch-webpack-plugin');
89
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
910
const { NativeScriptWorkerPlugin } = require("nativescript-worker-loader/NativeScriptWorkerPlugin");
1011
const TerserPlugin = require("terser-webpack-plugin");
@@ -39,12 +40,14 @@ module.exports = env => {
3940

4041
// You can provide the following flags when running 'tns run android|ios'
4142
snapshot, // --env.snapshot
43+
production, // --env.production
4244
uglify, // --env.uglify
4345
report, // --env.report
4446
sourceMap, // --env.sourceMap
4547
hiddenSourceMap, // --env.hiddenSourceMap
4648
hmr, // --env.hmr,
47-
unitTesting, // --env.unitTesting
49+
unitTesting, // --env.unitTesting,
50+
verbose, // --env.verbose
4851
} = env;
4952

5053
const isAnySourceMapEnabled = !!sourceMap || !!hiddenSourceMap;
@@ -55,14 +58,21 @@ module.exports = env => {
5558
const entryModule = nsWebpack.getEntryModule(appFullPath, platform);
5659
const entryPath = `.${sep}${entryModule}.js`;
5760
const entries = { bundle: entryPath, application: "./application.android" };
58-
if (platform === "ios") {
61+
const areCoreModulesExternal = Array.isArray(env.externals) && env.externals.some(e => e.indexOf("tns-core-modules") > -1);
62+
if (platform === "ios" && !areCoreModulesExternal) {
5963
entries["tns_modules/tns-core-modules/inspector_modules"] = "inspector_modules";
6064
};
6165

6266
let sourceMapFilename = nsWebpack.getSourceMapFilename(hiddenSourceMap, __dirname, dist);
6367

68+
const itemsToClean = [`${dist}/**/*`];
69+
if (platform === "android") {
70+
itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "src", "main", "assets", "snapshots")}`);
71+
itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "build", "configurations", "nativescript-android-snapshot")}`);
72+
}
73+
6474
const config = {
65-
mode: uglify ? "production" : "development",
75+
mode: production ? "production" : "development",
6676
context: appFullPath,
6777
externals,
6878
watchOptions: {
@@ -208,7 +218,7 @@ module.exports = env => {
208218
"process": undefined,
209219
}),
210220
// Remove all files from the out dir.
211-
new CleanWebpackPlugin([`${dist}/**/*`]),
221+
new CleanWebpackPlugin(itemsToClean, { verbose: !!verbose }),
212222
// Copy assets to out dir. Add your own globs as needed.
213223
new CopyWebpackPlugin([
214224
{ from: { glob: "fonts/**" } },
@@ -226,21 +236,12 @@ module.exports = env => {
226236
}),
227237
// Does IPC communication with the {N} CLI to notify events when running in watch mode.
228238
new nsWebpack.WatchStateLoggerPlugin(),
239+
new ExtraWatchWebpackPlugin({
240+
files: [`node_modules/**/*.${platform}.js`]
241+
})
229242
],
230243
};
231244

232-
// Copy the native app resources to the out dir
233-
// only if doing a full build (tns run/build) and not previewing (tns preview)
234-
if (!externals || externals.length === 0) {
235-
config.plugins.push(new CopyWebpackPlugin([
236-
{
237-
from: `${appResourcesFullPath}/${appResourcesPlatformDir}`,
238-
to: `${dist}/App_Resources/${appResourcesPlatformDir}`,
239-
context: projectRoot
240-
},
241-
]));
242-
}
243-
244245
if (report) {
245246
// Generate report files for bundles content
246247
config.plugins.push(new BundleAnalyzerPlugin({

‎demo/TypeScriptApp/app/activity.android.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {setActivityCallbacks, AndroidActivityCallbacks} from "ui/frame";
1+
import {setActivityCallbacks, AndroidActivityCallbacks} from "tns-core-modules/ui/frame";
22

33
@JavaProxy("org.myApp.MainActivity")
44
class Activity extends androidx.appcompat.app.AppCompatActivity {

‎demo/TypeScriptApp/app/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ You can use this file to perform app-level initialization, but the primary
44
purpose of the file is to pass control to the app’s first module.
55
*/
66

7-
import * as app from 'application';
7+
import * as app from 'tns-core-modules/application';
88

99
app.start({ moduleName: 'main-page' });
1010

‎demo/TypeScriptApp/app/main-page.ios.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ a code-behind file. The code-behind is a great place to place your view
44
logic, and to set up your page’s data binding.
55
*/
66

7-
import { EventData } from 'data/observable';
8-
import { Page } from 'ui/page';
7+
import { EventData } from 'tns-core-modules/data/observable';
8+
import { Page } from 'tns-core-modules/ui/page';
99
import { HelloWorldModel } from './main-view-model';
10-
import { Label } from 'ui/label';
11-
import * as frameModule from 'ui/frame';
10+
import { Label } from 'tns-core-modules/ui/label';
11+
import * as frameModule from 'tns-core-modules/ui/frame';
1212

1313
// Event handler for Page "navigatingTo" event attached in main-page.xml
1414
export function onNavigatingTo(args: EventData) {

‎demo/TypeScriptApp/app/main-view-model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Observable} from 'data/observable';
1+
import {Observable} from 'tns-core-modules/data/observable';
22

33
export class HelloWorldModel extends Observable {
44

‎demo/TypeScriptApp/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@
2727
"mocha": "~5.2.0",
2828
"mochawesome": "~3.1.2",
2929
"nativescript-dev-appium": "next",
30-
"nativescript-dev-sass": "next",
31-
"nativescript-dev-typescript": "next",
3230
"nativescript-dev-webpack": "next",
33-
"typescript": "~3.2.2"
31+
"typescript": "~3.2.2",
32+
"node-sass": "^4.12.0"
3433
},
3534
"scripts": {
3635
"setup": "npm pack ../../ && npm i -D nativescript-dev-webpack*.tgz",

‎demo/TypeScriptApp/webpack.config.js

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const nativescriptTarget = require("nativescript-dev-webpack/nativescript-target
66
const CleanWebpackPlugin = require("clean-webpack-plugin");
77
const CopyWebpackPlugin = require("copy-webpack-plugin");
88
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
9+
const ExtraWatchWebpackPlugin = require('extra-watch-webpack-plugin');
910
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
1011
const { NativeScriptWorkerPlugin } = require("nativescript-worker-loader/NativeScriptWorkerPlugin");
1112
const TerserPlugin = require("terser-webpack-plugin");
@@ -40,12 +41,14 @@ module.exports = env => {
4041

4142
// You can provide the following flags when running 'tns run android|ios'
4243
snapshot, // --env.snapshot
44+
production, // --env.production
4345
uglify, // --env.uglify
4446
report, // --env.report
4547
sourceMap, // --env.sourceMap
4648
hiddenSourceMap, // --env.hiddenSourceMap
4749
hmr, // --env.hmr,
48-
unitTesting, // --env.unitTesting
50+
unitTesting, // --env.unitTesting,
51+
verbose, // --env.verbose
4952
} = env;
5053
const isAnySourceMapEnabled = !!sourceMap || !!hiddenSourceMap;
5154
const externals = nsWebpack.getConvertedExternals(env.externals);
@@ -59,14 +62,21 @@ module.exports = env => {
5962

6063
const tsConfigPath = resolve(projectRoot, "tsconfig.tns.json");
6164

62-
if (platform === "ios") {
65+
const areCoreModulesExternal = Array.isArray(env.externals) && env.externals.some(e => e.indexOf("tns-core-modules") > -1);
66+
if (platform === "ios" && !areCoreModulesExternal) {
6367
entries["tns_modules/tns-core-modules/inspector_modules"] = "inspector_modules";
6468
};
6569

6670
let sourceMapFilename = nsWebpack.getSourceMapFilename(hiddenSourceMap, __dirname, dist);
6771

72+
const itemsToClean = [`${dist}/**/*`];
73+
if (platform === "android") {
74+
itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "src", "main", "assets", "snapshots")}`);
75+
itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "build", "configurations", "nativescript-android-snapshot")}`);
76+
}
77+
6878
const config = {
69-
mode: uglify ? "production" : "development",
79+
mode: production ? "production" : "development",
7080
context: appFullPath,
7181
externals,
7282
watchOptions: {
@@ -212,10 +222,13 @@ module.exports = env => {
212222
loader: "ts-loader",
213223
options: {
214224
configFile: tsConfigPath,
215-
transpileOnly: !!hmr,
225+
// https://github.com/TypeStrong/ts-loader/blob/ea2fcf925ec158d0a536d1e766adfec6567f5fb4/README.md#faster-builds
226+
// https://github.com/TypeStrong/ts-loader/blob/ea2fcf925ec158d0a536d1e766adfec6567f5fb4/README.md#hot-module-replacement
227+
transpileOnly: true,
216228
allowTsInNodeModules: true,
217229
compilerOptions: {
218-
sourceMap: isAnySourceMapEnabled
230+
sourceMap: isAnySourceMapEnabled,
231+
declaration: false
219232
}
220233
},
221234
}
@@ -229,7 +242,7 @@ module.exports = env => {
229242
"process": undefined,
230243
}),
231244
// Remove all files from the out dir.
232-
new CleanWebpackPlugin([`${dist}/**/*`]),
245+
new CleanWebpackPlugin(itemsToClean, { verbose: !!verbose }),
233246
// Copy assets to out dir. Add your own globs as needed.
234247
new CopyWebpackPlugin([
235248
{ from: { glob: "fonts/**" } },
@@ -246,21 +259,20 @@ module.exports = env => {
246259
}),
247260
// Does IPC communication with the {N} CLI to notify events when running in watch mode.
248261
new nsWebpack.WatchStateLoggerPlugin(),
262+
// https://github.com/TypeStrong/ts-loader/blob/ea2fcf925ec158d0a536d1e766adfec6567f5fb4/README.md#faster-builds
263+
// https://github.com/TypeStrong/ts-loader/blob/ea2fcf925ec158d0a536d1e766adfec6567f5fb4/README.md#hot-module-replacement
264+
new ForkTsCheckerWebpackPlugin({
265+
tsconfig: tsConfigPath,
266+
async: false,
267+
useTypescriptIncrementalApi: true,
268+
memoryLimit: 4096
269+
}),
270+
new ExtraWatchWebpackPlugin({
271+
files: [`node_modules/**/*.${platform}.ts`]
272+
})
249273
],
250274
};
251275

252-
// Copy the native app resources to the out dir
253-
// only if doing a full build (tns run/build) and not previewing (tns preview)
254-
if (!externals || externals.length === 0) {
255-
config.plugins.push(new CopyWebpackPlugin([
256-
{
257-
from: `${appResourcesFullPath}/${appResourcesPlatformDir}`,
258-
to: `${dist}/App_Resources/${appResourcesPlatformDir}`,
259-
context: projectRoot
260-
},
261-
]));
262-
}
263-
264276
if (report) {
265277
// Generate report files for bundles content
266278
config.plugins.push(new BundleAnalyzerPlugin({
@@ -285,12 +297,6 @@ module.exports = env => {
285297

286298
if (hmr) {
287299
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-
}));
294300
}
295301

296302

‎lib/after-prepare.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@ const { installSnapshotArtefacts } = require("../snapshot/android/project-snapsh
22
const { shouldSnapshot } = require("./utils");
33

44
module.exports = function (hookArgs) {
5-
const env = hookArgs.env || {};
6-
env.hmr = hookArgs.appFilesUpdaterOptions.useHotModuleReload;
5+
const env = hookArgs.prepareData.env || {};
76
const shouldSnapshotOptions = {
8-
platform: hookArgs.platform,
9-
bundle: hookArgs.appFilesUpdaterOptions.bundle,
10-
release: hookArgs.appFilesUpdaterOptions.release
7+
platform: hookArgs.prepareData.platform,
8+
release: hookArgs.prepareData.release
119
};
1210

1311
if (env.snapshot && shouldSnapshot(shouldSnapshotOptions)) {
14-
installSnapshotArtefacts(hookArgs.projectData.projectDir);
12+
installSnapshotArtefacts(hookArgs.prepareData.projectDir);
1513
}
1614
}

‎lib/after-watch.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

‎lib/before-cleanApp.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

‎lib/before-prepareJS.js

Lines changed: 0 additions & 17 deletions
This file was deleted.

‎lib/before-preview-sync.js

Lines changed: 0 additions & 22 deletions
This file was deleted.

‎lib/before-shouldPrepare.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

‎lib/before-watch.js

Lines changed: 0 additions & 35 deletions
This file was deleted.

‎lib/before-watchPatterns.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

‎lib/compiler.js

Lines changed: 0 additions & 182 deletions
This file was deleted.

‎lib/utils.js

Lines changed: 2 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,18 @@
11
const os = require("os");
2-
3-
const {
4-
getAppPathFromProjectData,
5-
getAppResourcesPathFromProjectData,
6-
isAndroid,
7-
} = require("../projectHelpers");
8-
9-
const eventHandlers = {};
10-
11-
function debuggingEnabled(liveSyncService, projectDir) {
12-
const deviceDescriptors = liveSyncService.getLiveSyncDeviceDescriptors(projectDir);
13-
return deviceDescriptors.some(device => device.debugggingEnabled);
14-
}
15-
16-
function buildEnvData($projectData, platform, env) {
17-
const envData = Object.assign({},
18-
env,
19-
{ [platform.toLowerCase()]: true }
20-
);
21-
22-
const appPath = getAppPathFromProjectData($projectData);
23-
const appResourcesPath = getAppResourcesPathFromProjectData($projectData);
24-
Object.assign(envData,
25-
appPath && { appPath },
26-
appResourcesPath && { appResourcesPath }
27-
);
28-
29-
return envData;
30-
}
31-
32-
/**
33-
* Checks if there's a file in the following pattern 5e0326f3bb50f9f26cf0.hot-update.json
34-
* if yes this is a HMR update and remove all bundle files as we don't need them to be synced,
35-
* but only the update chunks
36-
*/
37-
function getUpdatedEmittedFiles(emittedFiles, webpackRuntimeFiles) {
38-
let fallbackFiles = [];
39-
let hotHash;
40-
if (emittedFiles.some(x => x.endsWith('.hot-update.json'))) {
41-
let result = emittedFiles.slice();
42-
const hotUpdateScripts = emittedFiles.filter(x => x.endsWith('.hot-update.js'));
43-
hotUpdateScripts.forEach(hotUpdateScript => {
44-
const { name, hash } = parseHotUpdateChunkName(hotUpdateScript);
45-
hotHash = hash;
46-
// remove bundle/vendor.js files if there's a bundle.XXX.hot-update.js or vendor.XXX.hot-update.js
47-
result = result.filter(file => file !== `${name}.js`);
48-
if (webpackRuntimeFiles && webpackRuntimeFiles.length) {
49-
// remove files containing only the Webpack runtime (e.g. runtime.js)
50-
result = result.filter(file => webpackRuntimeFiles.indexOf(file) === -1);
51-
}
52-
});
53-
//if applying of hot update fails, we must fallback to the full files
54-
fallbackFiles = emittedFiles.filter(file => result.indexOf(file) === -1);
55-
return { emittedFiles: result, fallbackFiles, hash: hotHash };
56-
}
57-
else {
58-
return { emittedFiles, fallbackFiles };
59-
}
60-
}
61-
62-
/**
63-
* Parse the filename of the hot update chunk.
64-
* @param name bundle.deccb264c01d6d42416c.hot-update.js
65-
* @returns { name: string, hash: string } { name: 'bundle', hash: 'deccb264c01d6d42416c' }
66-
*/
67-
function parseHotUpdateChunkName(name) {
68-
const matcher = /^(.+)\.(.+)\.hot-update/gm;
69-
const matches = matcher.exec(name);
70-
return {
71-
name: matches[1] || "",
72-
hash: matches[2] || "",
73-
};
74-
}
2+
const { isAndroid } = require("../projectHelpers");
753

764
function shouldSnapshot(config) {
775
const platformSupportsSnapshot = isAndroid(config.platform);
786
const osSupportsSnapshot = os.type() !== "Windows_NT";
797

80-
return config.bundle && config.release && platformSupportsSnapshot && osSupportsSnapshot;
81-
}
82-
83-
function addListener(eventEmitter, name, handler) {
84-
if (!eventHandlers[name]) {
85-
eventEmitter.on(name, handler);
86-
eventHandlers[name] = handler;
87-
}
88-
}
89-
90-
function removeListener(eventEmitter, name) {
91-
if (eventHandlers[name]) {
92-
eventEmitter.removeListener(name, eventHandlers[name]);
93-
delete eventHandlers[name];
94-
}
8+
return config.release && platformSupportsSnapshot && osSupportsSnapshot;
959
}
9610

9711
function convertToUnixPath(relativePath) {
9812
return relativePath.replace(/\\/g, "/");
9913
}
10014

10115
module.exports = {
102-
buildEnvData,
103-
debuggingEnabled,
10416
shouldSnapshot,
105-
getUpdatedEmittedFiles,
106-
parseHotUpdateChunkName,
107-
addListener,
108-
removeListener,
10917
convertToUnixPath
11018
};

‎package.json

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,45 +10,10 @@
1010
],
1111
"nativescript": {
1212
"hooks": [
13-
{
14-
"type": "before-prepareJSApp",
15-
"script": "lib/before-prepareJS.js",
16-
"inject": true
17-
},
18-
{
19-
"type": "before-cleanApp",
20-
"script": "lib/before-cleanApp.js",
21-
"inject": true
22-
},
23-
{
24-
"type": "before-watch",
25-
"script": "lib/before-watch.js",
26-
"inject": true
27-
},
28-
{
29-
"type": "after-watch",
30-
"script": "lib/after-watch.js",
31-
"inject": true
32-
},
33-
{
34-
"type": "before-watchPatterns",
35-
"script": "lib/before-watchPatterns.js",
36-
"inject": true
37-
},
38-
{
39-
"type": "before-shouldPrepare",
40-
"script": "lib/before-shouldPrepare.js",
41-
"inject": true
42-
},
4313
{
4414
"type": "after-prepare",
4515
"script": "lib/after-prepare.js",
4616
"inject": true
47-
},
48-
{
49-
"type": "before-preview-sync",
50-
"script": "lib/before-preview-sync",
51-
"inject": true
5217
}
5318
]
5419
},
@@ -79,6 +44,7 @@
7944
"copy-webpack-plugin": "~4.6.0",
8045
"css-loader": "~2.1.1",
8146
"fork-ts-checker-webpack-plugin": "1.3.0",
47+
"extra-watch-webpack-plugin": "1.0.3",
8248
"global-modules-path": "2.0.0",
8349
"minimatch": "3.0.4",
8450
"nativescript-hook": "0.2.4",

‎plugins/WatchStateLoggerPlugin.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { join } from "path";
2-
31
export enum messages {
42
compilationComplete = "Webpack compilation complete.",
53
startWatching = "Webpack compilation complete. Watching for file changes.",
@@ -31,24 +29,21 @@ export class WatchStateLoggerPlugin {
3129
console.log(messages.compilationComplete);
3230
}
3331

34-
const runtimeOnlyFiles = getWebpackRuntimeOnlyFiles(compilation, compiler.context);
3532
let emittedFiles = Object
3633
.keys(compilation.assets)
3734
.filter(assetKey => compilation.assets[assetKey].emitted);
3835

39-
// provide fake paths to the {N} CLI - relative to the 'app' folder
40-
// in order to trigger the livesync process
41-
const emittedFilesFakePaths = emittedFiles
42-
.map(file => join(compiler.context, file));
36+
const webpackRuntimeFiles = getWebpackRuntimeOnlyFiles(compilation);
37+
const entryPointFiles = getEntryPointFiles(compilation);
4338

4439
process.send && process.send(messages.compilationComplete, error => null);
4540
// Send emitted files so they can be LiveSynced if need be
46-
process.send && process.send({ emittedFiles: emittedFilesFakePaths, webpackRuntimeFiles: runtimeOnlyFiles }, error => null);
41+
process.send && process.send({ emittedFiles, webpackRuntimeFiles, entryPointFiles }, error => null);
4742
});
4843
}
4944
}
5045

51-
function getWebpackRuntimeOnlyFiles(compilation, basePath) {
46+
function getWebpackRuntimeOnlyFiles(compilation) {
5247
let runtimeOnlyFiles = [];
5348
try {
5449
runtimeOnlyFiles = [].concat(...Array.from<any>(compilation.entrypoints.values())
@@ -57,13 +52,32 @@ function getWebpackRuntimeOnlyFiles(compilation, basePath) {
5752
.filter(runtimeChunk => !!runtimeChunk && runtimeChunk.preventIntegration)
5853
.map(runtimeChunk => runtimeChunk.files))
5954
// get only the unique files in case of "single" runtime (e.g. runtime.js)
60-
.filter((value, index, self) => self.indexOf(value) === index)
61-
// convert to absolute paths
62-
.map(fileName => join(basePath, fileName));
55+
.filter((value, index, self) => self.indexOf(value) === index);
6356
} catch (e) {
6457
// breaking change in the Webpack API
6558
console.log("Warning: Unable to find Webpack runtime files.");
6659
}
6760

6861
return runtimeOnlyFiles;
6962
}
63+
64+
function getEntryPointFiles(compilation) {
65+
const entryPointFiles = [];
66+
try {
67+
Array.from(compilation.entrypoints.values())
68+
.forEach((entrypoint: any) => {
69+
const entryChunk = entrypoint.chunks.find(chunk => chunk.name === entrypoint.options.name);
70+
if (entryChunk) {
71+
entryChunk.files.forEach(fileName => {
72+
if (fileName.indexOf("hot-update") === -1) {
73+
entryPointFiles.push(fileName);
74+
}
75+
});
76+
}
77+
});
78+
} catch (e) {
79+
console.log("Warning: Unable to find Webpack entry point files.");
80+
}
81+
82+
return entryPointFiles;
83+
}

‎projectHelpers.js

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@ const fs = require("fs");
33

44
const hook = require("nativescript-hook")(__dirname);
55

6-
const PROJECT_DATA_GETTERS = {
7-
appPath: "getAppDirectoryRelativePath",
8-
appResourcesPath: "getAppResourcesRelativeDirectoryPath",
9-
};
10-
116
const isTypeScript = ({ projectDir, packageJson } = {}) => {
127
packageJson = packageJson || getPackageJson(projectDir);
138

@@ -78,14 +73,6 @@ const getPackageJsonPath = projectDir => resolve(projectDir, "package.json");
7873
const isAndroid = platform => /android/i.test(platform);
7974
const isIos = platform => /ios/i.test(platform);
8075

81-
function getAppPathFromProjectData(data) {
82-
return safeGet(data, PROJECT_DATA_GETTERS.appPath);
83-
}
84-
85-
function getAppResourcesPathFromProjectData(data) {
86-
return safeGet(data, PROJECT_DATA_GETTERS.appResourcesPath);
87-
}
88-
8976
function safeGet(object, property, ...args) {
9077
if (!object) {
9178
return;
@@ -112,8 +99,6 @@ function convertSlashesInPath(modulePath) {
11299
const isWindows = process.platform.startsWith("win32");
113100

114101
module.exports = {
115-
getAppPathFromProjectData,
116-
getAppResourcesPathFromProjectData,
117102
getPackageJson,
118103
getProjectDir,
119104
isAndroid,

‎templates/webpack.angular.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,15 @@ module.exports = env => {
4343

4444
// You can provide the following flags when running 'tns run android|ios'
4545
aot, // --env.aot
46-
snapshot, // --env.snapshot
46+
snapshot, // --env.snapshot,
47+
production, // --env.production
4748
uglify, // --env.uglify
4849
report, // --env.report
4950
sourceMap, // --env.sourceMap
5051
hiddenSourceMap, // --env.hiddenSourceMap
5152
hmr, // --env.hmr,
5253
unitTesting, // --env.unitTesting
54+
verbose, // --env.verbose
5355
} = env;
5456

5557
const isAnySourceMapEnabled = !!sourceMap || !!hiddenSourceMap;
@@ -101,8 +103,14 @@ module.exports = env => {
101103

102104
let sourceMapFilename = nsWebpack.getSourceMapFilename(hiddenSourceMap, __dirname, dist);
103105

106+
const itemsToClean = [`${dist}/**/*`];
107+
if (platform === "android") {
108+
itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "src", "main", "assets", "snapshots")}`);
109+
itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "build", "configurations", "nativescript-android-snapshot")}`);
110+
}
111+
104112
const config = {
105-
mode: uglify ? "production" : "development",
113+
mode: production ? "production" : "development",
106114
context: appFullPath,
107115
externals,
108116
watchOptions: {
@@ -257,7 +265,7 @@ module.exports = env => {
257265
"process": undefined,
258266
}),
259267
// Remove all files from the out dir.
260-
new CleanWebpackPlugin([`${dist}/**/*`]),
268+
new CleanWebpackPlugin(itemsToClean, { verbose: !!verbose }),
261269
// Copy assets to out dir. Add your own globs as needed.
262270
new CopyWebpackPlugin([
263271
{ from: { glob: "fonts/**" } },
@@ -274,19 +282,6 @@ module.exports = env => {
274282
],
275283
};
276284

277-
// Copy the native app resources to the out dir
278-
// only if doing a full build (tns run/build) and not previewing (tns preview)
279-
if (!externals || externals.length === 0) {
280-
config.plugins.push(new CopyWebpackPlugin([
281-
{
282-
from: `${appResourcesFullPath}/${appResourcesPlatformDir}`,
283-
to: `${dist}/App_Resources/${appResourcesPlatformDir}`,
284-
context: projectRoot
285-
},
286-
]));
287-
}
288-
289-
290285
if (report) {
291286
// Generate report files for bundles content
292287
config.plugins.push(new BundleAnalyzerPlugin({

‎templates/webpack.javascript.js

Lines changed: 15 additions & 15 deletions
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 ExtraWatchWebpackPlugin = require('extra-watch-webpack-plugin');
89
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
910
const { NativeScriptWorkerPlugin } = require("nativescript-worker-loader/NativeScriptWorkerPlugin");
1011
const TerserPlugin = require("terser-webpack-plugin");
@@ -38,12 +39,14 @@ module.exports = env => {
3839

3940
// You can provide the following flags when running 'tns run android|ios'
4041
snapshot, // --env.snapshot
42+
production, // --env.production
4143
uglify, // --env.uglify
4244
report, // --env.report
4345
sourceMap, // --env.sourceMap
4446
hiddenSourceMap, // --env.hiddenSourceMap
4547
hmr, // --env.hmr,
46-
unitTesting, // --env.unitTesting
48+
unitTesting, // --env.unitTesting,
49+
verbose, // --env.verbose
4750
} = env;
4851

4952
const isAnySourceMapEnabled = !!sourceMap || !!hiddenSourceMap;
@@ -61,8 +64,14 @@ module.exports = env => {
6164

6265
let sourceMapFilename = nsWebpack.getSourceMapFilename(hiddenSourceMap, __dirname, dist);
6366

67+
const itemsToClean = [`${dist}/**/*`];
68+
if (platform === "android") {
69+
itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "src", "main", "assets", "snapshots")}`);
70+
itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "build", "configurations", "nativescript-android-snapshot")}`);
71+
}
72+
6473
const config = {
65-
mode: uglify ? "production" : "development",
74+
mode: production ? "production" : "development",
6675
context: appFullPath,
6776
externals,
6877
watchOptions: {
@@ -208,7 +217,7 @@ module.exports = env => {
208217
"process": undefined,
209218
}),
210219
// Remove all files from the out dir.
211-
new CleanWebpackPlugin([`${dist}/**/*`]),
220+
new CleanWebpackPlugin(itemsToClean, { verbose: !!verbose }),
212221
// Copy assets to out dir. Add your own globs as needed.
213222
new CopyWebpackPlugin([
214223
{ from: { glob: "fonts/**" } },
@@ -226,21 +235,12 @@ module.exports = env => {
226235
}),
227236
// Does IPC communication with the {N} CLI to notify events when running in watch mode.
228237
new nsWebpack.WatchStateLoggerPlugin(),
238+
new ExtraWatchWebpackPlugin({
239+
files: [`node_modules/**/*.${platform}.js`]
240+
})
229241
],
230242
};
231243

232-
// Copy the native app resources to the out dir
233-
// only if doing a full build (tns run/build) and not previewing (tns preview)
234-
if (!externals || externals.length === 0) {
235-
config.plugins.push(new CopyWebpackPlugin([
236-
{
237-
from: `${appResourcesFullPath}/${appResourcesPlatformDir}`,
238-
to: `${dist}/App_Resources/${appResourcesPlatformDir}`,
239-
context: projectRoot
240-
},
241-
]));
242-
}
243-
244244
if (report) {
245245
// Generate report files for bundles content
246246
config.plugins.push(new BundleAnalyzerPlugin({

‎templates/webpack.typescript.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const nativescriptTarget = require("nativescript-dev-webpack/nativescript-target
66
const CleanWebpackPlugin = require("clean-webpack-plugin");
77
const CopyWebpackPlugin = require("copy-webpack-plugin");
88
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
9+
const ExtraWatchWebpackPlugin = require('extra-watch-webpack-plugin');
910
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
1011
const { NativeScriptWorkerPlugin } = require("nativescript-worker-loader/NativeScriptWorkerPlugin");
1112
const TerserPlugin = require("terser-webpack-plugin");
@@ -39,12 +40,14 @@ module.exports = env => {
3940

4041
// You can provide the following flags when running 'tns run android|ios'
4142
snapshot, // --env.snapshot
43+
production, // --env.production
4244
uglify, // --env.uglify
4345
report, // --env.report
4446
sourceMap, // --env.sourceMap
4547
hiddenSourceMap, // --env.hiddenSourceMap
4648
hmr, // --env.hmr,
47-
unitTesting, // --env.unitTesting
49+
unitTesting, // --env.unitTesting,
50+
verbose, // --env.verbose
4851
} = env;
4952
const isAnySourceMapEnabled = !!sourceMap || !!hiddenSourceMap;
5053
const externals = nsWebpack.getConvertedExternals(env.externals);
@@ -65,8 +68,14 @@ module.exports = env => {
6568

6669
let sourceMapFilename = nsWebpack.getSourceMapFilename(hiddenSourceMap, __dirname, dist);
6770

71+
const itemsToClean = [`${dist}/**/*`];
72+
if (platform === "android") {
73+
itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "src", "main", "assets", "snapshots")}`);
74+
itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "build", "configurations", "nativescript-android-snapshot")}`);
75+
}
76+
6877
const config = {
69-
mode: uglify ? "production" : "development",
78+
mode: production ? "production" : "development",
7079
context: appFullPath,
7180
externals,
7281
watchOptions: {
@@ -232,7 +241,7 @@ module.exports = env => {
232241
"process": undefined,
233242
}),
234243
// Remove all files from the out dir.
235-
new CleanWebpackPlugin([`${dist}/**/*`]),
244+
new CleanWebpackPlugin(itemsToClean, { verbose: !!verbose }),
236245
// Copy assets to out dir. Add your own globs as needed.
237246
new CopyWebpackPlugin([
238247
{ from: { glob: "fonts/**" } },
@@ -256,22 +265,13 @@ module.exports = env => {
256265
async: false,
257266
useTypescriptIncrementalApi: true,
258267
memoryLimit: 4096
268+
}),
269+
new ExtraWatchWebpackPlugin({
270+
files: [`node_modules/**/*.${platform}.ts`]
259271
})
260272
],
261273
};
262274

263-
// Copy the native app resources to the out dir
264-
// only if doing a full build (tns run/build) and not previewing (tns preview)
265-
if (!externals || externals.length === 0) {
266-
config.plugins.push(new CopyWebpackPlugin([
267-
{
268-
from: `${appResourcesFullPath}/${appResourcesPlatformDir}`,
269-
to: `${dist}/App_Resources/${appResourcesPlatformDir}`,
270-
context: projectRoot
271-
},
272-
]));
273-
}
274-
275275
if (report) {
276276
// Generate report files for bundles content
277277
config.plugins.push(new BundleAnalyzerPlugin({

‎templates/webpack.vue.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const { join, relative, resolve, sep } = require("path");
33
const webpack = require("webpack");
44
const CleanWebpackPlugin = require("clean-webpack-plugin");
55
const CopyWebpackPlugin = require("copy-webpack-plugin");
6+
const ExtraWatchWebpackPlugin = require('extra-watch-webpack-plugin');
67
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
78
const TerserPlugin = require("terser-webpack-plugin");
89

@@ -48,6 +49,7 @@ module.exports = env => {
4849
sourceMap, // --env.sourceMap
4950
hiddenSourceMap, // --env.hiddenSourceMap
5051
unitTesting, // --env.unitTesting
52+
verbose, // --env.verbose
5153
} = env;
5254

5355
const isAnySourceMapEnabled = !!sourceMap || !!hiddenSourceMap;
@@ -69,6 +71,12 @@ module.exports = env => {
6971

7072
let sourceMapFilename = nsWebpack.getSourceMapFilename(hiddenSourceMap, __dirname, dist);
7173

74+
const itemsToClean = [`${dist}/**/*`];
75+
if (platform === "android") {
76+
itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "src", "main", "assets", "snapshots")}`);
77+
itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "build", "configurations", "nativescript-android-snapshot")}`);
78+
}
79+
7280
const config = {
7381
mode: mode,
7482
context: appFullPath,
@@ -234,7 +242,7 @@ module.exports = env => {
234242
"TNS_ENV": JSON.stringify(mode)
235243
}),
236244
// Remove all files from the out dir.
237-
new CleanWebpackPlugin([`${dist}/**/*`]),
245+
new CleanWebpackPlugin(itemsToClean, { verbose: !!verbose }),
238246
// Copy assets to out dir. Add your own globs as needed.
239247
new CopyWebpackPlugin([
240248
{ from: { glob: "fonts/**" } },
@@ -251,6 +259,9 @@ module.exports = env => {
251259
}),
252260
// Does IPC communication with the {N} CLI to notify events when running in watch mode.
253261
new nsWebpack.WatchStateLoggerPlugin(),
262+
new ExtraWatchWebpackPlugin({
263+
files: [`node_modules/**/*.${platform}.ts`, `node_modules/**/*.${platform}.js`]
264+
})
254265
],
255266
};
256267

@@ -269,18 +280,6 @@ module.exports = env => {
269280
);
270281
}
271282

272-
// Copy the native app resources to the out dir
273-
// only if doing a full build (tns run/build) and not previewing (tns preview)
274-
if (!externals || externals.length === 0) {
275-
config.plugins.push(new CopyWebpackPlugin([
276-
{
277-
from: `${appResourcesFullPath}/${appResourcesPlatformDir}`,
278-
to: `${dist}/App_Resources/${appResourcesPlatformDir}`,
279-
context: projectRoot
280-
},
281-
]));
282-
}
283-
284283
if (report) {
285284
// Generate report files for bundles content
286285
config.plugins.push(new BundleAnalyzerPlugin({

0 commit comments

Comments
 (0)
This repository has been archived.