From 252fce90e177227b37f20fba7e6a33922b73abbe Mon Sep 17 00:00:00 2001 From: Andrey Morozov Date: Thu, 14 Apr 2022 17:17:53 +0300 Subject: [PATCH] Initial commit --- .editorconfig | 17 +++++++++++++++ .gitignore | 1 + CONTRIBUTING.md | 36 +++++++++++++++++++++++++++++++ LICENSE | 21 ++++++++++++++++++ README.md | 26 ++++++++++++++++++++++ index.js | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 28 ++++++++++++++++++++++++ 7 files changed, 186 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitignore create mode 100644 CONTRIBUTING.md create mode 100644 LICENSE create mode 100644 README.md create mode 100644 index.js create mode 100644 package.json diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..0cb914a --- /dev/null +++ b/.editorconfig @@ -0,0 +1,17 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_size = 4 +indent_style = space +insert_final_newline = true +max_line_length = 120 +trim_trailing_whitespace = true + +[*.json] +indent_size = 2 + +[*.md] +max_line_length = 0 +trim_trailing_whitespace = false diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..485dee6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..1ba8ba9 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,36 @@ +# Notice to external contributors + +## General info + +Hello! In order for us (YANDEX LLC) to accept patches and other contributions from you, you will have to adopt our Yandex Contributor License Agreement (the “**CLA**”). The current version of the CLA can be found here: + +1. https://yandex.ru/legal/cla/?lang=en (in English) and +2. https://yandex.ru/legal/cla/?lang=ru (in Russian). + +By adopting the CLA, you state the following: + +- You obviously wish and are willingly licensing your contributions to us for our open source projects under the terms of the CLA, +- You have read the terms and conditions of the CLA and agree with them in full, +- You are legally able to provide and license your contributions as stated, +- We may use your contributions for our open source projects and for any other our project too, +- We rely on your assurances concerning the rights of third parties in relation to your contributions. + +If you agree with these principles, please read and adopt our CLA. By providing us your contributions, you hereby declare that you have already read and adopt our CLA, and we may freely merge your contributions with our corresponding open source project and use it in further in accordance with terms and conditions of the CLA. + +## Provide contributions + +If you have already adopted terms and conditions of the CLA, you are able to provide your contributions. When you submit your pull request, please add the following information into it: + +``` +I hereby agree to the terms of the CLA available at: [link]. +``` + +Replace the bracketed text as follows: + +- [link] is the link to the current version of the CLA: https://yandex.ru/legal/cla/?lang=en (in English) or https://yandex.ru/legal/cla/?lang=ru (in Russian). + +It is enough to provide us such notification once. + +## Other questions + +If you have any questions, please mail us at opensource@yandex-team.ru. diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..2a8d6f2 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2021 YANDEX LLC + +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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..d58b1ab --- /dev/null +++ b/README.md @@ -0,0 +1,26 @@ +# @yandex-cloud/babel-preset + +Babel preset for Yandex Cloud projects + +## Installation +``` +npm install --save-dev @yandex-cloud/babel-preset +``` + +## Usage + +### Via `.babelrc` + +```json5 +{ + "presets": [ + "@yandex-cloud/babel-preset", + { + "env": {modules: false}, // defaults to {} + "runtime": {useESModules: true}, // defaults to {} + "typescript": true, // defaults to false + "react": {runtime: "automatic"} // defaults to {} + } + ] +} +``` diff --git a/index.js b/index.js new file mode 100644 index 0000000..28cd664 --- /dev/null +++ b/index.js @@ -0,0 +1,57 @@ + +const env = process.env.BABEL_ENV || process.env.NODE_ENV || 'development'; +const isEnvDevelopment = env === 'development'; +const isEnvProduction = env === 'production'; +const isEnvTest = env === 'test'; + +function getOption(value, defaultValue) { + if (typeof value === 'undefined') { + return defaultValue; + } + + return value; +} + +module.exports = function (context, options = {}) { + const envOptions = getOption(options.env, {}); + const runtimeOptions = getOption(options.runtime, {}); + const isTypeScriptEnabled = getOption(options.typescript, false); + const reactOptions = getOption(options.react, {}); + + const presets = [ + // Latest stable ECMAScript features + (isEnvDevelopment || isEnvProduction) && [require.resolve('@babel/preset-env'), envOptions], + // ES features necessary for current Node version + isEnvTest && [require.resolve('@babel/preset-env'), Object.assign({}, envOptions, { + targets: { + node: 'current', + }, + })], + // JSX + [require.resolve('@babel/preset-react'), Object.assign({}, reactOptions, { + development: isEnvDevelopment || isEnvTest, + useBuiltIns: true, + })], + isTypeScriptEnabled && [require.resolve('@babel/preset-typescript')], + ].filter(Boolean); + + const plugins = [ + isTypeScriptEnabled && [require.resolve('@babel/plugin-proposal-decorators'), { + // @decorator + // export class Foo {} + decoratorsBeforeExport: true, + }], + // Polyfills the runtime needed for async/await and generators + [require.resolve('@babel/plugin-transform-runtime'), runtimeOptions], + isEnvProduction && [require.resolve('babel-plugin-transform-react-remove-prop-types'), { + removeImport: true, + }], + // Compiles import() to a deferred require() + isEnvTest && require.resolve('babel-plugin-dynamic-import-node'), + ].filter(Boolean); + + return { + presets, + plugins, + }; +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..761d044 --- /dev/null +++ b/package.json @@ -0,0 +1,28 @@ +{ + "name": "@yandex-cloud/babel-preset", + "version": "1.0.0", + "description": "Yandex.Cloud Babel preset", + "keywords": [ + "babel", + "preset" + ], + "license": "MIT", + "repository": { + "type": "git", + "url": "https://github.com/yandex-cloud/babel-preset" + }, + "files": [ + "index.js" + ], + "dependencies": { + "@babel/core": "^7.17.9", + "@babel/plugin-proposal-decorators": "^7.17.9", + "@babel/plugin-transform-runtime": "^7.17.0", + "@babel/preset-env": "^7.16.11", + "@babel/preset-react": "^7.16.7", + "@babel/preset-typescript": "^7.16.7", + "@babel/runtime": "^7.17.9", + "babel-plugin-dynamic-import-node": "^2.3.3", + "babel-plugin-transform-react-remove-prop-types": "^0.4.24" + } +}