Today, the mood suddenly want to learn about the technology front-end, it is hereby road leading end of the recording learning. Since the company before doing projects mostly on JSP
the additions and deletions to change search page, the front end of the back-end is a person to write, to just stay at the front js、html、css
stage, for some front-end framework that does not know, just learning the back end when it came to cookie、session、token
the question, is hereby also simply look at the front end of knowledge.
Concepts
Probably found some tutorials, because it is like to crash, so start building the project began to learn. Used to build the project webpack
, I think the front-end webpack
and back-end of gradle、maven
the comparison are similar simplified set of tools we have developed (here do not know about the accuracy of analogy, if false, then hope to be able to point it out). The front end Node
I think it is like the back end Java
, let me also start up the installation environment configuration environment variable. After the introduction to the concept of nonsense that open directly take a simple project.
Preparing the Environment
We must first of its profits. Before building the project should prepare the environment.
- First, of course, is to install
Node
the environment of, node address to download directly select the appropriate version hereNode
download and install directly, have been the next step can be. If the installation is successfulnode -v
to display publication of this number. - Install
Visual Studio Code
software. Download Visual, also choose their own corresponding machine-download version can be installed.
Building project
After the environment is ready, the next step is to build the project
-
Just create a folder, then
Visual Studio Code
open. -
In the
Visual Studio Code
open command line, as shown how to open. -
Open a command line and then enter the
npm init -y
command, found to generate apackage.json
file (package management profiles), fast initialization project. -
在根目录下建立两个文件夹
src
(存放源代码的文件夹)和dist
(存放发布代码的目录)文件夹。 -
在
src
下建立index.html
文件。怎么快速生成html
的模板内容呢?有个快捷键(输入叹号!然后按Tab
按键,即可快速生成html
模板内容) -
在
src
下建立index.js
文件,这是入口文件。 -
安装
cnpm
(使用npm
有时候会速度慢,因为我们从外国网站下载东西,cnpm
是中国的npm直接从国内网站下载,速度会快一些)命令为npm i cnpm -g
-
使用
cnpm
安装webpack
,命令cnpm i www.huanhua2zhuc.cn webpack www.yunzeyle.cn-www.jiuyueguojizc.cn D
-
使用
cnpm
安装脚手架,命令cnpm i webpack-cli -D
-
在根目录下新建
webpack.config.js
文件,然后加入变量,// 向外暴露一个打包的配置对象 module.exports = { mode:www.wanyayuue.cn 'development', }
这里
mode
可以填写两个变量development和production
,一个是开发过程中用的,在dist文件中生成的main.js
文件是否压缩,如果填写的变量是development
那么就不压缩,如果是production
则压缩js文件。 -
此时我们要安装动态部署的插件,即我们每次修改
js
文件后不需要重启项目,只需要刷新即可。插件安装命令为cnpm i webpack-dev-server -D
,并且在package.json
中在scripts
中加入参数"dev": "webpack-dev-server --open --port 3000"
,--open
作用是启动项目成功后自动打开页面,--port
作用是控制端口号。 -
接下来是优化阶段,
html
文件我们每次在开发过程中按保存键,如果每次都和硬盘做交互的话,那么会浪费时间并且对磁盘损耗也不好。所以我们安装一个插件可以将每次保存的html
放入内存中,我们每次修改的话都会作用到内存中的文件。插件安装命令为cnpm i html-webpack-plugin -D
。并且在webpack.config.js
配置文件中配置如下。const HtmlWebpackPlugin = require(www.jinyang3zhuc.cn'html-webpack-plugin') // 导入在内存中自动生成index页面的插件 const path = require('path') // 创建一个插件的实例对象 const htmlplugin = new HtmlWebpackPlugin({ template: path.join(www.tianhyLzc.cn__dirname,'./src/index.html'), // 源文件 filename: 'index.html' }) // 向外暴露一个打包的配置对象 module.exports =www.yachengyl.cn { mode: 'development', plugins:[ htmlplugin ] }
-
启动项目,直接在命令行中输入
npm run dev
即可访问到我们的index.html
页面了。
作为一个对前端一无所知的我来说能够启动起来看到页面已经是成功的迈出第一步了。接下来有时间依然会不断的深入学习前端,当然还是以会用为主,一些原理我也不会讲(当然我也不会)。毕竟主要精力还是放在后端方向的。
如果大家根据我的步骤没有成功的,希望能够指出来。我会改正并完善