使用GULP打包TS库文件

1 篇文章 0 订阅
1 篇文章 0 订阅

 gulpfile.js

const gulp = require('gulp')
const minify = require('gulp-minify');
const replace = require('gulp-string-replace');
const ts = require('gulp-typescript');
const rename = require("gulp-rename");
const concat = require('gulp-concat');
const uglify = require('gulp-uglify-es').default;
const rollup = require('rollup')
const fs = require("fs");
const tsProject = ts.createProject('tsconfig.json', { declaration: true, "removeComments": true });


const onwarn = warning => {
    // Silence circular dependency warning for moment package
    if (warning.code === 'CIRCULAR_DEPENDENCY')
        return

    console.warn(`(!) ${warning.message}`)
}

let first = true;
//生成 js文件, 需要有命名空间的类写法,打包库文件,无需入口文件
gulp.task('buildJs', () => {
    return tsProject.src().pipe(tsProject())
        .pipe(replace('var long;', function () {//替换第一行文本
            if (first) {
                first = false;
                return "window.long = {};";
            }
            else
                return "";
        }, { logs: { enabled: false } }))
        .pipe(gulp.dest('./dist/long/'));
})

// gulp.task("buildDts", () => {
//     return tsProject.src()
//         .pipe(tsProject())
//         .dts.pipe(gulp.dest('./build/'))
// });

gulp.task('buildDts', () => {
    return gulp
        .src(['./scripts/long/**/*.ts'])//, './!(node_modules)/*.ts', './!(src)/*.ts'
        .pipe(ts({
            // 这里对应参数 
            // { "declaration": true }
            // /* Generates corresponding '.d.ts' file. */
            "declaration": true,
            "removeComments": true
        }))
        .pipe(gulp.dest('./build/long/'))
});

//打包所有 d.ts文件 ,在jsmin任务执行完后执行
// gulp.task('concatAllTs', function () {
//     gulp.src('build/long/**/*.d.ts')
//         .pipe(concat('long.d.ts')) //合并之后的文件名
//         .pipe(gulp.dest('dist/long/')) //合并之后保存的路径
// });

//运行文件时,有入口时使用,一般编译文件时使用次,
gulp.task("rollup", async function () {
    let config = {
        // input: "build/Long.js",
        input: "src/Main.ts",
        external: ['Laya', "fgui"],
        onwarn: onwarn,
        output: {
            file: 'bin/bundle.js',//运行文件
            format: 'umd',
            extend: true,
            name: 'long'
        }
    };
    const subTask = await rollup.rollup(config);
    await subTask.write(config);
});

//压缩混淆并生成min.js
gulp.task("uglify", function () {
    return gulp.src("dist/long/long.js")
        .pipe(rename({ suffix: '.min' }))
        .pipe(uglify({
            mangle: {
                keep_fnames: true
            }
        }))
        .pipe(gulp.dest("dist/long/"));
});


gulp.task('build'
    , gulp.series(
        gulp.parallel('buildJs'),
        gulp.parallel('uglify'),
        gulp.parallel('buildDts'),
        // gulp.parallel("concatAllTs")
        // gulp.parallel('rollup'),

    )
)

见附件

scripts目录中ts文件标准为namespace long(自定义命名空间),必须此方式,才能打包

如Logger.ts


namespace long {

    /**
     * 日志类
     */
    export class Logger {

        private static readonly isNet: boolean = false;

        /**是否显示日志 */
        public static isShowLog: boolean = false;

        public constructor() {
        }

        public static log(obj: any, str: any): void {
            if (Logger.isShowLog) {
                Logger.outPut("[log] [" + Logger.getClassName(obj) + "] " + str);
            }
        }
    }
}

如见

https://github.com/taojianong/OpenLongLib

解压包

cmd 打开解压包对应的目录

cnpm install 安装依赖

使用VSC打开目录,执行gulp build任务后,将在dist/long下生成新的long.js,long.min.js,long.d.ts.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值