Home > Web Front-end > HTML Tutorial > Summary of methods for optimizing directory structure in front-end projects

Summary of methods for optimizing directory structure in front-end projects

不言
Release: 2018-09-17 14:22:38
Original
2846 people have browsed it

What this article brings to you is a summary of methods for optimizing directory structure in front-end projects. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Directory structure optimization

Now front-end projects are becoming more and more like large-scale projects, and they are becoming more and more complex. Collaboration between team members needs to be handled well, and business needs to be done well. Blocking and decoupling reduce maintenance costs while maintaining high-efficiency development.

Optimizing the project directory structure is one way to achieve this goal. Generally speaking, whether it is a multi-page project or a single-page application, or both, the directory structure is the following two ways:

  1. type grouping (by file type, business type, etc. Grouping)

  2. Module block (block by page module, business module, etc.)

1. Type grouping

This method is to group by file type, business type or other types.

Multi-page project

|-- src/ 源代码目录

    |-- html/ html 文件目录
        |-- page1.html page1 页面的 html 文件
        |-- module1/ 子目录
            |-- page2.html page2 页面的 html 文件
            |-- ...
            
        |-- ...    
        
    |-- js/ js 文件目录
        |-- common/ 公共文件目录
        |-- page1/ page1 页面的 js 目录
        |-- module1/ 子目录
            |-- page2/ page2 页面的 js 目录
            |-- ...
            
        |-- ...
        
    |-- css/ css 文件目录
        |-- common/ 公共文件目录
        |-- page1/ page1 页面的 css 目录
        |-- module1/ 子目录
            |-- page2/ page2 页面的 css 目录
            |-- ...
            
        |-- ...
        
    |-- less/ less 文件目录(内部结构跟上面类似)
    |-- images/ 图片文件目录(内部结构跟上面类似)
    |-- data/ api-mock 文件目录(内部结构跟上面类似)
    |-- ...
Copy after login

Single-page application

|-- src/ 源代码目录
    |-- components/ 组件文件目录(如 react)
        |-- common/ 公共文件目录
        |-- page1.js page1 页面的组件文件
        |-- module1/ 子目录
            |-- page2.js page2 页面的组件文件
            |-- ...
            
        |-- ...    
        
    |-- services/ service 文件目录
        |-- service1.js page1 页面的 service
        |-- module1/ 子目录
            |-- service2.js page2 页面的 service
            |-- ...
            
        |-- ...
        
    |-- models/ model 文件目录
        |-- model1.js page1 页面的 model
        |-- module1/ 子目录
            |-- model2.js page2 页面的 model
            |-- ...
            
        |-- ...
        
    |-- ...
    
|-- images/ 图片文件目录(内部结构跟上面类似)
|-- data/ api-mock 文件目录(内部结构跟上面类似)
|-- ...
Copy after login

The advantage of this method is that it can make the file classification and function classification very clear, and it can constrain the writing method (directory structure) of team members to a certain extent. It is also clear, simple and easy to understand. But this method has obvious shortcomings:

  1. It is not easy and quick to know which files a certain page or function block has;

  2. Creating, updating, and deleting pages will become very inefficient because you need to go to different file category directories to find files;

  3. The development efficiency is not high, and it is easy to get tired because the editor When working on a page, you need to expand each file in the editor's file navigation, and the navigation will be very long.

So, for front-end projects, in most cases I will not use this directory structure.

2. Module Blocking

This method is divided into page modules, business modules or other types.

Multi-page project

|-- src/ 源代码目录

    |-- page1/ page1 页面的工作空间
        |-- index.html html 入口文件
        |-- index.js js 入口文件
        |-- index.(css|less|scss) 样式入口文件
        |-- html/ html 片段目录
        |-- js/ js 文件目录
        |-- (css|less|scss)/ 样式文件目录
        |-- data/ 本地 json 数据模拟
        |-- images/ 图片文件目录
        |-- components/ 组件目录(如果基于 react, vue 等组件化框架)
        |-- ...
        
    |-- module1/ 子目录
        |-- page2/ page2 页面的工作空间(内部结构跟 page1 类似)
        
    |-- ...
    
|-- html/ 公共 html 片段
|-- less/ 公共 less 目录
|-- components/ 公共组件目录
|-- images/ 公共图片目录
|-- data/ 公共 api-mock 文件目录
|-- ...
Copy after login

Single-page application

|-- src/ 源代码目录
    |-- page1/ page1 页面的工作空间
        |-- index.js 入口文件
        |-- service.js
        |-- model.js
        |-- data/ 本地 json 数据模拟
        |-- images/ 图片文件目录
        |-- components/ 组件目录(如果基于 react, vue 等组件化框架)
        |-- ...
        
    |-- module1/ 子目录
        |-- page2/ page2 页面的工作空间(内部结构跟 page1 类似)
        
    |-- ...
    
|-- images/ 公共图片目录
|-- data/ 公共 api-mock 文件目录
|-- components/ 公共组件目录   
|-- ...
Copy after login

This method avoids the problem of "type grouping", but it also has some shortcomings:

  1. The constraints on group members are very small, and the directory structure under each workspace can be completely different;

  2. The directory structure under the workspace is not easy to define, and the requirements for developers are higher.

Although there are some shortcomings, they can be eliminated with the build tool, so generally I will choose this directory structure.

3. Use together

In many cases, these two methods can be used together, such as a small single-page application in a multi-page project.

|-- src/ 源代码目录

    |-- page1/ page1 页面的工作空间
        |-- index.html html 入口文件
        |-- index.js js 入口文件
        |-- index.(css|less|scss) 样式入口文件
        |-- html/ html 片段目录
        |-- js/ js 文件目录
            |-- ajax/ 对 ajax 封装的目录
            |-- util/ 工具类函数的目录
            |-- pages/ spa 应用页面目录
            |-- data/ 静态数据目录
            |-- tpl/ 模板目录
            |-- (event|view)/ 事件监听文件目录
            |-- ...
        |-- data/ 本地 json 数据模拟
        |-- (css|less|scss)/ 样式文件目录
        |-- images/ 图片文件目录
        |-- components/ 组件目录(如果基于 react, vue 等组件化框架)
        |-- ...
        
    |-- ...
Copy after login

The above is the detailed content of Summary of methods for optimizing directory structure in front-end projects. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template