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 / externals.ts
Size: Mime:
/**
 * We don't want our node_modules to be bundled with any bundle that is
 * targetting the node environment, prefering them to be resolved via
 * native node module system. Therefore we use the `webpack-node-externals`
 * library to help us generate an externals configuration that will
 * ignore all the node_modules.
 */
import nodeExternals from 'webpack-node-externals'
import { Configuration, Options } from '../../typings'

/**
 * Some of our node_modules may contain files that depend on our
 * webpack loaders, e.g. CSS or SASS.
 * For these cases please make sure that the file extensions are
 * registered within the following configuration setting.
 */
const whitelist = [
  // We always want the source-map-support included in our node target bundles.
  'source-map-support/register',
]
const nodeConfig = {
  __dirname: true,
  __filename: true,
  fs: 'empty',
  bufferutil: 'empty',
  'utf-8-validate': 'empty',
}

const clientExternal = ['nock']

/**
 * node_modulesList are not included in any bundles that target "node" as a
 * runtime (e.g.. the server bundle) as including them often breaks builds
 * due to thinks like require statements containing expressions..
 * However. some of the modules contain files need to be processed by
 * one of our Webpack loaders (e.g. CSS). Add any file types to the list
 * below to allow them to be processed by Webpack.
 */
const nodeExternalsFileTypeWhitelist = [
  /\.(eot|woff|woff2|ttf|otf)$/,
  /\.(svg|png|jpg|jpeg|gif|ico)$/,
  /\.(mp4|mp3|ogg|swf|webp)$/,
  /\.(css|scss|sass|sss|less)$/,
]

export default function externalsMiddleware(
  config: Configuration,
  options: Options
) {
  const { isClient, isServer } = options

  // Ensure that webpack polyfills the following node features for use
  // within any bundles that are targetting node as a runtime. This will be
  // ignored otherwise.
  config.node = nodeConfig

  if (isClient) {
    config.externals = clientExternal
  } else if (isServer) {
    // And any items that have been whitelisted in the config need
    // to be included in the bundling process too.
    const allWhitelisted = [...whitelist, ...nodeExternalsFileTypeWhitelist]
    config.externals = nodeExternals({ whitelist: allWhitelisted })
  } else {
    config.externals = clientExternal
  }
}