Skip to content

Commit

Permalink
Merge pull request #11717 from Automattic/vkarpov15/fix-webpack-build
Browse files Browse the repository at this point in the history
fix: fix browser build for Webpack 5
  • Loading branch information
vkarpov15 authored Apr 28, 2022
2 parents cd6735b + 2ef4618 commit 94d96b8
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
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
26 changes: 19 additions & 7 deletions lib/helpers/isAsyncFunction.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
'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) {
return (
typeof v === 'function' &&
Object.getPrototypeOf(v) === asyncFunctionPrototype
);
};
if (asyncFunctionPrototype == null) {
module.exports = function isAsyncFunction() {
return false;
};
} else {
module.exports = function isAsyncFunction(v) {
return (
typeof v === 'function' &&
Object.getPrototypeOf(v) === asyncFunctionPrototype
);
};
}
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

0 comments on commit 94d96b8

Please sign in to comment.