-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.js
More file actions
207 lines (184 loc) · 7.02 KB
/
Copy pathwebpack.config.js
File metadata and controls
207 lines (184 loc) · 7.02 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
const defaultConfig = require('@wordpress/scripts/config/webpack.config');
const path = require('path');
const fs = require('fs');
const CopyPlugin = require('copy-webpack-plugin');
// When HANDOFF_CONTENT_DIR env var is set (Composer installs, `wp handoff build`),
// block sources and build output point to the external content directory.
// When unset, everything stays within the plugin root (development / self-contained mode).
const contentDir = process.env.HANDOFF_CONTENT_DIR || 'wp-content/handoff';
const isExternalContent = contentDir !== __dirname;
const blocksDir = path.resolve(contentDir, 'blocks');
const blockFolders = fs.existsSync(blocksDir)
? fs.readdirSync(blocksDir).filter(file => {
const blockPath = path.join(blocksDir, file);
return fs.statSync(blockPath).isDirectory() &&
fs.existsSync(path.join(blockPath, 'index.js'));
})
: [];
if (blockFolders.length === 0 && !isExternalContent) {
console.log('\n⚠️ No blocks found in blocks/');
console.log(' Run "npm run compile:all" first to generate blocks from Handoff.\n');
}
const entry = {};
blockFolders.forEach(block => {
entry[`${block}/index`] = path.resolve(blocksDir, block, 'index.js');
const viewJs = path.join(blocksDir, block, 'view.js');
if (fs.existsSync(viewJs)) {
entry[`${block}/view`] = viewJs;
}
});
blockFolders.forEach(block => {
const variationsDir = path.join(blocksDir, block, 'variations');
if (fs.existsSync(variationsDir)) {
const variationFiles = fs.readdirSync(variationsDir).filter(f => f.endsWith('.js'));
variationFiles.forEach(file => {
const name = file.replace(/\.js$/, '');
entry[`${block}/variations/${name}`] = path.join(variationsDir, file);
});
}
});
// Admin dashboard entry — always included. When building with an external
// content dir the output lands in contentDir/build/admin/ and is then copied
// back to the plugin's own build/admin/ (where PHP expects it).
const adminEntry = path.resolve(__dirname, 'src/admin/index.js');
if (fs.existsSync(adminEntry)) {
entry['admin/index'] = adminEntry;
}
// Block editor: warn when a block was removed from Handoff compile output
const blockDeprecationEntry = path.resolve(__dirname, 'src/block-editor/block-deprecation.js');
if (fs.existsSync(blockDeprecationEntry)) {
entry['editor/block-deprecation'] = blockDeprecationEntry;
}
if (Object.keys(entry).length === 0) {
module.exports = { entry: {}, plugins: [] };
} else {
const copyPatterns = blockFolders.flatMap(block => {
const patterns = [];
const blockPath = path.join(blocksDir, block);
if (fs.existsSync(path.join(blockPath, 'block.json'))) {
patterns.push({
from: path.join(blockPath, 'block.json'),
to: path.join(block, 'block.json'),
});
}
if (fs.existsSync(path.join(blockPath, 'render.php'))) {
patterns.push({
from: path.join(blockPath, 'render.php'),
to: path.join(block, 'render.php'),
});
}
if (fs.existsSync(path.join(blockPath, 'view.css'))) {
patterns.push({
from: path.join(blockPath, 'view.css'),
to: path.join(block, 'view.css'),
});
}
if (fs.existsSync(path.join(blockPath, 'migration-schema.json'))) {
patterns.push({
from: path.join(blockPath, 'migration-schema.json'),
to: path.join(block, 'migration-schema.json'),
});
}
if (fs.existsSync(path.join(blockPath, 'schema-changelog.json'))) {
patterns.push({
from: path.join(blockPath, 'schema-changelog.json'),
to: path.join(block, 'schema-changelog.json'),
});
}
const screenshotFiles = fs.readdirSync(blockPath).filter(f => f.startsWith('screenshot') && f.endsWith('.png'));
screenshotFiles.forEach(file => {
patterns.push({
from: path.join(blockPath, file),
to: path.join(block, file),
});
});
const variationsDir = path.join(blockPath, 'variations');
if (fs.existsSync(variationsDir)) {
const variationPhp = fs.readdirSync(variationsDir).filter(f => f.endsWith('.php'));
variationPhp.forEach(file => {
patterns.push({
from: path.join(variationsDir, file),
to: path.join(block, 'variations', file),
});
});
}
return patterns;
});
const isProduction = process.env.NODE_ENV === 'production';
// Plugin's own shared/ directory contains the actual component source files.
// When building from an external content dir, blocks use relative paths like
// ../../shared/... which resolve to contentDir/shared/ — but only barrel files
// exist there. We alias that path back to the plugin's shared/ so webpack finds
// the real component sources.
const pluginSharedDir = path.resolve(__dirname, 'shared');
// Handoff design-system JS (scoped inits for interactive block editor canvas).
const handoffDesignSystemJs =
process.env.HANDOFF_DESIGN_SYSTEM_JS ||
path.resolve(contentDir, '../../handoff/js');
const handoffDesignSystemNodeModules = path.resolve(
handoffDesignSystemJs,
'../node_modules',
);
module.exports = {
...defaultConfig,
entry,
devtool: isProduction ? 'source-map' : 'eval-source-map',
output: {
...defaultConfig.output,
path: path.resolve(contentDir, 'build'),
filename: '[name].js',
},
resolve: {
...defaultConfig.resolve,
modules: [
...(defaultConfig.resolve?.modules || ['node_modules']),
// Ensure the plugin's own node_modules is searched for npm deps
// (e.g. @10up/block-components) even when blocks live outside the plugin.
...(isExternalContent ? [path.resolve(__dirname, 'node_modules')] : []),
...(fs.existsSync(handoffDesignSystemNodeModules)
? [handoffDesignSystemNodeModules]
: []),
],
alias: {
...(defaultConfig.resolve?.alias || {}),
// Bare "shared/..." imports
shared: pluginSharedDir,
// Redirect resolved relative paths from contentDir/shared → plugin shared
...(isExternalContent
? { [path.resolve(contentDir, 'shared')]: pluginSharedDir }
: {}),
...(fs.existsSync(handoffDesignSystemJs)
? { '@handoff-ds': handoffDesignSystemJs }
: {}),
},
},
plugins: [
...defaultConfig.plugins.filter(
plugin => plugin.constructor.name !== 'CopyPlugin'
),
...(copyPatterns.length > 0 ? [
new CopyPlugin({
patterns: copyPatterns,
}),
] : []),
// When output.path is the external content dir, the admin bundle lands
// there too. Copy it back to the plugin root where PHP loads it from.
// This runs after webpack emits all files so the admin assets exist on disk.
...(isExternalContent ? [{
apply(compiler) {
compiler.hooks.afterEmit.tapAsync('CopyPluginBundlesToPluginRoot', (_compilation, callback) => {
for (const sub of ['admin', 'editor']) {
const src = path.resolve(contentDir, 'build', sub);
const dest = path.resolve(__dirname, 'build', sub);
if (fs.existsSync(src)) {
fs.mkdirSync(dest, { recursive: true });
fs.cpSync(src, dest, { recursive: true });
}
}
callback();
});
},
}] : []),
],
};
}