Code Monkey home page Code Monkey logo

mini-webpack's Introduction

这是一个mini-webpack,为了方便大家最简单理解webpack打包过程中的原理知识

打包大体思路

  1. 从入口开始,利用node文件模块系统读取文件内容,借助@babel/paserer解析内容生成ast, 借助@bable/traverse遍历器 找出模块依赖关系deps,这一步还需要借助@bable/core 中的transformer 转换器转换es6->es5
  2. 根据入口deps的依赖递归读取文件内容,生成模块队列信息,转换模块信息成依赖图的数据结构,如下
  const graph = {
  './example/main.js': { //入口
    code: "import foo from './foo.js'\n" +
      'foo()\n' +
      'function main(){\n' +
      "    console.log('mian module')\n" +
      '}',
    deps: [ './foo.js' ]
  },
  './example/bar.js': { // bar.js
    code: "export default function bar(){\n    console.log('bar module')\n}",
    deps: []
  },
  './example/foo.js': { //foo.js
    code: "import bar from './bar.js'\n" +
      'export default function foo(){\n' +
      "    console.log('foo module')\n" +
      '}',
    deps: [ './bar.js' ]
  }
 }

  1. 浏览不支持require()方法,module.exports对象,那我们需要模拟实现
  2. 动态生成bundle.js 文件后写入文件

mini-webpack's People

Contributors

chw1617 avatar

Watchers

 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.