Example #1
0
File: index.js Project: lxe/babel
  addImport(source: string, imported: string, name?: string = imported): Object {
    let alias = `${source}:${imported}`;
    let id = this.dynamicImportIds[alias];

    if (!id) {
      source = this.resolveModuleSource(source);
      id = this.dynamicImportIds[alias] = this.scope.generateUidIdentifier(name);

      let specifiers = [];

      if (imported === "*") {
        specifiers.push(t.importNamespaceSpecifier(id));
      } else if (imported === "default") {
        specifiers.push(t.importDefaultSpecifier(id));
      } else {
        specifiers.push(t.importSpecifier(id, t.identifier(imported)));
      }

      let declar = t.importDeclaration(specifiers, t.stringLiteral(source));
      declar._blockHoist = 3;

      this.path.unshiftContainer("body", declar);
    }

    return id;
  }
Example #2
0
 File.prototype._addAst = function _addAst(ast) {
   this.path = /*istanbul ignore next*/_babelTraverse.NodePath.get({
     hub: this.hub,
     parentPath: null,
     parent: ast,
     container: ast,
     key: "program"
   }).setContext();
   this.scope = this.path.scope;
   this.ast = ast;
   this.getMetadata();
 };
Example #3
0
File: index.js Project: lxe/babel
 _addAst(ast) {
   this.path = NodePath.get({
     hub: this.hub,
     parentPath: null,
     parent: ast,
     container: ast,
     key: "program"
   }).setContext();
   this.scope = this.path.scope;
   this.ast   = ast;
   this.getMetadata();
 }
Example #4
0
File: index.js Project: lxe/babel
 getMetadata() {
   let has = false;
   for (let node of (this.ast.program.body: Array<Object>)) {
     if (t.isModuleDeclaration(node)) {
       has = true;
       break;
     }
   }
   if (has) {
     this.path.traverse(metadataVisitor, this);
   }
 }
Example #5
0
File: index.js Project: lxe/babel
  initOptions(opts) {
    opts = new OptionManager(this.log, this.pipeline).init(opts);

    if (opts.inputSourceMap) {
      opts.sourceMaps = true;
    }

    if (opts.moduleId) {
      opts.moduleIds = true;
    }

    opts.basename = path.basename(opts.filename, path.extname(opts.filename));

    opts.ignore = util.arrayify(opts.ignore, util.regexify);

    if (opts.only) opts.only = util.arrayify(opts.only, util.regexify);

    defaults(opts, {
      moduleRoot: opts.sourceRoot
    });

    defaults(opts, {
      sourceRoot: opts.moduleRoot
    });

    defaults(opts, {
      filenameRelative: opts.filename
    });

    let basenameRelative = path.basename(opts.filenameRelative);

    defaults(opts, {
      sourceFileName:   basenameRelative,
      sourceMapTarget:  basenameRelative
    });

    return opts;
  }
Example #6
0
File: index.js Project: lxe/babel
  addHelper(name: string): Object {
    let declar = this.declarations[name];
    if (declar) return declar;

    if (!this.usedHelpers[name]) {
      this.metadata.usedHelpers.push(name);
      this.usedHelpers[name] = true;
    }

    let generator = this.get("helperGenerator");
    let runtime   = this.get("helpersNamespace");
    if (generator) {
      let res = generator(name);
      if (res) return res;
    } else if (runtime) {
      return t.memberExpression(runtime, t.identifier(name));
    }

    let ref = getHelper(name);
    let uid = this.declarations[name] = this.scope.generateUidIdentifier(name);

    if (t.isFunctionExpression(ref) && !ref.id) {
      ref.body._compact = true;
      ref._generated = true;
      ref.id = uid;
      ref.type = "FunctionDeclaration";
      this.path.unshiftContainer("body", ref);
    } else {
      ref._compact = true;
      this.scope.push({
        id: uid,
        init: ref,
        unique: true
      });
    }

    return uid;
  }