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

Commit 79057b2

Browse files
authored
Merge pull request #187 from NativeScript/vlaeva/postinstall-replace-snapshot
Vlaeva/postinstall replace snapshot
2 parents 20916b3 + 935f9c6 commit 79057b2

File tree

5 files changed

+88
-21
lines changed

5 files changed

+88
-21
lines changed

bin/update-ns-webpack

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
#!/usr/bin/env node
2-
const path = require("path");
3-
const fs = require("fs");
2+
const { resolve } = require("path");
43

5-
const helpers = require("../projectHelpers");
6-
const forceUpdateProjectDeps = require("../dependencyManager").forceUpdateProjectDeps;
4+
const { getPackageJson, writePackageJson } = require("../projectHelpers");
5+
const { forceUpdateProjectDeps } = require("../dependencyManager");
6+
const { editExistingProjectFiles } = require("../projectFilesManager");
77

8-
const PROJECT_DIR = path.resolve(__dirname, "../../../");
9-
const packageJson = helpers.getPackageJson(PROJECT_DIR);
8+
const PROJECT_DIR = resolve(__dirname, "../../../");
109

10+
console.info("Updating dev dependencies...");
11+
const packageJson = getPackageJson(PROJECT_DIR);
1112
const { deps } = forceUpdateProjectDeps(packageJson);
1213
packageJson.devDependencies = deps;
14+
writePackageJson(packageJson, PROJECT_DIR);
15+
16+
console.info("\nUpdating configuration files...");
17+
editExistingProjectFiles(PROJECT_DIR);
18+
19+
console.info("\nProject successfully updated! Don't forget to run `npm install`");
1320

14-
helpers.writePackageJson(packageJson, PROJECT_DIR);

index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,3 @@ function getAotEntry(entry) {
5151

5252
return existsSync(aotEntryPath) ? aotEntry : null;
5353
}
54-

installer.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ function install() {
1313
let packageJson = helpers.getPackageJson(PROJECT_DIR);
1414

1515
projectFilesManager.addProjectFiles(PROJECT_DIR, APP_DIR);
16-
projectFilesManager.editExistingProjectFiles(PROJECT_DIR);
1716

1817
let scripts = packageJson.scripts || {};
1918
scripts = npmScriptsManager.removeDeprecatedNpmScripts(scripts);

plugins/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@ module.exports = Object.assign({},
33
require("./NativeScriptJsonpPlugin"),
44
require("./NativeScriptSnapshotPlugin")
55
);
6-

projectFilesManager.js

Lines changed: 75 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,35 @@
11
const path = require("path");
22
const fs = require("fs");
33

4-
const helpers = require("./projectHelpers");
4+
const { isTypeScript, isAngular } = require("./projectHelpers");
5+
6+
const FRAME_MATCH = /(\s*)(require\("ui\/frame"\);)(\s*)(require\("ui\/frame\/activity"\);)/g;
7+
const SCOPED_FRAME = `
8+
if (!global["__snapshot"]) {
9+
// In case snapshot generation is enabled these modules will get into the bundle
10+
// but will not be required/evaluated.
11+
// The snapshot webpack plugin will add them to the tns-java-classes.js bundle file.
12+
// This way, they will be evaluated on app start as early as possible.
13+
$1\t$2$3\t$4
14+
}`;
15+
16+
const CONFIG_MATCH = /(exports = [^]+?)\s*return ({[^]+target:\s*nativescriptTarget[^]+?};)/;
17+
const CONFIG_REPLACE = `$1
18+
19+
const config = $2
20+
21+
if (env.snapshot) {
22+
plugins.push(new nsWebpack.NativeScriptSnapshotPlugin({
23+
chunk: "vendor",
24+
projectRoot: __dirname,
25+
webpackConfig: config,
26+
targetArchs: ["arm", "arm64"],
27+
tnsJavaClassesOptions: { packages: ["tns-core-modules" ] },
28+
useLibs: false
29+
}));
30+
}
31+
32+
return config;`;
533

634
function addProjectFiles(projectDir, appDir) {
735
const projectTemplates = getProjectTemplates(projectDir);
@@ -51,10 +79,10 @@ function copyTemplate(templateName, destinationPath) {
5179
function getProjectTemplates(projectDir) {
5280
let templates = {}
5381

54-
if (helpers.isAngular({projectDir})) {
82+
if (isAngular({projectDir})) {
5583
templates["webpack.angular.js"] = "webpack.config.js";
5684
templates["tsconfig.aot.json"] = "tsconfig.aot.json";
57-
} else if (helpers.isTypeScript({projectDir})) {
85+
} else if (isTypeScript({projectDir})) {
5886
templates["webpack.typescript.js"] = "webpack.config.js";
5987
} else {
6088
templates["webpack.javascript.js"] = "webpack.config.js";
@@ -69,7 +97,7 @@ function getAppTemplates(projectDir, appDir) {
6997
"vendor-platform.ios.ts": tsOrJs(projectDir, "vendor-platform.ios"),
7098
};
7199

72-
if (helpers.isAngular({projectDir})) {
100+
if (isAngular({projectDir})) {
73101
templates["vendor.angular.ts"] = tsOrJs(projectDir, "vendor");
74102
} else {
75103
templates["vendor.nativescript.ts"] = tsOrJs(projectDir, "vendor");
@@ -95,31 +123,67 @@ function editExistingProjectFiles(projectDir) {
95123
const webpackConfigPath = getFullPath(projectDir, "webpack.config.js");
96124
const webpackCommonPath = getFullPath(projectDir, "webpack.common.js");
97125

98-
editWebpackConfig(webpackConfigPath, replaceStyleUrlResolvePlugin);
99-
editWebpackConfig(webpackCommonPath, replaceStyleUrlResolvePlugin);
126+
const configChangeFunctions = [
127+
replaceStyleUrlResolvePlugin,
128+
addSnapshotPlugin,
129+
];
130+
131+
editFileContent(webpackConfigPath, ...configChangeFunctions);
132+
editFileContent(webpackCommonPath, ...configChangeFunctions);
133+
134+
const extension = isAngular({projectDir}) ? "ts" : "js";
135+
const vendorAndroidPath = getFullPath(
136+
projectDir,
137+
`app/vendor-platform.android.${extension}`
138+
);
139+
140+
editFileContent(vendorAndroidPath, addSnapshotToVendor);
100141
}
101142

102-
function editWebpackConfig(path, fn) {
143+
function editFileContent(path, ...funcs) {
103144
if (!fs.existsSync(path)) {
104145
return;
105146
}
106147

107-
const config = fs.readFileSync(path, "utf8");
108-
const newConfig = fn(config);
148+
let content = fs.readFileSync(path, "utf8");
149+
funcs.forEach(fn => content = fn(content));
109150

110-
fs.writeFileSync(path, newConfig, "utf8");
151+
fs.writeFileSync(path, content, "utf8");
111152
}
112153

113154
function replaceStyleUrlResolvePlugin(config) {
155+
if (config.indexOf("StyleUrlResolvePlugin") === -1) {
156+
return config;
157+
}
158+
159+
console.info("Replacing deprecated StyleUrlsResolvePlugin with UrlResolvePlugin...");
114160
return config.replace(/StyleUrlResolvePlugin/g, "UrlResolvePlugin");
115161
}
116162

163+
function addSnapshotPlugin(config) {
164+
if (config.indexOf("NativeScriptSnapshotPlugin") > -1) {
165+
return config;
166+
}
167+
168+
console.info("Adding NativeScriptSnapshotPlugin configuration...");
169+
return config.replace(CONFIG_MATCH, CONFIG_REPLACE);
170+
}
171+
172+
function addSnapshotToVendor(content) {
173+
if (content.indexOf("__snapshot") > -1) {
174+
return content;
175+
}
176+
177+
console.info("Adding __snapshot configuration to app/vendor-platform.android ...");
178+
return content.replace(FRAME_MATCH, SCOPED_FRAME);
179+
}
180+
117181
function getFullPath(projectDir, filePath) {
118182
return path.resolve(projectDir, filePath);
119183
}
120184

121185
function tsOrJs(projectDir, name) {
122-
const extension = helpers.isTypeScript({projectDir}) ? "ts" : "js";
186+
const extension = isTypeScript({projectDir}) ? "ts" : "js";
123187
return `${name}.${extension}`;
124188
}
125189

0 commit comments

Comments
 (0)