generated from lowdefy/blocks-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
webpack.common.js
91 lines (87 loc) · 2.53 KB
/
webpack.common.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const { ModuleFederationPlugin } = require('webpack').container;
const path = require('path');
const fs = require('fs');
const packageJson = require('./package.json');
const sanitizeName = (name) => {
return name
.replace(/@/g, '_at_')
.replace(/\//g, '_slash_')
.replace(/-/g, '_dash_')
.replace(/^[a-zA-Z0-9_]/g, '_');
};
// Get all directories in ./src/blocks folder and create module definition for ModuleFederation
const getDirectories = (srcPath) =>
fs.readdirSync(srcPath).filter((file) => fs.statSync(path.join(srcPath, file)).isDirectory());
const blockModules = () => {
const blocks = getDirectories('./src/blocks');
const modules = {};
blocks.forEach((block) => {
modules[`./${block}`] = `./src/blocks/${block}/${block}.js`;
// modules[`./${block}/meta`] = `./src/blocks/${block}/${block}.json`;
});
return modules;
};
module.exports = {
output: {
path: path.resolve(__dirname, 'dist'),
},
resolve: { fallback: { buffer: require.resolve('buffer/') } },
module: {
rules: [
// TODO: FIXME: do NOT webpack 5 support with this
// x-ref: https://github.com/webpack/webpack/issues/11467
// waiting for babel fix: https://github.com/vercel/next.js/pull/17095#issuecomment-692435147
{
test: /\.m?js/,
resolve: {
fullySpecified: false,
},
},
{
test: /\.jsx?$/,
loader: 'babel-loader',
exclude: /node_modules/,
options: {
presets: ['@babel/preset-react'],
},
},
{
test: /\.ya?ml$/,
type: 'json',
use: 'yaml-loader',
},
{
test: /\.css$/,
use: [
{
loader: 'style-loader',
},
{
loader: 'css-loader', // translates CSS into CommonJS
},
],
},
],
},
plugins: [
new ModuleFederationPlugin({
name: sanitizeName(packageJson.name),
library: { type: 'var', name: sanitizeName(packageJson.name) },
filename: 'remoteEntry.js',
exposes: blockModules(),
shared: {
...packageJson.dependencies,
react: {
singleton: true, // only a single version of the shared module is allowed
requiredVersion: '~17.0.0',
version: packageJson.dependencies.react,
},
'react-dom': {
singleton: true, // only a single version of the shared module is allowed
requiredVersion: '~17.0.0',
version: packageJson.dependencies['react-dom'],
},
},
}),
],
};