本文整理匯總了TypeScript中@babel/core.transformFromAst函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript transformFromAst函數的具體用法?TypeScript transformFromAst怎麽用?TypeScript transformFromAst使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了transformFromAst函數的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: transformDew
function transformDew (ast, source, resolveMap) {
const { code: dewTransform } = babel.transformFromAst(ast, source, {
babelrc: false,
babelrcRoots: false,
configFile: false,
highlightCode: false,
compact: false,
sourceType: 'script',
parserOpts: {
allowReturnOutsideFunction: true,
plugins: stage3Syntax
},
plugins: [[dewTransformPlugin, {
resolve: (name, opts) => {
if ((opts.optional || opts.wildcard) && !resolveMap[name])
return null;
return resolveMap[name];
},
wildcardExtensions: ['.js', '.json', '.node'],
esmDependencies: resolved => isESM(resolved),
filename: `import.meta.url.startsWith('file:') ? decodeURI(import.meta.url.slice(7 + (typeof process !== 'undefined' && process.platform === 'win32'))) : new URL(import.meta.url).pathname`,
dirname: `import.meta.url.startsWith('file:') ? decodeURI(import.meta.url.slice(0, import.meta.url.lastIndexOf('/')).slice(7 + (typeof process !== 'undefined' && process.platform === 'win32'))) : new URL(import.meta.url.slice(0, import.meta.url.lastIndexOf('/'))).pathname`
}]]
});
return dewTransform;
}
示例2: resolve
return new Promise<Babel.BabelFileResult | null>((resolve) => {
Babel.transformFromAst(ast, code, options, (error, res) => {
if (error != null) {
console.error("fable: Error transforming Babel AST", error);
resolve(null);
} else {
resolve(res);
}
});
});
示例3:
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!;
示例4: jsTransform
//.........這裏部分代碼省略.........
if (options.compile === true || options.compile === 'es5') {
doBabelTransform = true;
plugins.push(...babelTransformEs2015);
plugins.push(...babelTransformEs2016);
plugins.push(...babelTransformEs2017);
plugins.push(...babelTransformEs2018);
} else if (options.compile === 'es2015') {
doBabelTransform = true;
plugins.push(...babelTransformEs2016);
plugins.push(...babelTransformEs2017);
plugins.push(...babelTransformEs2018);
} else if (options.compile === 'es2016') {
doBabelTransform = true;
plugins.push(...babelTransformEs2017);
plugins.push(...babelTransformEs2018);
} else if (options.compile === 'es2017') {
doBabelTransform = true;
plugins.push(...babelTransformEs2018);
}
if (options.moduleResolution === 'node') {
if (!options.filePath) {
throw new Error(
'Cannot perform node module resolution without filePath.');
}
doBabelTransform = true;
plugins.push(resolveBareSpecifiers(
options.filePath,
!!options.isComponentRequest,
options.packageName,
options.componentDir,
options.rootDir));
}
// When the AMD option is "auto", these options will change based on whether
// we have a module or not (unless they are already definitely true).
let transformModulesToAmd = options.transformModulesToAmd;
if (transformModulesToAmd === true) {
doBabelTransform = true;
}
const maybeDoBabelTransform =
doBabelTransform || transformModulesToAmd === 'auto';
if (maybeDoBabelTransform) {
let ast;
try {
ast = babylon.parse(js, {
// TODO(aomarks) Remove any when typings are updated for babylon 7.
// tslint:disable-next-line: no-any
sourceType: transformModulesToAmd === 'auto' ? 'unambiguous' as any :
'module',
plugins: [
'asyncGenerators',
'dynamicImport',
// tslint:disable-next-line: no-any
'importMeta' as any,
'objectRestSpread',
],
});
} catch (e) {
if (options.softSyntaxError && e.constructor.name === 'SyntaxError') {
console.error(
'ERROR [polymer-build]: failed to parse JavaScript' +
(options.filePath ? ` (${options.filePath}):` : ':'),
e);
return js;
} else {
throw e;
}
}
if (transformModulesToAmd === 'auto' &&
ast.program.sourceType === 'module') {
transformModulesToAmd = true;
}
if (transformModulesToAmd) {
doBabelTransform = true;
plugins.push(...babelTransformModulesAmd);
}
if (doBabelTransform) {
const result = babelCore.transformFromAst(ast, js, {presets, plugins});
if (result.code === undefined) {
throw new Error(
'Babel transform failed: resulting code was undefined.');
}
js = result.code;
if (!options.externalHelpers && options.compile === 'es5' &&
js.includes('regeneratorRuntime')) {
js = externalJs.getRegeneratorRuntime() + js;
}
}
}
js = replaceTemplateObjectNames(js);
return js;
}