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

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.

主站蜘蛛池模板: 三河市| 天津市| 上蔡县| 无为县| 巫溪县| 车险| 康平县| 株洲县| 玉田县| 南涧| 桦南县| 义乌市| 蓬莱市| 六枝特区| 曲水县| 吴旗县| 阿瓦提县| 呼和浩特市| 漯河市| 镇巴县| 砀山县| 潞西市| 璧山县| 凌海市| 惠州市| 新巴尔虎左旗| 抚远县| 铜川市| 淮阳县| 鸡西市| 宜兰县| 普格县| 方山县| 乐业县| 横山县| 海林市| 苏尼特左旗| 大悟县| 浑源县| 响水县| 高安市|