Skip to content

Commit

Permalink
Append template parent directory to Liquid condition.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidwarrington committed Jun 10, 2021
1 parent 15b87f6 commit 7077282
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions utils/render-asset-snippets.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const path = require('path');
const Config = require('../packages/Config');

/** @typedef {{[key: string]: string}} Entrypoints */
/** @typedef {{[key: string]: string[]}} Entrypoints */
/**
* @typedef {{
* entrypoint: string;
Expand Down Expand Up @@ -47,11 +47,21 @@ const assignBaseUrl = () => {
`;
};

/**
* @param {string} entry
* @param {Entrypoints} entrypoints
*/
const getParentDirectory = (entry, entrypoints) => {
const dirname = path.dirname(entrypoints[entry][0]);

return path.basename(dirname);
};

const getChunkForFile = (filename, compilation) => {
return [...compilation.chunks].find(chunk => chunk.files.has(filename));
};

const getLiquidConditionsFromChunk = chunk => {
const getLiquidConditionsFromChunk = (chunk, entrypoints) => {
const liquidAssociations = {
layouts: [],
templates: [],
Expand All @@ -67,11 +77,15 @@ const getLiquidConditionsFromChunk = chunk => {

runtimes.forEach(runtime => {
if (runtime.startsWith('layout.', '')) {
liquidAssociations.layouts.push(runtime.replace('layout.', ''));
liquidAssociations.layouts.push(runtime.replace(/^layout\./, ''));
} else if (runtime.startsWith('templates.', '')) {
liquidAssociations.templates.push(
runtime.replace('templates.', '')
);
let template = runtime.replace(/^templates\./, '');
const parentDirectory = getParentDirectory(runtime, entrypoints);
if (parentDirectory !== 'templates') {
template = `${parentDirectory}/template`;
}

liquidAssociations.templates.push(template);
} else {
liquidAssociations.other.push(runtime);
}
Expand Down Expand Up @@ -101,7 +115,10 @@ const renderScriptTagsSnippet = ({ compilation, htmlWebpackPlugin }) => {

const associatedChunk = getChunkForFile(filename, compilation);

const conditions = getLiquidConditionsFromChunk(associatedChunk);
const conditions = getLiquidConditionsFromChunk(
associatedChunk,
htmlWebpackPlugin.options.entrypoints
);
const assetSrc = getAssetSrc(filename);

return `
Expand Down Expand Up @@ -138,7 +155,10 @@ const renderStyleTagsSnippet = ({ compilation, htmlWebpackPlugin }) => {
.map(filename => {
const associatedChunk = getChunkForFile(filename, compilation);

const conditions = getLiquidConditionsFromChunk(associatedChunk);
const conditions = getLiquidConditionsFromChunk(
associatedChunk,
htmlWebpackPlugin.options.entrypoints
);
const assetSrc = getAssetSrc(filename);

return `
Expand Down

0 comments on commit 7077282

Please sign in to comment.