forked from vuestorefront/vsf-capybara
-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.js
97 lines (89 loc) · 3.27 KB
/
webpack.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
// You can extend default webpack build here.
// Read more in docs: https://github.com/DivanteLtd/vue-storefront/blob/master/docs/guide/core-themes/webpack.md
const merge = require('webpack-merge');
const themeRoot = require('@vue-storefront/core/build/theme-path');
const filterStream = require('postcss-filter-stream');
/**
* Searches for given plugin name and wraps it with 'postcss-filter-stream' plugin to configure
* paths that must be skipped for requested plugin.
*
* @param {object} loader
* @param {string} pluginName
* @param {string} filter
*/
function addFilterStreamForPostCSSPlugin (loader, pluginName, filter) {
const plugins = loader.options.plugins instanceof Function
? loader.options.plugins()
: loader.options.plugins;
const index = plugins.findIndex(plugin => plugin().postcssPlugin === pluginName);
if (index !== -1) {
plugins[index] = filterStream(filter, plugins[index]());
loader.options.plugins = loader.options.plugins instanceof Function
? () => plugins
: plugins;
}
}
/**
* Traverses webpack's module rules to find all PostCSS loaders. For each PostCSS loader
* the 'postcss-flexbugs-fixes' plugin has to be found and it has to be configured to skip
* @storefront-ui library from being processed.
*
* @param {object} rules
*/
function fixPostCSSPlugins (rules) {
const processedLoaders = [];
rules.forEach(rule => {
const loader = rule.use ? rule.use.find(item => item.loader === 'postcss-loader') : null;
if (loader && !processedLoaders.includes(loader)) {
addFilterStreamForPostCSSPlugin(loader, 'postcss-flexbugs-fixes', '**/@storefront-ui/**');
processedLoaders.push(loader);
}
});
}
module.exports = function (config, { isClient }) {
const clientConfig = isClient ? {
optimization: {
splitChunks: {
chunks: 'all',
cacheGroups: {
// cache groups are handled basing on priority - given module will belong to the cache group with a higher priority
sfui: {
// create 'sfui' group from Storefront UI only
test: /@storefront-ui|@glidejs/,
name: 'sfui',
priority: 2
},
vendorInitial: {
// create 'vendor' group from initial packages from node_modules except Storefront UI
test: /node_modules/,
name: 'vendor-initial',
chunks: 'initial',
priority: 1
},
vendorAsync: {
// create 'vendor' group from async packages from node_modules except Storefront UI
test: /node_modules/,
name: 'vendor-async',
chunks: 'async',
priority: 1
},
searchAdapter: {
// create one 'vsf-search-adapter' group
test: /vsf-search-adapter/,
name: 'vsf-search-adapter',
priority: 1
}
}
}
}
} : {}
const mergedConfig = merge(
// alias for 'src/modules/client' has to be the first one, because it has to be
// handled earlier than already existing aliases in VSF (like general 'src' path)
{ resolve: { alias: { 'src/modules/client': `${themeRoot}/config/modules` } } },
config, // default vsf config
clientConfig
);
fixPostCSSPlugins(mergedConfig.module.rules);
return mergedConfig;
};