DanielSchaffer / webpack-babel-multi-target-plugin

A Webpack plugin that works with Babel to allow differential loading - production deployment of ES2015 builds targeted to modern browsers, with an ES5 fallback for legacy browsers.
153 stars 14 forks source link

Unable to import 'crypto' when plugin is used #48

Closed Artur- closed 4 years ago

Artur- commented 4 years ago

Trying to use

const c = require("crypto");

when the plugin is included in a webpack build results in

ERROR in .?babel-target=modern
Module not found: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type boolean
 @ .?babel-target=modern 1:10-27

ERROR in .?babel-target=legacy
Module not found: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type boolean
 @ .?babel-target=legacy 1:10-27

To reproduce:

npm init -y
npm i --save-dev webpack webpack-cli webpack-babel-multi-target-plugin
echo 'const c = require("crypto");console.log(c);' > index.js
cat > webpack.config.js << EOF
const { BabelMultiTargetPlugin } = require("webpack-babel-multi-target-plugin");

module.exports = {
  mode: "production",
  entry: {
    bundle: "./index.js"
  },

  plugins: [
    new BabelMultiTargetPlugin({})
  ]
};
EOF
node_modules/.bin/webpack

If you remove the BabelMultiTargetPlugin, webpack finishes successfully

Artur- commented 4 years ago

My problem originates in https://github.com/dmonad/lib0/blob/master/random.js#L36 which uses the crypto module when running on Node and something else in the browser. With this plugin however, lib0 cannot be used.

The real code that fails is something like

import { uint32 } from 'lib0/random.js';

console.log(uint32)
DanielSchaffer commented 4 years ago

It looks like this may have been the same or similar root cause as this issue in #51, as I can't reproduce your error in 2.3.3. Let me know if you're still seeing errors after upgrading.

DanielSchaffer commented 4 years ago

Closing this due to inactivity.

Artur- commented 4 years ago

Thanks, fails with 2.3.1 and works with 2.3.3