gulp-webpack工程化实现electron
2024-08-28 19:31:02
参考网站:
https://www.npmjs.com/package/babel-loader
https://www.npmjs.com/package/gulp
https://www.npmjs.com/package/gulp-webpack
新建个文件夹
安装gulp
安装gulp-webpack
创建gulpfile文件
并写入
/**
* Created by Administrator on 2016/11/16.
*/
const gulp = require("gulp");
const webpack = require("gulp-webpack"); gulp.task("copy_html", function () {
gulp.src("src/*.html").pipe(gulp.dest("build"));
}); gulp.task("copy_package", function () {
gulp.src("src/package.json").pipe(gulp.dest("build"));
}); gulp.task("com_js", function () {
gulp.src("src/index.js").pipe(webpack({
output: {
filename: "index.js"
},
module: {
loaders: [
{
test: /\.js$/,
loader: 'babel', // 'babel-loader' is also a valid name to reference
query: {
presets: ['es2017']
}
}
]
},
externals: {
electron: "require('electron')",
path: "require('path')",
url: "require('url')"
}
})).pipe(gulp.dest("build"));
}); gulp.task("copy_main", function () {
gulp.src("main.js").pipe(gulp.dest("build"));
}); gulp.task("default", ["copy_html", "copy_package", "com_js", "copy_main"]);
安装babel
现在看下目录结构
说明下,目的是把src文件和main.js文件通过编译放入build文件夹中
main.js代码
/**
* Created by Administrator on 2016/11/16.
*/
const {app, BrowserWindow} = require('electron')
const path = require('path');
const url = require('url'); // Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win; function createWindow() {
// Create the browser window.
win = new BrowserWindow({width: 800, height: 600}) var filepath = path.join(__dirname, 'index.html'); console.log(filepath); // and load the index.html of the app.
win.loadURL(url.format({
pathname: path.join(__dirname,'index.html'),
protocol: 'file:',
slashes: true
})); // Open the DevTools.
win.webContents.openDevTools(); // Emitted when the window is closed.
win.on('closed', () => {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
win = null
})
} // This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow) // Quit when all windows are closed.
app.on('window-all-closed', () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
}); app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (win === null) {
createWindow()
}
});
src下的packjson文件内容:
{
"scripts": {
"starts":"electron ."
},
"name": "application-name",
"version": "0.0.1",
"main": "main.js"
}
控制台中输入
编译文件 gulp (在主文件下)
打开build文件 cd build
运行 electron .
结果
最后说明:gulp编译后有可能出现can not find module,解决方法,安装缺失的模块即可。。
最新文章
- s:if 判断
- 【2016-10-31】【坚持学习】【Day16】【MongoDB】【入门 -概念】
- shell使用随笔
- git branch几个简单操作
- Java文件下载的几种方式
- 极限编程(XP)基本原则与规范
- 原生javascript写的侧栏跟随效果
- python操作redis--string
- springmvc+mybatis+redis(转)
- 如何运行 rpcz python example
- SQL Server 表的管理_关于数据增删查改的操作的详解(案例代码)
- oracle+mybatis 使用动态Sql在要insert的字段不确定的情况下实现批量insert
- freeswitch替换默认保持音乐
- 1-添加自己的Lua执行函数(ESP8266-SDK开发(lua版本))
- Winter-Camp欠债记录
- 在ubuntu14系统中将redis-server设置为开机启动项
- django -- model中只有Field类型的数据才能成为数据库中的列
- SQL 事务(Transaction)
- 什么是Spark(四)运算过程中的黑科技
- python 开始学习