Giter Club home page Giter Club logo

fed-e-task-02-02's Introduction

简答题

1. Webpack 的构建流程主要有哪些环节?如果可以请尽可能详尽的描述 Webpack 打包的整个过程。

解答: 构建流程:

  • 确定入口:通过配置文件中webpack.config.js的entry找到打包的入口文件
  • 分析依赖:根据代码中的引入分析文件的依赖资源,并进行解析
  • 确定依赖:分析完成,获取到所有模块的依赖关系树
  • 开始编译:对依赖树开始递归,找到每个节点以来的资源文件。
  • 编译模块:通过配置文件中webpack.config.js的rules找到对应的加载器,来加载模块,进行编译。
  • 输出结果:将加载结果放置到打包结果中。
  • 写入资源:通过配置文件中webpack.config.js的output将文件输出到配置的路径和文件名。

打包结果分析:

  • 最后输出的代码是一个IIFE,接收一个modules参数
  • 参数是一个由全部模块集合成的数组,数组中每一项的都是一个参数列表相同的函数,模块运行在该私有域中。
  • 输出的文件中定义了缓存对象、require函数等。
  • 在require上挂载数据和工具函数,从0开始加载模块。
  • 加载模块时先判定该模块是否有缓存,有缓存时从缓存对象中读取,无缓存时用require加载该模块。
  • 加载后调用模块对应的函数并进行传参。
  • 导出对象并打上标记。
  • 多个模块时从require开始重复步骤,直到全部模块加载结束。

2. Loader 和 Plugin 有哪些不同?请描述一下开发 Loader 和 Plugin 的思路。

解答: Loader用于处理除JS外的其他类型的资源文件,对其进行编译转换,例如scss、html、img、font等

plugin用于解决其他自动化问题,例如目录清除、代码压缩等。

Loader开发:将需要处理的资源作为参数传入,进行处理,完成后需要返回的字符串是可执行的js代码,代码需符合CommonJS规范或是ESM,也可以返回结果,将其交给一下个loader来处理。

plugin开发: 本质是利用webpack的生命周期,将插件挂载到生命周期中的钩子函数上,插件必须为一个函数或包含apply方法的对象。挂载后在函数体内执行要进行的操作,最后在webpack.config的plugin中使用。

fed-e-task-02-02's People

Contributors

sylvanase avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.