GithubHelp home page GithubHelp logo

wbpck-bundler's Introduction

wbpck

A toy module bundler, kind of like webpack but missing loads of features โค๏ธ

A module bundler is a tool that takes all of your code and all of your codes dependencies and all of their dependencies (and so on) and puts it into a single, standalone file, usually so it can be run in the browser.

In this repo, I will (attempt to) build a little toy module bundler for learning purposes ๐Ÿ“

The Code

You should look at this post about the code. There's only about 60 lines of code, mixed in with some explanations of how it all fits together.

Usage

If you want to test out how it works, the code exports a function that you pass in the full path to your entry point, and it will return the bundle.

const bundle = require('./index')
const entry = __dirname + '/entry.js'

console.log(bundle(entry))

Thanks

Big thanks to Luciano Mammino for his talk Unbundling the JavaScript module bundler at the July DublinJS meetup - that gave me everything I needed to know to write this. Also giving credit to minipack, which is very similar to this project, mostly as I looked at the source to see how it solved some problems (like name collisions)

License

MIT

wbpck-bundler's People

Contributors

adamisntdead avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

wbpck-bundler's Issues

Avoid module duplication

If we require the same module in multiple places the bundler adds the same dependency multiple times into the bundle. Eg.

// main.js
const a = require('./a');
const b = require('./b');

// a.js
module.exports = () => {};

// b.js
const a = require('./a');

module.exports => () => { a(); };

The bundler will generate following dependency graph

main module <- a module <- b module <- a module

We can avoid this duplication by checking module existence in the graph. Inside the getModules function

module.requires.forEach(dependency => {
  const basedir = path.dirname(module.filepath)
  const dependencyPath = resolve(dependency, { basedir })

  // to avoid module duplication
  let dependencyObject = modules.find(m => m.filepath === dependencyPath);

  if (dependencyObject) {
    module.map[dependency] = dependencyObject.id
   } else {
    dependencyObject = createModuleObject(dependencyPath)
    module.map[dependency] = dependencyObject.id
    modules.push(dependencyObject)
  }
})

@adamisntdead I would like create a PR for the above changes, If you like this solution. Thanks.

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.