• Stars
    star
    381
  • Rank 108,357 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 9 years ago
  • Updated almost 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Yeoman generator to author libraries in ES2015 (and beyond!) for Node and the browser.

generator-babel-boilerplate

Travis build status Dependency Status devDependency Status

A Yeoman generator to author libraries in ES2015 (and beyond!) for Node and the browser.

Features

โœ“ Author in ES2015 (including the unit tests)
โœ“ Export as ES5 & UMD
โœ“ Mocha-Chai-Sinon testing stack
โœ“ Unit tests that work in Node and in the browser

Installation

Install yo and this generator globally.

npm install -g yo generator-babel-boilerplate

Using Yeoman

Navigate to the directory you'd like to use for your project, then run yo babel-boilerplate.

Answer a few questions, and your project will be scaffolded.

Basic Guide

Write your code in src. The entry file is what you named the project in kebab case (although the filename can be changed).

Run npm run build to compile the source into a distributable format.

Put your unit tests in test/unit. The npm test command runs the tests using Node. If your library / tests require the DOM API, see the test/setup/node.js file.

npm Scripts

  • npm test - Lint the library and tests, then run the unit tests
  • npm run lint - Lint the source and unit tests
  • npm run watch - Continuously run the unit tests as you make changes to the source and test files themselves
  • npm run test-browser - Build the library for use with the browser spec runner. Changes to the source will cause the runner to automatically refresh.
  • npm run build - Lint then build the library
  • npm run coverage - Generate a coverage report

Browser Tests

The browser spec runner can be opened in a browser to run your tests. For it to work, you must first run npm run test-browser. This will set up a watch task that will automatically refresh the tests when your scripts, or the tests, change. The spec runner file is located at test/runner.html: open it in your browser to run tests.

Code Climate

This library is set up to integrate with Code Climate. If you've never used Code Climate, then you might be wondering why it's useful. There are two reasons:

  1. It consumes code coverage reports, and provides a coverage badge for the README
  2. It provides interesting stats on your library, if you're into that kinda thing

Either of these items on the list can simply be ignored if you're uninterested in them. Or you can pull Code Climate out entirely from the boilerplate and not worry about it. To do that, update the relevant Gulp tasks and the Travis build.

If you'd like to set up Code Climate for your project, follow the steps here.

Linting

This boilerplate uses ESLint to lint your source. To change the rules, edit the .eslintrc files in the root directory, respectively.

Given that your unit tests typically follow different rules from your library code, it makes sense to lint them against a separate ESLint configuration. For this reason, a separate, unit-test specific .eslintrc can be found in the test directory.

FAQ

What Babel features are supported?

Nearly all Babel features should be supported by this boilerplate.

If you're using certain experimental features, like class properties, async-await, types, or decorators, then you'll need to install babel-eslint to use as the parser for ESLint.

If you're still getting an error, follow these steps:

  1. Double check to make sure that you're not typoing the new syntax ;)
  2. Determine what task is failing (for instance, is it ESLint?)
  3. Check that project's issue tracker to see if it is a known issue
  4. If it isn't, then open an issue here

Because of the fact that dependencies of this boilerplate, such as ESLint, are maintained by a team separate from Babel, there may be a delay between when a new feature is added to Babel and when those other libraries add support for it.

When should I consider using this boilerplate?

This library is ideal for libraries that export a single file.

When might I not want to use this boilerplate?

You can always use this boilerplate as inspiration, but it works best for smaller libraries. If you're building a full-scale webapp, you will likely need to make more changes to the build system. This is because the boilerplate only deals with JavaScript; common build tasks like CSS preprocessing, image minification, or HTML template building are intentionally omitted from this boilerplate.

There are so many different preferences and needs when it comes to building a webapp, it wouldn't make sense to pick any one configuration for this boilerplate.

In the broader scheme of things, there's been discussion in the JavaScript community over whether or not boilerplates are good, or if they are bad. There's no denying that you might could yourself lost is you pick this up without much experience with the tools used in this project. But it could also save you a lot of time if you're simply trying to get a build system set up and running.

Even if you don't intend to use this boilerplate, I believe that boilerplates are useful as an example and as a source of inspiration. I encourage you to look through the current state of the project, and through its history, to see different ways to use Babel with various tools!

What's the browser compatibility?

As a rule of thumb, Babel is most reliable in IE9 and above.

Are there examples?

Quite a few. Check them out on the wiki.

Is there a version for Node-only projects?

There is not a maintained version for Node-only projects. As of Node v6, many of the most commonly used ES2015 features are now supported natively in Node. I strongly recommend that you weigh the pros and cons of adding a transpiling step to your server-side code!

What's the cost of transpiling?

A thorough analysis of this question can be found here.

How can I use this with React?

Do you wish to use JSX? If the answer is no, then there is nothing that you need to do. If the answer is yes, then you need to install the babel-preset-react, and add it to your .babelrc file.

An example .babelrc file with this preset configured can be seen here.

Also, if you plan to use jsx files, you'll need to update the webpack configuration to apply the Babel loader to .jsx files. Out of the box, it only looks for .js files.

This involves changing this line to be {test: /\.js|.jsx$/, exclude: /node_modules/, loader: 'babel-loader'},

How can I export my library without the "default" property?

As stated here, https://github.com/59naga/babel-plugin-add-module-exports:

Babel@6 doesn't export default module.exports any more So just npm install babel-plugin-add-module-exports --save-dev and then add it to your .babelrc file:

{
  "presets": ["latest"],
  "plugins": [
    "add-module-exports",
    "transform-es2015-modules-umd"
  ]
}

Troubleshooting

Accessing the window or DOM in tests isn't working

The tests run in Node, which has neither a window nor the DOM. To access the DOM, you'll need to do a bit of configuration. The quickest solution would be to add JSDom. The test setup file has a comment on the easiest way to get JSDom up-and-running (it takes all of 10 seconds).

Alternatively, you could set up PhantomJS to run the tests in a headless webkit environment, or set up Karma to run the tests in real browsers.

Customizing

This boilerplate is, to a degree, customizable. To make changes, find what you're looking to do below and follow the instructions.

I want to change the primary source file

The primary source file for the library is src/index.js. Only the files that this file imports will be included in the final build. To change the name of this entry file:

  1. Rename the file
  2. Update the value of entryFileName in package.json under babelBoilerplateOptions

I want to change the destination file name or directory

  1. Update main in package.json

I want to change what variable my module exports

MyLibrary is the name of the variable exported from this boilerplate. You can change this by following these steps:

  1. Ensure that the variable you're exporting exists in your scripts
  2. Update the value of exportVarName in package.json under babelBoilerplateOptions
  3. Check that the unit tests have been updated to reference the new value

I don't want to export a variable

When prompted for the name of the library's main variable, leave the response empty.

My library depends on an external module

Install the module and use it in your scripts like usual. Then, go into the Gulpfile and add the externals to the Webpack build options.

If you want to access the module itself in your unit test files, you will need to set up the test environment to support the module. To do this:

  1. Load the module in the test setup file.
  2. Add any imported variables to globals object in the test globals JSON.

More Repositories

1

babel

๐Ÿ  Babel is a compiler for writing next generation JavaScript.
TypeScript
42,835
star
2

babel-loader

๐Ÿ“ฆ Babel loader for webpack
JavaScript
4,794
star
3

minify

โœ‚๏ธ An ES6+ aware minifier based on the Babel toolchain (beta)
JavaScript
4,375
star
4

babel-preset-env

PSA: this repo has been moved into babel/babel -->
JavaScript
3,502
star
5

babel-sublime

Syntax definitions for ES6 JavaScript with React JSX extensions.
JavaScript
3,260
star
6

babel-eslint

๐Ÿ—ผ A wrapper for Babel's parser used for ESLint (renamed to @babel/eslint-parser)
JavaScript
2,970
star
7

example-node-server

Example Node Server w/ Babel
JavaScript
2,842
star
8

babylon

PSA: moved into babel/babel as @babel/parser -->
JavaScript
1,714
star
9

babelify

Browserify transform for Babel
JavaScript
1,680
star
10

gulp-babel

Gulp plugin for Babel
JavaScript
1,322
star
11

babel-upgrade

โฌ†๏ธ A tool for upgrading Babel versions (to v7): `npx babel-upgrade`
JavaScript
1,308
star
12

awesome-babel

๐Ÿ˜ŽA list of awesome Babel plugins, presets, etc.
857
star
13

babel-standalone

๐ŸŽฎ Now located in the Babel repo! Standalone build of Babel for use in non-Node.js environments, including browsers.
JavaScript
821
star
14

preset-modules

A Babel preset that targets modern browsers by fixing engine bugs (will be merged into preset-env eventually)
JavaScript
740
star
15

website

๐ŸŒ The Babel documentation website
TypeScript
740
star
16

kneden

Transpile ES2017 async/await to vanilla ES6 Promise chains: a Babel plugin
JavaScript
514
star
17

grunt-babel

Grunt plugin for Babel
JavaScript
438
star
18

proposals

โœ๏ธ Tracking the status of Babel's implementation of TC39 proposals (may be out of date)
433
star
19

eslint-plugin-babel

An ESlint rule plugin companion to babel-eslint
JavaScript
390
star
20

babel-time-travel

Time travel through babel transformations one by one (implemented in the Babel REPL now)
JavaScript
345
star
21

babel-polyfills

A set of Babel plugins that enable injecting different polyfills with different strategies in your compiled code.
TypeScript
321
star
22

babel-sublime-snippets

Next generation JavaScript and React snippets for Sublime
264
star
23

karma-babel-preprocessor

Karma plugin for Babel
JavaScript
167
star
24

ruby-babel-transpiler

Ruby Babel is a bridge to the JS Babel transpiler.
Ruby
158
star
25

babel-jest

Jest plugin for Babel (moved) ->
139
star
26

notes

โ™ฌ Notes from the @Babel Team (discuss in PRs)
121
star
27

generator-babel-plugin

Babel Plugin generator for Yeoman
JavaScript
118
star
28

babel-bridge

A placeholder package that bridges babel-core to @babel/core.
JavaScript
94
star
29

babel-bot

๐Ÿค– A helpful bot to automate common tasks on Babel Issues/PRs
JavaScript
75
star
30

babel-brunch

Brunch plugin for Babel
JavaScript
69
star
31

broccoli-babel-transpiler

Broccoli plugin for Babel
JavaScript
58
star
32

jade-babel

Jade plugin for Babel
JavaScript
40
star
33

sandboxes

Babel repl-like codesandbox: check out link =>
JavaScript
39
star
34

jekyll-babel

A Babel converter for Jekyll.
Ruby
34
star
35

acorn-to-esprima

(unmaintained) Converts acorn tokens to esprima tokens
JavaScript
33
star
36

babel-connect

Connect plugin for Babel
JavaScript
27
star
37

podcast.babeljs.io

The Babel Podcast site!
JavaScript
25
star
38

phabricator-to-github

A tool to migrate phabricator issues to github
JavaScript
24
star
39

actions

Babel specific github actions ๐Ÿค–
JavaScript
24
star
40

rfcs

RFCs for changes to Babel
21
star
41

metalsmith-babel

A Metalsmith plugin to compile JavaScript with Babel
JavaScript
20
star
42

babel-configuration-examples

WIP
JavaScript
18
star
43

duo-babel

Duo plugin for Babel
JavaScript
16
star
44

logo

Babel logo
15
star
45

parser_performance

JavaScript
13
star
46

babel-standalone-bower

Bower build of babel-standalone. See https://github.com/Daniel15/babel-standalone
JavaScript
10
star
47

gobble-babel

Gobble plugin for Babel
JavaScript
9
star
48

eslint-config-babel

ESLint config for all Babel repos
JavaScript
9
star
49

eslint-plugin-babel-plugin

A set of eslint rules to enforce best practices in the development of Babel plugins.
JavaScript
8
star
50

flavortown

what else?
7
star
51

.github

Community health files for the Babel organization
7
star
52

babel-archive

๐Ÿ—‘ Packages that are deprecated or don't need to be updated
JavaScript
6
star
53

babel-test262-runner

Run test262 tests on Node 0.10 using Babel 7 and `core-js@3`.
JavaScript
6
star
54

babel-plugin-proposal-private-property-in-object

@babel/plugin-proposal-private-property-in-object, with an added warning for when depending on this package without explicitly listing it in dependencies or devDependencies
JavaScript
6
star
55

phabricator-redirects

โ†ฉ๏ธ Redirects for old phabricator urls
HTML
5
star
56

babel-plugin-transform-async-functions

https://github.com/MatAtBread/fast-async/issues/46
4
star
57

slack-invite-link

Source of slack.babeljs.io
1
star