awesome-cordova-plugins/scripts/build/ngx.ts

98 lines
3.1 KiB
TypeScript
Raw Permalink Normal View History

2021-09-28 02:32:20 +08:00
import { CompilerHost, CompilerOptions, createCompilerHost, createProgram, EmitFlags } from '@angular/compiler-cli';
import { copyFileSync, mkdirpSync, readJSONSync, writeJSONSync } from 'fs-extra';
2021-09-28 02:32:20 +08:00
import { clone } from 'lodash';
import { dirname, join, resolve } from 'path';
import { sync } from 'rimraf';
import { rollup } from 'rollup';
import { ModuleKind, ModuleResolutionKind, ScriptTarget } from 'typescript';
2021-09-28 02:32:20 +08:00
import { COMPILER_OPTIONS, PLUGIN_PATHS, ROOT } from './helpers';
2017-12-28 20:28:44 +08:00
import { importsTransformer } from './transformers/imports';
import { pluginClassTransformer } from './transformers/plugin-class';
2021-09-28 02:32:20 +08:00
import { generateDeclarations } from './transpile';
2017-12-28 20:28:44 +08:00
export function getProgram(rootNames: string[] = createSourceFiles()) {
const options: CompilerOptions = clone(COMPILER_OPTIONS);
2017-12-28 20:28:44 +08:00
options.basePath = ROOT;
options.moduleResolution = ModuleResolutionKind.NodeJs;
options.module = ModuleKind.ES2015;
options.target = ScriptTarget.ES5;
2017-12-28 20:28:44 +08:00
options.lib = ['dom', 'es2017'];
options.inlineSourceMap = true;
options.importHelpers = true;
2017-12-28 20:28:44 +08:00
options.inlineSources = true;
options.enableIvy = true;
options.compilationMode = 'partial';
2017-12-28 20:28:44 +08:00
delete options.baseUrl;
2017-12-28 20:28:44 +08:00
const host: CompilerHost = createCompilerHost({ options });
return createProgram({
2017-12-28 20:28:44 +08:00
rootNames,
options,
2020-05-16 20:40:49 +08:00
host,
2017-12-28 20:28:44 +08:00
});
}
// hacky way to export metadata only for core package
export function transpileNgxCore() {
2021-09-28 02:32:20 +08:00
getProgram([resolve(ROOT, 'src/@awesome-cordova-plugins/core/index.ts')]).emit({
2017-12-28 20:28:44 +08:00
emitFlags: EmitFlags.Metadata,
emitCallback: ({ program, writeFile, customTransformers, cancellationToken, targetSourceFile }) => {
return program.emit(targetSourceFile, writeFile, cancellationToken, true, customTransformers);
2020-05-16 20:40:49 +08:00
},
2017-12-28 20:28:44 +08:00
});
}
export function transpileNgx() {
getProgram().emit({
emitFlags: EmitFlags.Metadata,
customTransformers: {
2020-05-16 20:40:49 +08:00
beforeTs: [importsTransformer(true), pluginClassTransformer(true)],
},
2017-12-28 20:28:44 +08:00
});
}
export function generateDeclarationFiles() {
2021-09-28 04:09:05 +08:00
generateDeclarations(PLUGIN_PATHS.map((p) => p.replace('index.ts', 'ngx/index.ts')));
2017-12-28 20:28:44 +08:00
}
export function generateLegacyBundles() {
[
2021-09-28 02:32:20 +08:00
resolve(ROOT, 'dist/@awesome-cordova-plugins/core/index.js'),
2021-09-28 04:09:05 +08:00
...PLUGIN_PATHS.map((p) => p.replace(join(ROOT, 'src'), join(ROOT, 'dist')).replace('index.ts', 'ngx/index.js')),
].forEach((p) =>
rollup({
input: p,
onwarn(warning, warn) {
if (warning.code === 'UNUSED_EXTERNAL_IMPORT') return;
warn(warning);
},
external: ['@angular/core', '@awesome-cordova-plugins/core', 'rxjs', 'tslib'],
2021-09-28 04:09:05 +08:00
}).then((bundle) =>
bundle.write({
file: join(dirname(p), 'bundle.js'),
format: 'cjs',
})
)
);
}
2017-12-28 20:28:44 +08:00
function createSourceFiles(): string[] {
return PLUGIN_PATHS.map((indexPath: string) => {
2021-09-28 02:32:20 +08:00
const ngxPath = resolve(indexPath.replace('index.ts', ''), 'ngx'),
newPath = resolve(ngxPath, 'index.ts');
2017-12-28 20:28:44 +08:00
// delete directory
sync(ngxPath);
mkdirpSync(ngxPath);
copyFileSync(indexPath, newPath);
2017-12-28 20:28:44 +08:00
return newPath;
});
}
export function cleanupNgx() {
PLUGIN_PATHS.forEach((indexPath: string) => sync(indexPath.replace('index.ts', 'ngx')));
2017-12-28 20:28:44 +08:00
}