ES6 transpiler + browserify +一饮而尽(ES6 transpiler +

2019-11-03 07:20发布

我想创建一个大口的任务,需要从(CJS使用)的ES6模块transpile生成的代码,并使用browserify来把它转换成一个单一的文件。 我有下面的代码: -

var gulp = require('gulp');

var browserify = require('gulp-browserify');
var es6transpiler = require('gulp-es6-module-transpiler');
var rjs = require('gulp-requirejs');


gulp.task('jscompile', function () {
    gulp.src('./app/scripts/**/*.js')
        .pipe(es6transpiler({
               type : "cjs"
            }))
        .pipe(gulp.dest('./dist'))
        .pipe(browserify(
            './dist/app.js'
        ))
        .pipe('dist');
});

不过,我不断收到以下错误: -

C:\dev\temp\angulargulp>gulp jscompile
[gulp] Using gulpfile C:\dev\temp\angulargulp\gulpfile.js
[gulp] Starting 'jscompile'...
[gulp] 'jscompile' errored after 5.48 ms Object dist has no method 'on'

C:\dev\temp\angulargulp\node_modules\gulp-browserify\node_modules\browserify\index.js:39
    if (xopts) Object.keys(xopts).forEach(function (key) {
                      ^
TypeError: Object.keys called on non-object
    at Function.keys (native)
    at module.exports (C:\dev\temp\angulargulp\node_modules\gulp-browserify\node_modules\browserify\index.js:39:23)
    at Transform.transform [as _transform] (C:\dev\temp\angulargulp\node_modules\gulp-browserify\index.js:91:19)
    at Transform._read (C:\dev\temp\angulargulp\node_modules\gulp-browserify\node_modules\through2\node_modules\readable-stream\lib\_stream_transform.js:184:10)
    at Transform._write (C:\dev\temp\angulargulp\node_modules\gulp-browserify\node_modules\through2\node_modules\readable-stream\lib\_stream_transform.js:172:12)
    at doWrite (C:\dev\temp\angulargulp\node_modules\gulp-browserify\node_modules\through2\node_modules\readable-stream\lib\_stream_writable.js:237:10)
    at writeOrBuffer (C:\dev\temp\angulargulp\node_modules\gulp-browserify\node_modules\through2\node_modules\readable-stream\lib\_stream_writable.js:227:5)
    at Transform.Writable.write (C:\dev\temp\angulargulp\node_modules\gulp-browserify\node_modules\through2\node_modules\readable-stream\lib\_stream_writable.js:194:11)
    at DestroyableTransform.ondata (stream.js:51:26)
    at DestroyableTransform.EventEmitter.emit (events.js:95:17)

我是新来CommonJS的,节点和咕嘟咕嘟,所以道歉,如果这是一个愚蠢的问题。

谢谢。

编辑我有以下文件: -应用程序/脚本/ MyController.js

var MyController = function($scope, MyService, $http) {
    $scope.doSomething = function() {
        console.log("Yo");
    }
}

export default MyControllerDef = [ "$scope", "MyService", "$http", MyController ];

应用程序/脚本/ app.js

/*jshint unused: vars */

import MyControllerDef from 'MyController';
import GetBookingServiceDef from 'bookingsummary/services/GetBookingService';

angular.module("test")
      .controller("MyController", MyControllerDef);

angular.module("test")
      .services("GetBookingService", GetBookingServiceDef);

应用程序/脚本/服务/ GetBookingService.js

"use strict";

var GetBookingService = function($rootScope, $http) {
    return {
        retrieveBooking : function() {
            return $http.get("/scripts/dummyValues/booking.js").then(function(inResponse) {
                if (inResponse.data instanceof String)
                    return JSON.toJSON(inResponse.data);
                 return inResponse.data;
            });
        },
        notifyBookingUpdates : function(data) {
            $rootScope.$emit("bookingsummary.update", data);
        },
        listenToBookingUpdates : function(bookingUpdateFn) {
            $rootScope.$on("bookingsummary.update", function(e, data) {
                bookingUpdateFn(data);
            });
        }
    };
};

export default GetBookingServiceDef = [ "$rootScope", "$http", GetBookingService ];

根据下面的反馈,我已经修改了我一口脚本: -

var gulp = require('gulp');

var browserify = require('gulp-browserify');
var es6transpiler = require('gulp-es6-module-transpiler');

gulp.task('jscompile', function () {
    return gulp.src('./app/scripts/**/*.js')
        .pipe(es6transpiler({
               type : "cjs"
            }))
        .pipe(gulp.dest('./dist'))
        .pipe(browserify())
        .pipe('./dist/finalapp.js');
});

这已经修改了错误这样的: -

C:\dev\temp\angulargulp>gulp jscompile
[gulp] Using gulpfile C:\dev\temp\angulargulp\gulpfile.js
[gulp] Starting 'jscompile'...
[gulp] 'jscompile' errored after 5.24 ms Object ./dist/finalapp.js has no method 'on'

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: module "MyController" not found from "C:\\dev\\temp\\angulargulp\\dist\\fake_da0c6a27.js"
    at notFound (C:\dev\temp\angulargulp\node_modules\gulp-browserify\node_modules\browserify\index.js:803:15)
    at C:\dev\temp\angulargulp\node_modules\gulp-browserify\node_modules\browserify\index.js:754:23
    at C:\dev\temp\angulargulp\node_modules\gulp-browserify\node_modules\browserify\node_modules\browser-resolve\index.js:185:24
    at C:\dev\temp\angulargulp\node_modules\gulp-browserify\node_modules\browserify\node_modules\resolve\lib\async.js:44:14
    at process (C:\dev\temp\angulargulp\node_modules\gulp-browserify\node_modules\browserify\node_modules\resolve\lib\async.js:113:43)
    at C:\dev\temp\angulargulp\node_modules\gulp-browserify\node_modules\browserify\node_modules\resolve\lib\async.js:122:21
    at load (C:\dev\temp\angulargulp\node_modules\gulp-browserify\node_modules\browserify\node_modules\resolve\lib\async.js:54:43)
    at C:\dev\temp\angulargulp\node_modules\gulp-browserify\node_modules\browserify\node_modules\resolve\lib\async.js:60:22
    at C:\dev\temp\angulargulp\node_modules\gulp-browserify\node_modules\browserify\node_modules\resolve\lib\async.js:16:47
    at Object.oncomplete (fs.js:107:15)

Answer 1:

使用这样的:

    .pipe(browserify())
    .pipe(gulp.dest('./dist/app.js'));

需要注意的是browserify插件需要一个对象{}作为选项。 不是一个字符串作为目的地。 退房一饮而尽,browserify插件,如何使用它。



文章来源: ES6 transpiler + browserify + gulp