From d85d0f8da70ea13248bf4af9d15aa85ed67abc98 Mon Sep 17 00:00:00 2001 From: Hristo Deshev Date: Fri, 8 Apr 2016 15:07:55 +0300 Subject: [PATCH] Add a --bundle option that disables node_modules -> tns_modules update. Delete /tns_modules, if present, and make sure we restore it on subsequent runs without the --bundle option set. --- lib/declarations.ts | 1 + lib/options.ts | 1 + lib/services/platform-service.ts | 9 +++++++-- lib/tools/broccoli/broccoli.d.ts | 3 ++- lib/tools/broccoli/builder.ts | 10 ++++++++++ test/npm-support.ts | 22 ++++++++++++++++++++++ 6 files changed, 43 insertions(+), 3 deletions(-) diff --git a/lib/declarations.ts b/lib/declarations.ts index f659e99aca..14aa5848f1 100644 --- a/lib/declarations.ts +++ b/lib/declarations.ts @@ -80,6 +80,7 @@ interface IOptions extends ICommonOptions { keyStorePath: string; linkTo: string; ng: boolean; + bundle: boolean; platformTemplate: string; port: Number; production: boolean; diff --git a/lib/options.ts b/lib/options.ts index ccc060c0bf..4a314c9645 100644 --- a/lib/options.ts +++ b/lib/options.ts @@ -37,6 +37,7 @@ export class Options extends commonOptionsLibPath.OptionsBase { baseConfig: { type: OptionType.String }, platformTemplate: { type: OptionType.String }, ng: {type: OptionType.Boolean }, + bundle: {type: OptionType.Boolean }, all: {type: OptionType.Boolean } }, path.join($hostInfo.isWindows ? process.env.AppData : path.join(osenv.home(), ".local/share"), ".nativescript-cli"), diff --git a/lib/services/platform-service.ts b/lib/services/platform-service.ts index 152740c660..327e66bc0f 100644 --- a/lib/services/platform-service.ts +++ b/lib/services/platform-service.ts @@ -280,11 +280,16 @@ export class PlatformService implements IPlatformService { platformData.platformProjectService.prepareProject().wait(); - // Process node_modules folder let appDir = path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME); try { let tnsModulesDestinationPath = path.join(appDir, constants.TNS_MODULES_FOLDER_NAME); - this.$broccoliBuilder.prepareNodeModules(tnsModulesDestinationPath, platform, lastModifiedTime).wait(); + if (!this.$options.bundle) { + // Process node_modules folder + this.$broccoliBuilder.prepareNodeModules(tnsModulesDestinationPath, platform, lastModifiedTime).wait(); + } else { + // Clean target node_modules folder. Not needed when bundling. + this.$broccoliBuilder.cleanNodeModules(tnsModulesDestinationPath, platform); + } } catch(error) { this.$logger.debug(error); shell.rm("-rf", appDir); diff --git a/lib/tools/broccoli/broccoli.d.ts b/lib/tools/broccoli/broccoli.d.ts index df7b6d6fdb..9486f0e882 100644 --- a/lib/tools/broccoli/broccoli.d.ts +++ b/lib/tools/broccoli/broccoli.d.ts @@ -154,6 +154,7 @@ interface BroccoliNode { interface IBroccoliBuilder { getChangedNodeModules(outputPath: string, platform: string, lastModifiedTime?: Date): IFuture; prepareNodeModules(outputPath: string, platform: string, lastModifiedTime?: Date): IFuture; + cleanNodeModules(outputPath: string, platform: string): void; } interface IDiffResult { @@ -169,4 +170,4 @@ interface IBroccoliPlugin { interface INodeModulesTree { makeNodeModulesTree(absoluteOutputPath: string, projectDir: string): any; -} \ No newline at end of file +} diff --git a/lib/tools/broccoli/builder.ts b/lib/tools/broccoli/builder.ts index c8be648318..1bc8bc437f 100644 --- a/lib/tools/broccoli/builder.ts +++ b/lib/tools/broccoli/builder.ts @@ -2,7 +2,9 @@ "use strict"; import * as constants from "../../../lib/constants"; +import * as fs from "fs"; import * as path from "path"; +import * as shelljs from "shelljs"; import Future = require("fibers/future"); import * as destCopyLib from "./node-modules-dest-copy"; import * as fiberBootstrap from "../../common/fiber-bootstrap"; @@ -112,6 +114,10 @@ export class Builder implements IBroccoliBuilder { public prepareNodeModules(absoluteOutputPath: string, platform: string, lastModifiedTime?: Date): IFuture { return (() => { + if (!fs.existsSync(absoluteOutputPath)) { + // Force copying if the destination doesn't exist. + lastModifiedTime = null; + } let nodeModules = this.getChangedNodeModules(absoluteOutputPath, platform, lastModifiedTime).wait(); let destCopy = this.$injector.resolve(destCopyLib.DestCopy, { inputPath: this.$projectData.projectDir, @@ -125,5 +131,9 @@ export class Builder implements IBroccoliBuilder { }).future()(); } + + public cleanNodeModules(absoluteOutputPath: string, platform: string): void { + shelljs.rm("-rf", absoluteOutputPath); + } } $injector.register("broccoliBuilder", Builder); diff --git a/test/npm-support.ts b/test/npm-support.ts index d5ed7b1215..5b2b1da568 100644 --- a/test/npm-support.ts +++ b/test/npm-support.ts @@ -232,6 +232,28 @@ describe("Npm support tests", () => { let scopedDependencyPath = path.join(tnsModulesFolderPath, "@reactivex", "rxjs"); assert.isTrue(fs.exists(scopedDependencyPath).wait()); }); + + it("Ensures that tns_modules absent when bundling", () => { + let fs = testInjector.resolve("fs"); + let options = testInjector.resolve("options"); + let tnsModulesFolderPath = path.join(appDestinationFolderPath, "app", "tns_modules"); + + try { + options.bundle = false; + preparePlatform(testInjector).wait(); + assert.isTrue(fs.exists(tnsModulesFolderPath).wait(), "tns_modules created first"); + + options.bundle = true; + preparePlatform(testInjector).wait(); + assert.isFalse(fs.exists(tnsModulesFolderPath).wait(), "tns_modules deleted when bundling"); + + options.bundle = false; + preparePlatform(testInjector).wait(); + assert.isTrue(fs.exists(tnsModulesFolderPath).wait(), "tns_modules recreated"); + } finally { + options.bundle = false; + } + }); }); describe("Flatten npm modules tests", () => {