{"id":165,"date":"2023-10-23T07:29:11","date_gmt":"2023-10-22T22:29:11","guid":{"rendered":"https:\/\/global-hack.com\/blog\/?p=165"},"modified":"2023-10-31T07:49:36","modified_gmt":"2023-10-30T22:49:36","slug":"%e3%80%90electron%e3%80%91%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab%e5%a4%89%e6%9b%b4%e3%82%92%e6%a4%9c%e5%87%ba%e3%81%97%e3%80%81%e3%82%b3%e3%83%b3%e3%83%91%e3%82%a4%e3%83%ab%e5%ae%8c%e4%ba%86%e5%be%8c","status":"publish","type":"post","link":"https:\/\/global-hack.com\/blog\/archives\/165","title":{"rendered":"\u3010Electron\u3011\u30d5\u30a1\u30a4\u30eb\u5909\u66f4\u3092\u691c\u51fa\u3057\u3001\u30c8\u30e9\u30f3\u30b9\u30d1\u30a4\u30eb\u5b8c\u4e86\u5f8c\u306b\u5b9f\u884c\u3059\u308b\u30b9\u30af\u30ea\u30d7\u30c8\u3010Node.js\u3011"},"content":{"rendered":"<h3>Electron\u3067\u306ejs\u30d5\u30a1\u30a4\u30eb\u306e\u53d6\u308a\u6271\u3044<\/h3>\n<p>\u901a\u5e38\u306a\u3089\u3070React\u306e\u30dc\u30a4\u30e9\u30fc\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306b\u5099\u308f\u3063\u3066\u3044\u308b\u30db\u30c3\u30c8\u30ea\u30ed\u30fc\u30c9\u306a\u3069\u306e\u30c7\u30d5\u30a9\u30eb\u30c8\u6a5f\u80fd\u3092\u7528\u3044\u3066\u884c\u3046\u5de5\u7a0b\u3092\u3001\u304a\u624b\u88fd\u306e\u30b9\u30af\u30ea\u30d7\u30c8\u3067\u4e00\u7e8f\u3081\u306b\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3068\u304d\u304c\u3042\u308b\u3068\u601d\u3044\u307e\u3059\u3002\u79c1\u306e\u5834\u5408\u3067\u3059\u304c\u3001<a href=\"https:\/\/www.ipa.go.jp\/security\/vuln\/websecurity\/cross-site-scripting.html\" target=\"_blank\" rel=\"nofollow noopener\" title=\"XSS\u306e\u5bfe\u7b56\u6982\u8981\">XSS<\/a>\u5bfe\u7b56\u3068\u3057\u3066<a href=\"https:\/\/developer.mozilla.org\/ja\/docs\/Web\/HTTP\/CSP\" target=\"_blank\" rel=\"nofollow noopener\" title=\"CSP(Content-Security-Policy)\">CSP(Content-Security-Policy)<\/a>\u306enonce\u5024\u3092\u8a2d\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u52dd\u624b\u306bjsx\u30d5\u30a1\u30a4\u30eb\u3092\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306b\u5dee\u3057\u8fbc\u307e\u308c\u308b\u3068\u56f0\u308b\u305f\u3081\u3001\u5225\u9014jsx\u30d5\u30a1\u30a4\u30eb\u3092\u30c8\u30e9\u30f3\u30b9\u30d1\u30a4\u30eb\u3057\u3066\u307f\u307e\u3057\u305f\u3002\u4eca\u56de\u306e\u51e6\u7406\u306b\u3064\u3044\u3066\u306f\u3001webpack\u3067\u3082\u554f\u984c\u306a\u304f\u540c\u69d8\u306e\u51e6\u7406\u304c\u3067\u304d\u307e\u3059\u3002\u3080\u3057\u308dwebpack\u306e\u65b9\u304c\u7c21\u5358\u3067\u308f\u304b\u308a\u3084\u3059\u3044\u304b\u3082\uff08\u6c57\uff09<\/p>\n<h3>\u5f53\u521d\u3057\u3088\u3046\u3057\u3066\u3044\u305f\u30b3\u30fc\u30c9<\/h3>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-plain\" data-lang=\"Plain Text\"><code>npx babel app\/renderer\/control.jsx --out-file app\/renderer\/control-compiled.js --presets @babel\/preset-env<\/code><\/pre>\n<\/div>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-plain\" data-lang=\"Plain Text\"><code>electron .<\/code><\/pre>\n<\/div>\n<p>\u4e0a\u8a18\u306e\u3088\u3046\u306b\uff12\u56de\u306b\u308f\u3051\u3066\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u3066\u3044\u307e\u3057\u305f\u304c\u3001\u975e\u5e38\u306b\u9762\u5012\u3067\u30bf\u30fc\u30df\u30ca\u30eb\u3082\u7121\u99c4\u306b\u6c5a\u3057\u307e\u3059\u3002<\/p>\n<p>\u6b21\u306bpackage.json\u3067\u4e0b\u8a18\u306e\u3088\u3046\u306b\u3057\u3066\u304b\u3089\u5b9f\u884c\u3092\u8a66\u307f\u307e\u3059\u3002<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-plain\" data-lang=\"Plain Text\"><code>\"scripts\": {\r\n    \"start:control\": \"npx babel --watch app\/renderer\/control.jsx --out-file app\/renderer\/control-compiled.js\",\r\n    \"start\": \"yarn start:control &amp;&amp; electron .\"\r\n}<\/code><\/pre>\n<\/div>\n<p>\u4f7f\u7528\u3059\u308b\u969b\u306b\u306f\u3001\u4e0b\u8a18\u306e\u307f\u3092\u30bf\u30fc\u30df\u30ca\u30eb\u4e0a\u3067\u5b9f\u884c\u3057\u307e\u3059\u3002<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-plain\" data-lang=\"Plain Text\"><code>npm run start<\/code><\/pre>\n<\/div>\n<p>\u3057\u304b\u3057\u3053\u3053\u3067\u554f\u984c\u304c\u767a\u751f\u3002\u76e3\u8996\u3057\u3066\u3044\u308b\u306f\u305a\u306e&#8211;watch\u30d5\u30e9\u30b0\u3067\u30d7\u30ed\u30bb\u30b9\u304c\u6b62\u307e\u3063\u3066\u3057\u307e\u3044\u3001\u300celectron .\u300d\u3078\u306f\u5230\u9054\u3067\u304d\u305a\u3002\u3053\u308c\u306f\u3001Babel\u306e&#8211;watch\u30d5\u30e9\u30b0\u306f\u30d5\u30a1\u30a4\u30eb\u304c\u5909\u66f4\u3055\u308c\u308b\u305f\u3073\u306b\u5b9f\u884c\u3092\u7dad\u6301\u3057\u7d9a\u3051\u308b\u305f\u3081\u3001\u30c8\u30e9\u30f3\u30b9\u30d1\u30a4\u30eb\u304c\u5b8c\u4e86\u3059\u308b\u307e\u3067\u5f85\u6a5f\u3059\u308b\u3053\u3068\u304c\u96e3\u3057\u3044\u3068\u3044\u3046\u554f\u984c\u3002\u3064\u307e\u308a\u3001\u30c8\u30e9\u30f3\u30b9\u30d1\u30a4\u30eb<span>\u304c\u6c38\u9060\u306b\u7d9a\u304f\u305f\u3081\u3001\u6b63\u3057\u304f\u6a5f\u80fd\u3057\u306a\u3044\u3068\u3044\u3046\u3053\u3068\u3002<\/span><\/p>\n<h3>\u30c8\u30e9\u30f3\u30b9\u30d1\u30a4\u30eb\u3068\u8d77\u52d5\u3092\u3084\u308a\u304f\u308a\u3059\u308b\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u4f5c\u6210\u3059\u308b<\/h3>\n<p>\u30d5\u30a1\u30a4\u30eb\u540d\u306f\u300cstart.js\u300d\u306b\u3057\u307e\u3057\u305f\u3002<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-js\" data-lang=\"JavaScript\"><code>const fs = require('fs');\r\nconst { exec } = require('child_process');\r\n\r\nconst sourceFile = 'app\/renderer\/control.jsx';\r\nconst compiledFile = 'app\/renderer\/control-compiled.js';\r\nlet isCompiling = false;\r\n\r\nfunction compileAndStartElectron() {\r\n  if (isCompiling) {\r\n    console.log('Compilation is already in progress, waiting...');\r\n    return;\r\n  }\r\n\r\n  isCompiling = true;\r\n\r\n  const compileProcess = exec(`npx babel ${sourceFile} --out-file ${compiledFile} --presets @babel\/preset-env`);\r\n\r\n  compileProcess.on('exit', (code) =&gt; {\r\n    isCompiling = false;\r\n    if (code === 0) {\r\n      console.log('Compilation complete, starting Electron...');\r\n      startElectron();\r\n    } else {\r\n      console.error('Compilation failed.');\r\n    }\r\n  });\r\n}\r\n\r\nfunction startElectron() {\r\n  const electronProcess = exec('electron .');\r\n\r\n  electronProcess.on('exit', (code) =&gt; {\r\n    console.log(`Electron process exited with code ${code}`);\r\n  });\r\n}\r\n\r\n\/\/ \u30d5\u30a1\u30a4\u30eb\u306e\u5909\u66f4\u3092\u76e3\u8996\r\nfs.watch(sourceFile, (event) =&gt; {\r\n  if (event === 'change') {\r\n    console.log('File changed, recompiling...');\r\n    compileAndStartElectron();\r\n  }\r\n});\r\n\r\n\/\/ Electron\u3092\u8d77\u52d5\r\ncompileAndStartElectron();<\/code><\/pre>\n<\/div>\n<p>\u30d5\u30e9\u30b0\u3092\u7528\u3044\u3066\u30d5\u30a1\u30a4\u30eb\u306e\u5909\u66f4\u3092\u76e3\u8996\u3057\u3001\u5909\u66f4\u5f8c\u306bElectron\u3092\u8d77\u52d5\u3055\u305b\u308b\u30b3\u30fc\u30c9\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002\u666e\u6bb5\u5df7\u3067\u5acc\u308f\u308c\u3066\u3044\u308bexec\u95a2\u6570\u3067\u3059\u304c\u3001\u3042\u304f\u307e\u3067\u3082\u958b\u767a\u6642\u306e\u30c8\u30e9\u30f3\u30b9\u30d1\u30a4\u30eb\u306e\u307f\u3067\u4f7f\u7528\u3059\u308b\u3060\u3051\u3067\u3059\u3002exec\u95a2\u6570\u306e\u4e2d\u8eab\u306f\u3001\u5148\u8ff0\u3057\u305f\u30b3\u30de\u30f3\u30c9\u3068\u540c\u69d8\u3067\u3059\u3002\u307e\u305f\u3001\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306e\u90e8\u5206\u306f\u300csourceFile\uff08\u5143\u30d5\u30a1\u30a4\u30eb\uff09\u300d\u3068\u300ccompiledFile\uff08\u30c8\u30e9\u30f3\u30b9\u30d1\u30a4\u30eb\u5148\uff09\u300d\u3092\u66f8\u304d\u63db\u3048\u308b\u3060\u3051\u306a\u306e\u3067\u3001\u3054\u81ea\u8eab\u306e\u958b\u767a\u306b\u3082\u7c21\u5358\u306b\u6d41\u7528\u3067\u304d\u308b\u304b\u3068\u601d\u308f\u308c\u307e\u3059\u3002<\/p>\n<p>\u5b9f\u884c\u30b3\u30de\u30f3\u30c9\u306b\u3064\u3044\u3066\u3067\u3059\u304c\u3001\u30d5\u30a1\u30a4\u30eb\u540d\u304c\u300cstart.js\u300d\u306a\u306e\u3067package.json\u3092\u4e00\u65e6\u4e0b\u8a18\u306e\u3088\u3046\u306b\u3057\u3066\u304a\u304d\u307e\u3059\u3002\u975e\u5e38\u306b\u30b7\u30f3\u30d7\u30eb\u306b\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-plain\" data-lang=\"Plain Text\"><code>\"scripts\": {\r\n    \"start\": \"node start.js\"\r\n}\r\n<\/code><\/pre>\n<\/div>\n<p>\u5f93\u3063\u3066\u3001\u5b9f\u884c\u30b3\u30de\u30f3\u30c9\u306f\u4e0b\u8a18\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-plain\" data-lang=\"Plain Text\"><code>npm run start\r\n<\/code><\/pre>\n<\/div>\n<p>\u4ee5\u4e0a\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Electron\u3067\u306ejs\u30d5\u30a1\u30a4\u30eb\u306e\u53d6\u308a\u6271\u3044 \u901a\u5e38\u306a\u3089\u3070React\u306e\u30dc\u30a4\u30e9\u30fc\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306b\u5099\u308f\u3063\u3066\u3044\u308b\u30db\u30c3\u30c8\u30ea\u30ed\u30fc\u30c9\u306a\u3069\u306e\u30c7\u30d5\u30a9\u30eb\u30c8\u6a5f\u80fd\u3092\u7528\u3044\u3066\u884c\u3046\u5de5\u7a0b\u3092\u3001\u304a\u624b\u88fd\u306e\u30b9\u30af\u30ea\u30d7\u30c8\u3067\u4e00\u7e8f\u3081\u306b\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3068\u304d\u304c\u3042\u308b\u3068\u601d\u3044\u307e\u3059\u3002\u79c1\u306e\u5834\u5408\u3067\u3059\u304c\u3001X <a class=\"mh-excerpt-more\" href=\"https:\/\/global-hack.com\/blog\/archives\/165\" title=\"\u3010Electron\u3011\u30d5\u30a1\u30a4\u30eb\u5909\u66f4\u3092\u691c\u51fa\u3057\u3001\u30c8\u30e9\u30f3\u30b9\u30d1\u30a4\u30eb\u5b8c\u4e86\u5f8c\u306b\u5b9f\u884c\u3059\u308b\u30b9\u30af\u30ea\u30d7\u30c8\u3010Node.js\u3011\">&#8230;<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":166,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[94,41,97,15],"tags":[108,53,43],"class_list":["post-165","post","type-post","status-publish","format-standard","has-post-thumbnail","category-javascript","category-node-js","category-react","category-15","tag-babel","tag-electron","tag-webpack"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/global-hack.com\/blog\/wp-content\/uploads\/2023\/10\/compile_React.jpg","_links":{"self":[{"href":"https:\/\/global-hack.com\/blog\/wp-json\/wp\/v2\/posts\/165","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/global-hack.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/global-hack.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/global-hack.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/global-hack.com\/blog\/wp-json\/wp\/v2\/comments?post=165"}],"version-history":[{"count":0,"href":"https:\/\/global-hack.com\/blog\/wp-json\/wp\/v2\/posts\/165\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/global-hack.com\/blog\/wp-json\/wp\/v2\/media\/166"}],"wp:attachment":[{"href":"https:\/\/global-hack.com\/blog\/wp-json\/wp\/v2\/media?parent=165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/global-hack.com\/blog\/wp-json\/wp\/v2\/categories?post=165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/global-hack.com\/blog\/wp-json\/wp\/v2\/tags?post=165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}