Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(build): add --cordova-mock option #63

Merged
merged 4 commits into from
Jan 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions builders/cordova-build/cordova.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// mock cordova.js
21 changes: 20 additions & 1 deletion builders/cordova-build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,25 @@ export class CordovaBuildBuilder implements Builder<CordovaBuildBuilderSchema> {
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 : '.');
Expand All @@ -41,7 +54,13 @@ export class CordovaBuildBuilder implements Builder<CordovaBuildBuilderSchema> {
// 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
Expand Down
3 changes: 2 additions & 1 deletion builders/cordova-build/schema.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export interface CordovaBuildBuilderSchema {
browserTarget: string;
platform: string;
platform?: string;
cordovaBasePath?: string;
sourceMap?: boolean;
cordovaAssets?: boolean;
cordovaMock?: boolean;
}
8 changes: 6 additions & 2 deletions builders/cordova-build/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
5 changes: 3 additions & 2 deletions builders/cordova-serve/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ export class CordovaServeBuilder implements Builder<CordovaServeBuilderSchema> {
}

protected _getCordovaBuildConfig(cordovaServeOptions: CordovaServeBuilderSchema): Observable<BuilderConfiguration<CordovaBuildBuilderSchema>> {
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<CordovaBuildBuilderSchema>(cordovaBuildTargetSpec);

return this.context.architect.getBuilderDescription(cordovaBuildTargetConfig).pipe(
Expand All @@ -54,6 +54,7 @@ class CordovaDevServerBuilder extends DevServerBuilder {

buildWebpackConfig(root: Path, projectRoot: Path, host: virtualFs.Host<fs.Stats>, browserOptions: NormalizedBrowserBuilderSchema) {
const builder = new CordovaBuildBuilder(this.context);
builder.validateBuilderConfig(this.cordovaBuildOptions);
builder.prepareBrowserConfig(this.cordovaBuildOptions, browserOptions);

return super.buildWebpackConfig(root, projectRoot, host, browserOptions);
Expand Down
4 changes: 3 additions & 1 deletion builders/cordova-serve/schema.d.ts
Original file line number Diff line number Diff line change
@@ -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;
}
13 changes: 11 additions & 2 deletions builders/cordova-serve/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}