Skip to content

putoutjs/bundler

Repository files navigation

Bundler License NPM version Build Status Coverage Status

Install

npm i @putout/bundle

API

import {bundle} from '@putout/bundler';

console.log(bundle(CWD, entry, filesystem));

Internals

Convert ESM to CommonJS

To Simplify things up all files converted to CommonJS first. Let's suppose none of them use top-level await to get things simpler.

Parse filenames

Traverse all files starting from entry and get all filenames.

Bundle all files to object

Traverse filesystem and create object that contains filename and file content:

const __filesystem = {
    '/entry.js': () => {
        const client = require('/client.js');
        console.log(client);
    },
    '/client.js': (exports, require, module) => {
        module.exports = 'hello';
    },
};

IIFE

Most likely we need IIFE so couple bundles can be loaded on page simultaneously.

Result Example

const __modules = {};
const __filesystem = {
    '/entry.js': () => {
        const client = require('/client.js');
        console.log(client);
    },
    '/client.js': (exports, require, module) => {
        module.exports = 'hello';
    },
};

const require = (name) => {
    const exports = {};
    const module = {
        exports,
    };
    
    if (__modules[name])
        return __modules[name];
    
    __filesystem[name](exports, require, module);
    __modules[name] = module.exports;
    
    return module.exports;
};

require('/entry.js');

License

MIT