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

Search for module from app path when URL is not file protocol #9095

Merged
merged 10 commits into from
Apr 20, 2017

Conversation

seanchas116
Copy link
Contributor

This PR makes require()to search for modules from app directory when the URL is not file protocol (such as http, data or about:blank).

I think this would be more intuitive solution for #8425 and #8963 (comment).

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()))
Copy link
Contributor

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?

w.loadURL('about:blank')
const result = await w.webContents.executeJavaScript('typeof require("q").when')
assert.equal(result, 'function')
w.destroy()
Copy link
Contributor

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.

@seanchas116
Copy link
Contributor Author

Updated, thank you 👍

Copy link
Contributor

@kevinsawicki kevinsawicki left a 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 👍

})

afterEach(() => {
w.destroy()
Copy link
Contributor

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

afterEach(function () {
return closeWindow(w).then(function () { w = null })
})
for an example

assert.equal(result, 'function')
})

afterEach(() => {
Copy link
Contributor

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?

@@ -70,6 +70,8 @@ class App : public AtomBrowserClient::Delegate,
std::unique_ptr<CertificateManagerModel> model);
#endif

std::string GetAppPath();
Copy link
Contributor

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.

@@ -70,6 +70,8 @@ class App : public AtomBrowserClient::Delegate,
std::unique_ptr<CertificateManagerModel> model);
#endif

base::FilePath GetAppPath();
Copy link
Contributor

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().

@kevinsawicki
Copy link
Contributor

Thanks for this @seanchas116 👍 🎆

@kevinsawicki kevinsawicki merged commit a004cad into electron:master Apr 20, 2017
@seanchas116
Copy link
Contributor Author

Thanks too 👍 🎉

@lastmjs
Copy link

lastmjs commented May 10, 2017

@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

@seanchas116
Copy link
Contributor Author

seanchas116 commented May 10, 2017

the app root is set to some strange location in node_modules/electron/bla/bla

The app path (which you can get by app.getAppPath()) would be usually the directory of the package.json you provide on Electron launch.
Probably Electron is launched without package.json?
If so you could provide package.json to require modules relative to it.

@lastmjs
Copy link

lastmjs commented May 10, 2017

Thanks! I still have a couple questions:

The app path (which you can get by app.getAppPath()) would be usually the directory of the package.json you provide on Electron launch.

What do you mean by the package.json I provide on Electron launch? Here is my package.json:

{
  "name": "jfive-web-components",
  "version": "0.0.8",
  "description": "Web components for controlling hardware with the Johnny-Five JavaScript Robotics & IoT platform.",
  "scripts": {
    "test": "electron --enable-logging node_modules/scram-engine/main.js --entry-file test/index.html --window"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/scramjs/jfive-web-components.git"
  },
  "author": "Jordan Last <jordan.michael.last@gmail.com>",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/scramjs/jfive-web-components/issues"
  },
  "homepage": "https://github.com/scramjs/jfive-web-components#readme",
  "dependencies": {
    "johnny-five": "^0.10.10"
  },
  "devDependencies": {
    "jsverify": "^0.8.2",
    "mocha": "^3.3.0",
    "scram-engine": "git+ssh://git@github.com/scramjs/scram-engine.git",
    "sinon": "^2.2.0",
    "tape": "^4.6.3"
  }
}

When I log app.getAppPath() I get:

/home/lastmj/development/scramjs/jfive-web-components/node_modules/electron/dist/resources/default_app.asar

I think I would like it to be:

/home/lastmj/development/scramjs/jfive-web-components

Am I thinking about it incorrectly?

@seanchas116
Copy link
Contributor Author

Probably
electron --enable-logging node_modules/scram-engine/main.js --entry-file test/index.html --window
would have to be
electron . --enable-logging node_modules/scram-engine/main.js --entry-file test/index.html --window
to specify app path.

@lastmjs
Copy link

lastmjs commented May 10, 2017

electron . doesn't work for me, but I just found in the commit app.setAppPath, and using that function it seems to be working, so thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants