astro/snowpack-plugin.cjs

32 lines
937 B
JavaScript
Raw Normal View History

const { readFile } = require('fs').promises;
2021-03-15 17:22:05 +00:00
// Snowpack plugins must be CommonJS :(
const transformPromise = import('./lib/compiler/index.js');
2021-03-15 17:22:05 +00:00
module.exports = function (snowpackConfig, { resolve, extensions, astroConfig } = {}) {
2021-03-15 17:22:05 +00:00
return {
name: 'snowpack-astro',
2021-04-11 05:02:19 +00:00
knownEntrypoints: [],
2021-03-15 17:22:05 +00:00
resolve: {
input: ['.astro', '.md'],
output: ['.js', '.css'],
2021-03-15 17:22:05 +00:00
},
async load({ filePath }) {
2021-03-21 07:44:42 +00:00
const { compileComponent } = await transformPromise;
const projectRoot = snowpackConfig.root;
const contents = await readFile(filePath, 'utf-8');
const compileOptions = {
astroConfig,
resolve,
extensions,
};
const result = await compileComponent(contents, { compileOptions, filename: filePath, projectRoot });
const output = {
'.js': result.contents,
};
if (result.css) output['.css'] = result.css;
return output;
2021-03-15 17:22:05 +00:00
},
};
};