0

I'm adding IE v11 support for web application. bitcoinjs-lib package contains arrow function causing IE v11 to break.

I have added babel-polyfill, react-app-polyfill/ie11, url-search-params-polyfill and react-app-polyfill/stable which makes up for most IE v11 support expect bitcoinjs-lib package.

Image in IE v11 browser

.bashrc

{ 
  "plugins": [
    ["@babel/plugin-transform-arrow-functions"],
    [
      "module-resolver", {
      "root": ["./"],
      "alias": {
        "~": "./"
      }
    }]
  ],
  "presets": [
    [
      "next/babel",
      {
        "preset-env": {
          "useBuiltIns": "usage"
        }
      }
    ]
  ]
}

source code inside package

'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const parser_1 = require('../parser');
function combine(psbts) {
  const self = psbts[0];
  const selfKeyVals = parser_1.psbtToKeyVals(self);
  const others = psbts.slice(1);
  if (others.length === 0) throw new Error('Combine: Nothing to combine');
  const selfTx = getTx(self);
  if (selfTx === undefined) {
    throw new Error('Combine: Self missing transaction');
  }
  const selfGlobalSet = getKeySet(selfKeyVals.globalKeyVals);
  const selfInputSets = selfKeyVals.inputKeyVals.map(input => getKeySet(input));
  const selfOutputSets = selfKeyVals.outputKeyVals.map(output =>
    getKeySet(output),
  );
  for (const other of others) {
    const otherTx = getTx(other);
    if (
      otherTx === undefined ||
      !otherTx.toBuffer().equals(selfTx.toBuffer())
    ) {
      throw new Error(
        'Combine: One of the Psbts does not have the same transaction.',
      );
    }
    const otherKeyVals = parser_1.psbtToKeyVals(other);
    const otherGlobalSet = getKeySet(otherKeyVals.globalKeyVals);
    otherGlobalSet.forEach(
      keyPusher(
        selfGlobalSet,
        selfKeyVals.globalKeyVals,
        otherKeyVals.globalKeyVals,
      ),
    );
    const otherInputSets = otherKeyVals.inputKeyVals.map(input =>
      getKeySet(input),
    );
    otherInputSets.forEach((inputSet, idx) =>
      inputSet.forEach(
        keyPusher(
          selfInputSets[idx],
          selfKeyVals.inputKeyVals[idx],
          otherKeyVals.inputKeyVals[idx],
        ),
      ),
    );
    const otherOutputSets = otherKeyVals.outputKeyVals.map(output =>
      getKeySet(output),
    );
    otherOutputSets.forEach((outputSet, idx) =>
      outputSet.forEach(
        keyPusher(
          selfOutputSets[idx],
          selfKeyVals.outputKeyVals[idx],
          otherKeyVals.outputKeyVals[idx],
        ),
      ),
    );
  }
  return parser_1.psbtFromKeyVals(selfTx, {
    globalMapKeyVals: selfKeyVals.globalKeyVals,
    inputKeyVals: selfKeyVals.inputKeyVals,
    outputKeyVals: selfKeyVals.outputKeyVals,
  });
}
exports.combine = combine;
function keyPusher(selfSet, selfKeyVals, otherKeyVals) {
  return key => {
    if (selfSet.has(key)) return;
    const newKv = otherKeyVals.filter(kv => kv.key.toString('hex') === key)[0];
    selfKeyVals.push(newKv);
    selfSet.add(key);
  };
}
function getTx(psbt) {
  return psbt.globalMap.unsignedTx;
}
function getKeySet(keyVals) {
  const set = new Set();
  keyVals.forEach(keyVal => {
    const hex = keyVal.key.toString('hex');
    if (set.has(hex))
      throw new Error('Combine: KeyValue Map keys should be unique');
    set.add(hex);
  });
  return set;
}

.next.config.js

const fs = require('fs')
const dotenv = require('dotenv')
const webpack = require('webpack')
const withCSS = require('@zeit/next-css')
const withFonts = require('next-fonts')
const withImages = require('next-images')
const withPlugins = require("next-compose-plugins")
const withTM = require('next-transpile-modules');

const getEnvFile = () => {
  const fileName =  process.env.NODE_ENV == 'development' ? '.env' : `.env.${process.env.NODE_ENV}`
  return fs.readFileSync(fileName)
}

const envConfig = dotenv.parse(getEnvFile())

const nextConfig = {
  webpack(config) {
    config.plugins.push(new webpack.EnvironmentPlugin(envConfig))
    const originalEntry = config.entry;
    config.entry = async () => {
      const entries = await originalEntry();
      if (entries['main.js'] && !entries['main.js'].includes('./polyfills.js')) {
          entries['main.js'].unshift('./polyfills.js');
      }

      return entries;
    };
    config.module.rules.push({
      test: /\.js$/,
      include: [/node_modules\/bitcoinjs-lib/],
      use: {
        loader: "next/babel",
        options: {
          presets: ["@babel/presets-env", {modules: false}],
          plugins: ["@babel/plugin-transform-arrow-functions"],
        },
      },
    })
    return config
  },
  exportPathMap: async function(defaultPathMap) {
    return {
      '/'                      : { page: '/people' },
      '/dashboard'             : { page: '/dashboard' },
      '/tenant-investigator'   : { page: '/tenant_investigator' },
      '/pricing-history'       : { page: '/pricingHistory' },
      '/algorithm'             : { page: '/algorithm' },
      '/group'                 : { page: '/group' },
      '/group-detail'          : { page: '/group_detail' },
      '/investigation'         : { page: '/investigation' },
      '/login'                 : { page: '/login' },
      '/map'                   : { page: '/map' },
      '/people'                : { page: '/people' },
      '/people-report'         : { page: '/people_report' },
      '/report'                : { page: '/report' },
      '/people-detail'         : { page: '/people_detail' },
      '/pricing-history'       : { page: '/pricingHistory' },
      '/settings'              : { page: '/settings' },
      '/tenant'                : { page: '/tenant' },
      '/wallets'               : { page: '/wallets' },
      '/transactions'          : { page: '/transactions' },
      '/taxable-event'         : { page: '/taxable_event' },
      '/subpoena-upload'       : { page: '/subpoenaUpload' },
      '/people-subpoena-upload': { page: '/peopleSubpoenaUpload' },
      '/subpoena-transactions' : { page: '/subpoenaTransactions' },
      '/event-detail'          : { page: '/peopleEvent' },
      '/grand-unified'         : { page: '/grandUnified' },
            '/blocks'                : { page: '/blocks' },
      '/person-edit'           : { page: '/personEdit' },
      '/addresses'             : { page: '/addresses' },
      '/exchanges'             : { page: '/exchanges' }
    }
  }
}
console.log(nextConfig)

// module.exports = withPlugins([withCSS, withFonts, withImages], nextConfig)
module.exports = withPlugins(
  [
    [
      withTM,
      {
        transpileModules: ['bitcoinjs-lib']
      }
    ],
    withCSS,
    withFonts,
    withImages
  ],
  nextConfig
);

As we see use of arrow function causing error

I want node_module package to be converted to native javascript to IE v11 can read easily. When I look in to node_module source, arrow function and ES6 can be seen in package shipment.

Roshan Shrestha
  • 421
  • 1
  • 3
  • 13
  • As you can see the => arrow functions in your code means that your code is not transpiled to ES5 code. It can be possible that your babel polyfil not applied correctly. You can again try to check it and verify that it is loading properly and transpiling the code for IE browser. Ref: https://babeljs.io/docs/en/babel-polyfill – Deepak-MSFT Oct 24 '19 at 09:31
  • babel-polyfill is is added properly from next.config.js. Code generating error is from third party npm package which resides on node_modules. `Object.defineProperty(exports, '__esModule', { value: true });` My expectation is that package installed should already be transpile in native javascript. – Roshan Shrestha Oct 24 '19 at 11:44
  • Try to check Next.js + Transpile node_modules. It transpile untranspiled modules from node_modules using the Next.js Babel configuration. Makes it easy to have local libraries and keep a slick, manageable dev experience. Ref: https://github.com/martpie/next-transpile-modules – Deepak-MSFT Oct 24 '19 at 12:19
  • I have added my next.config.js. When using transpile for 'bitcoinjs-lib' package. Reference error for export is raise. As js file is semi transpiled from my guess – Roshan Shrestha Oct 24 '19 at 12:27
  • You can refer this suggestion for adding babel polyfil in next.js. https://github.com/zeit/next.js/issues/2060#issuecomment-385199026 If issue persist then I suggest you to create issue on their github page for next.js – Deepak-MSFT Oct 24 '19 at 13:20
  • Thank you for suggestion. I think problem exists in transpile for third party package not in babel-polyfill. I have raised issue in [bitcoinjs-lib](https://github.com/bitcoinjs/bitcoinjs-lib). – Roshan Shrestha Oct 24 '19 at 16:25
  • Thanks for updating the status for the issue. – Deepak-MSFT Oct 28 '19 at 07:55

0 Answers0