์ ๋ ์ ์๋ฅผ ์ฒ์ ์ ํ๊ณ require
๋ด ์ ํ๋ฆฌ์ผ์ด์
๋ฐ ๋ถ์ฐ ํด๋ผ์ด์ธํธ ์ธก js ํ์ผ์ ์ฌ์ฉ์ ์ ์ ํ๋กํ ์ฝ์ ์ฌ์ฉํ๋ ค๊ณ ํฉ๋๋ค. d. ๊ทธ๋ฌ๋ require
์ด ๋ด ์ฌ์ฉ์ ์ง์ ๊ตฌ์ฑํ๋ฅผ ๋ฌด์ํ๊ธฐ ๋๋ฌธ์ ๋ชจ๋์ ๋ก๋ํ ์ ์์ต๋๋ค. ์ด๊ฒ์ ๋ด ํ๊ฒฝ์
๋๋ค.
lib/ - node scripts
main.js - node entry script
resources/ - client side resources
css/
html/
index.html
js/ - client side scripts
main.js
test.js
package.json
lib/main.js๋ ๊ณต๊ฐ ํํ์ด์ง์ ์๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํฉ๋๋ค.
// lib/main.js
'use strict';
const electron = require('electron');
const app = electron.app; // Module to control application life.
const BrowserWindow = electron.BrowserWindow; // Module to create native browser window.
const path = require('path');
var mainWindow = null;
app.on('ready', function() {
const protocol = electron.protocol;
/* js, css, fonts, ... */
protocol.registerFileProtocol('resource', (request, callback) => {
var url = request.url.substr(11);
callback({path: path.join(__dirname, '../resources', url)});
});
/* html, possible rendered from other files like markdown or jade */
protocol.registerFileProtocol('view', (request, callback) => {
var url = request.url.substr(11);
callback({path: path.join(__dirname, '../resources/html', url)});
});
// Create the browser window.
mainWindow = new BrowserWindow({width: 800, height: 600});
// and load the index.html of the app.
mainWindow.loadURL('view://index.html');
// Open the DevTools.
mainWindow.webContents.openDevTools();
// ...
});
view://
ํ๋กํ ์ฝ ๋ค์ ์๋ ํ๋ก์ธ์ค๋ markdown ๋๋ jade์ ๊ฐ์ ๋ค๋ฅธ ํ์ผ์ html๋ก ์ปดํ์ผํ ๊ฒ์
๋๋ค( view://index.jade
๋ jade ํ์ผ์ ์ปดํ์ผํ๊ณ html ๋ฌธ์์ด์ ๋ฐํํจ).
<!-- resources/html/index.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Electron Test</title>
</head>
<body>
<h1>Electron Test</h1>
<script src="resource://js/main.js"></script>
</body>
</html>
html ํ์ผ์ ๋ด ์ฌ์ฉ์ ์ ์ resource://
ํ๋กํ ์ฝ์ ํตํด css ๋๋ js ํ์ผ๊ณผ ๊ฐ์ ๋ฆฌ์์ค์ ์ก์ธ์คํฉ๋๋ค. ๊ธฐ๋ณธ ํ์ผ์ ์ฌ๋ฐ๋ฅด๊ฒ ์คํ๋์ง๋ง require
๋ค๋ฅธ ํ์ผ์ ์คํํ ์ ์์ต๋๋ค.
// resources/js/main.js
const test = require('./test'); // Error: Cannot find module './test'
test.sayHello();
// resources/js/test.js
exports.sayHello = function() {
console.log('Hello!');
};
electron .
, v0.36.7๋ก ์คํํ๊ณ ์์ต๋๋ค.
devtools์์ require
๋ค์ ์๋ ์ฝ๋๋ฅผ ๊ฒ์ฌํ ๋(์ฝ module.js:336
, ๋
ธ๋์ ํ์ฌ ๋ง์คํฐ์์ 337 ๋ฒ์งธ ์ค) ์ค๋ฅ๊ฐ ๋ฐ์ํ์ ๋ request
๋ "./test"
์ด๊ณ paths
์๋ ์ ์์ ํต์ฌ ๋ชจ๋์ ๋ํ ํญ๋ชฉ์ด ํ๋๋ง ํฌํจ๋์ด ์์ผ๋ฉฐ ๊ฒ์ ๊ฒฝ๋ก ๋ชฉ๋ก์ ํ์ฅํ๋ ๋ฐฉ๋ฒ์ ์ฐพ์ง ๋ชปํ์ต๋๋ค.
๋์ผํ ๋ฌธ์ ์ ๋ํ ๊ฒ์ผ๋ก ๋ณด์ด๋ ๋ค๋ฅธ ๋ฌธ์ (#4459, #2539)๋ฅผ ํ์ธํ์ง๋ง ๋์์ด ๋์ง ์์์ต๋๋ค(์: module
์ ๊ฒฝ๋ก ๋๋ globalPaths๋ฅผ ์กฐ์ํ๋ ์ฌ์ ๋ก๋ ์คํฌ๋ฆฝํธ ์ฌ์ฉ์ด ์๋ํ์ง ์์ .
์ด ์์ ์ ์ํํ๋ ๋ฐฉ๋ฒ์ ๋ํ ์์ด๋์ด๊ฐ ์์ต๋๊น? ์๋๋ฉด ๋จ์ผ JavaScript ํ์ผ์ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข์ต๋๊น? (์ ๋ ๋ชจ๋ ์์ค ํ์ผ์ ์ฐ๊ฒฐํ ์ ์๋ ์ ์ฒ๋ฆฌ๊ธฐ๋ก TypeScript๋ฅผ ์ฌ์ฉํ๊ณ ์์ต๋๋ค.)
module.paths
์์ ๋
ธ๋ ๋ชจ๋์ ๊ฒ์ ๊ฒฝ๋ก๋ฅผ ํ์ธํ ์ ์์ต๋๋ค. resources/js
๊ฐ ์์ผ๋ฉด require
๋ test.js
๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค. .
๋ชจ๋ ๊ฒ์ ๊ฒฝ๋ก๋ฅผ ์ฌ๋ฐ๋ฅด๊ฒ ์กฐ์ํ์ง ์์๊ธฐ ๋๋ฌธ์ ์ปค๋ฎค๋ํฐ ์์ ์์ด๋์ด๋ฅผ ์ป์ ์ ์๋ค๊ณ ์๊ฐ
<script>
ํ๊ทธ๋ฅผ ํตํด .js
ํ์ผ์ ๋ก๋ํ๋ฉด ๋ฌธ์์ ํฌํจ๋๋ฏ๋ก
<body>
<h1>Electron Test</h1>
<script src="resource://js/main.js"></script>
</body>
๊ธฐ๋ฅ์ ์ผ๋ก ๋๋ฑํ๋ค
<body>
<h1>Electron Test</h1>
<script>
const test = require('./test'); // Error: Cannot find module './test'
test.sayHello();
</script>
</body>
์ด๋ค ๊ฒ์ ์๋ฏธ ./test
์๋์ ํด๊ฒฐ index.html
์ ์๋ํ์ง js/main.js
. ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ค๋ฉด ๋ค์์ ์ํํ ์ ์์ต๋๋ค.
<body>
<h1>Electron Test</h1>
<script>
require('../js/main.js');
</script>
</body>
์ฐ๋ คํ ์ ์๋ ์ฌ๋์ ์ํด ์ด ์๋ฃจ์
์ ์ฐพ์์ต๋๋ค.
WEB_FOLDER์๋ index.html ๋ฐ ๊ธฐํ ๋ชจ๋ ์๋ ์ฐธ์กฐ ์ฝํ
์ธ ๊ฐ ํฌํจ๋์ด ์์ต๋๋ค.
์ธ๋ฑ์ค ํ์ผ์ด ํ์ ํด๋(์ด ์์์๋ web) ์์ ์์ง ์์ผ๋ฉด ๋น ๋ฌธ์์ด๋ก ์ค์ ํ ์ ์์ต๋๋ค.
function createWindow() {
const WEB_FOLDER = 'web';
const PROTOCOL = 'file';
electron.protocol.interceptFileProtocol(PROTOCOL, (request, callback) => {
// // Strip protocol
let url = request.url.substr(PROTOCOL.length + 1);
// Build complete path for node require function
url = path.join(__dirname, WEB_FOLDER, url);
// Replace backslashes by forward slashes (windows)
// url = url.replace(/\\/g, '/');
url = path.normalize(url);
console.log(url);
callback({path: url});
});
// Create the browser window.
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: false
}
});
// and load the index.html of the app.
mainWindow.loadURL(url.format({
pathname: 'index.html',
protocol: PROTOCOL + ':',
slashes: true
}));
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
์ฐ๋ คํ ์ ์๋ ์ฌ๋์ ์ํด ์ด ์๋ฃจ์ ์ ์ฐพ์์ต๋๋ค.
WEB_FOLDER์๋ index.html ๋ฐ ๊ธฐํ ๋ชจ๋ ์๋ ์ฐธ์กฐ ์ฝํ ์ธ ๊ฐ ํฌํจ๋์ด ์์ต๋๋ค.
์ธ๋ฑ์ค ํ์ผ์ด ํ์ ํด๋(์ด ์์์๋ web) ์์ ์์ง ์์ผ๋ฉด ๋น ๋ฌธ์์ด๋ก ์ค์ ํ ์ ์์ต๋๋ค.