如何结合Webpack 4和Babel 7创建一个出色的React应用
I previously wrote an article called “How to conquer Webpack 4 and build a sweet React app.” Soon after I wrote the article, babel swooped in with a major breaking change and many of the packages got deprecated. So I decided to write a new tutorial.
我之前写过一篇名为“ 如何征服Webpack 4并构建一个出色的React应用程序 ”的文章。 在我写了这篇文章后不久,babel进行了重大的重大更改,许多软件包被弃用了。 所以我决定写一个新的教程。
I will focus on setting up webpack with react which will have .scss support along with code splitting
我将重点介绍如何使用ract设置webpack ,它具有.scss支持以及代码拆分功能
The purpose for writing this again is simple: I want everyone to feel comfortable. Because setting up webpack can be really daunting. Especially for new developers out there. Follow along, and what seemed difficult and maybe scary will seem like a piece of cake.
再次编写此代码的目的很简单:我希望每个人都感到舒服。 因为设置webpack确实很艰巨。 特别是对于那里的新开发人员。 跟着走,似乎困难甚至可怕的事情似乎只是小菜一碟。
Before we start, here is the source code. I know this has loads of things in it. I plan to use the same code base to talk about webpack, react, SCSS, hot module replacement, testing with jest and enzyme, linting code, and adding a code formatter like prettier in other articles to come, so I will continuously keep on updating this code base. I will not bloat this code base — I promise.
在开始之前,这里是源代码 。 我知道里面有很多东西。 我计划使用相同的代码库来谈论Webpack,React,SCSS,热模块替换,用玩笑和酶进行测试,整理代码,并在以后的其他文章中添加像prettier这样的代码格式化程序,因此我将不断更新此代码库。 我保证不会夸大此代码库。
Note: If you feel like making a PR for the repository, you are more than welcome :) So let’s get started.
注意:如果您想为存储库创建PR,欢迎您::因此,让我们开始吧。
For simplicity sake, this article is only going to focus on;
为了简单起见,本文仅着重讨论;
- Setting up Webpack 4 with Babel 7 for React 使用Babel 7为React设置Webpack 4
- Support for .SCSS 支持.SCSS
- Code Splitting 代码分割
- Development environment with HMR (Hot Module Replacement) 具有HMR(热模块替换)的开发环境
- Production configuration 生产配置
- Dividing your Webpack configuration into chunks 将您的Webpack配置分为多个块
- Handling staging, demo, production, test and other environments in code 在代码中处理登台,演示,生产,测试和其他环境
- Generating a visualizer in production build to check which chunk of code took how much size and what are the dependencies of the chunks. Super useful. 在生产版本中生成可视化工具,以检查哪个代码块占用了多少大小以及这些块的依赖关系是什么。 超级有用。
先决条件 (Prerequisite)
You need to have node installed in order to use npm (node package manager).
您需要安装节点才能使用npm(节点程序包管理器)。
First things first, create a folder called app then open up your terminal and go into that app folder and type:
首先,创建一个名为app的文件夹,然后打开您的终端并进入该app文件夹并键入:
console.warn('I am a Star Trek nerd');
console.log('So through out this tutorial, you will see a lot of Star Trek quotes');
console.log('Starting now');
console.log("Compassion: that’s the one thing no machine ever had. Maybe it’s the one thing that keeps men ahead of them. -Dr McCoy"); You can write anything above of course. I chose Star Trek.
您当然可以写任何东西。 我选择了《星际迷航》。
Next we need to install a couple of dependencies. You can just copy the dependencies & devDependencies from the package.json below into your own and do an npm install:
接下来,我们需要安装几个依赖项。 您可以将下面的package.json中的dependencies和devDependencies复制到自己的文件中,然后执行npm install :
{"name": "react-boiler-plate","version": "1.0.0","description": "A react boiler plate","main": "src/index.js","author": "Adeel Imran","license": "MIT","scripts": {"start": "a script will come here"},"dependencies": {"react": "^16.5.2","react-dom": "^16.5.2"},"devDependencies": {"@babel/core": "^7.0.0","@babel/plugin-proposal-class-properties": "^7.0.0","@babel/plugin-proposal-export-namespace-from": "^7.0.0","@babel/plugin-proposal-throw-expressions": "^7.0.0","@babel/plugin-syntax-dynamic-import": "^7.0.0","@babel/polyfill": "^7.0.0-beta.51","@babel/preset-env": "^7.0.0-beta.51","@babel/preset-react": "^7.0.0-beta.51","babel-loader": "^8.0.0-beta.0","copy-webpack-plugin": "^4.5.1","css-loader": "^0.28.11","html-webpack-plugin": "^3.2.0","mini-css-extract-plugin": "^0.4.3","node-sass": "^4.8.3","optimize-css-assets-webpack-plugin": "^4.0.0","sass-loader": "^7.0.3","style-loader": "^0.21.0","uglifyjs-webpack-plugin": "^1.2.5","webpack": "^4.12.0","webpack-cli": "^3.0.8","webpack-dev-server": "^3.1.4","webpack-merge": "^4.1.3","webpack-visualizer-plugin": "^0.1.11"}
} Yes I know, I know! That’s a lot to create a hello world react app. But wait, this is all you will need. Even if you want to create a enterprise level app. (Maybe one or two more things depending on your requirements, but this is the backbone for it.)
是的,我知道,我知道! 创建一个hello world react应用程序需要大量资源。 但是,等等,这就是您所需要的。 即使您要创建企业级应用程序。 (根据您的要求,也许还有一两件事,但这是它的基础。)
So let’s talk about each and everyone of them before we dive deep into the code.
因此,在深入研究代码之前,让我们谈谈每个人。
webpack: We need Webpack to bundle our code.
webpack :我们需要Webpack来捆绑我们的代码。
webpack-cli: We will be using some CLI features of Webpack to make our lives easier while writing some scripts.
webpack-cli :我们将使用Webpack的某些CLI功能,以简化编写脚本的过程。
webpack-dev-server: I will create a server using the webpack-dev-server package. This is only meant to be used in the development environment, and not for production. This means while developing and working on my code, I don’t need a separate server like NodeJS to setup manually.
webpack-dev-server :我将使用webpack-dev-server包创建服务器。 这仅用于开发环境,而不用于生产。 这意味着在开发和处理代码时,不需要像NodeJS这样的单独服务器即可进行手动设置。
webpack-merge: To divide our configuration into chunks, more on this later
webpack-merge :将我们的配置分为多个块,稍后会详细介绍
webpack-visualizer-plugin: To see a visual representation of each of our bundle size — how much space they are taking and what are their dependencies.
webpack-visualizer-plugin :要查看每个捆绑包大小的可视化表示-它们占用了多少空间以及它们之间的依赖关系。
style-loader: This adds CSS to the DOM by injecting a
