5

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

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

吞咽任务:

// 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 {

  ...

}
4

3 回答 3

4

您可以使用此汇总插件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'; 
//...

更多关于外部解决方案的讨论在这里

于 2016-03-10T02:40:19.967 回答
1

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

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'));
});
于 2016-08-25T20:27:41.410 回答
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。

于 2015-12-09T23:47:29.093 回答