Set Up Babel and Webpack

1. Create a project directory and cd in to that directory from the terminal

2. Initiate npm:

npm init -y

3. Install webpack and webpack-cli

npm i webpack --save-dev

npm i webpack-cli --save-dev

4. Add webpack command under scripts in package.json

"build": "webpack --mode production"

5. Install babel and babel loader

npm i @babel/core babel-loader @babel/preset-env @babel/preset-react --save-dev

6. Create .babelrc file and configure it

{ "presets": ["@babel/preset-env", "@babel/preset-react"] }

To run / test async code .baelrc file should be:

{ "presets": [
[
  "@babel/preset-env", {
"targets": {
  "node": "current"
}
  }
]
]
}

7. Create webpack.config.js file and configure it

const path = require('path');
module.exports = {
  entry: path.resolve(__dirname, 'src'),
  output: {
        path: path.resolve(__dirname, 'build'),
        filename: 'bundle.js'
    },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
exclude: /node_modules/,
        use: {
          loader: "babel-loader"
        }
      }
    ]
  }
};

8. Create a src folder and create index.js file in that.

9. Run the following command

npm run build

The above command create a dist folder with the transpiled javascript file.

10. Run the transpiled js file:

node ./dist/main.js

Comments

Popular posts from this blog

Typescript Setup

Typescript + React Setup