In this article, we mainly share with you Vue front-end architecture learning (1). This is a sharing of Vue front-end architecture from scratch. I hope it can help everyone.
Think about it, I have already done a lot of architectural projects, some based on vue, based on react, based on thinkPHP, based on laravel. If you do it too much, you will have various ideas about the existing architecture, some good and some bad. In short, it is still not comfortable to use. Although vue-cli can be built and used quickly, especially vue-cli v3.0, webpack is sealed into the @vue/cli
SDK, making it cleaner and more concise to use.
Okay, now that the introduction is complete, I will start from scratch and build a front-end architecture step by step with completely separated front and back ends.
Steps
Since there is a lot to introduce, it is all written in one article, which is too long.
So, I will divide it into:
Create the webpack configuration file in the development environment
Configure eslint, babel, postcss
Create project files and directory structure
Implement local data interface simulation through koa
Create the webpack configuration file in the release environment
Create the webpack configuration file in the test environment and test cases (TODO)
Automatic initialization and build Project (TODO)
These seven articles will be introduced separately.
Development
1. Initialize the project
Create the project folder
We will call itvue-construct
吧
- ##Initialize git
- Initialize npm
- Create project file
- In order to make webpack run, instead of just talking about the configuration without running it, it would be a bit empty, so we first create some project files and directories.
npm i -S vue vue-router
. We put all project code related files in a folder named
app
. I'll create them all first, and then introduce them one by one. <pre class="brush:php;toolbar:false">├── app
│ ├── app.vue
│ ├── common
│ │ ├── img
│ │ ├── js
│ │ └── scss
│ ├── index.html
│ ├── index.js
│ ├── router
│ │ └── index.js
│ └── views
│ └── home
│ └── index.vue
├── .gitignore
├── package-lock.json
├── package.json
└── webpack.config.js</pre>
node_modules will be ignored.
Purpose | |
---|---|
As the main file of vue | |
Put public code in it | |
Page template file | |
Project main entry file | |
Put the router file corresponding to vue | |
Put the view file | |
Ignore node_module |
2. Configure webpack.config.js
- Install a series of packages:
- In order to run webpack, you need Installation
webpack webpack-dev-server
In order to process vue single page files, install:
vue-loader
In order to process scss files and extract them from js, install:
node-sass style-loader css-loader sass-loader vue-style-loader postcss postcss-loader autoprefixer extract-text-webpack-plugin
In order to process image and font files , Installation:
file-loader url-loader
To support advanced syntax-babel, Installation:
babel babel-loader babel-plugin-syntax-dynamic-import babel-plugin-transform-object-rest-spread babel-polyfill babel-preset-env
To verify code format-eslint, Installation:
eslint eslint-loader eslint-plugin-html babel-eslint
- Configure webpack .config.js file
-
const webpack = require('webpack') const path = require('path') const HtmlWebpackPlugin = require('html-webpack-plugin') const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin') // 为了抽离出两份CSS,创建两份ExtractTextPlugin // base作为基础的css,基本不变,所以,可以抽离出来充分利用浏览器缓存 // app作为迭代的css,会经常改变 const isProduction = process.env.NODE_ENV === 'production' const ExtractTextPlugin = require('extract-text-webpack-plugin') const extractBaseCSS = new ExtractTextPlugin( { filename:'static/css/base.[chunkhash:8].css', allChunks: true, disable: !isProduction // 开发环境下不抽离css } ) const extractAppCSS = new ExtractTextPlugin( { filename:'static/css/app.[chunkhash:8].css', allChunks: true, disable: !isProduction // 开发环境下不抽离css } ) // 减少路径书写 function resolve(dir) { return path.join(__dirname, dir) } // 网站图标配置 const favicon = resolve('favicon.ico') // __dirname: 总是返回被执行的 js 所在文件夹的绝对路径 // __filename: 总是返回被执行的 js 的绝对路径 // process.cwd(): 总是返回运行 node 命令时所在的文件夹的绝对路径 const config = { // sourcemap 模式 devtool: 'cheap-module-eval-source-map', // 入口 entry: { app: [ 'babel-polyfill', // 这里是配合babel-present-env导入的动态babel-polyfill,因此npm需dev依赖 resolve('app/index.js') ] }, // 输出 output: { path: resolve('dev'), filename: 'index.bundle.js' }, resolve: { // 扩展名,比如import 'app.vue',扩展后只需要写成import 'app'就可以了 extensions: ['.js', '.vue', '.scss', '.css'], // 取路径别名,方便在业务代码中import alias: { api: resolve('app/api/'), common: resolve('app/common/'), views: resolve('app/views/'), components: resolve('app/components/'), componentsBase: resolve('app/componentsBase/'), directives: resolve('app/directives/'), filters: resolve('app/filters/'), mixins: resolve('app/mixins/') } }, // loaders处理 module: { rules: [ { test: /\.js$/, include: [resolve('app')], loader: [ 'babel-loader', 'eslint-loader' ] }, { test: /\.vue$/, exclude: /node_modules/, loader: 'vue-loader', options: { extractCSS: true, loaders: { scss: extractAppCSS.extract({ fallback: 'vue-style-loader', use: [ { loader: 'css-loader', options: { sourceMap: true } }, { loader: 'postcss-loader', options: { sourceMap: true } }, { loader: 'sass-loader', options: { sourceMap: true } } ] }) } } }, { test: /\.(css|scss)$/, use: extractBaseCSS.extract({ fallback: 'style-loader', use: [ { loader: 'css-loader', options: { sourceMap: true } }, { loader: 'postcss-loader', options: { sourceMap: true } }, { loader: 'sass-loader', options: { sourceMap: true } } ] }) }, { test: /\.(png|jpe?g|gif|svg|ico)(\?.*)?$/, loader: 'url-loader', options: { limit: 8192, name: isProduction ? 'static/img/[name].[hash:8].[ext]' : 'static/img/[name].[ext]' } }, { test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/, loader: 'url-loader', options: { limit: 8192, name: isProduction ? 'static/font/[name].[hash:8].[ext]' : 'static/font/[name].[ext]' } } ] }, plugins: [ // html 模板插件 new HtmlWebpackPlugin({ favicon, filename: 'index.html', template: resolve('app/index.html') }), // 抽离出css extractBaseCSS, extractAppCSS, // 热替换插件 new webpack.HotModuleReplacementPlugin(), // 更友好地输出错误信息 new FriendlyErrorsPlugin() ], devServer: { proxy: { // 凡是 `/api` 开头的 http 请求,都会被代理到 localhost:7777 上,由 koa 提供 mock 数据。 // koa 代码在 ./mock 目录中,启动命令为 npm run mock。 '/api': { target: 'http://localhost:7777', // 如果说联调了,将地址换成后端环境的地址就哦了 secure: false } }, host: '0.0.0.0', port: '9999', disableHostCheck: true, // 为了手机可以访问 contentBase: resolve('dev'), // 本地服务器所加载的页面所在的目录 // historyApiFallback: true, // 为了SPA应用服务 inline: true, //实时刷新 hot: true // 使用热加载插件 HotModuleReplacementPlugin } } module.exports = { config: config, extractBaseCSS: extractBaseCSS, extractAppCSS: extractAppCSS }
Summary
This article mainly does three things:
- Easy to create Project structure
- After installing this article, and later using the npm package
- Webpack to configure the development environment
The above is the detailed content of Vue front-end architecture learning (1). For more information, please follow other related articles on the PHP Chinese website!

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.