astro/snowpack-plugin.cjs

27 lines
800 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 } = {}) {
2021-03-15 17:22:05 +00:00
return {
name: 'snowpack-astro',
knownEntrypoints: ['deepmerge'],
2021-03-15 17:22:05 +00:00
resolve: {
input: ['.astro', '.md'],
output: ['.js'],
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 = {
resolve,
extensions,
};
const result = await compileComponent(contents, { compileOptions, filename: filePath, projectRoot });
2021-03-15 17:22:05 +00:00
return result.contents;
},
};
};