javascript - ES2015 module import polluting global namespace -


i have rewritten bunch of old js es2015 making use of module import/exports. i'm using rollup , babel transpile back.

the libraries integrated number of other sites don't have control of need cautious code make sure don't pollute global, doesn't throw errors, etc.

gulpfile.js

var rollupbabel = rolluppluginbabel({   babelrc: false,   presets: [     "babel-preset-es2015-rollup"   ] });  merged.add(rollup({   entry: './js/bnr.js',   format: "es",   plugins: [     rollupbabel   ] }) .pipe(source('bnr.js')) .pipe(gulp.dest('./compiled/js/'))); 

bnr.js

import * helpers "../lib/helpers"; import moment "../../node_modules/moment/src/moment";  class connect {    constructor(window, document) {     this.init();   }    init()   {     // stuff happens here   } } 

output

// helpers , not here  var hookcallback;  function hooks() {     return hookcallback.apply(null, arguments); }  // done register method called moment() // without creating circular dependencies. function sethookcallback(callback) {     hookcallback = callback; }  function isarray(input) {     return input instanceof array || object.prototype.tostring.call(input) === '[object array]'; }  // rest of moment.js 

as can see moment.js related code being output without closure/wrapper keep out of global. result i'm getting various errors on consuming sites.

how can import moment.js or reconfigure gulp task import moment without polluting global namespace?

thanks

as suggested @bergi format issue, switching iife wraps whole thing in closure solve problem.


Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -