2021-03-19 20:55:06 +00:00
|
|
|
const { readFile } = require('fs').promises;
|
2021-03-15 17:22:05 +00:00
|
|
|
|
|
|
|
// Snowpack plugins must be CommonJS :(
|
2021-03-19 20:55:06 +00:00
|
|
|
const transformPromise = import('./lib/transform2.js');
|
2021-03-15 17:22:05 +00:00
|
|
|
|
|
|
|
module.exports = function (snowpackConfig, { resolve } = {}) {
|
|
|
|
return {
|
2021-03-19 20:55:06 +00:00
|
|
|
name: 'snowpack-hmx',
|
|
|
|
knownEntrypoints: ['deepmerge'],
|
2021-03-15 17:22:05 +00:00
|
|
|
resolve: {
|
2021-03-19 20:55:06 +00:00
|
|
|
input: ['.hmx', '.md'],
|
|
|
|
output: ['.js'],
|
2021-03-15 17:22:05 +00:00
|
|
|
},
|
|
|
|
async load({ filePath }) {
|
|
|
|
const { compilePage, compileComponent } = await transformPromise;
|
2021-03-18 17:25:19 +00:00
|
|
|
const projectRoot = snowpackConfig.root;
|
2021-03-19 20:55:06 +00:00
|
|
|
const contents = await readFile(filePath, 'utf-8');
|
2021-03-15 17:22:05 +00:00
|
|
|
|
2021-03-19 20:55:06 +00:00
|
|
|
if (!filePath.includes('/pages/') && !filePath.includes('/layouts/')) {
|
2021-03-18 17:25:19 +00:00
|
|
|
const result = await compileComponent(contents, { compileOptions: { resolve }, filename: filePath, projectRoot });
|
2021-03-15 17:22:05 +00:00
|
|
|
return result.contents;
|
|
|
|
}
|
2021-03-19 20:55:06 +00:00
|
|
|
const result = await compilePage(contents, {
|
|
|
|
compileOptions: { resolve },
|
|
|
|
filename: filePath,
|
|
|
|
projectRoot,
|
|
|
|
});
|
2021-03-15 17:22:05 +00:00
|
|
|
|
|
|
|
try {
|
2021-03-19 20:55:06 +00:00
|
|
|
return /* js */ `
|
2021-03-15 17:22:05 +00:00
|
|
|
${result.contents}
|
|
|
|
|
|
|
|
export default async (childDatas, childRenderFns) => {
|
|
|
|
// Kind of hacky, can clean up if this works
|
|
|
|
const renderHmx = {setup, head, body};
|
|
|
|
const merge = (await import('deepmerge')).default;
|
|
|
|
const content = childDatas && childDatas[0].content;
|
|
|
|
const _data = await renderHmx.setup({content});
|
|
|
|
if (_data.layout) {
|
|
|
|
const renderLayout = (await import('/_hmx/layouts/' + _data.layout.replace(/.*layouts\\//, "").replace(/\.hmx$/, '.js'))).default;
|
|
|
|
return renderLayout(
|
2021-03-19 20:55:06 +00:00
|
|
|
[...(childDatas || []), _data],
|
2021-03-15 17:22:05 +00:00
|
|
|
[...(childRenderFns || []), renderHmx]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
const data = merge.all([_data, ...(childDatas || [])]);
|
2021-03-19 20:55:06 +00:00
|
|
|
let headResult;
|
2021-03-15 17:22:05 +00:00
|
|
|
let bodyResult;
|
|
|
|
for (const renderFn of (childRenderFns || [])) {
|
|
|
|
let headAndBody = await Promise.all([
|
|
|
|
renderFn.head(data, headResult),
|
|
|
|
renderFn.body(data, bodyResult)
|
|
|
|
]);
|
|
|
|
headResult = headAndBody[0];
|
|
|
|
bodyResult = headAndBody[1];
|
|
|
|
}
|
|
|
|
return h(Fragment, null, [
|
|
|
|
renderHmx.head(data, headResult, true),
|
|
|
|
renderHmx.body(data, bodyResult, true),
|
2021-03-19 20:55:06 +00:00
|
|
|
]);
|
2021-03-15 17:22:05 +00:00
|
|
|
};
|
|
|
|
`;
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.contents;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|