@babel/core#BabelFileResult TypeScript Examples

The following examples show how to use @babel/core#BabelFileResult. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: local-state.tsx    From nota with MIT License 6 votes vote down vote up
tryTranslate(): TranslationResult {
    let tree = tryParse(this.contents);
    if (isErr(tree)) {
      return err(tree.value.stack!);
    }
    let js = translateAst(this.contents, tree.value);
    let transpiledResult = babel.transformFromAst(js, undefined, {
      plugins: [optimizePlugin],
    }) as any as BabelFileResult;
    let loweredResult = babel.transformFromAst(js, undefined, {
      presets: [["env", { targets: { browsers: "last 1 safari version" } }]],
    }) as any as BabelFileResult;
    let lowered = `let exports = {};\n${loweredResult.code}\nlet notaDocument = exports;`;
    return ok({
      transpiled: transpiledResult.code!,
      lowered,
      css: null,
    });
  }
Example #2
Source File: translate.ts    From nota with MIT License 6 votes vote down vote up
replaceNotaCalls(node: SyntaxNode): string {
    let cursor = node.cursor();
    let replacements: [number, number, string][] = [];
    while (node.from <= cursor.from && cursor.to <= node.to) {
      if (matches(cursor.node, terms.AtCommand)) {
        let expr = this.translateAtcommand(cursor.node);
        let result = babel.transformFromAst(
          t.program([t.expressionStatement(expr)]),
          undefined,
          {}
        ) as any as BabelFileResult;
        let code = result.code!.slice(0, -1);
        replacements.push([cursor.from - node.from, cursor.to - node.from, code]);

        if (!cursor.next(false)) {
          break;
        }
      } else if (!cursor.next()) {
        break;
      }
    }

    let code = this.text(node);
    replacements = _.sortBy(replacements, [0]);
    let expanded = "";
    let i = 0;
    replacements.forEach(([from, to, expr]) => {
      expanded += code.slice(i, from);
      expanded += expr;
      i = to;
    });
    expanded += code.slice(i);

    return expanded;
  }
Example #3
Source File: translate.ts    From nota with MIT License 6 votes vote down vote up
translate = (input: string, tree: Tree): string => {
  let program = translateAst(input, tree);
  let result = babel.transformFromAst(program, undefined, {
    plugins: [optimizePlugin],
  }) as any as BabelFileResult;
  let js = result.code!;

  return js;
}
Example #4
Source File: translate.test.ts    From nota with MIT License 6 votes vote down vote up
gen =
  (f: (tr: Translator, node: SyntaxNode) => Statement) =>
  (input: string): string => {
    try {
      let tree = resUnwrap(tryParse(input));
      let translator = new Translator(input);
      let stmt = f(translator, tree.topNode);
      let program = t.program([stmt]);
      let result = babel.transformFromAst(program, undefined, {}) as any as BabelFileResult;
      return result.code!;
    } catch (e) {
      console.error(input);
      throw e;
    }
  }
Example #5
Source File: translate.ts    From nota with MIT License 5 votes vote down vote up
parse = (code: string): Statement[] => {
  let result = babel.transform(code, {
    ast: true,
  }) as any as BabelFileResult;
  return result.ast!.program.body;
}