Skip to content

Instantly share code, notes, and snippets.

@knksmith57
Last active December 8, 2018 21:43
Show Gist options
  • Save knksmith57/a554defde2d3d7cf64c4f453565352a0 to your computer and use it in GitHub Desktop.
Save knksmith57/a554defde2d3d7cf64c4f453565352a0 to your computer and use it in GitHub Desktop.
configure babel to support ESM and async/await (SO #53540716)
$ tree
.
├── README.md
├── babel.config.js
├── lib
├── package.json
└── src
    ├── app.js
    └── lib.js

2 directories, 5 files

$ npm install

$ npm run build

$ tree -I node_modules
.
├── README.md
├── babel.config.js
├── lib
│   ├── app.js
│   └── lib.js
├── package-lock.json
├── package.json
└── src
    ├── app.js
    └── lib.js

references

// src/app.js
import { guid, currentDateTimeISOString, stringToBoolean } from './lib'
async function hello() {
return 'world'
}
module.exports = {
plugins: ['@babel/plugin-transform-async-to-generator'],
presets: [
[
'@babel/env',
{
targets: {
node: 'current'
},
modules: 'cjs'
}
]
]
}
// src/lib.js
// generate unique id
export const guid = () => {
const s4 = () => {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1)
}
return (
s4() +
s4() +
'-' +
s4() +
'-' +
s4() +
'-' +
s4() +
'-' +
s4() +
s4() +
s4()
)
}
// get current date as ISO string
export const currentDateTimeISOString = () => {
var iso_string = new Date().toISOString()
return iso_string
}
// convert boolean string to boolean
export const stringToBoolean = val => {
var a = {
true: true,
false: false
}
return a[val]
}
{
"name": "babel-esm-async",
"version": "0.0.0",
"main": "lib/app.js",
"scripts": {
"build": "babel src -d lib"
},
"devDependencies": {
"@babel/cli": "^7.2.0",
"@babel/core": "^7.2.0",
"@babel/plugin-transform-async-to-generator": "^7.2.0",
"@babel/preset-env": "^7.2.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment