How to use the @babel/core.transform function in @babel/core

To help you get started, we’ve selected a few @babel/core examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mattdesl / canvas-sketch-cli / src / walk-local-deps.js View on Github external
// mark this file as checked
    checked.push(file);

    const ext = (path.extname(file) || '').toLowerCase();
    if (!extensions.includes(ext)) {
      return;
    }

    if (typeof src === 'undefined') {
      src = await readFile(file, 'utf-8');
    }

    const basedir = path.dirname(file);
    let deps;
    try {
      const babelResult = babel.transform(src, {
        ast: true,
        babelrc: true,
        plugins: [
          pluginSyntaxRestSpread,
          pluginSyntaxGenerator,
          pluginDynamicImport,
          pluginCJS
        ],
        filename: file,
        sourceFileName: file,
        highlightCode: true
      });
      const result = konan(babelResult.ast);
      deps = result.strings;
    } catch (err) {
      throw err;
github ttag-org / babel-plugin-ttag / tests / functional / test_entries_sort.js View on Github external
it('should sort entries by msgid', () => {
        const expectedPath = 'tests/fixtures/expected_sort.pot';
        const input = `
        t\`bbbb\`;
        t\`aaaaa\`;
        t\`ccccc\`;
        t\`fffff\`;
        t\`eeeee\`;
        `;
        babel.transform(input, options);
        const result = fs.readFileSync(output).toString();
        const expected = fs.readFileSync(expectedPath).toString();
        expect(result).to.eql(expected);
    });
});
github ZengLingYong / Blog / Webpack / Loader / babel-loader.js View on Github external
function loader(source) {
  let options = loaderUtils.getOptions(this);
  let cb = this.async();
  babel.transform(source, {
    ...options,
    filename: this.resourcePath.split('/').pop(),
    sourceMaps: true
  }, (err, result) => {
    cb(null, result.code, result.map);
  })
}
github erukiti / ast-book-sample / chapter5 / binding.js View on Github external
console.log()
        console.log(`${binding.kind} ${name}`)
        console.log(`  referencese: ${binding.references}`)
        binding.referencePaths.forEach(refPath => {
          console.log(`    ${refPath.parentPath.getSource()}`)
        })
        console.log(`  constantViolations: ${binding.constantViolations.length}`)
        binding.constantViolations.forEach(vPath => {
          console.log(`    ${vPath.getSource()}`)
        })
      }
    }
  }
}

transform(src, {plugins: [plugin]})
github ant-move / Antmove / packages / @antmove / utils / src / babel / index.js View on Github external
function transformSjsToWxs (code) {
    return babel.transform(code, {
        "presets": [
            [
                babelPreset
            ]
        ],
        plugins: [
            [
                require('@babel/plugin-transform-modules-commonjs'),
                {
                    allowTopLevelThis: true,
                    loose: true,
                    strict: true
                }
            ]
        ]
    }).code;
github laat / readme-assert / packages / readme-assert / src / index.js View on Github external
const sourceMaps = !shouldPrintCode;
  const sourceMapsFile = shouldPrintCode
    ? path.join(path.dirname(filePath), "readme.md.js")
    : filePath;

  req.forEach(f => {
    require(require.resolve(f, { paths: [process.cwd()] }));
  });
  const mdDirname = path.dirname(filePath);
  process.chdir(mdDirname);
  const mdText = read(filePath);
  const rootPkg = pkgUp.sync();
  const pkg = JSON.parse(read(rootPkg));
  const { code, hasTypescript } = extract(mdText, { auto, all });

  const transformed = babel.transform(code, {
    babelrc,
    sourceMaps,
    plugins: [
      ...(hasTypescript ? [typescriptTransform] : []),
      commentPlugin,
      rootPkg
        ? [
            importPlugin,
            { replacement: main || process.cwd(), original: pkg.name }
          ]
        : undefined
    ],
    presets: [presetEnv],
    filename: sourceMapsFile
  });
github Persper / js-callgraph / src / srcPreprocessor.js View on Github external
function typescriptPrep(fname, src) {
    return babel.transform(src, {
        presets: ["@babel/preset-typescript"],
        plugins: ["@babel/plugin-proposal-class-properties"],
        filename: fname,
        retainLines: true,
        parserOpts: {strictMode: false}
    }).code;
}
github mattzeunert / FromJS / packages / core / src / compile.ts View on Github external
export function compileSync(code, extraBabelOptions = {}, url = "/no_url.js") {
  code = removeSourceMapIfAny(code);
  const babelResult = babel.transform(
    code,
    getBabelOptions(plugin, extraBabelOptions, url)
  );
  return {
    map: babelResult.map,
    code: babelResult.code + "\n//# sourceMappingURL=" + url + ".map",
    locs: getAndResetLocs()
  };
}
github walrusjs / walrus / packages / walrus-shared-utils / src / config-loader.ts View on Github external
loadSync(id) {
    const content = require('@babel/core').transform(readFileSync(id, 'utf8'), {
      babelrc: false,
      configFile: false,
      filename: id,
      presets: [
        [
          require('@babel/preset-env'),
          {
            targets: {
              node: 'current'
            }
          }
        ],
        id.endsWith('.ts') && require('@babel/preset-typescript')
      ].filter(Boolean)
    });
    const m = requireFromString(content && content.code ? content.code : '', id);
github jspm / jspm-cli / src / compile / dew-worker.ts View on Github external
function tryParseCjs (source) {
  const requires = new Set();
  let hasProcess, hasBuffer;
  const { ast } = babel.transform(source, {
    ast: true,
    babelrc: false,
    babelrcRoots: false,
    configFile: false,
    highlightCode: false,
    compact: false,
    sourceType: 'script',
    parserOpts: {
      allowReturnOutsideFunction: true,
      plugins: stage3Syntax
    },
    plugins: [({ types: t }) => {
      function resolvePartialWildcardString (node, lastIsWildcard) {
        if (t.isStringLiteral(node))
          return node.value;
        if (t.isTemplateLiteral(node)) {