old-rolldown icon indicating copy to clipboard operation
old-rolldown copied to clipboard

more API

Open tunnckoCore opened this issue 8 years ago • 1 comments

rolldown.transform = (source, options) => {
  if (!source) {
    // or something like that
    // (need more strict check for that param)
    return Promise.reject(new TypeError('expect a string'))
  }

  return utils.tmpFile(source).then((file) => {
    options.entry = file.path
    delete options['source']

    // we don't want to (force to not) write to disk
    // but resolve `{ code, map }` object
    delete options['targets']
    delete options['dest']

    return rolldown(options)
  })
}

rolldown.plugin = (plugin, options) => {
  rolldown.plugins.push([plugin, options])
  return rolldown
}

// usage

// { name: 'my-awesome-plugin', transform: (code, id) => {} }
rolldown.plugin(Object)

 // e.g. buble
rolldown.plugin(Function)

// e.g. buble, { target: { node: '4' } }
rolldown.plugin(Function, Object)

// e.g. 'buble'
rolldown.plugin(String)

// e.g. 'buble', { target: { node: '4' } }
rolldown.plugin(String, Object)

rolldown.transform('some source code', {
  plugins: [
    'commonjs',
    ['node-resolve', { jsnext: true }],
    ['buble', { target: { node: '4' } }]
  ]
}).then(({ code, map }) => {})

tunnckoCore avatar Jan 03 '17 14:01 tunnckoCore

So couple of things to note:

  • default export is rolldown
    • it accept single options argument
    • it can accept options.source and options.entry
    • it can define options.dest and options.targets (so write to disk)
  • .transform is thin layer on top of rolldown
    • it accept string source code as first argument - must be non empty string or non empty buffer
    • second argument is options - same as options passed to rolldown
    • in that options you are not allowed to pass options.dest and options.targets
    • it never writes to disk, always resolves { code, map } object or reject if some error
  • .plugin is used to add to options.plugins
    • it can accept one or two arguments
    • first argument can be: object, function or string
    • second argument can be: object (options)
    • if first argument is function: passes second argument to it
    • if first argument is string: it is resolved synchronously and second is passed to it
      • assumes that resolved module/package exports a function

tunnckoCore avatar Jan 03 '17 14:01 tunnckoCore