-
Notifications
You must be signed in to change notification settings - Fork 0
/
razzle.config.js
113 lines (98 loc) · 3.24 KB
/
razzle.config.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
* Replace with custom razzle config when needed.
* @module razzle.config
*/
const path = require('path');
const fs = require('fs');
const nodeExternals = require('webpack-node-externals');
const projectRootPath = path.resolve('.');
let voltoPath = `${projectRootPath}/node_modules/@plone/volto`;
const AddonConfigurationRegistry = require(`${voltoPath}/addon-registry`);
const defaultVoltoRazzleConfig = require(`${voltoPath}/razzle.config`);
const createAddonsLoader = require(`${voltoPath}/create-addons-loader`);
const { modifyWebpackConfig } = defaultVoltoRazzleConfig;
const packageJson = require(path.join(projectRootPath, 'package.json'));
const registry = new AddonConfigurationRegistry(projectRootPath);
const customModifyWebpackConfig = ({
env: { target, dev },
webpackConfig,
webpackObject,
options,
}) => {
const config = modifyWebpackConfig({
env: { target, dev },
webpackConfig,
webpackObject,
options,
});
// This is a workaround to make sure that swiper package is treated as external
let addonsAsExternals = [];
try {
fs.unlinkSync(config.resolve.alias['load-volto-addons']);
const addonsLoaderPath = createAddonsLoader(
[
...registry
.getAddonDependencies()
.filter((addon) => addon !== '@eeacms/volto-block-style'),
'@eeacms/volto-block-style',
],
registry.getAddons(),
);
config.resolve.alias['load-volto-addons'] = addonsLoaderPath;
} catch {}
const { include } = options.webpackOptions.babelRule;
if (packageJson.name !== '@plone/volto') {
include.push(fs.realpathSync(`${registry.voltoPath}/src`));
}
// Add babel support external (ie. node_modules npm published packages)
const packagesNames = Object.keys(registry.packages);
if (registry.packages && packagesNames.length > 0) {
packagesNames.forEach((addon) => {
const p = fs.realpathSync(registry.packages[addon].modulePath);
if (include.indexOf(p) === -1) {
include.push(p);
}
});
addonsAsExternals = packagesNames.map((addon) => new RegExp(addon));
}
if (process.env.ADDONS) {
addonsFromEnvVar.forEach((addon) => {
const normalizedAddonName = addon.split(':')[0];
const p = fs.realpathSync(
registry.packages[normalizedAddonName].modulePath,
);
if (include.indexOf(p) === -1) {
include.push(p);
}
addonsAsExternals = [
...addonsAsExternals,
...packagesNames.map(
(normalizedAddonName) => new RegExp(normalizedAddonName),
),
];
});
}
config.externals =
target === 'node'
? [
nodeExternals({
allowlist: [
dev ? 'webpack/hot/poll?300' : null,
/\.(eot|woff|woff2|ttf|otf)$/,
/\.(svg|png|jpg|jpeg|gif|ico)$/,
/\.(mp4|mp3|ogg|swf|webp)$/,
/\.(css|scss|sass|sss|less)$/,
// Add support for addons to include externals (ie. node_modules npm published packages)
...addonsAsExternals,
/swiper/,
/^@plone\/volto/,
].filter(Boolean),
}),
]
: [];
return config;
};
module.exports = {
...defaultVoltoRazzleConfig,
modifyWebpackConfig: customModifyWebpackConfig,
};