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

Commit f2a3566

Browse files
authored
test: update demos (#705)
* Update `App_Resources` * Update extended activity * Update webpack configs
1 parent 4782bf6 commit f2a3566

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+119
-49
lines changed

Diff for: demo/AngularApp/app/activity.android.ts

+5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ import {setActivityCallbacks, AndroidActivityCallbacks} from "ui/frame";
22

33
@JavaProxy("org.myApp.MainActivity")
44
class Activity extends android.support.v7.app.AppCompatActivity {
5+
public isNativeScriptActivity: boolean;
56
private _callbacks: AndroidActivityCallbacks;
67

78
protected onCreate(savedInstanceState: any): void { // android.os.Bundle
9+
// Set isNativeScriptActivity in onCreate (as done in the original NativeScript activity code).
10+
// The JS constructor might not be called because the activity is created from Android.
11+
this.isNativeScriptActivity = true;
12+
813
if (!this._callbacks) {
914
setActivityCallbacks(this);
1015
}

Diff for: demo/AngularApp/tsconfig.tns.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "./tsconfig",
3+
"compilerOptions": {
4+
"module": "es2015",
5+
"moduleResolution": "node"
6+
}
7+
}

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

+29-18
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 nsWebpack = require("nativescript-dev-webpack");
55
const nativescriptTarget = require("nativescript-dev-webpack/nativescript-target");
6+
const { nsReplaceBootstrap } = require("nativescript-dev-webpack/transformers/ns-replace-bootstrap");
67
const CleanWebpackPlugin = require("clean-webpack-plugin");
78
const CopyWebpackPlugin = require("copy-webpack-plugin");
89
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
@@ -15,7 +16,7 @@ module.exports = env => {
1516
const appComponents = [
1617
"tns-core-modules/ui/frame",
1718
"tns-core-modules/ui/frame/activity",
18-
resolve(__dirname, "app/activity.android.ts"),
19+
resolve(__dirname, "app/activity.android.ts")
1920
];
2021

2122
const platform = env && (env.android && "android" || env.ios && "ios");
@@ -42,19 +43,31 @@ module.exports = env => {
4243
uglify, // --env.uglify
4344
report, // --env.report
4445
sourceMap, // --env.sourceMap
46+
hmr, // --env.hmr,
4547
} = env;
48+
const externals = (env.externals || []).map((e) => { // --env.externals
49+
return new RegExp(e + ".*");
50+
});
4651

4752
const appFullPath = resolve(projectRoot, appPath);
4853
const appResourcesFullPath = resolve(projectRoot, appResourcesPath);
4954

50-
const entryModule = aot ?
51-
nsWebpack.getAotEntryModule(appFullPath) :
52-
`${nsWebpack.getEntryModule(appFullPath)}.ts`;
55+
const entryModule = `${nsWebpack.getEntryModule(appFullPath)}.ts`;
5356
const entryPath = `.${sep}${entryModule}`;
5457

58+
const ngCompilerPlugin = new AngularCompilerPlugin({
59+
hostReplacementPaths: nsWebpack.getResolver([platform, "tns"]),
60+
platformTransformers: aot ? [nsReplaceBootstrap(() => ngCompilerPlugin)] : null,
61+
mainPath: resolve(appPath, entryModule),
62+
tsConfigPath: join(__dirname, "tsconfig.tns.json"),
63+
skipCodeGeneration: !aot,
64+
sourceMap: !!sourceMap,
65+
});
66+
5567
const config = {
5668
mode: uglify ? "production" : "development",
5769
context: appFullPath,
70+
externals,
5871
watchOptions: {
5972
ignored: [
6073
appResourcesFullPath,
@@ -109,7 +122,7 @@ module.exports = env => {
109122
test: (module, chunks) => {
110123
const moduleName = module.nameForCondition ? module.nameForCondition() : '';
111124
return /[\\/]node_modules[\\/]/.test(moduleName) ||
112-
appComponents.some(comp => comp === moduleName);
125+
appComponents.some(comp => comp === moduleName);
113126
},
114127
enforce: true,
115128
},
@@ -118,9 +131,9 @@ module.exports = env => {
118131
minimize: !!uglify,
119132
minimizer: [
120133
new UglifyJsPlugin({
134+
parallel: true,
135+
cache: true,
121136
uglifyOptions: {
122-
parallel: true,
123-
cache: true,
124137
output: {
125138
comments: false,
126139
},
@@ -177,9 +190,9 @@ module.exports = env => {
177190
{ test: /\.css$/, exclude: /[\/|\\]app\.css$/, use: "raw-loader" },
178191
{ test: /\.scss$/, exclude: /[\/|\\]app\.scss$/, use: ["raw-loader", "resolve-url-loader", "sass-loader"] },
179192

180-
// Compile TypeScript files with ahead-of-time compiler.
181193
{
182-
test: /.ts$/, use: [
194+
test: /(?:\.ngfactory\.js|\.ngstyle\.js|\.ts)$/,
195+
use: [
183196
"nativescript-dev-webpack/moduleid-compat-loader",
184197
"@ngtools/webpack",
185198
]
@@ -200,7 +213,7 @@ module.exports = env => {
200213
"process": undefined,
201214
}),
202215
// Remove all files from the out dir.
203-
new CleanWebpackPlugin([ `${dist}/**/*` ]),
216+
new CleanWebpackPlugin([`${dist}/**/*`]),
204217
// Copy native app resources to out dir.
205218
new CopyWebpackPlugin([
206219
{
@@ -223,19 +236,13 @@ module.exports = env => {
223236
// For instructions on how to set up workers with webpack
224237
// check out https://github.com/nativescript/worker-loader
225238
new NativeScriptWorkerPlugin(),
226-
227-
new AngularCompilerPlugin({
228-
hostReplacementPaths: nsWebpack.getResolver([platform, "tns"]),
229-
entryModule: resolve(appPath, "app.module#AppModule"),
230-
tsConfigPath: join(__dirname, "tsconfig.tns.json"),
231-
skipCodeGeneration: !aot,
232-
sourceMap: !!sourceMap,
233-
}),
239+
ngCompilerPlugin,
234240
// Does IPC communication with the {N} CLI to notify events when running in watch mode.
235241
new nsWebpack.WatchStateLoggerPlugin(),
236242
],
237243
};
238244

245+
239246
if (report) {
240247
// Generate report files for bundles content
241248
config.plugins.push(new BundleAnalyzerPlugin({
@@ -265,5 +272,9 @@ module.exports = env => {
265272
}));
266273
}
267274

275+
if (hmr) {
276+
config.plugins.push(new webpack.HotModuleReplacementPlugin());
277+
}
278+
268279
return config;
269280
};

Diff for: demo/JavaScriptApp/app/activity.android.js

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ const frame = require("ui/frame");
33
const superProto = android.support.v7.app.AppCompatActivity.prototype;
44
android.support.v7.app.AppCompatActivity.extend("org.myApp.MainActivity", {
55
onCreate: function(savedInstanceState) {
6+
// Set isNativeScriptActivity in onCreate.
7+
// The JS constructor might not be called because the activity is created from Android.
8+
this.isNativeScriptActivity = true;
9+
610
if(!this._callbacks) {
711
frame.setActivityCallbacks(this);
812
}

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

+28-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ module.exports = env => {
4141
uglify, // --env.uglify
4242
report, // --env.report
4343
sourceMap, // --env.sourceMap
44+
hmr, // --env.hmr,
4445
} = env;
46+
const externals = (env.externals || []).map((e) => { // --env.externals
47+
return new RegExp(e + ".*");
48+
});
4549

4650
const appFullPath = resolve(projectRoot, appPath);
4751
const appResourcesFullPath = resolve(projectRoot, appResourcesPath);
@@ -52,6 +56,7 @@ module.exports = env => {
5256
const config = {
5357
mode: uglify ? "production" : "development",
5458
context: appFullPath,
59+
externals,
5560
watchOptions: {
5661
ignored: [
5762
appResourcesFullPath,
@@ -116,9 +121,9 @@ module.exports = env => {
116121
minimize: !!uglify,
117122
minimizer: [
118123
new UglifyJsPlugin({
124+
parallel: true,
125+
cache: true,
119126
uglifyOptions: {
120-
parallel: true,
121-
cache: true,
122127
output: {
123128
comments: false,
124129
},
@@ -152,6 +157,21 @@ module.exports = env => {
152157
].filter(loader => !!loader)
153158
},
154159

160+
{
161+
test: /-page\.js$/,
162+
use: "nativescript-dev-webpack/script-hot-loader"
163+
},
164+
165+
{
166+
test: /\.(css|scss)$/,
167+
use: "nativescript-dev-webpack/style-hot-loader"
168+
},
169+
170+
{
171+
test: /\.(html|xml)$/,
172+
use: "nativescript-dev-webpack/markup-hot-loader"
173+
},
174+
155175
{ test: /\.(html|xml)$/, use: "nativescript-dev-webpack/xml-namespace-loader"},
156176

157177
{
@@ -165,7 +185,7 @@ module.exports = env => {
165185
{ loader: "css-loader", options: { minimize: false, url: false } },
166186
"sass-loader"
167187
]
168-
}
188+
},
169189
]
170190
},
171191
plugins: [
@@ -229,5 +249,10 @@ module.exports = env => {
229249
}));
230250
}
231251

252+
if (hmr) {
253+
config.plugins.push(new webpack.HotModuleReplacementPlugin());
254+
}
255+
256+
232257
return config;
233258
};

Diff for: demo/TypeScriptApp/app/activity.android.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ import {setActivityCallbacks, AndroidActivityCallbacks} from "ui/frame";
22

33
@JavaProxy("org.myApp.MainActivity")
44
class Activity extends android.support.v7.app.AppCompatActivity {
5+
public isNativeScriptActivity: boolean;
56
private _callbacks: AndroidActivityCallbacks;
6-
77
protected onCreate(savedInstanceState: any): void { // android.os.Bundle
8+
// Set isNativeScriptActivity in onCreate (as done in the original NativeScript activity code).
9+
// The JS constructor might not be called because the activity is created from Android.
10+
this.isNativeScriptActivity = true;
11+
812
if (!this._callbacks) {
913
setActivityCallbacks(this);
1014
}

Diff for: demo/TypeScriptApp/app/main-page.android.ts

+4-22
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,15 @@ 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';
9+
import { Label } from 'tns-core-modules/ui/label';
10+
import * as frameModule from 'tns-core-modules/ui/frame';
911
import { HelloWorldModel } from './main-view-model';
10-
import { Label } from 'ui/label';
11-
import * as frameModule from 'ui/frame';
1212

13-
// Event handler for Page "navigatingTo" event attached in main-page.xml
1413
export function onNavigatingTo(args: EventData) {
15-
/*
16-
This gets a reference this page’s <Page> UI component. You can
17-
view the API reference of the Page to see what’s available at
18-
https://docs.nativescript.org/api-reference/classes/_ui_page_.page.html
19-
*/
2014
let page = <Page>args.object;
21-
22-
/*
23-
A page’s bindingContext is an object that should be used to perform
24-
data binding between XML markup and TypeScript code. Properties
25-
on the bindingContext can be accessed using the {{ }} syntax in XML.
26-
In this example, the {{ message }} and {{ onTap }} bindings are resolved
27-
against the object returned by createViewModel().
28-
29-
You can learn more about data binding in NativeScript at
30-
https://docs.nativescript.org/core-concepts/data-binding.
31-
*/
3215
page.bindingContext = new HelloWorldModel();
33-
3416
page.getViewById<Label>("platform").text = "android";
3517
}
3618

Diff for: demo/TypeScriptApp/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@
3838
"e2e": "tsc -p e2e && mocha --opts ../config/mocha.opts --recursive e2e --appiumCapsLocation ../config/appium.capabilities.json",
3939
"compile-tests": "tsc -p e2e --watch"
4040
}
41-
}
41+
}

Diff for: demo/TypeScriptApp/tsconfig.tns.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "./tsconfig",
3+
"compilerOptions": {
4+
"module": "es2015",
5+
"moduleResolution": "node"
6+
}
7+
}

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

+29-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = env => {
1414
const appComponents = [
1515
"tns-core-modules/ui/frame",
1616
"tns-core-modules/ui/frame/activity",
17-
resolve(__dirname, "app/activity.android.ts"),
17+
resolve(__dirname, "app/activity.android.ts")
1818
];
1919

2020
const platform = env && (env.android && "android" || env.ios && "ios");
@@ -41,7 +41,11 @@ module.exports = env => {
4141
uglify, // --env.uglify
4242
report, // --env.report
4343
sourceMap, // --env.sourceMap
44+
hmr, // --env.hmr,
4445
} = env;
46+
const externals = (env.externals || []).map((e) => { // --env.externals
47+
return new RegExp(e + ".*");
48+
});
4549

4650
const appFullPath = resolve(projectRoot, appPath);
4751
const appResourcesFullPath = resolve(projectRoot, appResourcesPath);
@@ -52,6 +56,7 @@ module.exports = env => {
5256
const config = {
5357
mode: uglify ? "production" : "development",
5458
context: appFullPath,
59+
externals,
5560
watchOptions: {
5661
ignored: [
5762
appResourcesFullPath,
@@ -62,7 +67,7 @@ module.exports = env => {
6267
target: nativescriptTarget,
6368
entry: {
6469
bundle: entryPath,
65-
application: "./application.android",
70+
application: "./application.android"
6671
},
6772
output: {
6873
pathinfo: false,
@@ -118,9 +123,9 @@ module.exports = env => {
118123
minimize: !!uglify,
119124
minimizer: [
120125
new UglifyJsPlugin({
126+
parallel: true,
127+
cache: true,
121128
uglifyOptions: {
122-
parallel: true,
123-
cache: true,
124129
output: {
125130
comments: false,
126131
},
@@ -154,6 +159,21 @@ module.exports = env => {
154159
].filter(loader => !!loader)
155160
},
156161

162+
{
163+
test: /-page\.ts$/,
164+
use: "nativescript-dev-webpack/script-hot-loader"
165+
},
166+
167+
{
168+
test: /\.(css|scss)$/,
169+
use: "nativescript-dev-webpack/style-hot-loader"
170+
},
171+
172+
{
173+
test: /\.(html|xml)$/,
174+
use: "nativescript-dev-webpack/markup-hot-loader"
175+
},
176+
157177
{ test: /\.(html|xml)$/, use: "nativescript-dev-webpack/xml-namespace-loader"},
158178

159179
{
@@ -239,5 +259,10 @@ module.exports = env => {
239259
}));
240260
}
241261

262+
if (hmr) {
263+
config.plugins.push(new webpack.HotModuleReplacementPlugin());
264+
}
265+
266+
242267
return config;
243268
};

0 commit comments

Comments
 (0)