0.2.1 • Published 10 months ago

whatplatformis v0.2.1

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

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 😼

0.2.1

10 months ago

0.2.0

10 months ago

0.1.0

1 year ago