Skip to content

Commit 1202623

Browse files
committed
fix: Better support for webpack
1 parent 59352ec commit 1202623

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ you want. `app-root-path` is aware of this edge-case and will strip the `/bin` a
9090

9191
## Change Log
9292

93+
### 2.2.1
94+
- Better handling of webpack
95+
9396
### 2.2.0
9497
- Added support for Yarn Plug'n'Play
9598
- Adjusted browser-shim to address webpack warnings

lib/resolve.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ var npmGlobalModuleDir = path.resolve(npmGlobalPrefix, 'lib', 'node_modules');
1818
// Save OS-specific path separator
1919
var sep = path.sep;
2020

21+
// If we're in webpack, force it to use the original require() method
22+
var requireFunction = ("function" === typeof __webpack_require__ || "function" === typeof __non_webpack_require__)
23+
? __non_webpack_require__
24+
: require;
25+
2126
// Resolver
2227
module.exports = function resolve(dirname) {
2328
// Check for environmental variable
@@ -27,15 +32,18 @@ module.exports = function resolve(dirname) {
2732

2833
// Defer to Yarn Plug'n'Play if enabled
2934
if (process.versions.pnp) {
30-
var pnp = require('pnpapi');
31-
return pnp.getPackageInformation(pnp.topLevel).packageLocation;
35+
try {
36+
var pnp = requireFunction('pnpapi');
37+
return pnp.getPackageInformation(pnp.topLevel).packageLocation;
38+
} catch (e) {}
3239
}
3340

3441
// Defer to main process in electron renderer
3542
if ('undefined' !== typeof window && window.process && 'renderer' === window.process.type) {
36-
var electron = 'electron';
37-
var remote = require(electron).remote;
38-
return remote.require('app-root-path').path;
43+
try {
44+
var remote = requireFunction('electron').remote;
45+
return remote.require('app-root-path').path;
46+
} catch (e) {}
3947
}
4048

4149
// Defer to AWS Lambda when executing there

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "app-root-path",
3-
"version": "2.2.0",
3+
"version": "2.2.1",
44
"description": "Determine an app's root path from anywhere inside the app",
55
"main": "index.js",
66
"browser": "browser-shim.js",

0 commit comments

Comments
 (0)