当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript core.transformFileSync函数代码示例

本文整理汇总了TypeScript中@babel/core.transformFileSync函数的典型用法代码示例。如果您正苦于以下问题:TypeScript transformFileSync函数的具体用法?TypeScript transformFileSync怎么用?TypeScript transformFileSync使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了transformFileSync函数的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1:

const options: babel.TransformOptions = {
    ast: true,
    sourceMaps: true
};

babel.transform("code();", options, (err, result) => {
    const { code, map, ast } = result!;
});

const transformSyncResult = babel.transformSync("code();", options);
if (transformSyncResult) {
    const { code, map, ast } = transformSyncResult;
}

babel.transformFile("filename.js", options, (err, result) => {
    const { code, map, ast } = result!;
});

babel.transformFileSync("filename.js", options)!.code;

const sourceCode = "if (true) return;";
const parsedAst = babel.parse(sourceCode, options);

babel.transformFromAst(parsedAst!, sourceCode, options, (err, result) => {
    const { code, map, ast } = result!;
});

const transformFromAstSyncResult = babel.transformFromAstSync(parsedAst!, sourceCode, options);
const { code, map, ast } = transformFromAstSyncResult!;
开发者ID:AlexGalays,项目名称:DefinitelyTyped,代码行数:29,代码来源:babel__core-tests.ts

示例2: buildBackend

export async function buildBackend(modulePath: string) {
  let babelConfig: babel.TransformOptions = {
    presets: [
      [
        '@babel/preset-env',
        {
          targets: {
            node: 'current'
          }
        }
      ],
      '@babel/preset-typescript',
      '@babel/preset-react'
    ],
    parserOpts: {
      allowReturnOutsideFunction: true
    },
    plugins: ['@babel/plugin-proposal-class-properties', '@babel/plugin-proposal-function-bind'],
    sourceType: 'module',
    cwd: modulePath
  }

  const babelFile = path.join(modulePath, 'babel.backend.js')

  if (fs.existsSync(babelFile)) {
    debug('Babel override found for backend')
    babelConfig = require(babelFile)(babelConfig)
  }

  const files = glob.sync('src/**/*.+(ts|js|jsx|tsx)', {
    cwd: modulePath,
    ignore: ['**/*.d.ts', '**/views/**/*.*', '**/config.ts']
  })

  rimraf.sync(path.join(modulePath, 'dist'))

  // Allows to copy additional files to the dist directory of the module
  const extrasFile = path.join(modulePath, 'build.extras.js')
  if (fs.existsSync(extrasFile)) {
    const extras = require(extrasFile)
    if (extras && extras.copyFiles) {
      for (const instruction of extras.copyFiles) {
        const toCopy = glob.sync(instruction, {
          cwd: modulePath
        })

        for (const file of toCopy) {
          const fromFull = path.join(modulePath, file)
          const dest = file.replace(/^src\//i, 'dist/').replace(/\.ts$/i, '.js')
          const destFull = path.join(modulePath, dest)
          mkdirp.sync(path.dirname(destFull))
          fse.copySync(fromFull, destFull)
          debug(`Copied "${file}" -> "${dest}"`)
        }
      }
    }
  }

  const outputFiles = []

  for (const file of files) {
    try {
      const dBefore = Date.now()
      const result = babel.transformFileSync(file, babelConfig)

      const dest = file.replace(/^src\//i, 'dist/').replace(/.ts$/i, '.js')
      mkdirp.sync(path.dirname(dest))
      fs.writeFileSync(dest, result.code)
      const totalTime = Date.now() - dBefore

      debug(`Generated "${dest}" (${totalTime} ms)`)

      outputFiles.push(dest)
    } catch (err) {
      error(`Error transpiling file "${file}"`) // TODO Better error
      throw err
    }
  }
}
开发者ID:seffalabdelaziz,项目名称:botpress,代码行数:79,代码来源:build.ts


注:本文中的@babel/core.transformFileSync函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。