old-rolldown
old-rolldown copied to clipboard
more API
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 }) => {})
So couple of things to note:
- default export is
rolldown- it accept single
optionsargument - it can accept
options.sourceandoptions.entry - it can define
options.destandoptions.targets(so write to disk)
- it accept single
.transformis thin layer on top ofrolldown- it accept string
sourcecode as first argument - must be non empty string or non empty buffer - second argument is
options- same as options passed torolldown - in that
optionsyou are not allowed to passoptions.destandoptions.targets - it never writes to disk, always resolves
{ code, map }object or reject if some error
- it accept string
.pluginis used to add tooptions.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