whatplatformis
why this repo
Thanks to
nodejs
condition exports feature,isServer
will automatic befalse
in browser side, and betrue
in node side. It's friendly for bundler to maketree shaking
work. No more runtimetypeof window === 'undefined'
check.
install
pnpm i whatplatformis
usage
import { isBrowser, isServer } from 'whatplatformis'
// isServer will be false in browser
// isBrowser will be true in browser
if (isServer) {
// code...
}
Code under isServer
will be automatic tree shaking in target browser
bundled codes.
rollup
Build for target browser
// rollup.config.mjs
module.exports = {
input: ['<entries>'],
plugins: [
// other plugins...
resolve({
browser: true,
}),
]
}
Build for target node
// rollup.config.mjs
module.exports = {
input: ['<entries>'],
plugins: [
// other plugins...
resolve(),
]
}
Check example/rollup for more details.
webpack
When splitChunks
is enabled, in webpack.config.js
, should add plugin into plugin list to make tree shaking work.
import { WhatPlatformIsPlugin } from 'whatplatformis/webpack'
{
// ...other configs
plugins: [
new WhatPlatformIsPlugin()
]
}
Check example/webpack for more details.
development
-
Setup -
pnpm i
-
Build -
pnpm build
built with