2021-03-19 15:55:06 -05:00
|
|
|
const { readFile } = require('fs').promises;
|
2021-03-15 12:22:05 -05:00
|
|
|
|
|
|
|
// Snowpack plugins must be CommonJS :(
|
2021-03-25 02:00:22 -05:00
|
|
|
const transformPromise = import('./lib/compiler/index.js');
|
2021-03-15 12:22:05 -05:00
|
|
|
|
2021-03-30 14:06:43 -05:00
|
|
|
module.exports = function (snowpackConfig, { resolve, extensions, astroConfig } = {}) {
|
2021-03-15 12:22:05 -05:00
|
|
|
return {
|
2021-03-24 10:45:38 -05:00
|
|
|
name: 'snowpack-astro',
|
2021-03-19 15:55:06 -05:00
|
|
|
knownEntrypoints: ['deepmerge'],
|
2021-03-15 12:22:05 -05:00
|
|
|
resolve: {
|
2021-03-24 10:45:38 -05:00
|
|
|
input: ['.astro', '.md'],
|
2021-03-19 15:55:06 -05:00
|
|
|
output: ['.js'],
|
2021-03-15 12:22:05 -05:00
|
|
|
},
|
|
|
|
async load({ filePath }) {
|
2021-03-21 02:44:42 -05:00
|
|
|
const { compileComponent } = await transformPromise;
|
2021-03-18 12:25:19 -05:00
|
|
|
const projectRoot = snowpackConfig.root;
|
2021-03-19 15:55:06 -05:00
|
|
|
const contents = await readFile(filePath, 'utf-8');
|
2021-03-23 12:47:54 -05:00
|
|
|
const compileOptions = {
|
2021-03-30 14:06:43 -05:00
|
|
|
astroConfig,
|
2021-03-23 12:47:54 -05:00
|
|
|
resolve,
|
2021-03-25 17:59:38 -05:00
|
|
|
extensions,
|
2021-03-23 12:47:54 -05:00
|
|
|
};
|
|
|
|
const result = await compileComponent(contents, { compileOptions, filename: filePath, projectRoot });
|
2021-03-15 12:22:05 -05:00
|
|
|
return result.contents;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|