Skip to content

Instantly share code, notes, and snippets.

@DrSensor
Created December 25, 2018 22:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DrSensor/b70a8a3fd51e039c1f164d600703a92e to your computer and use it in GitHub Desktop.
Save DrSensor/b70a8a3fd51e039c1f164d600703a92e to your computer and use it in GitHub Desktop.
Bundler tricks

Globbing rollup input

In my case, I use it to bundle CLI app that use oclif which also act (and can be used) as a library.

Setup
npm i -D globby
Config
import {sync as glob} from "globby"

const mapInput = inputs => {
	const result = {}
	for (const key in inputs) {
		if (!key.includes('*')) Object.assign(result, {[key]: inputs[key]})
		else { // support glob pattern
			const [prefix, suffix] = key.split('*')
			const input = glob(inputs[key]).reduce(
				(obj, item) => (obj[`${prefix}/${parse(item).name}${suffix}`] = item, obj), {}
			)
			Object.assign(result, input)
		}
	}
	return result
}

export default {
  input: mapInput({
    index: "src/index.ts",
    "commands/*": "src/commands/*.ts",
    "mocks/*.spec": "src/mocker/*.ts"
  }),
  output: {
    dir: dirname(pkg.main),
    chunkFileNames: "chunks/[name]-[hash].js",
    format: "cjs",
    exports: "named"
  },
  experimentalCodeSplitting: true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment