The author's writing skills are still shallow, if there is anything wrong, please point out generously, I will be grateful
Today, I am suddenly interested in learning about front-end technology, so I hereby record the way to learn front-end. Since most of the projects done in the company before are about JSP
adding, deleting, modifying and checking pages, the front-end and back-end are all written by one person. The front-end is still in the js、html、css
stage, and I don’t know anything about some front-end frameworks. The problems encountered in the back-end cookie、session、token
, hereby simply understand the front-end knowledge.
Concept introduction
I probably found some tutorials on the Internet. Because I want to get it fast, I started to learn from building a project. For building projects webpack
, I think the front-end webpack
and back-end gradle、maven
comparisons are similar to a set of tools to simplify our development (I don't know if the analogy is accurate or not, if there is any error, I hope to point it out). Node
I think the front-end is like the back-end, Java
and it also asks me to install the environment and configure the environment variables from the beginning. After the concept is introduced, I will not talk about nonsense and start a simple project directly.
Environmental preparation
If a worker wants to do a good job, he must first sharpen his tools. Before building the project, prepare the environment.
- First of all, of course, is the installation
Node
environment. The node download address directly selects theNode
direct download and installation of the corresponding version, and you can go to the next step. If the installation is successful, thenode -v
version number will be displayed. - Install
Visual Studio Code
the software. The Visual download address is also to select the corresponding machine version to download and install.
Build the project
After the environment is ready, the next step is to build the project
-
Just create a folder and use it
Visual Studio Code
to open. -
Open the command line in
Visual Studio Code
, how to open it as shown below. -
After opening it, enter the
npm init -y
command , and find apackage.json
file (package management configuration file) to quickly initialize the project. -
Create two folders in the root directory
src
(the folder where the source code is stored) and the folderdist
(the directory where the released code is stored). -
src
Createindex.html
files under. How to quickly generatehtml
template content? There is a shortcut key (enter an exclamation mark! Then press theTab
key to quickly generatehtml
template content) -
src
Create theindex.js
file under, this is the entry file. -
Installation
cnpm
(npm
sometimes it is slow to use, because we download things from foreign websites, andcnpm
Chinese npm downloads directly from domestic websites, and the speed will be faster) The command isnpm i cnpm -g
-
cnpm
Install usingwebpack
the commandcnpm i webpack -D
-
cnpm
Install scaffolding using the commandcnpm i webpack-cli -D
-
Create a new file in the root directory
webpack.config.js
and add variables,// 向外暴露一个打包的配置对象 module.exports = { mode: 'development', }
mode
Two variables can be filled in heredevelopment和production
, one is used in the development process, whether the file generated in the dist filemain.js
is compressed, if the variable filled in is,development
then it will not be compressed, if it isproduction
, the js file will be compressed. -
At this point, we need to install the plug-in for dynamic deployment, that is, we
js
do not need to restart the project every time we modify the file, we only need to refresh it. The plug-in installation command iscnpm i webpack-dev-server -D
, and the parameter is addedpackage.json
in the middle , the function is to automatically open the page after the project is started successfully, and the function is to control the port number.scripts
"dev": "webpack-dev-server --open --port 3000"
--open
--port
-
The next step is the optimization phase.
html
We press the save button every time during the development process. If we interact with the hard disk every time, it will waste time and be bad for the disk loss. So we install a plug-in that can put each savedhtml
into memory, and every time we modify it, it will affect the file in memory. The plugin installation command iscnpm i html-webpack-plugin -D
. And configured in thewebpack.config.js
configuration file as follows.const HtmlWebpackPlugin = require('html-webpack-plugin') // 导入在内存中自动生成index页面的插件 const path = require('path') // 创建一个插件的实例对象 const htmlplugin = new HtmlWebpackPlugin({ template: path.join(__dirname,'./src/index.html'), // 源文件 filename: 'index.html' }) // 向外暴露一个打包的配置对象 module.exports = { mode: 'development', plugins:[ htmlplugin ] }
-
Start the project and enter directly on the command line
npm run dev
to access ourindex.html
page.
As someone who doesn't know anything about front-end, being able to start up and see the page is a successful first step. In the future, I will continue to learn the front-end in depth, and of course I will use it mainly. I will not talk about some principles (of course I will not). After all, the main focus is still on the rear end.
If you follow my steps without success, I hope to be able to point it out. I will correct and improve