Created
January 20, 2017 22:25
-
-
Save HenrikJoreteg/38b2717c138b5997ae406b1e2d9c556d to your computer and use it in GitHub Desktop.
Simple rollup plugin to automatically add an import line
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
though I was only one in the universe, who is thinking about this .. thanks .. 🤟