From a6596367653802ab44fb499f2583d3caf4e7137e Mon Sep 17 00:00:00 2001 From: Tim Lancina Date: Mon, 28 Jan 2019 17:50:16 -0600 Subject: [PATCH] feat(build): add `--cordova-mock` option (#63) Add support for adding a blank `cordova.js` to the build with `--cordova-mock`. --- builders/cordova-build/cordova.js | 1 + builders/cordova-build/index.ts | 21 ++++++++++++++++++++- builders/cordova-build/schema.d.ts | 3 ++- builders/cordova-build/schema.json | 8 ++++++-- builders/cordova-serve/index.ts | 5 +++-- builders/cordova-serve/schema.d.ts | 4 +++- builders/cordova-serve/schema.json | 13 +++++++++++-- 7 files changed, 46 insertions(+), 9 deletions(-) create mode 100644 builders/cordova-build/cordova.js diff --git a/builders/cordova-build/cordova.js b/builders/cordova-build/cordova.js new file mode 100644 index 0000000..12ad702 --- /dev/null +++ b/builders/cordova-build/cordova.js @@ -0,0 +1 @@ +// mock cordova.js diff --git a/builders/cordova-build/index.ts b/builders/cordova-build/index.ts index a88af77..219e4b0 100644 --- a/builders/cordova-build/index.ts +++ b/builders/cordova-build/index.ts @@ -23,12 +23,25 @@ export class CordovaBuildBuilder implements Builder { tap(description => browserDescription = description), concatMap(() => this.context.architect.validateBuilderOptions(browserConfig, browserDescription)), tap(config => browserConfig = config), + tap(() => this.validateBuilderConfig(builderConfig.options)), tap(() => this.prepareBrowserConfig(builderConfig.options, browserConfig.options)), concatMap(() => of(this.context.architect.getBuilder(browserDescription, this.context))), concatMap(builder => builder.run(browserConfig)) ); } + // Mutates builderOptions + validateBuilderConfig(builderOptions: CordovaBuildBuilderSchema) { + // if we're mocking cordova.js, don't build cordova bundle + if (builderOptions.cordovaMock) { + builderOptions.cordovaAssets = false; + } + + if (builderOptions.cordovaAssets && !builderOptions.platform) { + throw new Error('The `--platform` option is required with `--cordova-assets`'); + } + } + // Mutates browserOptions prepareBrowserConfig(options: CordovaBuildBuilderSchema, browserOptions: BrowserBuilderSchema) { const cordovaBasePath = normalize(options.cordovaBasePath ? options.cordovaBasePath : '.'); @@ -41,7 +54,13 @@ export class CordovaBuildBuilder implements Builder { // by default. Let's keep it around. browserOptions.deleteOutputPath = false; - if (options.cordovaAssets) { + if (options.cordovaMock) { + browserOptions.scripts.push({ + input: getSystemPath(join(normalize(__dirname), normalize('cordova.js'))), + bundleName: 'cordova', + lazy: false, + }); + } else if (options.cordovaAssets) { const platformWWWPath = join(cordovaBasePath, normalize(`platforms/${options.platform}/platform_www`)); // Add Cordova www assets that were generated whenever platform(s) and diff --git a/builders/cordova-build/schema.d.ts b/builders/cordova-build/schema.d.ts index e81b062..8f17bf3 100644 --- a/builders/cordova-build/schema.d.ts +++ b/builders/cordova-build/schema.d.ts @@ -1,7 +1,8 @@ export interface CordovaBuildBuilderSchema { browserTarget: string; - platform: string; + platform?: string; cordovaBasePath?: string; sourceMap?: boolean; cordovaAssets?: boolean; + cordovaMock?: boolean; } diff --git a/builders/cordova-build/schema.json b/builders/cordova-build/schema.json index 83d1e24..7e3583b 100644 --- a/builders/cordova-build/schema.json +++ b/builders/cordova-build/schema.json @@ -23,11 +23,15 @@ "type": "boolean", "description": "Bundle Cordova assets with build", "default": true + }, + "cordovaMock": { + "type": "boolean", + "description": "Bundle empty cordova.js with build", + "default": false } }, "additionalProperties": false, "required": [ - "browserTarget", - "platform" + "browserTarget" ] } diff --git a/builders/cordova-serve/index.ts b/builders/cordova-serve/index.ts index f067353..f555d34 100644 --- a/builders/cordova-serve/index.ts +++ b/builders/cordova-serve/index.ts @@ -36,9 +36,9 @@ export class CordovaServeBuilder implements Builder { } protected _getCordovaBuildConfig(cordovaServeOptions: CordovaServeBuilderSchema): Observable> { - const { platform } = cordovaServeOptions; + const { platform, cordovaAssets, cordovaMock } = cordovaServeOptions; const [ project, target, configuration ] = cordovaServeOptions.cordovaBuildTarget.split(':'); - const cordovaBuildTargetSpec = { project, target, configuration, overrides: { platform } }; + const cordovaBuildTargetSpec = { project, target, configuration, overrides: { platform, cordovaAssets, cordovaMock } }; const cordovaBuildTargetConfig = this.context.architect.getBuilderConfiguration(cordovaBuildTargetSpec); return this.context.architect.getBuilderDescription(cordovaBuildTargetConfig).pipe( @@ -54,6 +54,7 @@ class CordovaDevServerBuilder extends DevServerBuilder { buildWebpackConfig(root: Path, projectRoot: Path, host: virtualFs.Host, browserOptions: NormalizedBrowserBuilderSchema) { const builder = new CordovaBuildBuilder(this.context); + builder.validateBuilderConfig(this.cordovaBuildOptions); builder.prepareBrowserConfig(this.cordovaBuildOptions, browserOptions); return super.buildWebpackConfig(root, projectRoot, host, browserOptions); diff --git a/builders/cordova-serve/schema.d.ts b/builders/cordova-serve/schema.d.ts index 7c2a183..c57680e 100644 --- a/builders/cordova-serve/schema.d.ts +++ b/builders/cordova-serve/schema.d.ts @@ -1,10 +1,12 @@ export interface CordovaServeBuilderSchema { cordovaBuildTarget: string; devServerTarget: string; - platform: string; + platform?: string; port?: number; host?: string; ssl?: boolean; cordovaBasePath?: string; sourceMap?: boolean; + cordovaAssets?: boolean; + cordovaMock?: boolean; } diff --git a/builders/cordova-serve/schema.json b/builders/cordova-serve/schema.json index 76a0958..00c08fc 100644 --- a/builders/cordova-serve/schema.json +++ b/builders/cordova-serve/schema.json @@ -37,12 +37,21 @@ "sourceMap": { "type": "boolean", "description": "Create source-map file" + }, + "cordovaAssets": { + "type": "boolean", + "description": "Bundle Cordova assets with build", + "default": true + }, + "cordovaMock": { + "type": "boolean", + "description": "Bundle empty cordova.js with build", + "default": false } }, "additionalProperties": false, "required": [ "cordovaBuildTarget", - "devServerTarget", - "platform" + "devServerTarget" ] }