Skip to content

Instantly share code, notes, and snippets.

@HenrikJoreteg
Created January 20, 2017 22:25
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HenrikJoreteg/38b2717c138b5997ae406b1e2d9c556d to your computer and use it in GitHub Desktop.
Save HenrikJoreteg/38b2717c138b5997ae406b1e2d9c556d to your computer and use it in GitHub Desktop.
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: '' }
}
}
}
}
@raghu-nanda
Copy link

though I was only one in the universe, who is thinking about this .. thanks .. 🤟

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment