diff --git a/lib/6to5/file.js b/lib/6to5/file.js index 518fddccb3..9420dfda58 100644 --- a/lib/6to5/file.js +++ b/lib/6to5/file.js @@ -13,8 +13,12 @@ var _ = require("lodash"); function File(opts) { this.dynamicImports = []; + this.dynamicImported = []; this.dynamicImportIds = {}; + + this.dynamicData = {}; this.data = {}; + this.opts = File.normaliseOptions(opts); this.transformers = this.getTransformers(); this.ast = {}; @@ -179,11 +183,23 @@ File.prototype.parseShebang = function (code) { }; File.prototype.set = function (key, val) { - this.data[key] = val; + return this.data[key] = val; +}; + +File.prototype.setDynamic = function (key, fn) { + this.dynamicData[key] = fn; }; File.prototype.get = function (key) { - return this.data[key]; + var data = this.data[key]; + if (data) { + return data; + } else { + var dynamic = this.dynamicData[key]; + if (dynamic) { + return this.set(key, dynamic()); + } + } }; File.prototype.addImport = function (source, name) { @@ -196,7 +212,9 @@ File.prototype.addImport = function (source, name) { var specifiers = [t.importSpecifier(t.identifier("default"), id)]; var declar = t.importDeclaration(specifiers, t.literal(source)); declar._blockHoist = 3; - this.dynamicImports.push(declar); + this.dynamicImported.push(declar); + + this.moduleFormatter.importSpecifier(specifiers[0], declar, this.dynamicImports); } return id;