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

Commit 641ac9b

Browse files
committed
fix(postinstall): add snapshot plugin to webpack config
1 parent 668ac18 commit 641ac9b

File tree

2 files changed

+39
-18
lines changed

2 files changed

+39
-18
lines changed

installer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ const APP_DIR = path.resolve(PROJECT_DIR, "app");
1212
function install() {
1313
let packageJson = helpers.getPackageJson(PROJECT_DIR);
1414

15-
projectFilesManager.addProjectFiles(PROJECT_DIR, APP_DIR);
1615
projectFilesManager.editExistingProjectFiles(PROJECT_DIR);
16+
projectFilesManager.addProjectFiles(PROJECT_DIR, APP_DIR);
1717

1818
let scripts = packageJson.scripts || {};
1919
scripts = npmScriptsManager.removeDeprecatedNpmScripts(scripts);

projectFilesManager.js

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,34 @@ const fs = require("fs");
33

44
const { isTypeScript, isAngular } = require("./projectHelpers");
55

6-
const FRAME_MATCH = /(\s*)(require\("ui\/frame"\);)(\s*)(require\("ui\/frame\/activity"\);)/;
6+
const FRAME_MATCH = /(\s*)(require\("ui\/frame"\);)(\s*)(require\("ui\/frame\/activity"\);)/g;
77
const SCOPED_FRAME = `
88
if (!global["__snapshot"]) {
99
// In case snapshot generation is enabled these modules will get into the bundle
10-
// but will not be required/evaluated.
10+
// but will not be required/evaluated.
1111
// The snapshot webpack plugin will add them to the tns-java-classes.js bundle file.
1212
// This way, they will be evaluated on app start as early as possible.
13-
$1\t$2$3\t$4
13+
$1\t$2$3\t$4
1414
}`;
1515

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;`;
33+
1634
function addProjectFiles(projectDir, appDir) {
1735
const projectTemplates = getProjectTemplates(projectDir);
1836
Object.keys(projectTemplates).forEach(function(templateName) {
@@ -105,8 +123,13 @@ function editExistingProjectFiles(projectDir) {
105123
const webpackConfigPath = getFullPath(projectDir, "webpack.config.js");
106124
const webpackCommonPath = getFullPath(projectDir, "webpack.common.js");
107125

108-
editFileContent(webpackConfigPath, replaceStyleUrlResolvePlugin);
109-
editFileContent(webpackCommonPath, replaceStyleUrlResolvePlugin);
126+
const configChangeFunctions = [
127+
replaceStyleUrlResolvePlugin,
128+
addSnapshotPlugin,
129+
];
130+
131+
editFileContent(webpackConfigPath, ...configChangeFunctions);
132+
editFileContent(webpackCommonPath, ...configChangeFunctions);
110133

111134
const extension = isAngular({projectDir}) ? "ts" : "js";
112135
const vendorAndroidPath = getFullPath(
@@ -117,33 +140,31 @@ function editExistingProjectFiles(projectDir) {
117140
editFileContent(vendorAndroidPath, addSnapshotToVendor);
118141
}
119142

120-
function editFileContent(path, fn) {
143+
function editFileContent(path, ...funcs) {
121144
if (!fs.existsSync(path)) {
122145
return;
123146
}
124147

125-
console.log('editing: ' + path)
126-
const config = fs.readFileSync(path, "utf8");
127-
const newConfig = fn(config);
148+
let content = fs.readFileSync(path, "utf8");
149+
funcs.forEach(fn => content = fn(content));
128150

129-
fs.writeFileSync(path, newConfig, "utf8");
151+
fs.writeFileSync(path, content, "utf8");
130152
}
131153

132154
function replaceStyleUrlResolvePlugin(config) {
133155
return config.replace(/StyleUrlResolvePlugin/g, "UrlResolvePlugin");
134156
}
135157

136158
function addSnapshotPlugin(config) {
137-
159+
return config.indexOf("NativeScriptSnapshotPlugin") > -1 ?
160+
config :
161+
config.replace(CONFIG_MATCH, CONFIG_REPLACE);
138162
}
139163

140164
function addSnapshotToVendor(content) {
141-
if (content.indexOf("__snapshot") > -1) {
142-
return content;
143-
}
144-
145-
146-
return content.replace(FRAME_MATCH, SCOPED_FRAME);
165+
return content.indexOf("__snapshot") > -1 ?
166+
content :
167+
content.replace(FRAME_MATCH, SCOPED_FRAME);
147168
}
148169

149170
function getFullPath(projectDir, filePath) {

0 commit comments

Comments
 (0)