汇总通过命令行从Gulp进行捆绑,但不是来自汇总流包

时间:2018-03-18 05:17:44

标签: typescript gulp child-process rollupjs

GitHub Issue
使用rollup-stream包时收到以下错误:

[10:01:14] Using gulpfile ~/myUser/myProjectFolder/myTestProject/gulpfile.js  
[10:01:14] Starting 'default'...  
[10:01:14] Starting 'transpile-it'...  
[10:01:16] Finished 'transpile-it' after 2.11 s  
[10:01:16] Starting 'bundle-it'...  

[10:01:16] 'bundle-it' errored after 31 ms  

[10:01:16] TypeError: parse is not a function in /home/myUser/myProjectFolder/myTestProject/obj/main.js  
    at error (/home/myUser/myProjectFolder/myTestProject/node_modules/rollup/dist/rollup.js:185:14)  
    at Promise.resolve.then.catch.err (/home/myUser/myProjectFolder/myTestProject/node_modules/rollup/dist/rollup.js:9182:6)  
    at <anonymous>  
    at process._tickDomainCallback (internal/process/next_tick.js:228:7)  

[10:01:16] 'default' errored after 2.15 s  

我在这种情况下使用以下package.json:

{  
  "name": "myTestProject",  
  "version": "1.0.0",  
  "description": "",  
  "main": "index.js",  
  "scripts": {  
    "test": "echo \"Error: no test specified\" && exit 1"  
  },  
  "keywords": [],  
  "author": "",  
  "license": "ISC",  
  "devDependencies": {  
    "@types/node": "^9.4.7",  
    "gulp": "^4.0.0",  
    "gulp-typescript": "^4.0.1",  
    "rollup-plugin-commonjs": "^9.1.0",  
    "rollup-plugin-copy": "^0.2.3",  
    "rollup-plugin-node-resolve": "^3.3.0",  
    "rollup-stream": "^1.24.1",  
    "typescript": "^2.7.2",  
    "vinyl-source-stream": "^2.0.0"  
  },  
  "dependencies": {  
    "asciify": "^1.3.5"  
  }  
}  

以及以下gulpfile.js:

var gulp = require('gulp');  
var rollup = require('rollup-stream');  
var source = require('vinyl-source-stream');  
var commonjs = require('rollup-plugin-commonjs');  
var resolve = require('rollup-plugin-node-resolve');  
var copy = require('rollup-plugin-copy');  
var ts = require('gulp-typescript');  
var tsProject = ts.createProject('tsconfig.json');  
gulp.task('transpile-it', function () {  
    return tsProject.src()  
        .pipe(tsProject())  
        .pipe(gulp.dest('obj'));  
});  
gulp.task('bundle-it', function () {  
    return rollup({  
            exports: 'named',  
            sourcemap: false,  
            input: 'obj/main.js',  
            format: 'cjs',  
            plugins: [  
                commonjs(),  
                resolve({  
                    extensions: ['.js', '.json', '.flt']  
                }),  
                copy({  
                    "node_modules/asciify/lib/figlet-js/fonts/short.flf": "prd/fonts/short.flf",  
                    verbose: true  
                })  
            ]  
        })  
        .pipe(source('main.js'))  
        .pipe(gulp.dest('./prd'));  
});  
gulp.task('default', gulp.series('transpile-it', 'bundle-it'));  

为了解决这个问题,我改为使用child_process包和以下rollup.config.js文件调用rollup direct,并且一切正常:

import commonjs from 'rollup-plugin-commonjs';  
import resolve  from 'rollup-plugin-node-resolve';  
import copy from 'rollup-plugin-copy';      
export default {  
  exports: 'named',   
  sourceMap: true,  
  input: 'obj/main.js',  
  output: {  
    file: './prd/main.js',  
    format: 'cjs'  
  },  
  plugins: [  
    commonjs(),  
    resolve({  
      extensions: [ '.js', '.json', '.flt' ],  
    }),  
    copy({  
       "node_modules/asciify/lib/figlet-js/fonts/short.flf": "prd/fonts/short.flf",  
       verbose: true  
    })  
  ]  
};  

gulpfile.js文件变为:

var gulp = require('gulp');  
var ts = require('gulp-typescript');  
var tsProject = ts.createProject('tsconfig.json');  
var cp = require('child_process');  
gulp.task('transpile-it', function () {  
    return tsProject.src()  
        .pipe(tsProject())  
        .pipe(gulp.dest('obj'));  
});  
gulp.task('bundle-it', function () {  
    const child = cp.exec('rollup -c', [], (error, stdout, stderr) => {  
        if (error) {  
            throw error;  
        }  
        let logIfNotNull = function (value) {  
            if (Boolean(value)) {  
                console.log(value);  
            }  
        }  
        logIfNotNull(error);          
        logIfNotNull(stderr);  
        logIfNotNull(stdout);  
    });  
    return child;  
});  
gulp.task('default', gulp.series('transpile-it', 'bundle-it'));    

配置是一样的,我只是通过shell调用而不是使用rollup-stream包,rollup-stream包有问题吗?

重现的完整脚本:

mkdir -p myTestProject/{obj,prd,src/M}
cd myTestProject
npm init -y
npm install -D typescript gulp gulp-typescript @types/node rollup-stream vinyl-source-stream rollup-plugin-commonjs rollup-plugin-node-resolve rollup-plugin-copy
npm install -D gulp@next
npm install -S asciify
cat <<EOT> ./tsconfig.json
{
  "compilerOptions": {
    "target": "ES5",
    "module": "commonjs",
    "strict": true,
    "sourceMap": true,
    "allowSyntheticDefaultImports": true
  }
}
EOT
cat <<EOT> ./gulpfile.js
var gulp = require('gulp');
var rollup = require('rollup-stream');
var source = require('vinyl-source-stream');
var commonjs = require('rollup-plugin-commonjs');
var resolve = require('rollup-plugin-node-resolve');
var copy = require('rollup-plugin-copy');
var ts = require('gulp-typescript');
var tsProject = ts.createProject('tsconfig.json');
gulp.task('transpile-it', function () {
    return tsProject.src()
        .pipe(tsProject())
        .pipe(gulp.dest('obj'));
});
gulp.task('bundle-it', function () {
    return rollup({
            exports: 'named',
            sourcemap: false,
            input: 'obj/main.js',
            format: 'cjs',
            plugins: [
                commonjs(),
                resolve({
                    extensions: ['.js', '.json', '.flt']
                }),
                copy({
                    "node_modules/asciify/lib/figlet-js/fonts/short.flf": "prd/fonts/short.flf",
                    verbose: true
                })
            ]
        })
        .pipe(source('main.js'))
        .pipe(gulp.dest('./prd'));
});
gulp.task('default', gulp.series('transpile-it', 'bundle-it'));
EOT
cat <<EOT> ./src/M/AuxClass.ts
class AuxClass{
    getMessage(){
        return "Class Works!";
    }
}
export { AuxClass }
EOT
cat <<EOT> ./src/main.ts
import { AuxClass } from "./M/AuxClass"
export class main {
  init() {
    let a = new AuxClass();
    let text = a.getMessage();
    let fontName = "short";
    let callback = function(err: any, value: any): void {
      if (err != null) {
        console.log(err);
      }
      console.log(value);
    };
    var asciify = require("./../node_modules/asciify/index.js");
    asciify(text, fontName, callback);
  }
}
let m = new main();
m.init();
EOT

1 个答案:

答案 0 :(得分:2)

您必须将rollup-plugin-commonjs回滚到8.4.1。这为我解决了。