NPM软件包[bitcoinjs-lib]与IE v11不兼容

时间:2019-10-24 08:20:30

标签: internet-explorer-11 babel next.js transpiler babel-polyfill

我正在为Web应用程序添加IE v11支持。 bitcoinjs-lib程序包包含导致IE v11中断的箭头功能。

我添加了babel-polyfill,react-app-polyfill / ie11,url-search-params-polyfill和react-app-polyfill / stable,它们弥补了大多数IE v11支持所期望的bitcoinjs-lib程序包。

Image in IE v11 browser

.bashrc

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

包中的源代码

'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
);

我们看到使用箭头功能会导致错误

我希望将node_module包转换为本机javascript才能轻松读取IE v11。当我查看node_module的源代码时,可以在包装发货中看到箭头功能和ES6。

0 个答案:

没有答案