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

Commit 668ac18

Browse files
committed
fix: snapshot scope "frame" requires in vendor android
1 parent d709021 commit 668ac18

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed

projectFilesManager.js

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
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"\);)/;
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+
}`;
515

616
function addProjectFiles(projectDir, appDir) {
717
const projectTemplates = getProjectTemplates(projectDir);
@@ -51,10 +61,10 @@ function copyTemplate(templateName, destinationPath) {
5161
function getProjectTemplates(projectDir) {
5262
let templates = {}
5363

54-
if (helpers.isAngular({projectDir})) {
64+
if (isAngular({projectDir})) {
5565
templates["webpack.angular.js"] = "webpack.config.js";
5666
templates["tsconfig.aot.json"] = "tsconfig.aot.json";
57-
} else if (helpers.isTypeScript({projectDir})) {
67+
} else if (isTypeScript({projectDir})) {
5868
templates["webpack.typescript.js"] = "webpack.config.js";
5969
} else {
6070
templates["webpack.javascript.js"] = "webpack.config.js";
@@ -69,7 +79,7 @@ function getAppTemplates(projectDir, appDir) {
6979
"vendor-platform.ios.ts": tsOrJs(projectDir, "vendor-platform.ios"),
7080
};
7181

72-
if (helpers.isAngular({projectDir})) {
82+
if (isAngular({projectDir})) {
7383
templates["vendor.angular.ts"] = tsOrJs(projectDir, "vendor");
7484
} else {
7585
templates["vendor.nativescript.ts"] = tsOrJs(projectDir, "vendor");
@@ -95,15 +105,24 @@ function editExistingProjectFiles(projectDir) {
95105
const webpackConfigPath = getFullPath(projectDir, "webpack.config.js");
96106
const webpackCommonPath = getFullPath(projectDir, "webpack.common.js");
97107

98-
editWebpackConfig(webpackConfigPath, replaceStyleUrlResolvePlugin);
99-
editWebpackConfig(webpackCommonPath, replaceStyleUrlResolvePlugin);
108+
editFileContent(webpackConfigPath, replaceStyleUrlResolvePlugin);
109+
editFileContent(webpackCommonPath, replaceStyleUrlResolvePlugin);
110+
111+
const extension = isAngular({projectDir}) ? "ts" : "js";
112+
const vendorAndroidPath = getFullPath(
113+
projectDir,
114+
`app/vendor-platform.android.${extension}`
115+
);
116+
117+
editFileContent(vendorAndroidPath, addSnapshotToVendor);
100118
}
101119

102-
function editWebpackConfig(path, fn) {
120+
function editFileContent(path, fn) {
103121
if (!fs.existsSync(path)) {
104122
return;
105123
}
106124

125+
console.log('editing: ' + path)
107126
const config = fs.readFileSync(path, "utf8");
108127
const newConfig = fn(config);
109128

@@ -114,12 +133,25 @@ function replaceStyleUrlResolvePlugin(config) {
114133
return config.replace(/StyleUrlResolvePlugin/g, "UrlResolvePlugin");
115134
}
116135

136+
function addSnapshotPlugin(config) {
137+
138+
}
139+
140+
function addSnapshotToVendor(content) {
141+
if (content.indexOf("__snapshot") > -1) {
142+
return content;
143+
}
144+
145+
146+
return content.replace(FRAME_MATCH, SCOPED_FRAME);
147+
}
148+
117149
function getFullPath(projectDir, filePath) {
118150
return path.resolve(projectDir, filePath);
119151
}
120152

121153
function tsOrJs(projectDir, name) {
122-
const extension = helpers.isTypeScript({projectDir}) ? "ts" : "js";
154+
const extension = isTypeScript({projectDir}) ? "ts" : "js";
123155
return `${name}.${extension}`;
124156
}
125157

0 commit comments

Comments
 (0)