framework/js-packages/webpack-config/index.js

127 lines
3.3 KiB
JavaScript
Raw Normal View History

const fs = require('fs');
2018-02-24 13:31:59 +08:00
const path = require('path');
const { NormalModuleReplacementPlugin } = require('webpack');
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
const useBundleAnalyzer = process.env.ANALYZER === 'true';
const plugins = [];
/**
* 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}`);
})
);
if (useBundleAnalyzer) {
plugins.push(new (require('webpack-bundle-analyzer').BundleAnalyzerPlugin)());
}
module.exports = function (options = {}) {
return {
// Set up entry points for each of the forum + admin apps, but only
// if they exist.
entry: getEntryPoints(),
2018-06-16 12:19:34 +08:00
plugins,
resolve: {
extensions: ['.ts', '.tsx', '.js', '.jsx', '.json'],
},
2018-02-24 13:31:59 +08:00
module: {
rules: [
{
// Matches .js, .jsx, .ts, .tsx
// See: https://regexr.com/5snjd
test: /\.[jt]sx?$/,
loader: require.resolve('babel-loader'),
options: require('./babel.config'),
resolve: {
fullySpecified: false,
},
},
],
2018-02-24 13:59:19 +08:00
},
output: {
2018-06-16 12:22:07 +08:00
path: path.resolve(process.cwd(), 'dist'),
library: 'module.exports',
libraryTarget: 'assign',
devtoolNamespace: require(path.resolve(process.cwd(), 'package.json')).name,
},
externals: [
{
2018-06-16 12:23:47 +08:00
'@flarum/core/forum': 'flarum.core',
'@flarum/core/admin': 'flarum.core',
jquery: 'jQuery',
},
(function () {
2018-06-16 13:04:38 +08:00
const externals = {};
if (options.useExtensions) {
for (const extension of options.useExtensions) {
externals['@' + extension] =
externals['@' + extension + '/forum'] =
externals['@' + extension + '/admin'] =
"flarum.extensions['" + extension + "']";
2018-06-16 13:04:38 +08:00
}
}
return externals;
})(),
2018-06-16 13:04:38 +08:00
// Support importing old-style core modules.
function ({ request }, callback) {
2018-02-24 13:31:59 +08:00
let matches;
if ((matches = /^flarum\/(.+)$/.exec(request))) {
return callback(null, "root flarum.core.compat['" + matches[1] + "']");
2018-02-24 13:31:59 +08:00
}
callback();
},
],
2018-02-24 13:31:59 +08:00
devtool: 'source-map',
};
2018-02-24 13:31:59 +08:00
};