0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00
astro/snowpack-plugin.cjs

32 lines
957 B
JavaScript
Raw Normal View History

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