Skip to content

Commit c9653e0

Browse files
committed
Merge pull request #1671 from NativeScript/hdeshev/node_modules-bundle
Add a --bundle option that disables node_modules -> tns_modules update.
2 parents 112e117 + d85d0f8 commit c9653e0

File tree

6 files changed

+43
-3
lines changed

6 files changed

+43
-3
lines changed

lib/declarations.ts

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ interface IOptions extends ICommonOptions {
8080
keyStorePath: string;
8181
linkTo: string;
8282
ng: boolean;
83+
bundle: boolean;
8384
platformTemplate: string;
8485
port: Number;
8586
production: boolean;

lib/options.ts

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export class Options extends commonOptionsLibPath.OptionsBase {
3737
baseConfig: { type: OptionType.String },
3838
platformTemplate: { type: OptionType.String },
3939
ng: {type: OptionType.Boolean },
40+
bundle: {type: OptionType.Boolean },
4041
all: {type: OptionType.Boolean }
4142
},
4243
path.join($hostInfo.isWindows ? process.env.AppData : path.join(osenv.home(), ".local/share"), ".nativescript-cli"),

lib/services/platform-service.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -280,11 +280,16 @@ export class PlatformService implements IPlatformService {
280280

281281
platformData.platformProjectService.prepareProject().wait();
282282

283-
// Process node_modules folder
284283
let appDir = path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME);
285284
try {
286285
let tnsModulesDestinationPath = path.join(appDir, constants.TNS_MODULES_FOLDER_NAME);
287-
this.$broccoliBuilder.prepareNodeModules(tnsModulesDestinationPath, platform, lastModifiedTime).wait();
286+
if (!this.$options.bundle) {
287+
// Process node_modules folder
288+
this.$broccoliBuilder.prepareNodeModules(tnsModulesDestinationPath, platform, lastModifiedTime).wait();
289+
} else {
290+
// Clean target node_modules folder. Not needed when bundling.
291+
this.$broccoliBuilder.cleanNodeModules(tnsModulesDestinationPath, platform);
292+
}
288293
} catch(error) {
289294
this.$logger.debug(error);
290295
shell.rm("-rf", appDir);

lib/tools/broccoli/broccoli.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ interface BroccoliNode {
154154
interface IBroccoliBuilder {
155155
getChangedNodeModules(outputPath: string, platform: string, lastModifiedTime?: Date): IFuture<any>;
156156
prepareNodeModules(outputPath: string, platform: string, lastModifiedTime?: Date): IFuture<void>;
157+
cleanNodeModules(outputPath: string, platform: string): void;
157158
}
158159

159160
interface IDiffResult {
@@ -169,4 +170,4 @@ interface IBroccoliPlugin {
169170

170171
interface INodeModulesTree {
171172
makeNodeModulesTree(absoluteOutputPath: string, projectDir: string): any;
172-
}
173+
}

lib/tools/broccoli/builder.ts

+10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
"use strict";
33

44
import * as constants from "../../../lib/constants";
5+
import * as fs from "fs";
56
import * as path from "path";
7+
import * as shelljs from "shelljs";
68
import Future = require("fibers/future");
79
import * as destCopyLib from "./node-modules-dest-copy";
810
import * as fiberBootstrap from "../../common/fiber-bootstrap";
@@ -112,6 +114,10 @@ export class Builder implements IBroccoliBuilder {
112114

113115
public prepareNodeModules(absoluteOutputPath: string, platform: string, lastModifiedTime?: Date): IFuture<void> {
114116
return (() => {
117+
if (!fs.existsSync(absoluteOutputPath)) {
118+
// Force copying if the destination doesn't exist.
119+
lastModifiedTime = null;
120+
}
115121
let nodeModules = this.getChangedNodeModules(absoluteOutputPath, platform, lastModifiedTime).wait();
116122
let destCopy = this.$injector.resolve(destCopyLib.DestCopy, {
117123
inputPath: this.$projectData.projectDir,
@@ -125,5 +131,9 @@ export class Builder implements IBroccoliBuilder {
125131

126132
}).future<void>()();
127133
}
134+
135+
public cleanNodeModules(absoluteOutputPath: string, platform: string): void {
136+
shelljs.rm("-rf", absoluteOutputPath);
137+
}
128138
}
129139
$injector.register("broccoliBuilder", Builder);

test/npm-support.ts

+22
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,28 @@ describe("Npm support tests", () => {
232232
let scopedDependencyPath = path.join(tnsModulesFolderPath, "@reactivex", "rxjs");
233233
assert.isTrue(fs.exists(scopedDependencyPath).wait());
234234
});
235+
236+
it("Ensures that tns_modules absent when bundling", () => {
237+
let fs = testInjector.resolve("fs");
238+
let options = testInjector.resolve("options");
239+
let tnsModulesFolderPath = path.join(appDestinationFolderPath, "app", "tns_modules");
240+
241+
try {
242+
options.bundle = false;
243+
preparePlatform(testInjector).wait();
244+
assert.isTrue(fs.exists(tnsModulesFolderPath).wait(), "tns_modules created first");
245+
246+
options.bundle = true;
247+
preparePlatform(testInjector).wait();
248+
assert.isFalse(fs.exists(tnsModulesFolderPath).wait(), "tns_modules deleted when bundling");
249+
250+
options.bundle = false;
251+
preparePlatform(testInjector).wait();
252+
assert.isTrue(fs.exists(tnsModulesFolderPath).wait(), "tns_modules recreated");
253+
} finally {
254+
options.bundle = false;
255+
}
256+
});
235257
});
236258

237259
describe("Flatten npm modules tests", () => {

0 commit comments

Comments
 (0)