add content in the rollup packing process.
npm install rollup-plugin-auto-add --save-dev
// rollup.config.js
import rollupPluginAutoAdd from 'rollup-plugin-auto-add'
export default {
plugins: [
rollupPluginAutoAdd({
include: ['src/index.tsx'],
inject: [{ content: `import React from 'react';` }],
}),
]
}
Type: Inject[]
Default: []
content to be added. adding a field allows the content to be placed at the end
import rollupPluginAutoAdd, { Mode } from 'rollup-plugin-auto-add'
export default {
plugins: [
rollupPluginAutoAdd({
include: ['src/index.tsx'],
inject: [{ content: `export default Component;`, mode: Mode.end }],
}),
]
}
you can also skip adding content when some content already exists in the target file.
import rollupPluginAutoAdd from 'rollup-plugin-auto-add'
export default {
plugins: [
rollupPluginAutoAdd({
// match /root/my-project/src/components/*/index.tsx
include: [/src\/components\/(((?!\/).)+?)\/index\.tsx/gi],
inject: [{ content: `import React from 'react';`, skip: [/import.*React.*'react';/g] }],
}),
]
}
Type: string[]
declare the directory of included files.
Type: string[]
declare the file directories that are not included.
Type: boolean
if need to watch which files being matched, can be set to true
.