2021-07-29 20:08:39 +02:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
|
|
// See LICENSE.txt for license information.
|
2021-05-24 19:06:11 +02:00
|
|
|
const exec = require('child_process').exec;
|
|
|
|
|
|
|
|
const path = require('path');
|
|
|
|
|
2021-10-22 18:13:31 +02:00
|
|
|
const webpack = require('webpack');
|
2022-08-17 22:13:35 +02:00
|
|
|
const {ModuleFederationPlugin} = require('webpack').container;
|
2021-10-22 18:13:31 +02:00
|
|
|
|
2021-07-29 20:08:39 +02:00
|
|
|
const tsTransformer = require('@formatjs/ts-transformer');
|
|
|
|
|
2021-05-24 19:06:11 +02:00
|
|
|
const PLUGIN_ID = require('../plugin.json').id;
|
|
|
|
|
|
|
|
const NPM_TARGET = process.env.npm_lifecycle_event; //eslint-disable-line no-process-env
|
2022-10-13 22:56:59 +02:00
|
|
|
const TARGET_IS_PRODUCT = NPM_TARGET?.endsWith(':product');
|
2022-08-17 22:13:35 +02:00
|
|
|
|
2021-05-24 19:06:11 +02:00
|
|
|
let mode = 'production';
|
2021-07-29 20:08:39 +02:00
|
|
|
let devtool;
|
2021-08-10 11:46:09 +02:00
|
|
|
const plugins = [];
|
2022-08-17 22:13:35 +02:00
|
|
|
if (NPM_TARGET === 'debug' || NPM_TARGET === 'debug:watch' || NPM_TARGET === 'start:product') {
|
2021-05-24 19:06:11 +02:00
|
|
|
mode = 'development';
|
|
|
|
devtool = 'source-map';
|
2021-08-10 11:46:09 +02:00
|
|
|
plugins.push(
|
|
|
|
new webpack.DefinePlugin({
|
|
|
|
'process.env.NODE_ENV': JSON.stringify('development'),
|
|
|
|
}),
|
|
|
|
);
|
2021-05-24 19:06:11 +02:00
|
|
|
}
|
|
|
|
|
2021-10-04 10:06:54 +02:00
|
|
|
if (NPM_TARGET === 'build:watch' || NPM_TARGET === 'debug:watch' || NPM_TARGET === 'live-watch') {
|
2021-05-24 19:06:11 +02:00
|
|
|
plugins.push({
|
|
|
|
apply: (compiler) => {
|
|
|
|
compiler.hooks.watchRun.tap('WatchStartPlugin', () => {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.log('Change detected. Rebuilding webapp.');
|
|
|
|
});
|
|
|
|
compiler.hooks.afterEmit.tap('AfterEmitPlugin', () => {
|
2021-10-04 10:06:54 +02:00
|
|
|
let command = 'cd .. && make deploy-from-watch';
|
|
|
|
if (NPM_TARGET === 'live-watch') {
|
|
|
|
command = 'cd .. && make deploy-to-mattermost-directory';
|
|
|
|
}
|
|
|
|
exec(command, (err, stdout, stderr) => {
|
2021-05-24 19:06:11 +02:00
|
|
|
if (stdout) {
|
|
|
|
process.stdout.write(stdout);
|
|
|
|
}
|
|
|
|
if (stderr) {
|
|
|
|
process.stderr.write(stderr);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-08-17 22:13:35 +02:00
|
|
|
const config = {
|
|
|
|
entry: TARGET_IS_PRODUCT ? './src/remote_entry.ts' : './src/plugin_entry.ts',
|
2021-05-24 19:06:11 +02:00
|
|
|
resolve: {
|
|
|
|
modules: [
|
|
|
|
'src',
|
|
|
|
'node_modules',
|
|
|
|
path.resolve(__dirname),
|
|
|
|
],
|
2021-08-11 19:55:41 +02:00
|
|
|
alias: {
|
|
|
|
moment: path.resolve(__dirname, '../../webapp/node_modules/moment/'),
|
|
|
|
},
|
2021-05-24 19:06:11 +02:00
|
|
|
extensions: ['*', '.js', '.jsx', '.ts', '.tsx'],
|
|
|
|
},
|
|
|
|
module: {
|
|
|
|
rules: [
|
|
|
|
{
|
2021-07-29 20:08:39 +02:00
|
|
|
test: /\.tsx?$/,
|
2021-05-24 19:06:11 +02:00
|
|
|
use: {
|
2021-07-29 20:08:39 +02:00
|
|
|
loader: 'ts-loader',
|
2021-05-24 19:06:11 +02:00
|
|
|
options: {
|
2021-07-29 20:08:39 +02:00
|
|
|
getCustomTransformers: {
|
|
|
|
before: [
|
|
|
|
tsTransformer.transform({
|
|
|
|
overrideIdFn: '[sha512:contenthash:base64:6]',
|
|
|
|
ast: true,
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
},
|
2021-05-24 19:06:11 +02:00
|
|
|
},
|
|
|
|
},
|
2021-07-29 20:08:39 +02:00
|
|
|
exclude: [/node_modules/],
|
|
|
|
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.html$/,
|
|
|
|
type: 'asset/resource',
|
2021-05-24 19:06:11 +02:00
|
|
|
},
|
|
|
|
{
|
2021-07-29 20:08:39 +02:00
|
|
|
test: /\.s[ac]ss$/i,
|
2021-05-24 19:06:11 +02:00
|
|
|
use: [
|
|
|
|
'style-loader',
|
2021-07-29 20:08:39 +02:00
|
|
|
'css-loader',
|
|
|
|
'sass-loader',
|
2021-08-25 13:05:12 +02:00
|
|
|
path.resolve(__dirname, 'loaders/globalScssClassLoader'),
|
2021-07-29 20:08:39 +02:00
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.css$/i,
|
|
|
|
use: [
|
|
|
|
'style-loader',
|
|
|
|
'css-loader',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.(tsx?|js|jsx|mjs|html)$/,
|
|
|
|
use: [
|
|
|
|
],
|
|
|
|
exclude: [/node_modules/],
|
|
|
|
},
|
|
|
|
{
|
2022-02-28 13:28:16 +02:00
|
|
|
test: /\.(png|eot|tiff|svg|woff2|woff|ttf|jpg|gif)$/,
|
2022-03-26 01:07:49 +02:00
|
|
|
type: 'asset/resource',
|
|
|
|
generator: {
|
2022-09-08 00:20:42 +02:00
|
|
|
filename: '[name][ext]',
|
2022-10-19 22:41:53 +02:00
|
|
|
publicPath: TARGET_IS_PRODUCT ? '/static/products/boards/' : '/static/',
|
2022-03-26 01:07:49 +02:00
|
|
|
}
|
2021-05-24 19:06:11 +02:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2022-08-17 22:13:35 +02:00
|
|
|
devtool,
|
|
|
|
mode,
|
|
|
|
plugins,
|
|
|
|
};
|
2022-07-07 16:46:53 +02:00
|
|
|
|
2022-08-17 22:13:35 +02:00
|
|
|
if (TARGET_IS_PRODUCT) {
|
2022-09-07 21:03:14 +02:00
|
|
|
// Set up module federation
|
2022-08-17 22:13:35 +02:00
|
|
|
function makeSingletonSharedModules(packageNames) {
|
|
|
|
const sharedObject = {};
|
|
|
|
|
|
|
|
for (const packageName of packageNames) {
|
2022-09-08 22:41:50 +02:00
|
|
|
// Set both versions to false so that the version of this module provided by the web app will be used
|
2022-08-17 22:13:35 +02:00
|
|
|
sharedObject[packageName] = {
|
2022-09-08 22:41:50 +02:00
|
|
|
requiredVersion: false,
|
2022-08-17 22:13:35 +02:00
|
|
|
singleton: true,
|
2022-09-08 22:41:50 +02:00
|
|
|
version: false,
|
2022-08-17 22:13:35 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return sharedObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
config.plugins.push(new ModuleFederationPlugin({
|
2022-10-13 22:56:42 +02:00
|
|
|
name: 'boards',
|
2022-08-17 22:13:35 +02:00
|
|
|
filename: 'remote_entry.js',
|
|
|
|
exposes: {
|
|
|
|
'.': './src/index',
|
|
|
|
|
|
|
|
// This probably won't need to be exposed in the long run, but its a POC for exposing multiple modules
|
|
|
|
'./manifest': './src/manifest',
|
|
|
|
},
|
|
|
|
shared: [
|
|
|
|
'@mattermost/client',
|
|
|
|
'prop-types',
|
|
|
|
|
|
|
|
makeSingletonSharedModules([
|
|
|
|
'react',
|
|
|
|
'react-dom',
|
|
|
|
'react-intl',
|
|
|
|
'react-redux',
|
|
|
|
'react-router-dom',
|
|
|
|
]),
|
|
|
|
],
|
|
|
|
}));
|
|
|
|
|
|
|
|
config.plugins.push(new webpack.DefinePlugin({
|
|
|
|
'process.env.TARGET_IS_PRODUCT': TARGET_IS_PRODUCT, // TODO We might want a better name for this
|
|
|
|
}));
|
2022-10-13 22:56:59 +02:00
|
|
|
|
|
|
|
config.output = {
|
|
|
|
path: path.join(__dirname, '/dist'),
|
|
|
|
};
|
2022-08-17 22:13:35 +02:00
|
|
|
} else {
|
|
|
|
config.resolve.alias['react-intl'] = path.resolve(__dirname, '../../webapp/node_modules/react-intl/');
|
|
|
|
|
2022-09-07 21:03:14 +02:00
|
|
|
config.externals = {
|
|
|
|
react: 'React',
|
2022-10-03 17:39:12 +02:00
|
|
|
'react-dom': 'ReactDOM',
|
2022-09-07 21:03:14 +02:00
|
|
|
redux: 'Redux',
|
|
|
|
'react-redux': 'ReactRedux',
|
|
|
|
'mm-react-router-dom': 'ReactRouterDom',
|
|
|
|
'prop-types': 'PropTypes',
|
|
|
|
'react-bootstrap': 'ReactBootstrap',
|
|
|
|
};
|
|
|
|
|
|
|
|
config.output = {
|
2021-05-24 19:06:11 +02:00
|
|
|
devtoolNamespace: PLUGIN_ID,
|
|
|
|
path: path.join(__dirname, '/dist'),
|
|
|
|
publicPath: '/',
|
|
|
|
filename: 'main.js',
|
2022-08-17 22:13:35 +02:00
|
|
|
};
|
|
|
|
}
|
2021-09-01 23:53:27 +02:00
|
|
|
|
|
|
|
const env = {};
|
|
|
|
env.RUDDER_KEY = JSON.stringify(process.env.RUDDER_KEY || ''); //eslint-disable-line no-process-env
|
|
|
|
env.RUDDER_DATAPLANE_URL = JSON.stringify(process.env.RUDDER_DATAPLANE_URL || ''); //eslint-disable-line no-process-env
|
|
|
|
|
2022-08-17 22:13:35 +02:00
|
|
|
config.plugins.push(new webpack.DefinePlugin({
|
2021-09-01 23:53:27 +02:00
|
|
|
'process.env': env,
|
|
|
|
}));
|
2022-08-17 22:13:35 +02:00
|
|
|
|
|
|
|
if (NPM_TARGET === 'start:product') {
|
2022-10-19 22:41:53 +02:00
|
|
|
const url = new URL('http://localhost:9006');
|
|
|
|
|
|
|
|
for (const rule of config.module.rules) {
|
|
|
|
if (rule.type === 'asset/resource' && rule.generator) {
|
|
|
|
rule.generator.publicPath = url.toString() + 'static/';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 22:13:35 +02:00
|
|
|
config.devServer = {
|
2022-10-19 22:41:53 +02:00
|
|
|
host: url.hostname,
|
|
|
|
port: url.port,
|
2022-08-17 22:13:35 +02:00
|
|
|
devMiddleware: {
|
|
|
|
writeToDisk: false,
|
|
|
|
},
|
2022-09-08 00:20:42 +02:00
|
|
|
static: {
|
|
|
|
directory: path.join(__dirname, '../../webapp/static'),
|
|
|
|
publicPath: '/static',
|
|
|
|
},
|
2022-08-17 22:13:35 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = config;
|