代码之家  ›  专栏  ›  技术社区  ›  Alvaro

使用汇总.js在同一个文件中使用jQuery适配器

  •  0
  • Alvaro  · 技术社区  · 7 年前

    我的主要功能是导出和jQuery适配器在同一个文件中。我想让他们这样。

    //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 回复  |  直到 7 年前
        1
  •  0
  •   Adrian Baran    7 年前

    请注意,在 main.js 文件 pepe foo )你也没这么叫它 $.fn.demo undefined pepe(); 以便执行适配器。

    另见 how to tell Rollup that jquery is external and the jquery module ID equates to the global $ variable .