带有 sqlite3 和 webpack 的电子打包器
electron-packager with sqlite3 and webpack
我正在制作一个 gets/stores 数据使用 sqlite3 的小型应用程序。
开发阶段一切正常。但是当我使用 electron-packager 打包我的应用程序时,sqlite3
不再起作用了。
控制台显示异常:找不到模块'sqlite3'。
这是我的渲染器配置:
return {
target: 'electron-renderer',
mode: argv.production ? 'production' : 'development',
context: paths.root,
devtool: !argv.production ? 'source-map' : false,
entry: {
'app': path.resolve(paths.app, 'app.js')
},
optimization: {
runtimeChunk: false,
splitChunks: {
chunks: 'all',
cacheGroups: {
default: {
enforce: true,
priority: 1
},
vendors: {
test: /[\/]node_modules[\/]/,
priority: 2,
name: 'vendors',
enforce: true,
chunks: 'async'
}
}
}
},
module: {
rules: require('./rule')(paths, argv).get()
},
plugins: require('./plugin')(paths, argv).get(),
output: {
path: paths.dist,
filename: 'app.bundled.js'
},
resolve: {
// Add `.ts` and `.tsx` as a resolvable extension.
extensions: [".ts", ".tsx", ".js"],
alias: {
// summernote: codemirror
'CodeMirror': 'codemirror',
}
},
watch: !argv.production,
watchOptions: {
poll: false
},
externals: {
sqlite3: 'commonjs sqlite3'
}
};
这是属于渲染器进程的文件,我在其中包含 sqlite3
:
const typeorm = require('typeorm');
const EntitySchema = typeorm.EntitySchema;
const path = require('path');
const electron = require('electron');
require('sqlite3');
module.exports = (ngModule) => {
ngModule.service('$db', (toastr) => {
//#region Properties
// Instance of database connection
let dbConnection = null;
//#endregion
let out = {
//#region Methods
if (dbConnection == null || forceReinitialize) {
// Build a absolute path to database.
const appPath = electron.remote.app.getAppPath();
const dbPath = path.join(appPath, 'assets/db/PersonalCv.db');
return typeorm
.createConnection({
type: "sqlite",
database: dbPath,
synchronize: false,
entities: [
new EntitySchema(require('../models/entities/user')),
new EntitySchema(require('../models/entities/user-decription')),
new EntitySchema(require('../models/entities/skill-category')),
new EntitySchema(require('../models/entities/skill')),
new EntitySchema(require('../models/entities/personal-skill')),
new EntitySchema(require('../models/entities/project')),
new EntitySchema(require('../models/entities/project-skill')),
new EntitySchema(require('../models/entities/project-responsibility')),
new EntitySchema(require('../models/entities/responsibility'))
]
})
.then((connection) => {
dbConnection = connection;
return dbConnection;
})
.catch((error) => {
toastr.error(error);
throw error;
});
}
return new Promise(resolve => {
resolve(dbConnection);
});
},
/*
* Get repository by using name (table name)
* */
getRepository: (name) => {
return out
.getConnection()
.then((connection) => {
return connection.getRepository(name);
});
}
//#endregion
};
return out;
});
};
这是我的repo,以备不时之需。
有人可以帮我吗?
谢谢,
sqlite3是依赖于系统架构的原生模块。
Electron-packager 不会做任何特别的事情来帮助你编译原生模块。
所以你可以使用像 https://github.com/kripken/sql.js/
这样的纯 JS SQLite 包
或者试试 electron-builder : https://github.com/electron-userland/electron-builder 它有一个 "postinstall" 脚本。
另请参阅:https://electronjs.org/docs/tutorial/using-native-node-modules
在完成 electron-packager
和 electron-builder
的一些问答之后。我找到了一种使 sqlite3
与 webpack
一起工作的解决方案。
在 webpack.config.js
中,我添加了:
externals: {
sqlite3: 'commonjs sqlite3'
}
而不是 electron-packager
,我使用 electron-builder
来构建我的应用程序。在 build
配置中,我将 sqlite3
模块复制到 dist
文件夹。
我正在制作一个 gets/stores 数据使用 sqlite3 的小型应用程序。
开发阶段一切正常。但是当我使用 electron-packager 打包我的应用程序时,sqlite3
不再起作用了。
控制台显示异常:找不到模块'sqlite3'。
这是我的渲染器配置:
return {
target: 'electron-renderer',
mode: argv.production ? 'production' : 'development',
context: paths.root,
devtool: !argv.production ? 'source-map' : false,
entry: {
'app': path.resolve(paths.app, 'app.js')
},
optimization: {
runtimeChunk: false,
splitChunks: {
chunks: 'all',
cacheGroups: {
default: {
enforce: true,
priority: 1
},
vendors: {
test: /[\/]node_modules[\/]/,
priority: 2,
name: 'vendors',
enforce: true,
chunks: 'async'
}
}
}
},
module: {
rules: require('./rule')(paths, argv).get()
},
plugins: require('./plugin')(paths, argv).get(),
output: {
path: paths.dist,
filename: 'app.bundled.js'
},
resolve: {
// Add `.ts` and `.tsx` as a resolvable extension.
extensions: [".ts", ".tsx", ".js"],
alias: {
// summernote: codemirror
'CodeMirror': 'codemirror',
}
},
watch: !argv.production,
watchOptions: {
poll: false
},
externals: {
sqlite3: 'commonjs sqlite3'
}
};
这是属于渲染器进程的文件,我在其中包含 sqlite3
:
const typeorm = require('typeorm');
const EntitySchema = typeorm.EntitySchema;
const path = require('path');
const electron = require('electron');
require('sqlite3');
module.exports = (ngModule) => {
ngModule.service('$db', (toastr) => {
//#region Properties
// Instance of database connection
let dbConnection = null;
//#endregion
let out = {
//#region Methods
if (dbConnection == null || forceReinitialize) {
// Build a absolute path to database.
const appPath = electron.remote.app.getAppPath();
const dbPath = path.join(appPath, 'assets/db/PersonalCv.db');
return typeorm
.createConnection({
type: "sqlite",
database: dbPath,
synchronize: false,
entities: [
new EntitySchema(require('../models/entities/user')),
new EntitySchema(require('../models/entities/user-decription')),
new EntitySchema(require('../models/entities/skill-category')),
new EntitySchema(require('../models/entities/skill')),
new EntitySchema(require('../models/entities/personal-skill')),
new EntitySchema(require('../models/entities/project')),
new EntitySchema(require('../models/entities/project-skill')),
new EntitySchema(require('../models/entities/project-responsibility')),
new EntitySchema(require('../models/entities/responsibility'))
]
})
.then((connection) => {
dbConnection = connection;
return dbConnection;
})
.catch((error) => {
toastr.error(error);
throw error;
});
}
return new Promise(resolve => {
resolve(dbConnection);
});
},
/*
* Get repository by using name (table name)
* */
getRepository: (name) => {
return out
.getConnection()
.then((connection) => {
return connection.getRepository(name);
});
}
//#endregion
};
return out;
});
};
这是我的repo,以备不时之需。
有人可以帮我吗?
谢谢,
sqlite3是依赖于系统架构的原生模块。 Electron-packager 不会做任何特别的事情来帮助你编译原生模块。
所以你可以使用像 https://github.com/kripken/sql.js/
这样的纯 JS SQLite 包或者试试 electron-builder : https://github.com/electron-userland/electron-builder 它有一个 "postinstall" 脚本。
另请参阅:https://electronjs.org/docs/tutorial/using-native-node-modules
在完成 electron-packager
和 electron-builder
的一些问答之后。我找到了一种使 sqlite3
与 webpack
一起工作的解决方案。
在 webpack.config.js
中,我添加了:
externals: {
sqlite3: 'commonjs sqlite3'
}
而不是 electron-packager
,我使用 electron-builder
来构建我的应用程序。在 build
配置中,我将 sqlite3
模块复制到 dist
文件夹。