Created
January 28, 2020 18:10
-
-
Save mhartington/2092b7c9bcb03c0b4bad685b70c48d74 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function serveCordova( | |
options: CordovaServeBuilderSchema, | |
context: BuilderContext | |
): Observable<DevServerBuilderOutput> { | |
const { devServerTarget, port, host, ssl } = options; | |
const root = context.workspaceRoot; | |
const devServerTargetSpec = targetFromTargetString(devServerTarget); | |
async function setup() { | |
const devServerTargetOptions = (await context.getTargetOptions( | |
devServerTargetSpec | |
)) as DevServerBuilderOptions; | |
devServerTargetOptions.port = port; | |
devServerTargetOptions.host = host; | |
devServerTargetOptions.ssl = ssl; | |
const formattedAssets = prepareServerConfig(options, root); | |
if (options.consolelogs && options.consolelogsPort) { | |
await createConsoleLogServer('localhost', options.consolelogsPort); | |
} | |
return { devServerTargetOptions, formattedAssets }; | |
} | |
return from(setup()).pipe( | |
switchMap(({ devServerTargetOptions, formattedAssets }) => | |
executeDevServerBuilder( | |
devServerTargetOptions, | |
context, | |
getTransforms(formattedAssets, context) | |
) | |
) | |
); | |
} | |
export default createBuilder<CordovaDevServerBuilderOptions, any>(serveCordova); | |
function getTransforms( | |
formattedAssets: FormattedAssets, | |
context: BuilderContext | |
) { | |
return { | |
webpackConfiguration: cordovaServeTransform(formattedAssets, context), | |
indexHtml: indexHtmlTransformFactory(formattedAssets, context), | |
}; | |
} | |
const cordovaServeTransform: ( | |
formattedAssets: FormattedAssets, | |
context: BuilderContext | |
) => ExecutionTransformer<Configuration> = ( | |
formattedAssets, | |
{ workspaceRoot } | |
) => browserWebpackConfig => { | |
const scriptExtras = formattedAssets.globalScriptsByBundleName.map( | |
(script: { bundleName: any; paths: any }) => { | |
const bundleName = script.bundleName; | |
return new ScriptsWebpackPlugin({ | |
name: bundleName, | |
sourceMap: true, | |
filename: `${basename(bundleName)}.js`, | |
scripts: script.paths, | |
basePath: workspaceRoot, | |
}); | |
} | |
); | |
const copyWebpackPluginOptions = { | |
ignore: ['.gitkeep', '**/.DS_Store', '**/Thumbs.db'] | |
}; | |
const copyWebpackPluginInstance = new CopyWebpackPlugin( | |
formattedAssets.copyWebpackPluginPatterns, | |
copyWebpackPluginOptions | |
); | |
// tslint:disable-next-line: no-non-null-assertion | |
browserWebpackConfig.plugins!.push( | |
...scriptExtras, | |
copyWebpackPluginInstance | |
); | |
return browserWebpackConfig; | |
}; | |
export const indexHtmlTransformFactory: ( | |
formattedAssets: FormattedAssets, | |
context: BuilderContext | |
) => IndexHtmlTransform = async () => (indexTransform: string) => { | |
const newIndex = await augmentIndexHtml({ | |
input: 'index.html', | |
inputContent: indexTransform, | |
files: [{ file: 'cordova.js', extension: '.js', name: 'cordova' }], | |
}); | |
return newIndex; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment