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-25 07:00:22 +00:00
|
|
|
const transformPromise = import('./lib/compiler/index.js');
|
2021-03-15 17:22:05 +00:00
|
|
|
|
2021-03-30 19:06:43 +00:00
|
|
|
module.exports = function (snowpackConfig, { resolve, extensions, astroConfig } = {}) {
|
2021-03-15 17:22:05 +00:00
|
|
|
return {
|
2021-03-24 15:45:38 +00:00
|
|
|
name: 'snowpack-astro',
|
2021-03-19 20:55:06 +00:00
|
|
|
knownEntrypoints: ['deepmerge'],
|
2021-03-15 17:22:05 +00:00
|
|
|
resolve: {
|
2021-03-24 15:45:38 +00:00
|
|
|
input: ['.astro', '.md'],
|
2021-03-31 19:04:18 +00:00
|
|
|
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;
|
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-23 17:47:54 +00:00
|
|
|
const compileOptions = {
|
2021-03-30 19:06:43 +00:00
|
|
|
astroConfig,
|
2021-03-23 17:47:54 +00:00
|
|
|
resolve,
|
2021-03-25 22:59:38 +00:00
|
|
|
extensions,
|
2021-03-23 17:47:54 +00:00
|
|
|
};
|
|
|
|
const result = await compileComponent(contents, { compileOptions, filename: filePath, projectRoot });
|
2021-03-31 19:04:18 +00:00
|
|
|
const output = {
|
|
|
|
'.js': result.contents,
|
|
|
|
};
|
|
|
|
if (result.css) output['.css'] = result.css;
|
|
|
|
return output;
|
2021-03-15 17:22:05 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|