2
0
mirror of https://github.com/frappe/books.git synced 2024-09-20 11:29:00 +00:00
books/webpack/config.js

214 lines
6.4 KiB
JavaScript
Raw Normal View History

const webpack = require('webpack');
2018-10-21 09:06:53 +00:00
// 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 appDependencies = require(resolveAppDir('./package.json')).dependencies;
2018-10-20 12:30:58 +00:00
const frappeDependencies = require('../package.json').dependencies;
2018-10-21 09:06:53 +00:00
let getConfig, getElectronMainConfig;
2018-10-20 12:30:58 +00:00
2018-10-21 09:06:53 +00:00
function makeConfig() {
const appConfig = getAppConfig();
const isProduction = process.env.NODE_ENV === 'production';
const isElectron = process.env.ELECTRON === 'true';
const isMonoRepo = process.env.MONO_REPO === 'true';
const whiteListedModules = ['vue'];
const allDependencies = Object.assign(frappeDependencies, appDependencies);
2019-07-12 10:53:19 +00:00
const externals = Object.keys(allDependencies).filter(
d => !whiteListedModules.includes(d)
);
2018-10-21 09:06:53 +00:00
getConfig = function getConfig() {
const config = {
2018-10-21 09:06:53 +00:00
mode: isProduction ? 'production' : 'development',
context: resolveAppDir(),
entry: isElectron ? appConfig.electron.entry : appConfig.dev.entry,
externals: isElectron ? externals : undefined,
2018-10-21 09:06:53 +00:00
target: isElectron ? 'electron-renderer' : 'web',
output: {
2019-07-12 10:53:19 +00:00
path: isElectron
? resolveAppDir('./dist/electron')
: resolveAppDir('./dist'),
2018-10-21 09:06:53 +00:00
filename: '[name].js',
// publicPath: appConfig.dev.assetsPublicPath,
libraryTarget: isElectron ? 'commonjs2' : undefined
2018-10-21 09:06:53 +00:00
},
devtool: !isProduction ? 'cheap-module-eval-source-map' : '',
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader'
},
{
test: /\.js$/,
loader: 'babel-loader',
2019-07-12 10:53:19 +00:00
exclude: file =>
/node_modules/.test(file) && !/\.vue\.js/.test(file)
2018-10-21 09:06:53 +00:00
},
{
test: /\.node$/,
use: 'node-loader'
},
{
test: /\.css$/,
2019-07-12 10:53:19 +00:00
use: ['vue-style-loader', 'css-loader']
2018-10-21 09:06:53 +00:00
},
{
test: /\.scss$/,
2019-07-12 10:53:19 +00:00
use: ['vue-style-loader', 'css-loader', 'sass-loader']
2018-10-21 09:06:53 +00:00
},
{
2019-09-11 07:07:44 +00:00
test: /\.(png|svg|jpg|woff|woff2|gif)$/,
2019-07-12 10:53:19 +00:00
use: ['file-loader']
2018-10-21 09:06:53 +00:00
}
]
},
resolve: {
extensions: ['.js', '.vue', '.json', '.css', '.node'],
alias: {
2019-07-12 10:53:19 +00:00
vue$: 'vue/dist/vue.esm.js',
deepmerge$: 'deepmerge/dist/umd.js',
2018-10-21 09:06:53 +00:00
'@': appConfig.dev.srcDir ? resolveAppDir(appConfig.dev.srcDir) : null
}
2018-10-21 09:06:53 +00:00
},
plugins: [
2019-07-12 10:53:19 +00:00
new webpack.DefinePlugin(
Object.assign(
{
'process.env': appConfig.dev.env,
'process.env.NODE_ENV': isProduction
? '"production"'
: '"development"',
'process.env.ELECTRON': JSON.stringify(process.env.ELECTRON)
},
!isProduction
? {
__static: `"${resolveAppDir(appConfig.staticPath).replace(
/\\/g,
'\\\\'
)}"`
}
2019-07-12 10:53:19 +00:00
: {}
)
),
2018-10-21 09:06:53 +00:00
new VueLoaderPlugin(),
new HtmlWebpackPlugin({
template: resolveAppDir(appConfig.dev.entryHtml),
nodeModules: !isProduction
2019-07-12 10:53:19 +00:00
? isMonoRepo
? resolveAppDir('../../node_modules')
: resolveAppDir('./node_modules')
2018-10-21 09:06:53 +00:00
: false
}),
new CaseSensitivePathsWebpackPlugin(),
new webpack.NamedModulesPlugin(),
new webpack.HotModuleReplacementPlugin(),
new FriendlyErrorsWebpackPlugin({
compilationSuccessInfo: {
2019-07-12 10:53:19 +00:00
messages: [
`FrappeJS server started at http://${
appConfig.dev.devServerHost
}:${appConfig.dev.devServerPort}`
]
2019-07-12 10:53:19 +00:00
}
2018-10-21 09:06:53 +00:00
}),
new webpack.ProgressPlugin(),
2019-07-12 10:53:19 +00:00
isProduction
? new CopyWebpackPlugin([
{
from: resolveAppDir(appConfig.staticPath),
to: resolveAppDir('./dist/electron/static'),
ignore: ['.*']
}
])
: null
2018-10-21 09:06:53 +00:00
// isProduction ? new BabiliWebpackPlugin() : null,
// isProduction ? new webpack.LoaderOptionsPlugin({ minimize: true }) : null,
2018-10-21 12:32:40 +00:00
].filter(Boolean),
2018-10-21 09:06:53 +00:00
optimization: {
noEmitOnErrors: false
},
devServer: {
// contentBase: './dist', // dist path is directly configured in express
hot: true,
quiet: true
},
node: {
// prevent webpack from injecting useless setImmediate polyfill because Vue
// source contains it (although only uses it if it's native).
setImmediate: false,
// process is injected via DefinePlugin, although some 3rd party
// libraries may require a mock to work properly (#934)
process: 'mock',
// prevent webpack from injecting mocks to Node native modules
// that does not make sense for the client
dgram: 'empty',
fs: 'empty',
net: 'empty',
tls: 'empty',
child_process: 'empty'
}
2019-07-12 10:53:19 +00:00
};
return config;
2019-07-12 10:53:19 +00:00
};
2018-10-21 09:06:53 +00:00
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(),
2019-07-12 10:53:19 +00:00
isProduction &&
new webpack.DefinePlugin({
'process.env.NODE_ENV': '"production"'
})
2018-10-21 12:32:40 +00:00
].filter(Boolean),
2018-10-21 09:06:53 +00:00
resolve: {
extensions: ['.js', '.json', '.node']
},
target: 'electron-main'
2019-07-12 10:53:19 +00:00
};
};
}
2018-10-21 09:06:53 +00:00
makeConfig();
module.exports = {
getConfig,
getElectronMainConfig
};