summaryrefslogtreecommitdiff
path: root/snowpack-plugin.cjs
blob: 8a3faed63c9f6236c16265a13d3f86572a022d9e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const { readFile } = require('fs').promises;

// Snowpack plugins must be CommonJS :(
const transformPromise = import('./lib/compiler/index.js');

module.exports = function (snowpackConfig, { resolve, extensions, astroConfig } = {}) {
  return {
    name: 'snowpack-astro',
    knownEntrypoints: [],
    resolve: {
      input: ['.astro', '.md'],
      output: ['.js', '.css'],
    },
    async load({ filePath }) {
      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;
    },
  };
};