1.0.1 • Published 4 years ago
@chin98edwin/dev-utils v1.0.1
0. Notes
- This is meant for library authors only.
- It has only been tested/used with rollup only so far. You may need some workarounds if you intend to use this with other bundling tools.
- This package includes some basic functions for showing logs and errors in development environments.
- Please do not specify this as an external dependency as some other packages.
- This package should be included in your package's code bundle as it requires additional processing as you bundle your code.
- Although this will result in a slightly larger code bundle, but this package is really² light. Still, it is encouraged to use ES6 style import/export statements to take advantage of tree shaking to further reduce the final bundle size of your code.
1. Installation
# NPM
npm install -D @chin98edwin/dev-utils
# Yarn
yarn add -D @chin98edwin/dev-utils
# The -D flag adds this package as a devDependency
2. Usage
// For example:
import { devPrint, devPrintOnce, IS_DEBUG_ENV } from '@chin98edwin/dev-utils'
// Print an 'info' log
// Can be one of 'log', 'info', 'warn', or 'error'
devPrint('log', 'Hello')
// Similar to `devPrint`, but only runs once for each unique key provided
devPrintOnce('log', 'key', 'Hello')
// Custom implementation
if (IS_DEBUG_ENV) {
hereAreMy()
customFunctions()
thatOnlyRun()
InTheDevelopmentBuild()
}
// For information on other functions that are available, please refer to the TypeScript definitions at 'node_modules/@chin98edwin/dev-utils/index.d.ts'
3. Bundling
- Add
@rollup/plugin-replace
as a devDependency to your package. - Add the
replace
plugin to theplugins
section of your Rollup config. - Then add the
process.env.NODE_ENV
value.
import replace from '@rollup/plugin-replace'
// For development build
replace({
preventAssignment: true,
values: {
'process.env.NODE_ENV': JSON.stringify('development'),
},
})
// For production build
replace({
preventAssignment: true,
values: {
'process.env.NODE_ENV': JSON.stringify('production'),
},
})