2019-01-08 17:35:50 -05:00
|
|
|
const childProcess = require('child_process');
|
2020-08-11 22:14:39 -04:00
|
|
|
const fs = require('fs');
|
2020-08-11 19:21:14 -04:00
|
|
|
const path = require('path');
|
2020-08-11 22:14:39 -04:00
|
|
|
const util = require('util');
|
2019-01-08 17:35:50 -05:00
|
|
|
|
|
|
|
const electronPath = require('electron');
|
2018-12-27 18:29:57 -05:00
|
|
|
const webpack = require('webpack');
|
2020-08-11 19:22:13 -04:00
|
|
|
const merge = require('webpack-merge');
|
|
|
|
|
|
|
|
// PostCss
|
|
|
|
const autoprefixer = require('autoprefixer');
|
|
|
|
const postcssImport = require('postcss-import');
|
2023-04-07 14:24:05 -04:00
|
|
|
const postcssVars = require('postcss-simple-vars');
|
2018-12-27 18:29:57 -05:00
|
|
|
|
|
|
|
const isProduction = (process.env.NODE_ENV === 'production');
|
|
|
|
|
2019-01-08 17:35:50 -05:00
|
|
|
const electronVersion = childProcess.execSync(`${electronPath} --version`, {encoding: 'utf8'}).trim();
|
2019-08-05 18:19:48 -04:00
|
|
|
console.log(`Targeting Electron ${electronVersion}`); // eslint-disable-line no-console
|
2019-01-08 17:35:50 -05:00
|
|
|
|
2020-08-11 19:22:13 -04:00
|
|
|
const makeConfig = function (defaultConfig, options) {
|
2018-12-27 18:29:57 -05:00
|
|
|
const babelOptions = {
|
|
|
|
// Explicitly disable babelrc so we don't catch various config in much lower dependencies.
|
|
|
|
babelrc: false,
|
|
|
|
plugins: [
|
|
|
|
'@babel/plugin-syntax-dynamic-import',
|
|
|
|
'@babel/plugin-transform-async-to-generator',
|
2023-07-24 10:18:24 -04:00
|
|
|
'@babel/plugin-proposal-object-rest-spread',
|
|
|
|
'@babel/plugin-transform-nullish-coalescing-operator',
|
|
|
|
'@babel/plugin-transform-optional-chaining'
|
2018-12-27 18:29:57 -05:00
|
|
|
],
|
|
|
|
presets: [
|
2019-01-08 17:35:50 -05:00
|
|
|
['@babel/preset-env', {targets: {electron: electronVersion}}]
|
2018-12-27 18:29:57 -05:00
|
|
|
]
|
|
|
|
};
|
|
|
|
|
|
|
|
const sourceFileTest = options.useReact ? /\.jsx?$/ : /\.js$/;
|
|
|
|
if (options.useReact) {
|
|
|
|
babelOptions.presets = babelOptions.presets.concat('@babel/preset-react');
|
|
|
|
babelOptions.plugins.push(['react-intl', {
|
|
|
|
messagesDir: './translations/messages/'
|
|
|
|
}]);
|
|
|
|
}
|
|
|
|
|
2020-08-11 19:22:13 -04:00
|
|
|
// TODO: consider adjusting these rules instead of discarding them in at least some cases
|
|
|
|
if (options.disableDefaultRulesForExtensions) {
|
|
|
|
defaultConfig.module.rules = defaultConfig.module.rules.filter(rule => {
|
|
|
|
if (!(rule.test instanceof RegExp)) {
|
|
|
|
// currently we don't support overriding other kinds of rules
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// disable default rules for any file extension listed here
|
|
|
|
// we will handle these files in some other way (see below)
|
|
|
|
// OR we want to avoid any processing at all (such as with fonts)
|
|
|
|
const shouldDisable = options.disableDefaultRulesForExtensions.some(
|
|
|
|
ext => rule.test.test(`test.${ext}`)
|
|
|
|
);
|
2020-08-11 19:21:14 -04:00
|
|
|
const statusWord = shouldDisable ? 'Discarding' : 'Keeping';
|
|
|
|
console.log(`${options.name}: ${statusWord} electron-webpack default rule for ${rule.test}`);
|
2020-08-11 19:22:13 -04:00
|
|
|
return !shouldDisable;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-11 22:14:39 -04:00
|
|
|
const config = merge.smart(defaultConfig, {
|
2018-12-27 18:29:57 -05:00
|
|
|
devtool: 'cheap-module-eval-source-map',
|
|
|
|
mode: isProduction ? 'production' : 'development',
|
|
|
|
module: {
|
|
|
|
rules: [
|
|
|
|
{
|
|
|
|
test: sourceFileTest,
|
|
|
|
include: options.babelPaths,
|
|
|
|
loader: 'babel-loader',
|
|
|
|
options: babelOptions
|
|
|
|
},
|
2020-08-11 19:22:13 -04:00
|
|
|
{ // coped from scratch-gui
|
|
|
|
test: /\.css$/,
|
|
|
|
use: [{
|
|
|
|
loader: 'style-loader'
|
|
|
|
}, {
|
|
|
|
loader: 'css-loader',
|
|
|
|
options: {
|
|
|
|
modules: true,
|
|
|
|
importLoaders: 1,
|
|
|
|
localIdentName: '[name]_[local]_[hash:base64:5]',
|
|
|
|
camelCase: true
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
loader: 'postcss-loader',
|
|
|
|
options: {
|
|
|
|
ident: 'postcss',
|
|
|
|
plugins: function () {
|
|
|
|
return [
|
|
|
|
postcssImport,
|
|
|
|
postcssVars,
|
|
|
|
autoprefixer
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.(svg|png|wav|gif|jpg)$/,
|
|
|
|
loader: 'file-loader',
|
|
|
|
options: {
|
|
|
|
outputPath: 'static/assets/'
|
|
|
|
}
|
2023-07-24 10:18:24 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.hex$/,
|
|
|
|
use: [{
|
|
|
|
loader: 'url-loader',
|
|
|
|
options: {
|
|
|
|
limit: 16 * 1024
|
|
|
|
}
|
|
|
|
}]
|
2018-12-27 18:29:57 -05:00
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
plugins: [
|
|
|
|
new webpack.SourceMapDevToolPlugin({
|
|
|
|
filename: '[file].map'
|
|
|
|
})
|
|
|
|
].concat(options.plugins || []),
|
|
|
|
resolve: {
|
|
|
|
cacheWithContext: false,
|
2020-08-11 19:21:14 -04:00
|
|
|
symlinks: false,
|
|
|
|
alias: {
|
|
|
|
// act like scratch-gui has this line in its package.json:
|
|
|
|
// "browser": "./src/index.js"
|
|
|
|
'scratch-gui$': path.resolve(__dirname, 'node_modules', 'scratch-gui', 'src', 'index.js')
|
|
|
|
}
|
2018-12-27 18:29:57 -05:00
|
|
|
}
|
2020-08-11 19:22:13 -04:00
|
|
|
});
|
2020-08-11 22:14:39 -04:00
|
|
|
|
2020-08-14 16:48:05 -04:00
|
|
|
// If we're not on CI, enable Webpack progress output
|
|
|
|
// Note that electron-webpack enables this by default, so use '--no-progress' to avoid double-adding this plugin
|
|
|
|
if (!process.env.CI) {
|
|
|
|
config.plugins.push(new webpack.ProgressPlugin());
|
|
|
|
}
|
|
|
|
|
2020-08-11 22:14:39 -04:00
|
|
|
fs.writeFileSync(
|
|
|
|
`dist/webpack.${options.name}.js`,
|
|
|
|
`module.exports = ${util.inspect(config, {depth: null})};\n`
|
|
|
|
);
|
|
|
|
|
|
|
|
return config;
|
2018-12-27 18:29:57 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = makeConfig;
|