在同一文件中将rollup.js与jQuery适配器一起使用

时间:2018-09-19 15:24:38

标签: javascript rollupjs

我具有导出的主要功能,并且jQuery适配器位于同一文件中。我想保持这种方式。

rollup.js是否可以创建与AMD,CommonJS和浏览器兼容的捆绑文件?

现在我有这个测试文件:

//src/foo.js
export default function(){
    function initialise() {
        var TEST = {};
        TEST.version = '1.0.0';
        TEST.hello = function(){
            console.log("hello man!!");
        };

        return TEST;
    }

    //jQuery adapter
    (function($){
        $.fn.demo = function(options) {
            $.fn.demo.test = 'test';
        };
    })(jQuery);

    return initialise;
};

我尝试使用此汇总配置:

// rollup.config.js
export default {
  input: 'src/main.js',
  output: {
    file: 'dist/bundle.js',
    format: 'umd',
    globals: {
        $: '$'
    }
  }
};

但是我似乎无法访问$.fn.demo

// src/main.js
import jQuery from 'jQuery';
import pepe from './foo.js';


console.log(jQuery);    //<--- undefined
console.log($);         //<--- undefined

console.log("-- JS--");
console.log(pepe);      //<---- working fine
console.log("-- $.fn.demo --");
console.log($.fn.demo); //<--- undefined

1 个答案:

答案 0 :(得分:0)

请注意,在main.js文件中,pepe是一个函数(您从foo导出的一个函数),您没有调用它,因此$.fn.demo是{{1 }}。您必须调用undefined才能执行适配器。

另请参阅how to tell Rollup that jquery is external and the jquery module ID equates to the global $ variable