├── .gitignore ├── package.json ├── lib └── index.js └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules/ 2 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "atom-babel6-transpiler", 3 | "version": "1.1.3", 4 | "description": "A transpiler for Atom packages that processes code with Babel", 5 | "main": "lib/index.js", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/atom/atom-babel6-transpiler.git" 9 | }, 10 | "scripts": { 11 | "test": "echo \"Error: no test specified\" && exit 1" 12 | }, 13 | "keywords": [ 14 | "atom", 15 | "babel", 16 | "transpile", 17 | "transpiler" 18 | ], 19 | "license": "MIT", 20 | "dependencies": { 21 | "babel-core": "6.x" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | function withBabelEnv(setBabelEnv, fn) { 4 | if (setBabelEnv === false) { 5 | return fn() 6 | } 7 | 8 | const oldBabelEnv = process.env.BABEL_ENV 9 | 10 | if (setBabelEnv !== undefined && setBabelEnv !== true) { 11 | const newBabelEnv = process.env[setBabelEnv] || 'development' 12 | process.env.BABEL_ENV = newBabelEnv 13 | } else if (global.atom) { 14 | process.env.BABEL_ENV = (atom.inDevMode() || atom.inSpecMode()) ? 'development' : 'production' 15 | } else { 16 | process.env.BABEL_ENV = 'development' 17 | } 18 | const res = fn() 19 | if (oldBabelEnv) { 20 | // Reset the BABEL_ENV if it was previously set 21 | process.env.BABEL_ENV = oldBabelEnv 22 | } else { 23 | // Otherwise delete it to prevent polluting the environment 24 | delete process.env.BABEL_ENV 25 | } 26 | return res 27 | } 28 | 29 | module.exports = { 30 | getCacheKeyData: function (source, filename, options, meta) { 31 | const fs = require('fs') 32 | const path = require('path') 33 | 34 | const pkgJsonData = fs.readFileSync(path.join(meta.path, 'package.json')) 35 | 36 | let cacheKeyData = '' 37 | if (options.cacheKeyFiles) { 38 | cacheKeyData = options.cacheKeyFiles.reduce((acc, relPath) => { 39 | return `${acc}\n${fs.readFileSync(path.join(meta.path, relPath))}` 40 | }, '') 41 | } 42 | 43 | return withBabelEnv(options.setBabelEnv, function() { 44 | return `${cacheKeyData}\nenv:${process.env.BABEL_ENV}\nv:${pkgJsonData.version}` 45 | }) 46 | }, 47 | 48 | transpile: function (source, filename, options, meta) { 49 | const path = require('path') 50 | const babel = require('babel-core') 51 | 52 | const opts = options.babel || {} 53 | return withBabelEnv(options.setBabelEnv, function () { 54 | const result = babel.transform(source, Object.assign({}, opts, { 55 | sourceRoot: path.dirname(filename), 56 | filename: filename 57 | })) 58 | return {code: result.code, map: result.map} 59 | }) 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ##### Atom and all repositories under Atom will be archived on December 15, 2022. Learn more in our [official announcement](https://github.blog/2022-06-08-sunsetting-atom/) 2 | # atom-babel6-transpiler 3 | 4 | This project implements an [Atom package transpiler]() that transpiles your package's files with Babel 6. 5 | 6 | ## Usage 7 | 8 | 1. Install the package 9 | 2. Add an `atomTranspilers` entry to your `package.json` 10 | 3. Install Babel presets and plugins, and configure Babel as you wish 11 | 12 | In detail: 13 | 14 | **1.** First, install the package from the npm registry: 15 | 16 | npm install --save atom-babel6-transpiler 17 | 18 | **2.** Next, modify your `package.json` to include a reference to the transpiler for any files you want Babel to process as described [in the Atom Flight Manual](). For example, to process every file ending in `.js` in your package, you could use: 19 | 20 | ```javascript 21 | { 22 | ... 23 | "atomTranspilers": [ 24 | { 25 | "glob": "**/*.js", 26 | "transpiler": "atom-babel6-transpiler" 27 | } 28 | ] 29 | } 30 | ``` 31 | 32 | **3.** Finally, install Babel and all the presets and plugins you want to use as normal. For a simple example, if you wanted to use the ES2015 and React presets, you might run: 33 | 34 | npm install --save babel-preset-es2015 babel-preset-react 35 | 36 | and then create a [`.babelrc` file](http://babeljs.io/docs/usage/babelrc/) to configure Babel to use them: 37 | 38 | ```json 39 | { 40 | "presets": ["es2015", "react"] 41 | } 42 | ``` 43 | 44 | You may also specify options in your `package.json` inside the optional `options` object; the subkey `babel`, if it exists, will be passed [as options to `babel.transform`](http://babeljs.io/docs/usage/api/#babeltransformcode-optionsdocsusageoptions). Note that if you don't want Babel to search up the directory hierarchy for a `.babelrc` file, you need to add the `"babelrc": false` option to the settings. Otherwise Babel may mistakenly use a user's `.babelrc` from elsewhere on the filesystem when trying to build your package. 45 | 46 | ```javascript 47 | { 48 | // ... 49 | "atomTranspilers": [ 50 | { 51 | "glob": "**/*.js", 52 | "transpiler": "atom-babel6-transpiler", 53 | "options": { 54 | "babel": { 55 | "presets": ["es2015", "react"], 56 | "babelrc": false 57 | } 58 | } 59 | } 60 | ] 61 | } 62 | ``` 63 | 64 | ### Options 65 | 66 | You may specify the following options as values of the `options` object in your `package.json`: 67 | 68 | |Option|Default|Description| 69 | |--:|---|---| 70 | |`setBabelEnv`|`false`, `true` or a string|Sets the `BABEL_ENV` environment variable. When `true`, sets it to `"development"` when `atom.inDevMode()` or `atom.inSpecMode()` is true and `"production"` otherwise. When given as a string, uses the value of the environment variable of that name instead. The feature returns `BABEL_ENV` to its prior value after transpilation finishes.| 71 | |`babel`|`{}`|Options to pass as the second argument to `babel.transform` (the same options you can put in a `.babelrc`).| 72 | |`cacheKeyFiles`|`[]`|An array of files to include when determining whether or not to use the cache. For example, to force a recompile anytime your `.babelrc` changes, add `.babelrc` to this array.| 73 | 74 | ## Other Details 75 | 76 | ### Source Maps 77 | 78 | To enable source maps within Atom, set the Babel `sourceMaps` option to `"inline"` in your Babel configuration. 79 | 80 | ### Babel Environment 81 | 82 | Babel supports [an option called `env`](https://babeljs.io/docs/usage/babelrc/#env-option) that allows you to configure Babel on a per-environment basis. The Babel environment is controlled via an environment variable called `BABEL_ENV`; this module automatically sets the environment variable to `"development"` if Atom is in dev mode (`atom.inDevMode()` returns `true`) and `"production"` otherwise. 83 | --------------------------------------------------------------------------------