nodePolyfill.js 630 Bytes
Newer Older
邓晓峰's avatar
邓晓峰 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
const { ProvidePlugin } = require('../compiled/webpack');

module.exports = async function addNodePolyfill(opts) {
  const { config } = opts;

  config.plugin('node-polyfill-provider').use(ProvidePlugin, [
    {
      Buffer: ['buffer', 'Buffer'],
    },
  ]);

  // eslint-disable-next-line global-require
  const nodeLibs = require('node-libs-browser');
  config.resolve.fallback.merge({
    ...Object.keys(nodeLibs).reduce((memo, key) => {
      if (nodeLibs[key]) {
        memo[key] = nodeLibs[key];
      } else {
        memo[key] = false;
      }
      return memo;
    }, {}),
    http: false,
    https: false,
  });
};