Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix browser build for Webpack 5 #11717

Merged
merged 4 commits into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/helpers/immediate.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

'use strict';

const nextTick = process.nextTick.bind(process);
const nextTick = typeof process !== 'undefined' && typeof process.nextTick === 'function' ?
process.nextTick.bind(process) :
cb => setTimeout(cb, 0); // Fallback for browser build

module.exports = function immediate(cb) {
return nextTick(cb);
Expand Down
9 changes: 8 additions & 1 deletion lib/helpers/isAsyncFunction.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
'use strict';

const asyncFunctionPrototype = Object.getPrototypeOf(async function() {});
let asyncFunctionPrototype = null;
// try/catch for Babel compatibility, because Babel preset-env requires
// regenerator-runtime for async/await and we don't want to include that
// for a simple check.
try {
asyncFunctionPrototype = Object.getPrototypeOf(async function() {});
} catch (err) {}

module.exports = function isAsyncFunction(v) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could avoid a potential performance penalty by checking the value of asyncFunctionPrototype only once.

module.exports =  asyncFunctionPrototype !== null
? function isAsyncFunction(v) {
  return (
    typeof v === 'function' &&
    Object.getPrototypeOf(v) === asyncFunctionPrototype
  );
}
: function () { return false; };

return (
asyncFunctionPrototype != null &&
typeof v === 'function' &&
Object.getPrototypeOf(v) === asyncFunctionPrototype
);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"babel-loader": "8.2.4",
"benchmark": "2.1.4",
"bluebird": "3.7.2",
"buffer": "6.0.3",
"cheerio": "1.0.0-rc.10",
"crypto-browserify": "3.12.0",
"dox": "0.3.1",
Expand Down
12 changes: 11 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const webpack = require('webpack');
const paths = require('path');

const webpackConfig = {
Expand Down Expand Up @@ -35,12 +36,21 @@ const webpackConfig = {
resolve: {
fallback: {
assert: require.resolve('assert-browserify'),
buffer: require.resolve('buffer/'),
crypto: require.resolve('crypto-browserify'),
stream: require.resolve('stream-browserify')
}
},
target: 'web',
mode: 'production'
mode: 'production',
plugins: [
new webpack.DefinePlugin({
process: '({env:{}})'
}),
new webpack.ProvidePlugin({
Buffer: ['buffer', 'Buffer'],
})
]
};

module.exports = webpackConfig;
Expand Down