Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@skava/bs / src / scripts / .plugins / env.ts
Size: Mime:
import { EnvironmentPlugin, DefinePlugin, ProgressPlugin } from 'webpack'
// import { stringify } from 'chain-able-boost'
import { config as di } from '@skava/di'
import { Configuration, Options } from '../../typings'

const THEME = process.env.THEME || 'wr' || 'classic'

// @NOTE when this is Boolean(x) it messes up assets, so the env check for it is wrongs
const toBooleanJSON = x => JSON.stringify(Boolean(x))

/**
 * @todo bake in all .env values here... or at least in oneConfig
 *
 * @todo use DefinePlugin
 *
 * @tutorial https://webpack.js.org/plugins/environment-plugin/#usage-with-default-values
 * @tutorial https://webpack.js.org/plugins/define-plugin/
 */
export default function envPlugin(config: Configuration, options: Options) {
  const { isClient, isServer, isDev, isProd } = options

  const SERVER_URL = process.env.SERVER_URL || ''
  const API_URL = process.env.API_URL || ''

  const ENV_CONFIG = {
    // @NOTE should be in DefinePlugin
    // It is really important to use NODE_ENV=production in order to use
    // optimised versions of some node_modules, such as React.
    // NODE_ENV: isProd ? 'production' : 'development',

    // Is this the "client" bundle?
    BUILD_FLAG_IS_CLIENT: toBooleanJSON(isClient),
    // Is this the "server" bundle?
    BUILD_FLAG_IS_SERVER: toBooleanJSON(isServer),

    // this one matters
    SERVER_URL,

    // if we have one, route graphql to that - otherwise run it ourselves
    API_URL: JSON.stringify(API_URL),
  }

  const DEFINE_CONFIG = {
    // __IS_BROWSER__:
    // __IS_SERVER__:
    // __VERSION__:
    // '__THEME_IMPORT__': `require("./${THEME}")`,
    __THEME_IMPORT__: `"./${THEME}"`,
    __DEV__: isDev,
    __PROD__: isProd,
    __PRODUCTION__: isProd,
    __THEME__: JSON.stringify(THEME),

    DEVTOOLS: false,
    INJECT_CLIENT: isDev,
  }

  const dotenv = di.get('dotenv')

  /**
   * @example { API_URL: 'eh' } => { 'process.env.API_URL': 'eh' }
   * @todo this has some acorn issue which means something is doing some fancy check with one of these :(
   */
  const asDefinePathConfig = {}
  Object.keys(dotenv).forEach(key => {
    // asDefinePathConfig[`process.env.${key}`] = dotenv[key]
    if (dotenv[key] === false) {
      console.warn(
        'ENV_TODO_FIXME - value was false, process.env stringy bools',
        key
      )
      return
    }
    asDefinePathConfig[`process.env.${key}`] = JSON.stringify(dotenv[key])
  })
  Object.assign(DEFINE_CONFIG, asDefinePathConfig)

  // ===
  // this one is safer, but will have stringy bools :(
  // Object.assign(ENV_CONFIG, global.ENV)

  // @todo @env
  // this will replace it everywhere, it better be correct
  // Object.keys(ENV_ENTRY).forEach(key => {
  //   DEFINE_CONFIG[key] = ENV_ENTRY[key]
  // })

  // const stringifyValue = key => DEFINE_CONFIG[key] = JSON.stringify(DEFINE_CONFIG[key])
  // Object.keys(DEFINE_CONFIG).forEach(stringifyValue)

  // @TODO use filter
  if (process.env.LOG_ENV_CONFIG) {
    require('fliplog').when(!process.env.CI, log => {
      log
        .yellow('ENV_CONFIG')
        .data({ ...ENV_CONFIG, NODE_ENV: process.env.NODE_ENV })
        .echo()

      log
        .yellow('DEFINE_CONFIG')
        .data(DEFINE_CONFIG)
        .echo()
    })
  }

  const webpackEnvPlugin = new EnvironmentPlugin(ENV_CONFIG)
  config.plugins.push(webpackEnvPlugin)
  const definePlugin = new DefinePlugin(DEFINE_CONFIG)
  config.plugins.push(definePlugin)
}