Skip to content

Commit

Permalink
fix: pass through asset info and use for hash removal
Browse files Browse the repository at this point in the history
  • Loading branch information
ampersarnie committed Nov 8, 2024
1 parent 8503456 commit 579eb76
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ class TemplateGenerator {
createTemplates(compilation) {
let files = [];
this.templates.forEach(({ filename, templateContent }) => {
const assetsInfo = compilation.assetsInfo;
for (const entrypoint of compilation.entrypoints.values()) {
for (const chunk of entrypoint.chunks) {
files = [...files, ...chunk.files];
}
}

const templateString = templateContent(files);
const templateString = templateContent(files, assetsInfo);

try {
fs.mkdir(dirname(filename), { recursive: true }, (err) => {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/build/plugins/template-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = ({ mode, paths, name, version }) =>
templates: [
{
filename: `${paths.project}/inc/asset-settings.php`,
templateContent: (config) => assetSettingsTemplate(config, name, mode, version),
templateContent: (config, assetInfo) => assetSettingsTemplate(config, assetInfo, name, mode, version),
},
],
});
21 changes: 11 additions & 10 deletions src/commands/build/templates/asset-settings.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
const path = require('path');

const getFileName = (file, mode = 'develop') => {
const getFileName = (file, assetInfo, mode = 'develop') => {
const parsedFile = path.parse(file);
let name = parsedFile.name;

let name = parsedFile.name.replace(/-/g, '_').toUpperCase();
const info = assetInfo?.get(file);
const hash = info?.fullhash ?? info?.contenthash;

if (mode === 'production') {
const splitName = name.split('_');
splitName.pop();
name = splitName.join('_');
if (mode === 'production' && hash) {
name = name.replaceAll(hash, '');
}

return name;
// Remove trailing dash, change dashes to underscores, remove consecutive underscores.
return name.replace(/-+$/, '').replaceAll(/-/g, '_').replaceAll(/_{2,}/g, '_').toUpperCase();
};

const getExtension = (file) => {
Expand All @@ -20,13 +21,13 @@ const getExtension = (file) => {
};

// eslint-disable-next-line arrow-body-style
const assetSettingsTemplate = (files, project, mode, version = 'v0.0.0') => {
const assetSettingsTemplate = (files, assetInfo, project, mode, version = 'v0.0.0') => {
const setProject = project === '.' ? path.basename(path.resolve('./')) : project;
const projectName = setProject.replace(/-/g, '_').toUpperCase();

const fileDefinitions = (file) => {
const parsedFile = path.parse(file);
const name = getFileName(file, mode);
const name = getFileName(file, assetInfo, mode);
const extension = getExtension(file);
return `define( '${projectName}_${name}_${extension}', '${parsedFile.base}' );`;
};
Expand All @@ -37,7 +38,7 @@ const assetSettingsTemplate = (files, project, mode, version = 'v0.0.0') => {
}

const parsedFile = path.parse(file);
const name = getFileName(file, mode);
const name = getFileName(file, assetInfo, mode);
const { dependencies } = global.DependencyExtraction[project][parsedFile.name];
return `define( '${projectName}_${name}_DEPENDENCIES', ${JSON.stringify(dependencies)} );`;
};
Expand Down

0 comments on commit 579eb76

Please sign in to comment.