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

Commit 14de7e1

Browse files
author
Vasil Chimev
authored
fix: resolve appComponents and xml namespaces absolute paths on Windows (#578)
* fix: resolve appComponents absolute paths on Windows Fix #573. * fix: resolve xml namespaces absolute paths on Windows * chore(JavaScriptApp): add webpack.config.js file Clean up npm scripts. * test(JavaScriptApp): extend android application * test(JavaScriptApp): extend android activity * chore(TypeScriptApp): add webpack.config.js file Clean up npm scripts. * chore(TypeScriptApp): add platform declarations * test(TypeScriptApp): extend android application * test(TypeScriptApp): extend android activity * chore(AngularApp): add webpack.config.js file Clean up npm scripts. * chore(AngularApp): add platform declarations * test(AngularApp): extend android application * test(AngularApp): extend android activity * chore(AngularApp): update @angular/compiler-cli version * refactor(demos): minor tweaks * chore(JavaScript): remove unused variables * refactor: address comments * chore(e2e): update application activity to launch * refactor: remove tns-platform-declarations dependency * chore(e2e): remove application activity from capabilities The nativescript-dev-appium plugin gets it automatically, no need to declare it in the `appium.capabilities.json` file.
1 parent 6b0f56b commit 14de7e1

24 files changed

+925
-42
lines changed

Diff for: android-app-components-loader.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
module.exports = function(source) {
1+
const { convertSlashesInPath } = require("./projectHelpers");
2+
3+
module.exports = function (source) {
24
this.cacheable();
35
const { modules } = this.query;
4-
const imports = modules.map(m => `require("${m}");`).join("\n");
6+
const imports = modules.map(convertSlashesInPath)
7+
.map(m => `require("${m}");`).join("\n");
58
const augmentedSource = `
69
if (!global["__snapshot"]) {
710
${imports}

Diff for: demo/.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ tsconfig.aot.json
1616
vendor.js
1717
vendor.ts
1818

19-
webpack.config.js
19+
tsconfig.esm.json

Diff for: demo/AngularApp/app/App_Resources/Android/AndroidManifest.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
<uses-permission android:name="android.permission.INTERNET"/>
2020

2121
<application
22-
android:name="com.tns.NativeScriptApplication"
22+
android:name="org.myApp.Application"
2323
android:allowBackup="true"
2424
android:icon="@drawable/icon"
2525
android:label="@string/app_name"
2626
android:theme="@style/AppTheme">
2727

2828
<activity
29-
android:name="com.tns.NativeScriptActivity"
29+
android:name="org.myApp.MainActivity"
3030
android:label="@string/title_activity_kimera"
3131
android:configChanges="keyboardHidden|orientation|screenSize"
3232
android:theme="@style/LaunchScreenTheme">

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

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import {setActivityCallbacks, AndroidActivityCallbacks} from "ui/frame";
2+
3+
@JavaProxy("org.myApp.MainActivity")
4+
class Activity extends android.app.Activity {
5+
private _callbacks: AndroidActivityCallbacks;
6+
7+
protected onCreate(savedInstanceState: any): void { // android.os.Bundle
8+
if (!this._callbacks) {
9+
setActivityCallbacks(this);
10+
}
11+
12+
this._callbacks.onCreate(this, savedInstanceState, super.onCreate);
13+
}
14+
15+
protected onSaveInstanceState(outState: any): void { // android.os.Bundle
16+
this._callbacks.onSaveInstanceState(this, outState, super.onSaveInstanceState);
17+
}
18+
19+
protected onStart(): void {
20+
this._callbacks.onStart(this, super.onStart);
21+
}
22+
23+
protected onStop(): void {
24+
this._callbacks.onStop(this, super.onStop);
25+
}
26+
27+
protected onDestroy(): void {
28+
this._callbacks.onDestroy(this, super.onDestroy);
29+
}
30+
31+
public onBackPressed(): void {
32+
this._callbacks.onBackPressed(this, super.onBackPressed);
33+
}
34+
35+
public onRequestPermissionsResult(requestCode: number, permissions: Array<String>, grantResults: Array<number>): void {
36+
this._callbacks.onRequestPermissionsResult(this, requestCode, permissions, grantResults, undefined /*TODO: Enable if needed*/);
37+
}
38+
39+
protected onActivityResult(requestCode: number, resultCode: number, data: any): void { // android.content.Intent
40+
this._callbacks.onActivityResult(this, requestCode, resultCode, data, super.onActivityResult);
41+
}
42+
}

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

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@JavaProxy("org.myApp.Application")
2+
class Application extends android.app.Application {
3+
onCreate(): void {
4+
super.onCreate();
5+
}
6+
7+
protected attachBaseContext(baseContext: any) { // android.content.Context
8+
super.attachBaseContext(baseContext);
9+
}
10+
}

Diff for: demo/AngularApp/app/application.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare const android: any;

Diff for: demo/AngularApp/package.json

+1-8
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
},
3131
"devDependencies": {
3232
"@angular-devkit/build-angular": "~0.7.0-beta.1",
33-
"@angular/compiler-cli": "~6.0.0",
33+
"@angular/compiler-cli": "~6.1.0-beta.1",
3434
"@types/chai": "^4.0.2",
3535
"@types/mocha": "^2.2.41",
3636
"@types/node": "^7.0.5",
@@ -50,13 +50,6 @@
5050
"typescript": "~2.7.2"
5151
},
5252
"scripts": {
53-
"ns-bundle": "ns-bundle",
54-
"start-android-bundle": "npm run ns-bundle --android --run-app",
55-
"start-ios-bundle": "npm run ns-bundle --ios --run-app",
56-
"build-android-bundle": "npm run ns-bundle --android --build-app",
57-
"build-ios-bundle": "npm run ns-bundle --ios --build-app",
58-
"publish-ios-bundle": "npm run ns-bundle --ios --publish-app",
59-
"generate-android-snapshot": "generate-android-snapshot --targetArchs arm,arm64,ia32 --install",
6053
"e2e": "tsc -p e2e && mocha --opts ../config/mocha.opts --recursive e2e --appiumCapsLocation ../config/appium.capabilities.json",
6154
"compile-tests": "tsc -p e2e --watch"
6255
}

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

+270
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
1+
const { join, relative, resolve, sep } = require("path");
2+
3+
const webpack = require("webpack");
4+
const nsWebpack = require("nativescript-dev-webpack");
5+
const nativescriptTarget = require("nativescript-dev-webpack/nativescript-target");
6+
const { PlatformReplacementHost } = require("nativescript-dev-webpack/host/platform");
7+
const CleanWebpackPlugin = require("clean-webpack-plugin");
8+
const CopyWebpackPlugin = require("copy-webpack-plugin");
9+
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
10+
const { NativeScriptWorkerPlugin } = require("nativescript-worker-loader/NativeScriptWorkerPlugin");
11+
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
12+
const { AngularCompilerPlugin } = require("@ngtools/webpack");
13+
14+
module.exports = env => {
15+
// Add your custom Activities, Services and other Android app components here.
16+
const appComponents = [
17+
"tns-core-modules/ui/frame",
18+
"tns-core-modules/ui/frame/activity",
19+
resolve(__dirname, "app/activity.android.ts"),
20+
];
21+
22+
const platform = env && (env.android && "android" || env.ios && "ios");
23+
if (!platform) {
24+
throw new Error("You need to provide a target platform!");
25+
}
26+
27+
const extensions = ["tns", platform];
28+
const platformHost = new PlatformReplacementHost(extensions);
29+
30+
const projectRoot = __dirname;
31+
32+
// Default destination inside platforms/<platform>/...
33+
const dist = resolve(projectRoot, nsWebpack.getAppPath(platform, projectRoot));
34+
const appResourcesPlatformDir = platform === "android" ? "Android" : "iOS";
35+
36+
const {
37+
// The 'appPath' and 'appResourcesPath' values are fetched from
38+
// the nsconfig.json configuration file
39+
// when bundling with `tns run android|ios --bundle`.
40+
appPath = "app",
41+
appResourcesPath = "app/App_Resources",
42+
43+
// You can provide the following flags when running 'tns run android|ios'
44+
aot, // --env.aot
45+
snapshot, // --env.snapshot
46+
uglify, // --env.uglify
47+
report, // --env.report
48+
} = env;
49+
50+
const appFullPath = resolve(projectRoot, appPath);
51+
const appResourcesFullPath = resolve(projectRoot, appResourcesPath);
52+
53+
const entryModule = aot ?
54+
nsWebpack.getAotEntryModule(appFullPath) :
55+
`${nsWebpack.getEntryModule(appFullPath)}.ts`;
56+
const entryPath = `.${sep}${entryModule}`;
57+
58+
const config = {
59+
mode: uglify ? "production" : "development",
60+
context: appFullPath,
61+
watchOptions: {
62+
ignored: [
63+
appResourcesFullPath,
64+
// Don't watch hidden files
65+
"**/.*",
66+
]
67+
},
68+
target: nativescriptTarget,
69+
entry: {
70+
bundle: entryPath,
71+
application: "./application.android",
72+
},
73+
output: {
74+
pathinfo: false,
75+
path: dist,
76+
libraryTarget: "commonjs2",
77+
filename: "[name].js",
78+
globalObject: "global",
79+
},
80+
resolve: {
81+
extensions: [".ts", ".js", ".scss", ".css"],
82+
// Resolve {N} system modules from tns-core-modules
83+
modules: [
84+
resolve(__dirname, "node_modules/tns-core-modules"),
85+
resolve(__dirname, "node_modules"),
86+
"node_modules/tns-core-modules",
87+
"node_modules",
88+
],
89+
alias: {
90+
'~': appFullPath
91+
},
92+
symlinks: true
93+
},
94+
resolveLoader: {
95+
symlinks: false
96+
},
97+
node: {
98+
// Disable node shims that conflict with NativeScript
99+
"http": false,
100+
"timers": false,
101+
"setImmediate": false,
102+
"fs": "empty",
103+
"__dirname": false,
104+
},
105+
devtool: "none",
106+
optimization: {
107+
splitChunks: {
108+
cacheGroups: {
109+
vendor: {
110+
name: "vendor",
111+
chunks: "all",
112+
test: (module, chunks) => {
113+
const moduleName = module.nameForCondition ? module.nameForCondition() : '';
114+
return /[\\/]node_modules[\\/]/.test(moduleName) ||
115+
appComponents.some(comp => comp === moduleName);
116+
},
117+
enforce: true,
118+
},
119+
}
120+
},
121+
minimize: !!uglify,
122+
minimizer: [
123+
new UglifyJsPlugin({
124+
uglifyOptions: {
125+
parallel: true,
126+
cache: true,
127+
output: {
128+
comments: false,
129+
},
130+
compress: {
131+
// The Android SBG has problems parsing the output
132+
// when these options are enabled
133+
'collapse_vars': platform !== "android",
134+
sequences: platform !== "android",
135+
}
136+
}
137+
})
138+
],
139+
},
140+
module: {
141+
rules: [
142+
{
143+
test: new RegExp(entryPath),
144+
use: [
145+
// Require all Android app components
146+
platform === "android" && {
147+
loader: "nativescript-dev-webpack/android-app-components-loader",
148+
options: { modules: appComponents }
149+
},
150+
151+
{
152+
loader: "nativescript-dev-webpack/bundle-config-loader",
153+
options: {
154+
angular: true,
155+
loadCss: !snapshot, // load the application css if in debug mode
156+
}
157+
},
158+
].filter(loader => !!loader)
159+
},
160+
161+
{ test: /\.html$|\.xml$/, use: "raw-loader" },
162+
163+
// tns-core-modules reads the app.css and its imports using css-loader
164+
{
165+
test: /[\/|\\]app\.css$/,
166+
use: {
167+
loader: "css-loader",
168+
options: { minimize: false, url: false },
169+
}
170+
},
171+
{
172+
test: /[\/|\\]app\.scss$/,
173+
use: [
174+
{ loader: "css-loader", options: { minimize: false, url: false } },
175+
"sass-loader"
176+
]
177+
},
178+
179+
// Angular components reference css files and their imports using raw-loader
180+
{ test: /\.css$/, exclude: /[\/|\\]app\.css$/, use: "raw-loader" },
181+
{ test: /\.scss$/, exclude: /[\/|\\]app\.scss$/, use: ["raw-loader", "resolve-url-loader", "sass-loader"] },
182+
183+
// Compile TypeScript files with ahead-of-time compiler.
184+
{
185+
test: /.ts$/, use: [
186+
"nativescript-dev-webpack/moduleid-compat-loader",
187+
"@ngtools/webpack",
188+
]
189+
},
190+
191+
// Mark files inside `@angular/core` as using SystemJS style dynamic imports.
192+
// Removing this will cause deprecation warnings to appear.
193+
{
194+
test: /[\/\\]@angular[\/\\]core[\/\\].+\.js$/,
195+
parser: { system: true },
196+
},
197+
],
198+
},
199+
plugins: [
200+
// Define useful constants like TNS_WEBPACK
201+
new webpack.DefinePlugin({
202+
"global.TNS_WEBPACK": "true",
203+
}),
204+
// Remove all files from the out dir.
205+
new CleanWebpackPlugin([ `${dist}/**/*` ]),
206+
// Copy native app resources to out dir.
207+
new CopyWebpackPlugin([
208+
{
209+
from: `${appResourcesFullPath}/${appResourcesPlatformDir}`,
210+
to: `${dist}/App_Resources/${appResourcesPlatformDir}`,
211+
context: projectRoot
212+
},
213+
]),
214+
// Copy assets to out dir. Add your own globs as needed.
215+
new CopyWebpackPlugin([
216+
{ from: "fonts/**" },
217+
{ from: "**/*.jpg" },
218+
{ from: "**/*.png" },
219+
], { ignore: [`${relative(appPath, appResourcesFullPath)}/**`] }),
220+
// Generate a bundle starter script and activate it in package.json
221+
new nsWebpack.GenerateBundleStarterPlugin([
222+
"./vendor",
223+
"./bundle",
224+
]),
225+
// For instructions on how to set up workers with webpack
226+
// check out https://github.com/nativescript/worker-loader
227+
new NativeScriptWorkerPlugin(),
228+
229+
new AngularCompilerPlugin({
230+
host: platformHost,
231+
entryModule: resolve(appPath, "app.module#AppModule"),
232+
tsConfigPath: join(__dirname, "tsconfig.esm.json"),
233+
skipCodeGeneration: !aot,
234+
}),
235+
// Does IPC communication with the {N} CLI to notify events when running in watch mode.
236+
new nsWebpack.WatchStateLoggerPlugin(),
237+
],
238+
};
239+
240+
if (report) {
241+
// Generate report files for bundles content
242+
config.plugins.push(new BundleAnalyzerPlugin({
243+
analyzerMode: "static",
244+
openAnalyzer: false,
245+
generateStatsFile: true,
246+
reportFilename: resolve(projectRoot, "report", `report.html`),
247+
statsFilename: resolve(projectRoot, "report", `stats.json`),
248+
}));
249+
}
250+
251+
if (snapshot) {
252+
config.plugins.push(new nsWebpack.NativeScriptSnapshotPlugin({
253+
chunk: "vendor",
254+
angular: true,
255+
requireModules: [
256+
"reflect-metadata",
257+
"@angular/platform-browser",
258+
"@angular/core",
259+
"@angular/common",
260+
"@angular/router",
261+
"nativescript-angular/platform-static",
262+
"nativescript-angular/router",
263+
],
264+
projectRoot,
265+
webpackConfig: config,
266+
}));
267+
}
268+
269+
return config;
270+
};

0 commit comments

Comments
 (0)