UNPKG

@tsdi/pack

Version:

@tsdi/pack is simple build tasks, base on AOP, Ioc container, via @tsdi. dev build pack activities.

359 lines (357 loc) 16 kB
import { __awaiter, __decorate, __metadata } from "tslib"; import { isBoolean, isArray, lang, Inject } from '@tsdi/ioc'; import { Input } from '@tsdi/components'; import { Task, Activities } from '@tsdi/activities'; import { PlatformService } from '../PlatformService'; import { join } from 'path'; const through = require('through2'); const resolve = require('rollup-plugin-node-resolve'); const rollupSourcemaps = require('rollup-plugin-sourcemaps'); const commonjs = require('rollup-plugin-commonjs'); const rename = require('gulp-rename'); const uglify = require('gulp-uglify-es').default; let LibPackBuilder = class LibPackBuilder { constructor() { } toOutputPath(...mdpath) { return join(...[this.outDir, ...mdpath.filter(f => f)]); } toModulePath(input, ...paths) { return join(...[ this.outDir, this.getModuleFolder(input), ...paths.filter(f => f) ]); } getTargetPath(input) { return this.toOutputPath(this.getTargetFolder(input)); } getTargetFolder(input) { return input.targetFolder || input.target; } getModuleFolder(input) { return input.moduleFolder || (isArray(input.moduleName) ? lang.first(input.moduleName) : input.moduleName); } transRollupInput(input) { let inputs = input.input; return isArray(inputs) ? inputs.map(i => this.toOutputPath(i)) : this.toOutputPath(inputs); } transRollupoutput(input) { return { format: input.format || 'cjs', file: input.outputFile ? this.toModulePath(input, input.outputFile) : undefined, dir: input.outputFile ? undefined : this.toModulePath(input), }; } transCompileOptions(input) { if (input.target) { return input.module ? { target: input.target, module: input.module } : { target: input.target }; } return {}; } getDtsPath(input) { let targetpath = this.getTargetPath(input); return this.dts ? join(targetpath, this.dts) : (input.dtsMain ? targetpath : null); } transPlugins(ctx) { let beforeResolve = this.beforeResolve || []; let sourcemap = this.sourcemap; let input = ctx.getInput(); return this.plugins ? [ ...beforeResolve, ...this.plugins, sourcemap ? rollupSourcemaps(isBoolean(sourcemap) ? undefined : sourcemap) : null ] : [ ...beforeResolve, resolve({ browser: input.format === 'umd' }), commonjs({ extensions: ['.js', '.ts', '.tsx'] }), sourcemap ? rollupSourcemaps(isBoolean(sourcemap) ? undefined : sourcemap) : null ]; } getBundleSrc(input) { return isArray(input.input) ? this.toModulePath(input, '/**/*.js') : this.toModulePath(input, input.outputFile); } json(json, input) { // to replace module export. if (input.target) { json[input.target] = ['.', this.getTargetFolder(input), input.main || 'index.js'].join('/'); } let outmain = ['.', this.getModuleFolder(input), input.outputFile || 'index.js'].join('/'); if (isArray(input.moduleName)) { input.moduleName.forEach(n => { json[n] = outmain; }); } else if (input.moduleName) { json[input.moduleName] = outmain; } if (input.dtsMain) { json['typings'] = ['.', this.getTargetFolder(input), input.dtsMain].join('/'); } return json; } onAfterInit() { return __awaiter(this, void 0, void 0, function* () { if (!this.external) { let func = (ctx) => { let packagejson = ctx.platform.getPackage(); let external = [ 'process', 'util', 'path', 'fs', 'events', 'stream', 'child_process', 'os', 'https', 'http', 'url', 'crypto', ...(this.externalLibs || []), ...Object.keys(packagejson.dependencies || {}), ...Object.keys(packagejson.peerDependencies || {}) ]; if (external.indexOf('rxjs')) { external.push('rxjs/operators'); } if (this.includeLib && this.includeLib.length) { external = external.filter(ex => this.includeLib.indexOf(ex) < 0); } return external; }; this.external = (ctx) => { return func(ctx); }; } // if (!this.plugins) { // this.plugins = async (ctx: NodeActivityContext, bind) => { // let beforeResolve = this.beforeResolve; // let sourcemap = this.sourcemap; // let input = bind.getInput<LibBundleOption>(); // return [ // ...(beforeResolve || []), // resolve({ browser: input.format === 'umd' }), // commonjs({ extensions: ['.js', '.ts', '.tsx'] }), // sourcemap ? rollupSourcemaps(isBoolean(sourcemap) ? undefined : sourcemap) : null // ]; // }; // } }); } static ρAnn() { return { "name": "LibPackBuilder", "params": { "toOutputPath": ["mdpath"], "toModulePath": ["input", "paths"], "getTargetPath": ["input"], "getTargetFolder": ["input"], "getModuleFolder": ["input"], "transRollupInput": ["input"], "transRollupoutput": ["input"], "transCompileOptions": ["input"], "getDtsPath": ["input"], "transPlugins": ["ctx"], "getBundleSrc": ["input"], "json": ["json", "input"] } }; } }; __decorate([ Inject(), __metadata("design:type", PlatformService) ], LibPackBuilder.prototype, "platform", void 0); __decorate([ Input(), __metadata("design:type", Object) ], LibPackBuilder.prototype, "src", void 0); __decorate([ Input(), __metadata("design:type", Object) ], LibPackBuilder.prototype, "test", void 0); __decorate([ Input(), __metadata("design:type", Array) ], LibPackBuilder.prototype, "bundles", void 0); __decorate([ Input(), __metadata("design:type", String) ], LibPackBuilder.prototype, "outDir", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], LibPackBuilder.prototype, "annotation", void 0); __decorate([ Input(), __metadata("design:type", String) ], LibPackBuilder.prototype, "dts", void 0); __decorate([ Input(), __metadata("design:type", Object) ], LibPackBuilder.prototype, "external", void 0); __decorate([ Input(), __metadata("design:type", Array) ], LibPackBuilder.prototype, "externalLibs", void 0); __decorate([ Input(), __metadata("design:type", Array) ], LibPackBuilder.prototype, "includeLib", void 0); __decorate([ Input(), __metadata("design:type", Array) ], LibPackBuilder.prototype, "plugins", void 0); __decorate([ Input(), __metadata("design:type", Object) ], LibPackBuilder.prototype, "globals", void 0); __decorate([ Input(), __metadata("design:type", Object) ], LibPackBuilder.prototype, "cache", void 0); __decorate([ Input(), __metadata("design:type", Object) ], LibPackBuilder.prototype, "watch", void 0); __decorate([ Input(), __metadata("design:type", Array) ], LibPackBuilder.prototype, "replaces", void 0); __decorate([ Input(), __metadata("design:type", Object) ], LibPackBuilder.prototype, "options", void 0); __decorate([ Input({ defaultValue: true }), __metadata("design:type", Object) ], LibPackBuilder.prototype, "sourcemap", void 0); __decorate([ Input(), __metadata("design:type", Array) ], LibPackBuilder.prototype, "beforeResolve", void 0); LibPackBuilder = __decorate([ Task({ selector: 'libs', template: [ { activity: 'clean', clean: 'binding: outDir' }, { activity: 'test', test: 'binding: test', }, { activity: 'asset', src: ['package.json', '*.md'], dist: 'binding: outDir' }, { activity: 'each', each: 'binding: bundles', body: [ { activity: Activities.if, condition: ctx => ctx.getInput().target, body: { activity: 'ts', src: 'binding: src', dist: (ctx, bind) => bind.getScope().getTargetPath(ctx.getInput()), dts: (ctx, bind) => bind.getScope().getDtsPath(ctx.getInput()), annotation: 'binding: annotation', sourcemap: 'binding: sourcemap', pipes: (ctx, bind) => { const replaces = bind.getScope().replaces; if (ctx.getInput().format == 'es' && replaces && replaces.length) { return [ () => through.obj(function (file, encoding, callback) { if (file.isNull()) { return callback(null, file); } if (file.isStream()) { return callback('doesn\'t support Streams'); } let contents = file.contents.toString('utf8'); replaces.forEach(r => { contents = contents.replace(r[0], r[1]); }); file.contents = new Buffer(contents, 'utf-8'); this.push(file); callback(); }) ]; } return []; }, tsconfig: (ctx, bind) => bind.getScope().transCompileOptions(ctx.getInput()) } }, { activity: Activities.if, condition: ctx => ctx.getInput().input, body: [ { activity: 'rollup', input: (ctx, bind) => bind.getScope().transRollupInput(ctx.getInput()), sourcemap: 'binding: sourcemap', plugins: (ctx, bind) => bind.getScope().transPlugins(ctx), external: 'binding: external', options: 'binding: options', globals: 'binding: globals', output: (ctx, bind) => bind.getScope().transRollupoutput(ctx.getInput()) }, { activity: Activities.if, condition: (ctx, bind) => bind.getInput().uglify, body: { activity: 'asset', src: (ctx, bind) => bind.getScope().getBundleSrc(ctx.getInput()), dist: (ctx, bind) => bind.getScope().toModulePath(bind.getInput()), sourcemap: 'binding: sourcemap | path:"./"', pipes: [ () => uglify(), () => rename({ suffix: '.min' }) ] } } ] }, { activity: Activities.if, condition: ctx => ctx.getInput().moduleName || ctx.getInput().target, body: { activity: 'asset', src: (ctx, bind) => bind.getScope().toOutputPath('package.json'), dist: 'binding: outDir', pipes: [ { activity: 'jsonEdit', json: (json, bind) => { let input = bind.getInput(); let scope = bind.getScope(); // to replace module export. if (input.target) { json[input.target] = ['.', scope.getTargetFolder(input), input.main || 'index.js'].join('/'); } let outmain = ['.', scope.getModuleFolder(input), input.outputFile || 'index.js'].join('/'); if (isArray(input.moduleName)) { input.moduleName.forEach(n => { json[n] = outmain; }); } else if (input.moduleName) { json[input.moduleName] = outmain; } if (input.dtsMain) { json['typings'] = ['.', scope.getTargetFolder(input), input.dtsMain].join('/'); } return json; } } ] } } ] } ] // template: ` // <clean [clean]="outDir"></clean> // <test [test]="outDir"></test> // <asset [src]="['package.json', '*.md']" [test]="outDir"></asset> // <sequence *each="let input in bundles"> // <rts *if="vaidts(input)" [input]="transRollupInput(input)" [sourcemap]="sourcemap" [plugins]="transPlugins(ctx)" [external]="external" // [options]="options" [globals]="globals" [output]="transRollupoutput(input)"></rts> // <rollup *elseif="input" [input]="transRollupInput(input)" [sourcemap]="sourcemap" [plugins]="transPlugins(ctx)" [external]="external" // [options]="options" [globals]="globals" [output]="transRollupoutput(input)"></rollup> // <asset *if="input.uglify" [src]="getBundleSrc(input)" [dist]="toModulePath(input)" [sourcemap]="sourcemap | path:'./'" ></asset> // <asset *if="input.moduleName || input.target" [src]="toOutputPath('package.json')" [dist]="outDir"> // <asset.pipes> // <jsonEdit [json]="json($event, input)"></jsonEdit> // </asset.pipes> // </asset> // </sequence> // ` }), __metadata("design:paramtypes", []) ], LibPackBuilder); export { LibPackBuilder }; //# sourceMappingURL=../sourcemaps/builds/LibPackBuilder.js.map