Giter Site home page Giter Site logo

doc-js-babel-intro's Introduction

Studying note and sample code - Using Babel

  • https://leanpub.com/doc-js
  • If you want to contribute to the book or join me as a coauthor pool, please get in contact mgalli at mgalli dot com subject "doc-js book"

Using babel to run an es2015-based script test.js

Babel basic install with NodeJs. If you have npm, Node, and npx installed, you can then proceed:

npm init

After initializing your project, let's install Babel and the command line project:

npm install babel --save-dev
npm install babel-cli --save-dev

Let's write your ES2015 script

Module script:

export default {
  foo: 'bar'
}

Import script:

import { foo } from './module-test';

console.log(foo);
npx babel test.js

Using npx will search for the executable, you can also do the following

./node_modules/babel-cli/bin/babel.js test.js

Which should do nothing and output:

import { foo } from './module-test';

console.log(foo);

So you need to have the es2015 transpiler function installed whcih can be done installing the Babel extension entitled "babel-preset-env"

 npm install babel-preset-env --save-dev

Legacy note: priorly to this a developer had to install specific presets, such as "babel-preset-es2015". From looking Babel site [1] it was deprecated and now you can simply get the [2] "babel-preset-env" instead.

[1] https://babeljs.io/docs/en/babel-preset-es2015/

[2] https://babeljs.io/docs/en/babel-preset-env

You can test now the transpilation:

 npx babel test.js --presets env

And you can write less informing a Babel configuration as a hidden file:

But this works because of you have defined a preset in a .babelrc:

{
  "presets": ["env"]
}

And test with a shorter command:

npx babel test.js --presets env

With the output being:

'use strict';

var _moduleTest = require('./module-test');

console.log(_moduleTest.foo);

Learnings

The notion of accessing a property, foo, from a module, is a basic need. In ES2015, the concept entitled destructuring refers to the ways you can access, or unpack, certain values or properties from objects:

import { foo } from './module-test';

Issue with this article

It's not clear the different between the above and the following:

import foo from './module-test';

See this https://stackoverflow.com/questions/33524696/es6-destructuring-and-module-imports?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

References

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

doc-js-babel-intro's People

Contributors

taboca avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.