whatplatformis
TypeScript icon, indicating that this package has built-in type declarations

0.2.1 • Public • Published

whatplatformis

npm GitHub

why this repo

Thanks to nodejs condition exports feature, isServer will automatic be false in browser side, and be true in node side. It's friendly for bundler to make tree shaking work. No more runtime typeof 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 ❤️ by 😼

Readme

Keywords

none

Package Sidebar

Install

npm i whatplatformis

Weekly Downloads

0

Version

0.2.1

License

MIT

Unpacked Size

10 kB

Total Files

17

Last publish

Collaborators

  • qidanta