@babel/core#parseSync TypeScript Examples

The following examples show how to use @babel/core#parseSync. 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: utils.ts    From vidact with MIT License 6 votes vote down vote up
export function parseJSX(code: string) {
  return parseSync(code, {
    plugins: ["@babel/plugin-syntax-jsx"]
  });
}
Example #2
Source File: runtimeHelpers.ts    From vidact with MIT License 6 votes vote down vote up
function getInlineRuntimeHelpers() {
  if (inlineFiles) {
    return inlineFiles;
  }

  inlineFiles = new Map<string, any>();

  for (const module of runtimeModules) {
    const code = fs
      .readFileSync(`${__dirname}/../../src/runtime/${module}.js`)
      .toString();
    const ast = parseSync(code, { babelrc: false });
    traverse(ast, {
      ExportNamedDeclaration(path) {
        path.replaceWith(path.node.declaration);
      },
      ImportDeclaration(path) {
        path.remove();
      },
    });
    inlineFiles.set(module, ast);
  }

  return inlineFiles;
}
Example #3
Source File: loader.ts    From xwind with MIT License 5 votes vote down vote up
function transform(
  source: string,
  sourceMap: RawSourceMap | undefined,
  {
    babelOptions,
    resourcePath,
  }: {
    babelOptions: object;
    resourcePath: string;
  }
) {
  const ast = parseSync(source, {
    ...babelOptions,
    filename: resourcePath,
    caller: { name: "xwind" },
  });

  if (!ast) {
    throw new Error(`XWIND: could not parse ${resourcePath}.`);
  }

  const transformResult = transformFromAstSync(ast, source, {
    filename: resourcePath,
    plugins: [xwindplugin],
    babelrc: false,
    configFile: false,
    sourceMaps: true,
    sourceFileName: resourcePath,
    inputSourceMap: sourceMap,
  });

  if (!transformResult || !transformResult.code) {
    throw new Error(`XWIND: could not transform ${resourcePath}.`);
  }

  interface Metadata extends BabelFileMetadata {
    xwind?: string;
  }

  const metadata: Metadata | undefined = transformResult?.metadata;

  return {
    code: transformResult.code,
    metadata,
    map: transformResult.map,
  };
}