Optional Chaining for JavaScript with Babel 7 and Webpack 4

Daniel Wild
1 min readMar 25, 2019

--

Super-quick-start guide to use transform-optional-chaining proposal with Babel 7 and Webpack 4.

Install babel loader and dependencies for Webpack:

npm install --save-dev babel-loader @babel/core @babel/plugin-proposal-optional-chaining

Add somebabel.config.js

module.exports = function (api) {  api.cache(true);
const presets = ["@babel/preset-env",];
const plugins = ["@babel/plugin-proposal-optional-chaining"];
return { presets, plugins };
};

Add babel-loader to webpack.config.js

module: {   
rules: [
{
test: /\.js$/, exclude: /node_modules/, loader: "babel-loader"
}
]
}

You should now be able to transpile and use the new (stage 4 proposal) syntax:

const baz = obj?.foo?.bar?.baz;

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Daniel Wild
Daniel Wild

Responses (1)

Write a response