[英]Electron + Typescript File Structure
I have a problem to structure my electron
project using typescript
.我在使用
typescript
构建我的electron
项目时遇到问题。 Basically, I want my file structure to look like this:基本上,我希望我的文件结构如下所示:
+dist
compiled .js files from .ts files
+src
.ts files
+gui
html pages
+package.json
+tsconfig.json
My tsconfig.json files is as follows:我的tsconfig.json文件如下:
"compilerOptions": {
"target": "es2015",
"module": "commonjs",
"outDir": "dist",
"sourceMap": true
},
and my package.json files is as follows:我的 package.json 文件如下:
"name": "electron_test",
"version": "1.0.0",
"description": "",
"main": "./dist/main.js",
"scripts": {
"build": "tsc",
"prestart": "npm run build",
"start": "tsc && electron ./dist/main.js",
"pack": "electron-packager . sample --out=dist --arch=x64 --platform=win32 --electron-version=3.0.3 --overwrite --prune --ignore=dist"
},
"author": "",
"license": "ISC",
"devDependencies": {
"electron": "^3.0.3",
"electron-packager": "^12.2.0",
"typescript": "^3.1.1"
}
}
The problem with these files is when typescript
files are compiled, they are converted to javascript
inside the dist
file.这些文件的问题是在编译
typescript
文件时,它们会在dist
文件中转换为javascript
。 Here I need to use main.js
for my electron
app to call within html
pages.在这里,我需要为我的
electron
应用程序使用main.js
在html
页面中调用。 However when Html
pages are not inside the dist
file, if I run the app, html
pages are not called.但是,当
Html
页面不在dist
文件中时,如果我运行该应用程序,则不会调用html
页面。 Somehow main.js
and html
pages should be inside the same directory.不知何故
main.js
和html
页面应该在同一个目录中。
src
directory.src
目录。 This is not required but this is how usually people structure projects.I would recommend to use webpack instead of raw tsc.我建议使用 webpack 而不是 raw tsc。 You want to use CopyWebpackPLugin
你想使用CopyWebpackPLugin
const CopyWebpackPlugin = require('copy-webpack-plugin'); plugins: [ new CopyWebpackPlugin([ {from: './src/gui', to: ''}, ]),
If you still want to use tsc, you can copy files manually my using cp
in package.json.如果你还想使用tsc,你可以在package.json中使用
cp
手动复制文件。
package.json :包.json :
"scripts": {
"copyHtml": "cp ./src/gui ./dist",
"build": "tsc && npm run copyHtml",
"prestart": "npm run build",
"start": "tsc && npm run copyHtml && electron ./dist/main.js",
"pack": "electron-packager . sample --out=dist --arch=x64 --platform=win32 --electron-version=3.0.3 --overwrite --prune --ignore=dist"
},
You can also use cpx if you worry about crossOs support.如果您担心 crossOs 支持,您也可以使用cpx 。
Anyway the point is that dist directory should contain all output files.无论如何,关键是 dist 目录应该包含所有输出文件。 And it's self-contained, meaning you can send this directory to anyway and he/she should be able to run your project w/o any other dependencies.
它是独立的,这意味着您可以将这个目录发送到任何地方,他/她应该能够在没有任何其他依赖项的情况下运行您的项目。
I know I'm a year late to the party, but I hope I can still help others with my answer.我知道我参加聚会晚了一年,但我希望我的回答仍然可以帮助其他人。 Here's an alternative solution to the complex file structure of electron apps that I came up with myself.
这是我自己提出的电子应用程序复杂文件结构的替代解决方案。 It's not as conventional, but to me it makes a lot more sense.
它并不那么传统,但对我来说它更有意义。
My project's directory structure is roughly as follows:我的项目的目录结构大致如下:
app/
|- index.html
|- index.js (electron main script)
|- bundle.js (generated via webpack)
build/
|- compiled ts -> js files
dist/
|- packaged executables
src/
|- all source files (in typescript)
Basically, I just put the electron main script into app/
alongside index.html
.基本上,我只是将电子主脚本与
index.html
一起放入app/
。 This makes sense to me since dist/
should be reserved for the distributed application delivered to the end user, and in the case of an electron app, we don't give the user access to index.html
directly anyways.这对我来说很有意义,因为
dist/
应该保留给交付给最终用户的分布式应用程序,并且在电子应用程序的情况下,我们无论如何都不让用户直接访问index.html
。 The build process then runs as follows:然后构建过程如下运行:
call tsc
on src/
, output into build/
在
src/
上调用tsc
,输出到build/
"outdir": "build"
in tsconfig.json
, then running npx tsc
tsconfig.json
设置"outdir": "build"
,然后运行tsconfig.json
npx tsc
call webpack
on build/
, bundle everything into a single app/bundle.js
在
build/
上调用webpack
,将所有内容捆绑到一个app/bundle.js
output: { filename: 'bundle.js', path: path.resolve(__dirname, 'app')}
, then running webpack build/src/[entry point]
output: { filename: 'bundle.js', path: path.resolve(__dirname, 'app')}
完成output: { filename: 'bundle.js', path: path.resolve(__dirname, 'app')}
,然后运行webpack build/src/[entry point]
index.html
loads bundle.js
via a script element index.html
通过脚本元素加载bundle.js
<script src="bundle.js"></script>
<script src="bundle.js"></script>
main.js
renders index.html
via electron main.js
通过电子呈现index.html
"main": "app/main.js"
in package.json
, then running npx electron .
package.json
设置"main": "app/main.js"
,然后运行"main": "app/main.js"
npx electron .
electron-builder
packages app/
into a single executable electron-builder
app/
打包成单个可执行文件
npx electron-builder
, with a corresponding config specified in package.json
npx electron-builder
,在package.json
指定了相应的配置Given how the build process runs, the directory structure makes a lot of sense to me.鉴于构建过程的运行方式,目录结构对我来说很有意义。
src/
stores the typescript source files, build/
stores the built javascript files, app/
stores all the files for the electron renderer process, and dist/
stores the packaged distributables. src/
存储打字稿源文件, build/
存储构建的 javascript 文件, app/
存储电子渲染器进程的所有文件, dist/
存储打包的分发文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.