Repository navigation
Description
Do you want to request a feature or report a bug?
bug
What is the current behavior?
typeof __non_webpack_require__ is not resolved, resulting in webpack considering this a runtime check and breaking its usage as a guard to choose which form of the require func to use.
If the current behavior is a bug, please provide the steps to reproduce.
The code:
var requireFunc = typeof __non_webpack_require__ === "function" ? __non_webpack_require__ : require;
var targetExports = requireFunc(name);
resolves to:
var requireFunc = typeof __non_webpack_require__ === "function" ? __non_webpack_require__ : require;
var targetExports = !(function webpackMissingModule() { var e = new Error("Cannot find module \".\""); e.code = 'MODULE_NOT_FOUND'; throw e; }());
with webpack config
const LibraryScopeRegex = /^\@bl-libraries\/(.+)$/;
module.exports = {
target: "node",
entry: path.resolve(pkg.main),
output: {
path: dest(),
filename: `./${libraryName}.js`,
library: libraryName,
libraryTarget: "commonjs2"
},
resolve: {
extensions: [".js", ".json"]
},
externals: [
function(context, request, callback) {
// Anything with this scope will be external and scopeless in the runtime env.
const match = LibraryScopeRegex.exec(request);
if (match) {
return callback(null, 'commonjs2 ' + match[1]);
}
// Everything else should be bundled
callback();
}
],
plugins: [
new webpack.NamedModulesPlugin()
]
}
I am attempting to make a library that will both function pre- and post- webpack with a nodejs target on both sides.
This module needs to forward a require statement to node, so it should get the top-level nodejs module require() func both from the original source and from the webpack output.
However, webpack seems not to be aware of the type of __non_webpack_require__, and tries to resolve the usage of requireFunc as a webpack-level require().
What is the expected behavior?
I expected webpack to identify __non_webpack_require__ as one of its own, resolve it in setting requireFunc, and to leave requireFunc alone after that.
I am able to get the desired results by changing REPLACEMENT_TYPES at lib/APIPlugin.js:21
const REPLACEMENT_TYPES = {
__webpack_public_path__: "string", // eslint-disable-line camelcase
__webpack_require__: "function", // eslint-disable-line camelcase
__webpack_modules__: "object", // eslint-disable-line camelcase
__webpack_chunk_load__: "function", // eslint-disable-line camelcase
__webpack_nonce__: "string" // eslint-disable-line camelcase
// FIX: Teach webpack about the type of __non_webpack_require__
__non_webpack_require__ : "function"
};
With this fix in place, the output code becomes
var requireFunc = true ? require : require;
var targetExports = requireFunc(name);
and functions the way I expect it to.
As a workaround, I am checking the existence of webpack using typeof __webpack_require__, which results in the same code as the proposed fix.
Am I on-target here? Is this the appropriate way to handle my use case?
If this is a feature request, what is motivation or use case for changing the behavior?
Please mention other relevant information such as the browser version, Node.js version, webpack version and Operating System.
Node 8.9.0
Webpack 3.8.1
Windows 10
Metadata
Metadata
Assignees
Labels
Type
Fields
Priority
Activity
[-]typeof __non_webpack_require__ is not resolved[/-][+]typeof __non_webpack_require__ is not known during bundle[/+]webpack-bot commentedon May 23, 2018
This issue had no activity for at least half a year.
It's subject to automatic issue closing if there is no activity in the next 15 days.
webpack-bot commentedon Jun 7, 2018
Issue was closed because of inactivity.
If you think this is still a valid issue, please file a new issue with additional information.
AshaParveenShaik commentedon May 11, 2022
operations-api\node_modules\bindings\bindings.js:135
throw err;
^
Error: Could not locate the bindings file. Tried:
Getting this error someone please help