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;

--

--

Daniel Wild
Daniel Wild

Responses (1)