2018-06-03 12:48:41 +08:00
|
|
|
const fs = require('fs');
|
2018-02-24 13:31:59 +08:00
|
|
|
const path = require('path');
|
2021-11-09 07:53:18 +08:00
|
|
|
const { NormalModuleReplacementPlugin } = require('webpack');
|
2021-05-12 08:28:31 +08:00
|
|
|
|
|
|
|
const entryPointNames = ['forum', 'admin'];
|
|
|
|
const entryPointExts = ['js', 'ts'];
|
|
|
|
|
|
|
|
function getEntryPoints() {
|
|
|
|
const entries = {};
|
|
|
|
|
|
|
|
appLoop: for (const app of entryPointNames) {
|
|
|
|
for (const ext of entryPointExts) {
|
|
|
|
const file = path.resolve(process.cwd(), `${app}.${ext}`);
|
|
|
|
|
|
|
|
if (fs.existsSync(file)) {
|
|
|
|
entries[app] = file;
|
|
|
|
continue appLoop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Object.keys(entries).length === 0) {
|
|
|
|
console.error('ERROR: No JS entrypoints could be found.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return entries;
|
|
|
|
}
|
2018-02-24 13:31:59 +08:00
|
|
|
|
2021-05-13 07:05:27 +08:00
|
|
|
const useBundleAnalyzer = process.env.ANALYZER === 'true';
|
|
|
|
const plugins = [];
|
|
|
|
|
2021-11-09 07:53:18 +08:00
|
|
|
/**
|
|
|
|
* Yarn Plug'n'Play means that dependency hoisting doesn't work like it normally
|
|
|
|
* would with the standard `node_modules` configuration. This is by design, as
|
|
|
|
* hoisting is unpredictable.
|
|
|
|
*
|
|
|
|
* This plugin works around this by ensuring references to `@babel/runtime` (which
|
|
|
|
* is required at build-time from an extension/core's scope) are redirected to the
|
|
|
|
* copy of `@babel/runtime` which is a dependency of this package.
|
|
|
|
*
|
|
|
|
* This removes the need for hoisting, and allows for Plyug'n'Play compatibility.
|
|
|
|
*
|
|
|
|
* Thanks goes to Yarn's lead maintainer @arcanis for helping me get to this
|
|
|
|
* solution.
|
|
|
|
*/
|
|
|
|
plugins.push(
|
|
|
|
new NormalModuleReplacementPlugin(/^@babel\/runtime(.*)/, (resource) => {
|
|
|
|
const path = resource.request.split('@babel/runtime')[1];
|
|
|
|
|
|
|
|
resource.request = require.resolve(`@babel/runtime${path}`);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2021-05-13 07:05:27 +08:00
|
|
|
if (useBundleAnalyzer) {
|
|
|
|
plugins.push(new (require('webpack-bundle-analyzer').BundleAnalyzerPlugin)());
|
|
|
|
}
|
|
|
|
|
2021-05-04 07:39:10 +08:00
|
|
|
module.exports = function (options = {}) {
|
2018-06-03 12:48:41 +08:00
|
|
|
return {
|
|
|
|
// Set up entry points for each of the forum + admin apps, but only
|
|
|
|
// if they exist.
|
2021-05-12 08:28:31 +08:00
|
|
|
entry: getEntryPoints(),
|
2018-06-16 12:19:34 +08:00
|
|
|
|
2021-05-13 07:05:27 +08:00
|
|
|
plugins,
|
|
|
|
|
2021-05-12 08:28:31 +08:00
|
|
|
resolve: {
|
|
|
|
extensions: ['.ts', '.tsx', '.js', '.jsx', '.json'],
|
|
|
|
},
|
2018-06-03 12:48:41 +08:00
|
|
|
|
2018-02-24 13:31:59 +08:00
|
|
|
module: {
|
|
|
|
rules: [
|
|
|
|
{
|
2021-05-12 08:28:31 +08:00
|
|
|
// Matches .js, .jsx, .ts, .tsx
|
|
|
|
// See: https://regexr.com/5snjd
|
2023-02-09 05:02:40 +08:00
|
|
|
test: /\.[jt]sx?$/,
|
2021-11-09 07:53:18 +08:00
|
|
|
loader: require.resolve('babel-loader'),
|
2023-02-09 05:02:40 +08:00
|
|
|
options: require('./babel.config'),
|
|
|
|
resolve: {
|
|
|
|
fullySpecified: false,
|
2021-05-04 07:39:10 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2018-02-24 13:59:19 +08:00
|
|
|
},
|
|
|
|
|
2018-06-03 12:48:41 +08:00
|
|
|
output: {
|
2018-06-16 12:22:07 +08:00
|
|
|
path: path.resolve(process.cwd(), 'dist'),
|
2018-06-16 12:22:53 +08:00
|
|
|
library: 'module.exports',
|
2018-08-25 18:12:08 +08:00
|
|
|
libraryTarget: 'assign',
|
2021-05-04 07:39:10 +08:00
|
|
|
devtoolNamespace: require(path.resolve(process.cwd(), 'package.json')).name,
|
2018-06-03 12:48:41 +08:00
|
|
|
},
|
|
|
|
|
2018-02-24 14:33:09 +08:00
|
|
|
externals: [
|
2018-06-03 12:48:41 +08:00
|
|
|
{
|
2018-06-16 12:23:47 +08:00
|
|
|
'@flarum/core/forum': 'flarum.core',
|
|
|
|
'@flarum/core/admin': 'flarum.core',
|
2021-05-04 07:39:10 +08:00
|
|
|
jquery: 'jQuery',
|
2018-06-03 12:48:41 +08:00
|
|
|
},
|
|
|
|
|
2021-05-04 07:39:10 +08:00
|
|
|
(function () {
|
2018-06-16 13:04:38 +08:00
|
|
|
const externals = {};
|
|
|
|
|
|
|
|
if (options.useExtensions) {
|
|
|
|
for (const extension of options.useExtensions) {
|
2021-05-13 07:05:27 +08:00
|
|
|
externals['@' + extension] =
|
|
|
|
externals['@' + extension + '/forum'] =
|
|
|
|
externals['@' + extension + '/admin'] =
|
|
|
|
"flarum.extensions['" + extension + "']";
|
2018-06-16 13:04:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return externals;
|
2021-05-04 07:39:10 +08:00
|
|
|
})(),
|
2018-06-16 13:04:38 +08:00
|
|
|
|
2018-06-03 12:48:41 +08:00
|
|
|
// Support importing old-style core modules.
|
2021-11-09 07:53:18 +08:00
|
|
|
function ({ request }, callback) {
|
2018-02-24 13:31:59 +08:00
|
|
|
let matches;
|
2018-02-24 14:33:09 +08:00
|
|
|
if ((matches = /^flarum\/(.+)$/.exec(request))) {
|
2021-05-04 07:39:10 +08:00
|
|
|
return callback(null, "root flarum.core.compat['" + matches[1] + "']");
|
2018-02-24 13:31:59 +08:00
|
|
|
}
|
|
|
|
callback();
|
2021-05-04 07:39:10 +08:00
|
|
|
},
|
2018-06-03 12:48:41 +08:00
|
|
|
],
|
2018-02-24 13:31:59 +08:00
|
|
|
|
2021-05-04 07:39:10 +08:00
|
|
|
devtool: 'source-map',
|
2018-06-03 12:48:41 +08:00
|
|
|
};
|
2018-02-24 13:31:59 +08:00
|
|
|
};
|