Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple rollup plugin to automatically add an import line
import { createFilter } from 'rollup-pluginutils'
function assign (target, source) {
Object.keys(source).forEach((key) => {
target[key] = source[key]
})
return target
}
const DEFAULT_HEADER = 'import React from \'react\';'
export default (opts) => {
opts = assign({}, opts || {})
if (!opts.include) {
throw Error('include option should be specified')
}
let filter = createFilter(opts.include, opts.exclude)
let header = opts.header !== undefined ? opts.header : DEFAULT_HEADER
return {
name: 'add-import',
transform (code, id) {
if (!filter(id)) return
return {
code: header + '\n' + code,
map: { mappings: '' }
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.