-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathentry.js
179 lines (151 loc) · 4.93 KB
/
entry.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
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
const { readFileSync } = require('fs');
const { dirname, extname, join, resolve } = require('path');
const { sync: glob } = require('fast-glob');
const removeDistFolder = (file) => {
return file.replace(/(^\.\/dist\/)|^dist\//, '');
};
module.exports = ({
buildType = 'script',
isPackage,
projectConfig: { devServer, paths, useBlockAssets, filenames, loadBlockSpecificStyles },
packageConfig: { packageType, source, main, umd, libraryName },
buildFiles,
moduleBuildFiles,
}) => {
let additionalEntrypoints = {};
if (useBlockAssets) {
// override default block filenames
filenames.block = 'blocks/[name].js';
filenames.blockCSS = 'blocks/[name].css';
const blocksSourceDirectory = resolve(process.cwd(), paths.blocksDir);
// get all block.json files in the blocks directory
const blockMetadataFiles = glob(
// glob only accepts forward-slashes this is required to make things work on Windows
`${blocksSourceDirectory.replace(/\\/g, '/')}/**/block.json`,
{
absolute: true,
},
);
// add any additional entrypoints we find in block.json filed to the webpack config
additionalEntrypoints = blockMetadataFiles.reduce((accumulator, blockMetadataFile) => {
// wrapping in try/catch in case the file is malformed
// this happens especially when new block.json files are added
// at which point they are completely empty and therefore not valid JSON
try {
// get all assets from the block.json file
const {
editorScript,
script,
viewScript,
scriptModule,
viewScriptModule,
style,
editorStyle,
viewStyle,
} = JSON.parse(readFileSync(blockMetadataFile));
const assets = [];
if (buildType === 'script') {
assets.push(
...[editorScript, script, viewScript, style, editorStyle, viewStyle].filter(
Boolean,
),
);
} else if (buildType === 'module') {
assets.push(...[scriptModule, viewScriptModule].filter(Boolean));
}
// generate a new entrypoint for each of the assets
assets
.flat()
.filter((rawFilepath) => rawFilepath && rawFilepath.startsWith('file:')) // assets can be files or handles. we only want files
.forEach((rawFilepath) => {
// Removes the `file:` prefix.
const filepath = join(
dirname(blockMetadataFile),
rawFilepath.replace('file:', ''),
);
// get the entrypoint name from the filepath by removing the blocks source directory and the file extension
const entryName = filepath
.replace(extname(filepath), '')
.replace(blocksSourceDirectory, '')
.replace(/\\/g, '/');
// Detects the proper file extension used in the defined source directory.
const [entryFilepath] = glob(
// glob only accepts forward-slashes this is required to make things work on Windows
`${blocksSourceDirectory.replace(
/\\/g,
'/',
)}/${entryName}.([jt]s?(x)|?(s)css)`,
{
absolute: true,
},
);
if (!entryFilepath) {
// eslint-disable-next-line no-console
console.warn('There was no entry file found for', entryName);
return;
}
accumulator[entryName] = entryFilepath;
});
return accumulator;
} catch (error) {
return accumulator;
}
}, {});
}
const blockStyleEntryPoints = {};
// Logic for loading CSS files per block.
if (loadBlockSpecificStyles) {
// get all stylesheets located in the assets/css/blocks directory and subdirectories
const blockStylesheetDirectory = resolve(process.cwd(), paths.blocksStyles).replace(
/\\/g,
'/',
);
// get all stylesheets in the blocks directory
const stylesheets = glob(`${blockStylesheetDirectory}/**/*.css`, {
absolute: true,
});
stylesheets.forEach((filePath) => {
const blockName = filePath
.replace(`${blockStylesheetDirectory}/`, '')
.replace(extname(filePath), '');
blockStyleEntryPoints[`autoenqueue/${blockName}`] = resolve(filePath);
});
}
if (buildType === 'module') {
Object.assign(moduleBuildFiles, additionalEntrypoints);
return moduleBuildFiles;
}
// merge the new entrypoints with the existing ones
Object.assign(buildFiles, additionalEntrypoints, blockStyleEntryPoints);
if (isPackage) {
const config = {};
const hasBuildFiles = Object.keys(buildFiles).length > 0;
if (hasBuildFiles) {
return buildFiles;
}
if (packageType !== 'umd') {
config.main = {
import: `./${source}`,
filename: removeDistFolder(main),
};
if (typeof packageType === 'undefined' || packageType !== 'none') {
config.main.library = {
type: ['commonjs2', 'commonjs', 'all'].includes(packageType)
? 'commonjs2'
: packageType,
};
}
}
if (umd && !devServer) {
config.umd = {
filename: removeDistFolder(umd),
import: `./${source}`,
};
if (typeof packageType === 'undefined' || packageType !== 'none') {
config.umd.library = { name: libraryName, type: 'umd' };
}
}
return config;
}
return buildFiles;
};