UNPKG

tpack-babel

Version:

TPack 插件:使用 Babel 编译 ES6/JSX。

142 lines (128 loc) 5.27 kB
/* * Copyright (C) 2016 xuld<xuld@vip.qq.com> * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, * publish, distribute, sublicense, and/or sell copies of the Software, * and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: *  * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * */ var Path = require("path"); var Babel = require("babel-core"); var Jsx = require("babel-plugin-transform-react-jsx"); var CommonJs = require("babel-plugin-transform-es2015-modules-commonjs"); var CommonJSWithoutStrict = function () { var obj = CommonJs(); delete obj.inherits; return obj; }; var presets = [ require("babel-plugin-check-es2015-constants"), require("babel-plugin-transform-es2015-template-literals"), require("babel-plugin-transform-es2015-literals"), require("babel-plugin-transform-es2015-function-name"), require("babel-plugin-transform-es2015-arrow-functions"), require("babel-plugin-transform-es2015-block-scoped-functions"), require("babel-plugin-transform-es2015-classes"), require("babel-plugin-transform-es2015-object-super"), require("babel-plugin-transform-es2015-shorthand-properties"), require("babel-plugin-transform-es2015-duplicate-keys"), require("babel-plugin-transform-es2015-computed-properties"), require("babel-plugin-transform-es2015-for-of"), require("babel-plugin-transform-es2015-sticky-regex"), require("babel-plugin-transform-es2015-unicode-regex"), require("babel-plugin-transform-es2015-spread"), require("babel-plugin-transform-es2015-parameters"), require("babel-plugin-transform-es2015-destructuring"), require("babel-plugin-transform-es2015-block-scoping"), require("babel-plugin-transform-es2015-typeof-symbol"), [require("babel-plugin-transform-regenerator"), { async: false, asyncGenerators: false }] ]; var plugins = [ require("babel-plugin-transform-exponentiation-operator"), require("babel-plugin-transform-class-constructor-call"), require("babel-plugin-transform-class-properties"), require("babel-plugin-transform-async-to-generator"), require("babel-plugin-transform-async-to-module-method"), require("babel-plugin-transform-decorators"), require("babel-plugin-transform-do-expressions"), require("babel-plugin-transform-exponentiation-operator"), require("babel-plugin-transform-export-extensions"), require("babel-plugin-transform-function-bind"), require("babel-plugin-transform-object-rest-spread") ]; /** * 编译 ES6/JSX。 * @param {BuildFile} file 要生成的文件。 * @param {Object} options 相关的选项。 * @see http://babeljs.io/docs/usage/options/ */ module.exports = function babel(file, options) { // 设置默认值。 options = Object.assign({ filename: file.srcPath, presets: { plugins: presets.slice(0) }, sourceMaps: !!file.sourceMap, inputSourceMap: file.sourceMapData, babelrc: false, compact: false, plugins: plugins }, options); // 更改扩展名。 file.extension = ".js"; // 禁止生成 use strict 语句。 if (options.strict) { delete options.strict; options.presets.plugins.push(CommonJs); } else { options.presets.plugins.push(CommonJSWithoutStrict); } // 支持 jsx 语法。 if (options.jsx !== false) { if (options.jsx === true) { options.jsx = {}; } options.presets.plugins.push([Jsx, options.jsx]); } delete options.jsx; // 生成。 var result; try { result = Babel.transform(file.content, options); } catch (e) { // 可用字段:e.name, e.message, e.loc, e.codeFrame。 e.name = "Babel" + e.name; e.sourceCode = e.codeFrame; try { var m = /^(.*?): (.*)\s*\(\d+:\d+\)$/.exec(e.message); e.message = m[2]; e.fileName = m[1]; e.startLine = e.loc && e.loc.line; e.startColumn = e.loc && e.loc.column; } catch (e2) { } file.error(e); return; } // 保存。 file.content = result.code; if (result.map) { file.sourceMap = result.map; } file.ast = result.ast; };