Giter VIP home page Giter VIP logo

miniwebpack's Introduction

miniWebpack

Dependencies

{
  "@babel/core": "^7.8.4",
  "@babel/parser": "^7.8.4",
  "@babel/preset-env": "^7.8.4",
  "@babel/traverse": "^7.8.4"
}

@babel/parser

编译代码拿到 AST。

var fs = require('fs');
var parser = require('@babel/parser');

function entryRead(pathname) {
  const entryContent = fs.readFileSync(pathname, 'utf-8');

  let AST = parser.parse(entryContent, {
    sourceType: "module",
  });
}

entryRead('./src/index.js');

@babel/traverse

Babel Traverse(遍历)模块维护了整棵树的状态,并且负责替换、移除和添加节点。

我们使用 @babel/traverse 递归(或者使用队列)拿到所有依赖文件信息数组。

const fs = require('fs');
const parser = require('@babel/parser');
const traverse = require('@babel/traverse').default;

const entryContent = fs.readFileSync(filename, 'utf-8');

let AST = parser.parse(entryContent, {
  sourceType: "module",
});

// 依赖项
const dependencies = [];

// visitor
traverse(AST, {
  ImportDeclaration: (path) => {
    const node = path.node;
    const value = node.source.value;

    dependencies.push(value);
  }
})

@babel/core 、@babel/preset-env

将 ES6 编译的的 AST 代码转回 ES5 语法代码。’

Run

$ node webpack.config.js

miniwebpack's People

Contributors

chokcoco avatar

Stargazers

kirara avatar Puff avatar  avatar starkwang avatar 大华 avatar shizhidi avatar 牧码人 avatar 丁少山 avatar 陈晓刚 avatar Haitao Lee avatar fatdoge avatar Keith avatar  avatar 胡伟杰 avatar

Watchers

James Cloos avatar  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.