unbundle
Version:
`require()` and `import`/`export` in the browser, without the bundling
36 lines (31 loc) • 1.05 kB
JavaScript
import debug from 'debug'
import streamToPromise from 'stream-to-promise'
import {resolve, relative} from 'path'
import mdeps from 'module-deps'
import {findOutputPath} from './findOutputPath'
import {processFile} from './processFile'
export default async (entry, destination) => {
const log = debug('unbundle')
entry = resolve(process.cwd(), entry)
destination = resolve(process.cwd(), destination)
log(`Entry: ${entry}`)
log(`Destination: ${destination}`)
const processors = []
const dependencies = mdeps({
ignoreMissing: true,
transform: [
['babelify', { /* babelrc options */ }]
]
})
dependencies.on('file', (file) => {
processors.push((async () => {
const output = findOutputPath(file, entry, destination)
log(relative(process.cwd(), output))
await processFile(file, output, destination)
})())
})
dependencies.on('missing', (id, parent) => log(`Missing: ${id}`))
dependencies.end({file: entry})
await streamToPromise(dependencies)
return await Promise.all(processors)
}