tunnckoCore / ideas

:notebook: My centralized place for ideas, thoughts and todos. Raw, PoC implementations and so on... :star:
http://j.mp/1stW47C
6 stars 0 forks source link

Tree-shaking bundler for modern javascript development #63

Open tunnckoCore opened 7 years ago

tunnckoCore commented 7 years ago

Smart Plugins API

'use strict'

/**
 * Tree-shaking bundler for modern
 * javascript development.
 */

import Frog from 'frog'

const app = new Frog({})

/**
 * Smart plugins concept.
 * Only the first one is called immeidately.
 * The other nested are lazy called if it needs.
 *
 * Before calling the plugins stack we will already
 * collected all the things for the given string
 * from the Babylon/Babel AST
 */

app.use(function (frog) {
  // this === frog === app
  // called once immediately, useful to extend the tool.

  // If it returns a function, it will be called
  // in later point of time with different context and argument
  return function ({ filepath }) {
    const imp = this
    // this !== frog !== app
    // this === imp
    // this.filepath === imp.filepath === filepath
    // this is equal to that object passed as first argument
    //
    // That context has `filepath` property, which will be
    // defined path in import string.
    //
    // Called once on each import.

    // If it returns a function, it will be called with object
    // context containing the `code/source` of that filepath.
    return function ({ source }) {
      const file = this // why not Vinyl file?
      // this !== frog !== app !== imp
      // this === file
      // this.source === file.source === source
    }
  }
})

app.parse('// some es5 or es6 code, but with ES6 Modules!')
tunnckoCore commented 7 years ago

Some more notes:

  1. Generate hash for each "module" from its full absolute path + its contents
  2. If >= node v4, use async/await (transpiled to generators), through fs.readFile
  3. If < node v4, use sync (fs.readFileSync)

some playing with rollup