2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 07:40:55 +00:00

Separate config for electron main

This commit is contained in:
Faris Ansari 2018-10-21 14:36:53 +05:30
parent af5c34c6fd
commit 82f769dcfe
3 changed files with 197 additions and 139 deletions

View File

@ -1,19 +1,30 @@
const webpack = require('webpack'); const webpack = require('webpack');
const getWebpackConfig = require('./config'); const { getConfig, getElectronMainConfig } = require('./config');
module.exports = function build(mode) { module.exports = function build(mode) {
const webpackConfig = getWebpackConfig(); const rendererConfig = getConfig();
const mainConfig = getElectronMainConfig();
process.env.NODE_ENV = 'production'; process.env.NODE_ENV = 'production';
if (mode === 'electron') { if (mode === 'electron') {
pack(webpackConfig) pack(rendererConfig)
.then(result => { .then(result => {
console.log(result); console.log(result);
}).catch(err => { }).catch(err => {
console.log(`\n Failed to build renderer process`); console.log(`\n Failed to build renderer process`);
console.error(`\n${err}\n`); console.error(`\n${err}\n`);
process.exit(1) process.exit(1)
}) });
pack(mainConfig)
.then(result => {
console.log(result);
}).catch(err => {
console.log(`\n Failed to build main process`);
console.error(`\n${err}\n`);
process.exit(1)
});
} }
} }

View File

@ -1,21 +1,19 @@
const webpack = require('webpack'); const webpack = require('webpack');
// plugins
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CaseSensitivePathsWebpackPlugin = require('case-sensitive-paths-webpack-plugin');
const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const { getAppConfig, resolveAppDir } = require('./utils'); const { getAppConfig, resolveAppDir } = require('./utils');
const appDependencies = require(resolveAppDir('./package.json')).dependencies; const appDependencies = require(resolveAppDir('./package.json')).dependencies;
const frappeDependencies = require('../package.json').dependencies; const frappeDependencies = require('../package.json').dependencies;
function getConfig() { let getConfig, getElectronMainConfig;
const plugins = {
NamedModules: webpack.NamedModulesPlugin,
HotModuleReplacement: webpack.HotModuleReplacementPlugin,
Define: webpack.DefinePlugin,
Progress: webpack.ProgressPlugin,
VueLoader: require('vue-loader/lib/plugin'),
Html: require('html-webpack-plugin'),
CaseSensitivePaths: require('case-sensitive-paths-webpack-plugin'),
FriendlyErrors: require('friendly-errors-webpack-plugin'),
CopyWebpackPlugin: require('copy-webpack-plugin')
}
function makeConfig() {
const appConfig = getAppConfig(); const appConfig = getAppConfig();
const isProduction = process.env.NODE_ENV === 'production'; const isProduction = process.env.NODE_ENV === 'production';
const isElectron = process.env.ELECTRON === 'true'; const isElectron = process.env.ELECTRON === 'true';
@ -23,8 +21,9 @@ function getConfig() {
const whiteListedModules = ['vue']; const whiteListedModules = ['vue'];
const allDependencies = Object.assign(frappeDependencies, appDependencies); const allDependencies = Object.assign(frappeDependencies, appDependencies);
const externals = Object.keys(allDependencies).filter(d => !whiteListedModules.includes(d)) const externals = Object.keys(allDependencies).filter(d => !whiteListedModules.includes(d));
getConfig = function getConfig() {
const config = { const config = {
mode: isProduction ? 'production' : 'development', mode: isProduction ? 'production' : 'development',
context: resolveAppDir(), context: resolveAppDir(),
@ -88,29 +87,29 @@ function getConfig() {
} }
}, },
plugins: [ plugins: [
new plugins.Define(Object.assign({ new webpack.DefinePlugin(Object.assign({
'process.env': appConfig.dev.env, 'process.env': appConfig.dev.env,
'process.env.NODE_ENV': isProduction ? '"production"' : '"development"', 'process.env.NODE_ENV': isProduction ? '"production"' : '"development"',
}, !isProduction ? { }, !isProduction ? {
'__static': `"${resolveAppDir(appConfig.staticPath).replace(/\\/g, '\\\\')}"` '__static': `"${resolveAppDir(appConfig.staticPath).replace(/\\/g, '\\\\')}"`
} : {})), } : {})),
new plugins.VueLoader(), new VueLoaderPlugin(),
new plugins.Html({ new HtmlWebpackPlugin({
template: resolveAppDir(appConfig.dev.entryHtml), template: resolveAppDir(appConfig.dev.entryHtml),
nodeModules: !isProduction nodeModules: !isProduction
? isMonoRepo ? resolveAppDir('../../node_modules') : resolveAppDir('./node_modules') ? isMonoRepo ? resolveAppDir('../../node_modules') : resolveAppDir('./node_modules')
: false : false
}), }),
new plugins.CaseSensitivePaths(), new CaseSensitivePathsWebpackPlugin(),
new plugins.NamedModules(), new webpack.NamedModulesPlugin(),
new plugins.HotModuleReplacement(), new webpack.HotModuleReplacementPlugin(),
new plugins.FriendlyErrors({ new FriendlyErrorsWebpackPlugin({
compilationSuccessInfo: { compilationSuccessInfo: {
messages: [`FrappeJS server started at http://${appConfig.dev.devServerHost}:${appConfig.dev.devServerPort}`], messages: [`FrappeJS server started at http://${appConfig.dev.devServerHost}:${appConfig.dev.devServerPort}`],
}, },
}), }),
new plugins.Progress(), new webpack.ProgressPlugin(),
isProduction ? new plugins.CopyWebpackPlugin([ isProduction ? new CopyWebpackPlugin([
{ {
from: resolveAppDir(appConfig.staticPath), from: resolveAppDir(appConfig.staticPath),
to: resolveAppDir('./dist/electron/static'), to: resolveAppDir('./dist/electron/static'),
@ -152,4 +151,52 @@ function getConfig() {
return config; return config;
} }
module.exports = getConfig; getElectronMainConfig = function getElectronMainConfig() {
return {
entry: {
main: resolveAppDir(appConfig.electron.paths.main)
},
externals: externals,
module: {
rules: [
{
test: /\.js$/,
use: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.node$/,
use: 'node-loader'
}
]
},
node: {
__dirname: !isProduction,
__filename: !isProduction
},
output: {
filename: '[name].js',
libraryTarget: 'commonjs2',
path: resolveAppDir('./dist/electron')
},
plugins: [
new webpack.NoEmitOnErrorsPlugin(),
// isProduction && new BabiliWebpackPlugin(),
isProduction && new webpack.DefinePlugin({
'process.env.NODE_ENV': '"production"'
})
],
resolve: {
extensions: ['.js', '.json', '.node']
},
target: 'electron-main'
}
}
}
makeConfig();
module.exports = {
getConfig,
getElectronMainConfig
};

View File

@ -5,7 +5,7 @@ const webpackHotMiddleware = require('webpack-hot-middleware');
const logger = require('./logger'); const logger = require('./logger');
const { getAppConfig, resolveAppDir } = require('./utils'); const { getAppConfig, resolveAppDir } = require('./utils');
const getWebpackConfig = require('./config'); const { getConfig: getWebpackConfig } = require('./config');
const log = logger('serve'); const log = logger('serve');
const warn = logger('serve', 'red'); const warn = logger('serve', 'red');