-
Notifications
You must be signed in to change notification settings - Fork 3
/
webpack.config.js
152 lines (137 loc) · 4.44 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const path = require('path');
const fs = require('fs');
const BrowserSyncPlugin = require('browser-sync-webpack-plugin');
const globImporter = require('node-sass-glob-importer');
const defaultConfig = require('@wordpress/scripts/config/webpack.config');
/**
* Resolves paths to absolute paths recursively. The input can be a string, an array, or an object containing paths.
* If the input is a string, it will resolve the single path.
* If the input is an array, it will resolve all paths within the array.
* If the input is an object, it will resolve all paths within the object, maintaining the same keys.
*
* @param {string|string[]|Object.<string, string>} paths - The path or paths to resolve.
* @returns {string|string[]|Object.<string, string>} The absolute path or paths.
*/
function resolvePathsRecursively(paths) {
if (typeof paths === 'string') {
return path.resolve(__dirname, paths);
} else if (Array.isArray(paths)) {
return paths.map(resolvePathsRecursively);
} else if (Object(paths) === paths) {
const resolvedPaths = {};
Object.keys(paths).forEach(key => {
resolvedPaths[key] = resolvePathsRecursively(paths[key]);
});
return resolvedPaths;
}
}
/**
* Finds the first file that matches the given pattern inside the .ddev folder,
* starting from the current working directory and traversing up the directory tree.
* The pattern can include a wildcard, such as 'traefik/certs/*.key'.
*
* @param {string} pattern - The relative path pattern to the target file inside the .ddev folder.
* @returns {string|null} The absolute path to the first file that matches the pattern, or null if no matching file is found.
*/
function findDdevFile(pattern) {
// Start from the current working directory
let currentDir = process.cwd();
// Separate pattern into directory and file part
const patternDir = path.dirname(pattern);
const patternFile = path.basename(pattern);
const isWildcard = patternFile.includes('*');
// Traverse up the directory tree to find the .ddev folder
while (currentDir !== path.dirname(currentDir)) {
const ddevPath = path.join(currentDir, '.ddev', patternDir);
// Check if the target directory exists inside the .ddev folder
if (fs.existsSync(ddevPath)) {
const files = fs.readdirSync(ddevPath);
// If wildcard, find the first matching file
if (isWildcard) {
const regex = new RegExp(`^${patternFile.replace('*', '.*')}$`);
const matchingFile = files.find((file) => regex.test(file));
if (matchingFile) {
return path.join(ddevPath, matchingFile);
}
} else if (files.includes(patternFile)) {
// If not wildcard, return the exact file if it exists
return path.join(ddevPath, patternFile);
}
}
// Move up to the parent directory
currentDir = path.dirname(currentDir);
}
// If the target file is not found, return null
return null;
}
module.exports = (env) => {
const config = {
entry: {
'wpify-custom-fields': [
'./assets/wpify-custom-fields.js',
'./assets/wpify-custom-fields.scss',
],
'wpify-custom-blocks': [
'./assets/wpify-custom-blocks.js',
'./assets/wpify-custom-fields.scss',
],
'int-tel-input-utils': './node_modules/intl-tel-input/build/js/utils.js',
},
output: {
path: 'build',
filename: '[name].[contenthash:16].js',
chunkFilename: '[name].[chunkhash:16].js',
},
browsersync: {
files: [
'build/**/*.css',
'build/**/*.js',
'build/**/*.svg',
'src/**/*.php',
],
},
};
const key = findDdevFile('traefik/certs/*.key');
const cert = findDdevFile('traefik/certs/*.crt');
return {
...defaultConfig,
entry: resolvePathsRecursively(config.entry),
output: {
...defaultConfig.output,
path: resolvePathsRecursively(config.output.path),
},
resolve: {
...defaultConfig.resolve,
...config.resolve,
extensions: ['.js', '.json', '.jsx'],
},
module: {
...defaultConfig.module,
rules: [
...defaultConfig.module.rules.map((rule) => {
if (rule.test.test('.scss')) {
rule.use.forEach(use => {
if (use.loader === require.resolve('sass-loader')) {
use.options.sassOptions = {
...(use.options.sassOptions || null),
importer: globImporter(),
};
}
});
}
return rule;
}),
],
},
plugins: [
...defaultConfig.plugins,
env.WEBPACK_WATCH && config.browsersync && new BrowserSyncPlugin({
...config.browsersync,
https: { key, cert },
}, {
injectCss: true,
reload: true
}),
].filter(Boolean),
};
};