如何将bower包依赖关系保留在我的汇总包之外?

时间:2015-12-02 17:36:38

标签: javascript gulp bower ecmascript-6 rollupjs

我目前正在创建一个导出单个ES6模块的bower软件包。

在为我的包构建dist时,我使用汇总将所有内部模块移动到一个模块中,只导出一个模块。

Gulp任务:

// Bundle ES6 modules into a single file
gulp.task('bundle', function(){
  return gulp.src('./src/GuacaMarkdownEditor.js', {read: false})
    .pipe(rollup({
        // any option supported by rollup can be set here, including sourceMap
        // https://github.com/rollup/rollup/wiki/JavaScript-API
        format: 'es6',
        sourceMap: true
    }))
    .pipe(sourcemaps.write(".")) // this only works if the sourceMap option is true
    .pipe(gulp.dest('./dist'));
});

这一切都运行正常,但我从其他bower软件包中导入一些依赖项,我不想与我的模块捆绑(jQuery,font-awesome)。

我的问题是:如何保持捆绑我的代码并保留bower软件包的ES6导入语句 - 但是没有汇总将外部代码捆绑到我的软件包中?

示例:

"use strict";

import $ from 'jquery'; // dont bundle this!
import GuacaAirPopUp from './GuacaAirPopUp'; // bundle this!

export
default class GuacaMarkdownEditor {

  ...

}

3 个答案:

答案 0 :(得分:3)

您可以使用此汇总插件rollup-plugin-includepaths

它允许您按名称导入模块,并且应该从捆绑包中排除定义模块。我在rollup.config.js中使用了它:

import babel from 'rollup-plugin-babel'; 
import includePaths from 'rollup-plugin-includepaths'; 

var includePathOptions = { 
    paths: ['es6'], 
    include: { 
      'd3': './global/js/' + 'base/d3.min'  // include library in es6 modules
    },   
    external: ['d3'] // but don't bundle them into bundle.js
};

export default { 
 entry: './es6/entry.js', 
 plugins: [ 
 includePaths(includePathOptions), 
 babel() 
 ], 
 format: 'amd', 
 dest: 'build/bundle.js', 
 sourceMap: true 
 };

在es6模块中:

// not using relative path since it is handled by the plugin
import d3 from 'd3';
import other from 'otherModules'; 
//...

有关外部解决方案here

的更多讨论

答案 1 :(得分:1)

似乎汇总将检测命名导入(而不是相对路径),作为外部依赖项。

捆绑此模块时:

import GuacaAirPopUp from './GuacaAirPopUp';
import ControlHandlerService from './ControlHandlerService';
import DefaultHandlerConfig from './DefaultHandlerConfig';
import toMarkdown from 'to-markdown';
import $ from 'jquery';

捆绑者给出了这些消息:

Treating 'to-markdown' as external dependency
Treating 'jquery' as external dependency

捆绑使用此模块的应用程序时,使用browserify正确导入了jquery。

答案 2 :(得分:1)

安特尔已经回答了,但如果您想在下面排除自己制造的模块,我相信这是一个明确的解释。

https://github.com/rollup/rollup/wiki/JavaScript-API#external

  

应保留在软件包外部的模块的ID列表

// main.js
import myMod from './my-module'; // <-- this module you don't wanna import

// build.js <--- gulp file
import * as path from 'path';

//...more of you gulp file code

rollup.rollup({
  entry: 'app.js',
  external: [
    './my-module', // <--- node module to be excluded from the bundle
    path.resolve( './src/special-file.js' ) // <--- file you made to be excluded from the bundle
  ]
}).then(...)

//...more of you gulp file code

// Bundle ES6 modules into a single file
gulp.task('bundle', function(){
  return gulp.src('./src/GuacaMarkdownEditor.js', {read: false})
    .pipe(rollup({
        // any option supported by rollup can be set here, including sourceMap
        // https://github.com/rollup/rollup/wiki/JavaScript-API
        format: 'es6',
        sourceMap: true
    }))
    .pipe(sourcemaps.write(".")) // this only works if the sourceMap option is true
    .pipe(gulp.dest('./dist'));
});