-
Notifications
You must be signed in to change notification settings - Fork 15.2k
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
Search for module from app path when URL is not file protocol #9095
Search for module from app path when URL is not file protocol #9095
Conversation
lib/renderer/init.js
Outdated
if (window.location.protocol !== 'chrome-devtools:') { | ||
// Search for module under the app directory | ||
// (remote.app doesn't work in devtools) | ||
module.paths = module.paths.concat(Module._nodeModulePaths(electron.remote.app.getAppPath())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remote.app.getAppPath()
is a synchronous IPC call which would block the renderer process, could this be done instead with a command line flag like opener id or guest instance id is done?
spec/modules-spec.js
Outdated
w.loadURL('about:blank') | ||
const result = await w.webContents.executeJavaScript('typeof require("q").when') | ||
assert.equal(result, 'function') | ||
w.destroy() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be done in an afterEach
block in case the assert fails, so the window does not leak across tests.
Updated, thank you 👍 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left some very minor comments, looks good overall though 👍
spec/modules-spec.js
Outdated
}) | ||
|
||
afterEach(() => { | ||
w.destroy() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should use closeWindow
helper instead, see
electron/spec/api-browser-window-spec.js
Lines 84 to 86 in efa2850
afterEach(function () { | |
return closeWindow(w).then(function () { w = null }) | |
}) |
spec/modules-spec.js
Outdated
assert.equal(result, 'function') | ||
}) | ||
|
||
afterEach(() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
afterEach
blocks usually go after the beforeEach
but before the it
blocks, mind moving it there?
atom/browser/api/atom_api_app.h
Outdated
@@ -70,6 +70,8 @@ class App : public AtomBrowserClient::Delegate, | |||
std::unique_ptr<CertificateManagerModel> model); | |||
#endif | |||
|
|||
std::string GetAppPath(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if this might be better to store as a base::FilePath
. That is what App::GetPath
returns and it looks like on Windows base::FilePath
will be converted to a std::wstring
and it will be a std::string
on other platforms.
atom/browser/api/atom_api_app.h
Outdated
@@ -70,6 +70,8 @@ class App : public AtomBrowserClient::Delegate, | |||
std::unique_ptr<CertificateManagerModel> model); | |||
#endif | |||
|
|||
base::FilePath GetAppPath(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could probably be const base::FilePath& GetAppPath()
.
Thanks for this @seanchas116 👍 🎆 |
Thanks too 👍 🎉 |
@seanchas116 I'm just trying to understand how to use this in my app. I load an HTML file from a local HTTP server with Electron, and I would like to require my modules relative to the index.html file that I load. Will this help me achieve that? So far with my experimenting it doesn't seem to help, the app root is set to some strange location in node_modules/electron/bla/bla, which doesn't help get to any of the node_modules that my application would actually use. Any insight would be appreciated |
The app path (which you can get by |
Thanks! I still have a couple questions:
What do you mean by the package.json I provide on Electron launch? Here is my
When I log
I think I would like it to be:
Am I thinking about it incorrectly? |
Probably |
|
This PR makes
require()
to search for modules from app directory when the URL is not file protocol (such as http, data orabout:blank
).I think this would be more intuitive solution for #8425 and #8963 (comment).