Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 如何使用Browserify+;Watchify&x2B;Tsify&x2B;吞下多个输入文件并快速获得多个输出文件_Typescript_Gulp_Browserify_Watchify_Tsify - Fatal编程技术网

Typescript 如何使用Browserify+;Watchify&x2B;Tsify&x2B;吞下多个输入文件并快速获得多个输出文件

Typescript 如何使用Browserify+;Watchify&x2B;Tsify&x2B;吞下多个输入文件并快速获得多个输出文件,typescript,gulp,browserify,watchify,tsify,Typescript,Gulp,Browserify,Watchify,Tsify,我一直在尝试使用Gulp+Browserify+Tsify将4个文件及其库转换为4个js文件,目前需要4到5秒钟 在我当前的构建脚本中,watchify会触发对我所有4个构建管道的任何ts文件的更改的更新,每个管道都会传输我项目中的每个ts文件,即使它没有在该管道中使用 目前,我正在寻找一种更好更快的方法来完成这项工作 import * as browserify from "browserify" import * as gulp from "gulp" import * as rename

我一直在尝试使用Gulp+Browserify+Tsify将4个文件及其库转换为4个js文件,目前需要4到5秒钟

在我当前的构建脚本中,watchify会触发对我所有4个构建管道的任何ts文件的更改的更新,每个管道都会传输我项目中的每个ts文件,即使它没有在该管道中使用

目前,我正在寻找一种更好更快的方法来完成这项工作

import * as browserify from "browserify"
import * as gulp from "gulp"
import * as rename from "gulp-rename"
import * as sass from "gulp-sass"
import * as uglify from "gulp-uglify"
import * as gutil from "gulp-util"
import * as fs from "fs"
import * as tsify from "tsify"
import * as buffer from "vinyl-buffer"
import * as source from "vinyl-source-stream"
import * as watchify from "watchify"
import {assign} from "lodash"

const sassOptions: sass.Options = {
    outputStyle: 'expanded'
}

function combinejs(update: boolean, minify: boolean) {
    ['backScript.ts', 'mainFrame.ts', 'commentFrame.ts','script-inject.ts'].forEach(f => {
        let b = browserify(assign({},watchify.args,{basedir:'src/', entries: f})),
            bundle = () => {
                let pipe = b.bundle().on('error',gutil.log)
                    .pipe(source(f)).pipe(rename({extname:'.js'}))
                if (minify) {
                    pipe = pipe.pipe(buffer()).pipe(uglify())
                }
                pipe.pipe(gulp.dest('build/files/src'))
            }
        b.plugin(tsify)
        if (update){
            b.plugin(watchify,{})
            b.on('update',()=>{
                console.log({update:f})
                bundle()
            })
        }
        b.on('log', console.log)
        console.log(f)
        bundle()
    })
}

gulp.add('js', () => combinejs(false, true))

gulp.add('css', () => {
    gulp.src('src/*.scss')
        .pipe(sass(sassOptions))
        .pipe(gulp.dest('build/files/src'))
})
gulp.add('copyFiles', () => {
    gulp.src(['manifest.json', 'popup.html', 'images/*'], { base: '.' })
        .pipe(gulp.dest('build/files'))
})

gulp.add("default", ['copyFiles','js', 'css'])
gulp.add('build',['copyFiles','css'],()=>{
    combinejs(false,false)
})

gulp.add("watch", ['copyFiles', 'css'], () => {
    combinejs(true, false)
    gulp.watch('src/*.scss', ['css'])
})

确保排除库(如
jquery
lodash
等)

您可能会从
因子包
(特别是
gulp watchify factor bundle
)中获益,将条目拆分,但将其共享代码保存在单独的库中。如果它们有很多共享代码,则会在每个条目的输出中编译和复制这些代码
tsify
+
gulp watchify factor bundle
似乎没有很好的文档记录,因此我猜测您可能需要什么:

const gulp = require("gulp");
const reduce = require("gulp-watchify-factor-bundle");
const buffer = require("vinyl-buffer");
const filelog = require("gulp-filelog");
const tsify = require("tsify");
const gutil = require("gulp-util");

const devenv = true; // bring your own env config


function configuredUglify() {
    return uglify(); // bring your own uglify
}

gulp.task("clean", () => {
    return del("./bin/js");
});

// full builds
gulp.task("build", ["clean"], () => {
    const basedir = __dirname;

    // browserify options here ()
    const b = reduce.create({
        basedir: basedir,
        debug: devenv,
        // the following may not apply to you
        builtins: [],
        insertGlobals: false,
        detectGlobals: false,
        browserField: false,
        commondir: false
    });

    // any large external libraries should be excluded (and loaded via <script src="..."> tags especially for development)
    b.exclude("jquery");
    b.exclude("moment");

    b.plugin(tsify);

    // NOTE: unlike browserify entries, reduce.src allows globs (like gulp.src) but will consider them a single bundle
    // this is especially handy for unit tests!
    return reduce.src(["backScript.ts", "mainFrame.ts", "commentFrame.ts", "script-inject.ts"], { cwd: basedir })
        // "outputs:" is required otherwise the .ts extension is kept (unlike jsify with vanilla browserify)
        // full:
        .pipe(reduce.bundle(b, { common: "sharedLib.js", outputs: ["backScripts.js", "mainFrame.js", "commentFrame.js", "script-inject.js"] }))
        // incrementals:
        // .pipe(reduce.watch(b, { common: "sharedLib.js", outputs: ["backScripts.js", "mainFrame.js", "commentFrame.js", "script-inject.js"] }))

        // incrementals:
        // .on("bundle", (vinylStream) => { /* vinylStream followed by lines below */ })

        .pipe(buffer())
        .pipe(devenv ? sourcemaps.init({ loadMaps: true, largeFile: true }) : gutil.noop()) // largeFile may be applicable
        .pipe(!devenv ? configuredUglify() : gutil.noop())
        .on("error", gutil.log)
        .pipe(devenv ? sourcemaps.write("./") : gutil.noop())
        .pipe(filelog("browserify"))
        .pipe(reduce.dest("./bin/js/"));
});
const gulp=require(“gulp”);
const reduce=require(“吞咽watchify因子包”);
常量缓冲=需要(“乙烯基缓冲”);
const filelog=require(“gulp filelog”);
const tsify=require(“tsify”);
const gutil=require(“gulp util”);
const devenv=true;//带上你自己的环境配置
函数配置duglify(){
return uglify();//带上你自己的丑
}
狼吞虎咽的任务(“清理”,()=>{
返回del(“/bin/js”);
});
//完整版本
吞咽任务(“构建”、[“清理”]、()=>{
const basedir=uu dirname;
//在此处浏览选项()
const b=reduce.create({
basedir:basedir,
调试:devenv,
//以下内容可能不适用于您
内置:[],
insertGlobals:false,
detectGlobals:错误,
布朗瑟菲尔德:错,
commondir:错误
});
//应排除任何大型外部库(并通过标签加载,尤其是用于开发的标签)
b、 排除(“jquery”);
b、 排除(“时刻”);
b、 插件(tsify);
//Note:不同于BrSeriTIVE条目,Real.Src允许GLUs(如GULP .Src),但会将它们视为单个束。
//这对于单元测试尤其方便!
返回reduce.src([“backScript.ts”、“mainFrame.ts”、“commentFrame.ts”、“script inject.ts”],{cwd:basedir})
//“outputs:”是必需的,否则保留.ts扩展名(不同于jsify和vanilla browserify)
//全部:
.pipe(reduce.bundle(b,{common:“sharedLib.js”,输出:[“backScripts.js”、“mainFrame.js”、“commentFrame.js”、“script inject.js”]}))
//增量:
//.pipe(reduce.watch(b,{common:“sharedLib.js”,输出:[“backScripts.js”、“mainFrame.js”、“commentFrame.js”、“script inject.js”]}))
//增量:
//.on(“束”,(乙烯基流)=>{/*乙烯基流,后跟下面的行*/})
.pipe(缓冲区())
.pipe(devenv?sourcemaps.init({loadMaps:true,largeFile:true}):gutil.noop())//largeFile可能适用
.pipe(!devenv?configuredUglify():gutil.noop())
.on(“错误”,gutil.log)
.pipe(devenv?sourcemaps.write(“./”):gutil.noop())
.pipe(文件日志(“browserify”))
.管道(减少目的地(“/bin/js/”);
});

确保排除库(如
jquery
lodash
等)

您可能会从
因子包
(特别是
gulp watchify factor bundle
)中获益,将条目拆分,但将其共享代码保存在单独的库中。如果它们有很多共享代码,则会在每个条目的输出中编译和复制这些代码
tsify
+
gulp watchify factor bundle
似乎没有很好的文档记录,因此我猜测您可能需要什么:

const gulp = require("gulp");
const reduce = require("gulp-watchify-factor-bundle");
const buffer = require("vinyl-buffer");
const filelog = require("gulp-filelog");
const tsify = require("tsify");
const gutil = require("gulp-util");

const devenv = true; // bring your own env config


function configuredUglify() {
    return uglify(); // bring your own uglify
}

gulp.task("clean", () => {
    return del("./bin/js");
});

// full builds
gulp.task("build", ["clean"], () => {
    const basedir = __dirname;

    // browserify options here ()
    const b = reduce.create({
        basedir: basedir,
        debug: devenv,
        // the following may not apply to you
        builtins: [],
        insertGlobals: false,
        detectGlobals: false,
        browserField: false,
        commondir: false
    });

    // any large external libraries should be excluded (and loaded via <script src="..."> tags especially for development)
    b.exclude("jquery");
    b.exclude("moment");

    b.plugin(tsify);

    // NOTE: unlike browserify entries, reduce.src allows globs (like gulp.src) but will consider them a single bundle
    // this is especially handy for unit tests!
    return reduce.src(["backScript.ts", "mainFrame.ts", "commentFrame.ts", "script-inject.ts"], { cwd: basedir })
        // "outputs:" is required otherwise the .ts extension is kept (unlike jsify with vanilla browserify)
        // full:
        .pipe(reduce.bundle(b, { common: "sharedLib.js", outputs: ["backScripts.js", "mainFrame.js", "commentFrame.js", "script-inject.js"] }))
        // incrementals:
        // .pipe(reduce.watch(b, { common: "sharedLib.js", outputs: ["backScripts.js", "mainFrame.js", "commentFrame.js", "script-inject.js"] }))

        // incrementals:
        // .on("bundle", (vinylStream) => { /* vinylStream followed by lines below */ })

        .pipe(buffer())
        .pipe(devenv ? sourcemaps.init({ loadMaps: true, largeFile: true }) : gutil.noop()) // largeFile may be applicable
        .pipe(!devenv ? configuredUglify() : gutil.noop())
        .on("error", gutil.log)
        .pipe(devenv ? sourcemaps.write("./") : gutil.noop())
        .pipe(filelog("browserify"))
        .pipe(reduce.dest("./bin/js/"));
});
const gulp=require(“gulp”);
const reduce=require(“吞咽watchify因子包”);
常量缓冲=需要(“乙烯基缓冲”);
const filelog=require(“gulp filelog”);
const tsify=require(“tsify”);
const gutil=require(“gulp util”);
const devenv=true;//带上你自己的环境配置
函数配置duglify(){
return uglify();//带上你自己的丑
}
狼吞虎咽的任务(“清理”,()=>{
返回del(“/bin/js”);
});
//完整版本
吞咽任务(“构建”、[“清理”]、()=>{
const basedir=uu dirname;
//在此处浏览选项()
const b=reduce.create({
basedir:basedir,
调试:devenv,
//以下内容可能不适用于您
内置:[],
insertGlobals:false,
detectGlobals:错误,
布朗瑟菲尔德:错,
commondir:错误
});
//应排除任何大型外部库(并通过标签加载,尤其是用于开发的标签)
b、 排除(“jquery”);
b、 排除(“时刻”);
b、 插件(tsify);
//Note:不同于BrSeriTIVE条目,Real.Src允许GLUs(如GULP .Src),但会将它们视为单个束。
//这对于单元测试尤其方便!
返回reduce.src([“backScript.ts”、“mainFrame.ts”、“commentFrame.ts”、“script inject.ts”],{cwd:basedir})
//“outputs:”是必需的,否则保留.ts扩展名(不同于jsify和vanilla browserify)
//全部:
.pipe(reduce.bundle(b,{common:“sharedLib.js”,输出:[“backScripts.js”、“mainFrame.js”、“commentFrame.js”、“script inject.js”]}))
//增量:
//.pipe(reduce.watch(b,{common:“sharedLib.js”,输出:[“backScripts.js”、“mainFrame.js”、“commentFrame.js”、“script inject.js”]}))
//增量:
//.on(“束”,(乙烯基流)=>{/*乙烯基流,后跟下面的行*/})
.pipe(缓冲区())
.pipe(devenv?sourcemaps.init({loadMaps:true,largeFile:true}):gutil.noop())//largeFile可能适用
.pipe(!devenv?configuredUglify():gutil.noop())
.on(“错误”,gutil.log)
.pipe(devenv?sourcemaps.write(“./”):gutil.noop())
.pipe(文件日志(“browserify”))