官术网_书友最值得收藏!

Putting it all together with Webpack

Jest includes Babel, which transpiles all our code when it's run in the test environment. But what about when we're serving our code via our website? Jest won't be able to help us there.

That's where Webpack comes in, and we can introduce it now to help us, do a quick manual test:

  1. Install Webpack using the following command:
npm install --save-dev webpack webpack-cli babel-loader
  1. Add the following to the scripts section of your package.json:
"build": "webpack",
  1. You'll also need to set some configuration for Webpack. Create the webpack.config.js file in your project root directory with the following content:
const path = require("path");
const webpack = require("webpack");

module.exports = {
mode: "development",
module: {
rules: [{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
loader: 'babel-loader'}]}
};

This configuration works for Webpack in development mode. Consult the Webpack documentation for information on setting up production builds.

  1. In your source directory, run the following commands:
mkdir dist
touch dist/index.html
  1. Add the following content to the file you just created:
<!DOCTYPE html>
<html>
<head>
<title>Appointments</title>
</head>
<body>
<div id="root"></div>
<script src="main.js"></script>
</body>
</html>
  1. You're now ready to run the build:
npm run build

You should see a bunch of output like this:

  Asset Size Chunks Chunk Names
main.js 764 KiB main [emitted] main
Entrypoint main = main.js
[./src/Appointment.js] 4.67 KiB {main} [built]
[./src/index.js] 544 bytes {main} [built]
[./src/sampleData.js] 726 bytes {main} [built]
+ 11 hidden modules
  1. Open index.html in your browser and behold your creation:
The following screenshot shows the application once the Exercises are completed, and with added CSS and extended sample data. To include the CSS, you'll need to pull dist/index.html and dist/styles.css from the chapter-2 tag. The sample data can be found in src/sampleData.js, within the same tag. If you're choosing not to complete the Exercises, you can skip to that tag now.

As you can see, we've only got a little part of the way to fully building our application. The first few tests of any application are always the hardest and take the longest to write. We are now over that hurdle, so we'll move quicker from here onward.

主站蜘蛛池模板: 邢台县| 灯塔市| 济源市| 万山特区| 阿合奇县| 威信县| 宁阳县| 天台县| 达拉特旗| 温宿县| 紫阳县| 铜鼓县| 沿河| 鹤庆县| 定远县| 安乡县| 怀柔区| 华安县| 秦安县| 呼伦贝尔市| 云南省| 吐鲁番市| 孟津县| 信宜市| 普安县| 昭通市| 石林| 宿松县| 滕州市| 宣威市| 潮安县| 陆川县| 武冈市| 获嘉县| 台东市| 水富县| 平舆县| 大港区| 兴宁市| 铜山县| 五台县|