refactor(build): use path es6 imports

This commit is contained in:
Daniel Sogl 2021-09-27 20:32:20 +02:00
parent 770052347e
commit f3160e4cc6
6 changed files with 39 additions and 42 deletions

View File

@ -1,16 +1,16 @@
import { readdirSync } from 'fs-extra';
import { camelCase, clone } from 'lodash';
import * as path from 'path';
import { join, resolve } from 'path';
import * as ts from 'typescript';
import { Logger } from '../logger';
export const ROOT = path.resolve(__dirname, '../../');
export const ROOT = resolve(__dirname, '../../');
// tslint:disable-next-line:no-var-requires
export const TS_CONFIG = clone(require(path.resolve(ROOT, 'tsconfig.json')));
export const TS_CONFIG = clone(require(resolve(ROOT, 'tsconfig.json')));
export const COMPILER_OPTIONS = TS_CONFIG.compilerOptions;
export const PLUGINS_ROOT = path.join(ROOT, 'src/@awesome-cordova-plugins/plugins/');
export const PLUGIN_PATHS = readdirSync(PLUGINS_ROOT).map(d => path.join(PLUGINS_ROOT, d, 'index.ts'));
export const PLUGINS_ROOT = join(ROOT, 'src/@awesome-cordova-plugins/plugins/');
export const PLUGIN_PATHS = readdirSync(PLUGINS_ROOT).map(d => join(PLUGINS_ROOT, d, 'index.ts'));
export function getDecorator(node: ts.Node, index = 0): ts.Decorator {
if (node.decorators && node.decorators[index]) {

View File

@ -1,14 +1,15 @@
import * as ts from 'typescript';
import { CompilerHost, CompilerOptions, createCompilerHost, createProgram, EmitFlags } from '@angular/compiler-cli';
import { copyFileSync, mkdirpSync, readJSONSync, writeJSONSync } from 'fs-extra';
import * as path from 'path';
import { clone } from 'lodash';
import { dirname, join, resolve } from 'path';
import * as rimraf from 'rimraf';
import * as rollup from 'rollup';
import { generateDeclarations } from './transpile';
import { clone } from 'lodash';
import { EmitFlags, createCompilerHost, CompilerOptions, CompilerHost, createProgram } from '@angular/compiler-cli';
import * as ts from 'typescript';
import { COMPILER_OPTIONS, PLUGIN_PATHS, ROOT } from './helpers';
import { importsTransformer } from './transformers/imports';
import { pluginClassTransformer } from './transformers/plugin-class';
import { COMPILER_OPTIONS, PLUGIN_PATHS, ROOT } from './helpers';
import { generateDeclarations } from './transpile';
export function getProgram(rootNames: string[] = createSourceFiles()) {
const options: CompilerOptions = clone(COMPILER_OPTIONS);
@ -34,7 +35,7 @@ export function getProgram(rootNames: string[] = createSourceFiles()) {
// hacky way to export metadata only for core package
export function transpileNgxCore() {
getProgram([path.resolve(ROOT, 'src/@awesome-cordova-plugins/core/index.ts')]).emit({
getProgram([resolve(ROOT, 'src/@awesome-cordova-plugins/core/index.ts')]).emit({
emitFlags: EmitFlags.Metadata,
emitCallback: ({ program, writeFile, customTransformers, cancellationToken, targetSourceFile }) => {
return program.emit(targetSourceFile, writeFile, cancellationToken, true, customTransformers);
@ -57,10 +58,8 @@ export function generateDeclarationFiles() {
export function generateLegacyBundles() {
[
path.resolve(ROOT, 'dist/@awesome-cordova-plugins/core/index.js'),
...PLUGIN_PATHS.map(p =>
p.replace(path.join(ROOT, 'src'), path.join(ROOT, 'dist')).replace('index.ts', 'ngx/index.js')
),
resolve(ROOT, 'dist/@awesome-cordova-plugins/core/index.js'),
...PLUGIN_PATHS.map(p => p.replace(join(ROOT, 'src'), join(ROOT, 'dist')).replace('index.ts', 'ngx/index.js')),
].forEach(p =>
rollup
.rollup({
@ -73,7 +72,7 @@ export function generateLegacyBundles() {
})
.then(bundle =>
bundle.write({
file: path.join(path.dirname(p), 'bundle.js'),
file: join(dirname(p), 'bundle.js'),
format: 'cjs',
})
)
@ -83,7 +82,7 @@ export function generateLegacyBundles() {
// remove reference to @awesome-cordova-plugins/core decorators
export function modifyMetadata() {
PLUGIN_PATHS.map(p =>
p.replace(path.join(ROOT, 'src'), path.join(ROOT, 'dist')).replace('index.ts', 'ngx/index.metadata.json')
p.replace(join(ROOT, 'src'), join(ROOT, 'dist')).replace('index.ts', 'ngx/index.metadata.json')
).forEach(p => {
const content = readJSONSync(p);
let _prop: { members: { [x: string]: any[] } };
@ -114,8 +113,8 @@ function removeIonicNativeDecorators(node: any) {
function createSourceFiles(): string[] {
return PLUGIN_PATHS.map((indexPath: string) => {
const ngxPath = path.resolve(indexPath.replace('index.ts', ''), 'ngx'),
newPath = path.resolve(ngxPath, 'index.ts');
const ngxPath = resolve(indexPath.replace('index.ts', ''), 'ngx'),
newPath = resolve(ngxPath, 'index.ts');
// delete directory
rimraf.sync(ngxPath);

View File

@ -1,5 +1,5 @@
import { unlinkSync, writeJSONSync } from 'fs-extra';
import * as path from 'path';
import { resolve } from 'path';
import * as ts from 'typescript';
import { hasDecorator, ROOT } from '../helpers';
@ -11,7 +11,7 @@ export interface InjectableClassEntry {
}
const injectableClasses: InjectableClassEntry[] = [];
export const EMIT_PATH = path.resolve(ROOT, 'injectable-classes.json');
export const EMIT_PATH = resolve(ROOT, 'injectable-classes.json');
/**
* This transformer extracts all the injectable classes

View File

@ -1,5 +1,5 @@
import { readJSONSync, writeFileSync } from 'fs-extra';
import * as path from 'path';
import { resolve } from 'path';
import * as uglifyJsPlugin from 'uglifyjs-webpack-plugin';
import * as unminifiedPlugin from 'unminified-webpack-plugin';
import * as webpack from 'webpack';
@ -8,8 +8,8 @@ import { ROOT } from '../build/helpers';
import { cleanEmittedData, EMIT_PATH, InjectableClassEntry } from '../build/transformers/extract-injectables';
import { Logger } from '../logger';
const DIST = path.resolve(ROOT, 'dist');
const INDEX_PATH = path.resolve(DIST, 'index.js');
const DIST = resolve(ROOT, 'dist');
const INDEX_PATH = resolve(DIST, 'index.js');
const INJECTABLE_CLASSES = readJSONSync(EMIT_PATH).map((item: InjectableClassEntry) => {
item.file =
'./' +
@ -33,14 +33,14 @@ const webpackConfig: webpack.Configuration = {
modules: ['node_modules'],
extensions: ['.js'],
alias: {
'@awesome-cordova-plugins/core': path.resolve(DIST, '@awesome-cordova-plugins/core/index.js'),
'@awesome-cordova-plugins/core': resolve(DIST, '@awesome-cordova-plugins/core/index.js'),
},
},
module: {
rules: [
{
test: /\.js$/,
use: path.resolve(ROOT, 'scripts/build/remove-tslib-helpers.js'),
use: resolve(ROOT, 'scripts/build/remove-tslib-helpers.js'),
},
],
},

View File

@ -1,5 +1,5 @@
import { readFileSync, readJSONSync, writeFileSync } from 'fs-extra';
import * as path from 'path';
import { join } from 'path';
import { PLUGIN_PATHS, ROOT } from '../build/helpers';
import { EMIT_PATH } from '../build/transformers/extract-injectables';
@ -8,16 +8,14 @@ import { generateDeclarations, transpile } from '../build/transpile';
generateDeclarations();
transpile();
const outDirs = PLUGIN_PATHS.map(p =>
p.replace(path.join(ROOT, 'src'), path.join(ROOT, 'dist')).replace(/[\\/]index.ts/, '')
);
const outDirs = PLUGIN_PATHS.map(p => p.replace(join(ROOT, 'src'), join(ROOT, 'dist')).replace(/[\\/]index.ts/, ''));
const injectableClasses = readJSONSync(EMIT_PATH);
outDirs.forEach(dir => {
const classes = injectableClasses.filter(entry => entry.dirName === dir.split(/[\\/]+/).pop());
let jsFile: string = readFileSync(path.join(dir, 'index.js'), 'utf-8'),
dtsFile: string = readFileSync(path.join(dir, 'index.d.ts'), 'utf-8');
let jsFile: string = readFileSync(join(dir, 'index.js'), 'utf-8'),
dtsFile: string = readFileSync(join(dir, 'index.d.ts'), 'utf-8');
classes.forEach(entry => {
dtsFile = dtsFile.replace(`class ${entry.className} `, 'class ' + entry.className + 'Original ');
@ -32,6 +30,6 @@ outDirs.forEach(dir => {
);
});
writeFileSync(path.join(dir, 'index.js'), jsFile, 'utf-8');
writeFileSync(path.join(dir, 'index.d.ts'), dtsFile, 'utf-8');
writeFileSync(join(dir, 'index.js'), jsFile, 'utf-8');
writeFileSync(join(dir, 'index.d.ts'), dtsFile, 'utf-8');
});

View File

@ -3,7 +3,7 @@ import { exec } from 'child_process';
import { writeJSONSync } from 'fs-extra';
import { merge } from 'lodash';
import { cpus } from 'os';
import * as path from 'path';
import { join, resolve } from 'path';
import { PLUGIN_PATHS, ROOT } from '../build/helpers';
import { Logger } from '../logger';
@ -26,7 +26,7 @@ const PACKAGE_JSON_BASE = {
},
};
const DIST = path.resolve(ROOT, 'dist/@awesome-cordova-plugins');
const DIST = resolve(ROOT, 'dist/@awesome-cordova-plugins');
const PACKAGES = [];
@ -48,27 +48,27 @@ function getPackageJsonContent(name: string, peerDependencies = {}, dependencies
}
function writePackageJson(data: any, dir: string) {
const filePath = path.resolve(dir, 'package.json');
const filePath = resolve(dir, 'package.json');
writeJSONSync(filePath, data);
PACKAGES.push(dir);
}
function writeNGXPackageJson(data: any, dir: string) {
const filePath = path.resolve(dir, 'package.json');
const filePath = resolve(dir, 'package.json');
writeJSONSync(filePath, data);
}
function prepare() {
// write @awesome-cordova-plugins/core package.json
writePackageJson(
getPackageJsonContent('core', { rxjs: RXJS_VERSION }, { '@types/cordova': 'latest' }),
path.resolve(DIST, 'core')
resolve(DIST, 'core')
);
// write plugin package.json files
PLUGIN_PATHS.forEach((pluginPath: string) => {
const pluginName = pluginPath.split(/[\/\\]+/).slice(-2)[0];
const packageJsonContents = getPackageJsonContent(pluginName, PLUGIN_PEER_DEPENDENCIES);
const dir = path.resolve(DIST, 'plugins', pluginName);
const ngxDir = path.join(dir, 'ngx');
const dir = resolve(DIST, 'plugins', pluginName);
const ngxDir = join(dir, 'ngx');
writePackageJson(packageJsonContents, dir);
writeNGXPackageJson(packageJsonContents, ngxDir);
});