ECMAScript Cookbook
上QQ阅读APP看书,第一时间看更新

How to do it...

  1. Open your command-line application and navigate to the directory containing the 02-creating-client-bundles package.
  2. Start the Python HTTP server.
  3. Update the main.js file to use the arrow function syntax:
import { atlas, saturnV } from './rockets/index.js' 
 
export function main () { 
  const rockets = [saturnV, atlas]; 
  rockets.map((rocket) => rocket.launch() ); 
} 
  1. Install Babel, the preset-es2015, and the associated webpack loader:
    npm install --save-dev babel-cli babel-preset-es2015 babel-loader
  1. Create a Babel configuration file named .babelrc:
// .babelrc 
{ 
  "presets": ["es2015"] 
} 
  1. Configure webpack to use Babel for transpiling new language features:
const path = require('path'); 
 
module.exports = { 
  entry: ['babel-polyfill', './index.js'], 
  output: { 
    filename: 'bundle.js', 
    path: path.resolve(__dirname) 
  },  module: {
rules: [
{
test: /.js$/,
exclude: /node_modules/,
use: 'babel-loader'
}
]
} };
  1. Add a webpack build command to the script section of the package.json file:
{
/* package.json configuration */

"scripts": {
"bundle": "webpack --config webpack.config.js",
}

/* remaining properties */
}
  1. Run the webpack build:
npm run bundle  
  1. Now, open a browser and open the Developer Console while visiting the URL:
    http://localhost:8000/.
  1. You should see the code running correctly: