Ensure certain files don't become part of the build.
Sometimes you want to have separate builds for dev and production. You might use the replace
plugin to conditionally import certain files. This plugin lets you make sure that a file that is meant for one build isn't accidentally compiled into another.
In this example if the __DEV_MODE__
check was missing, the non-dev build would fail because it would contain "debug-logger.devonly.js". With the __DEV_MODE__
check "debug-logger.devonly.js" would be omitted from the build by rollup given it's unused.
import { debugLogger } from './debug-logger.devonly.js';
if (__DEV_MODE__) {
debugLogger.log('In dev mode.');
}
export const debugLogger = {
log: msg => console.log(msg)
};
import rollupPluginBlock from 'rollup-plugin-block';
import replace from 'rollup-plugin-replace';
export default {
input: 'main.js',
plugins: [
replace({
__DEV_MODE__: JSON.stringify(!!process.env.DEV_BUILD)
}),
rollupPluginBlock({
blockPattern: '.devonly.'
})
]
};
npm install --save-dev rollup-plugin-block
import { rollup } from 'rollup';
import rollupPluginBlock from 'rollup-plugin-block';
export default {
input: 'main.js',
plugins: [
rollupPluginBlock({
blockPattern: '.devonly.' // or regex
})
]
});