Skip to content

Commit

Permalink
Use rollup util to import JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic committed Jun 26, 2024
1 parent 6ca356a commit 839838e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
"@babel/plugin-transform-react-jsx": "^7.24.7",
"@babel/traverse": "^7.24.7",
"@babel/types": "^7.24.7",
"@rollup/pluginutils": "5.1.0",
"@types/babel__core": "^7.20.5",
"@types/cookie": "^0.6.0",
"acorn": "^8.12.0",
Expand Down
21 changes: 17 additions & 4 deletions packages/astro/src/content/vite-plugin-content-virtual-mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { fileURLToPath, pathToFileURL } from 'node:url';
import glob from 'fast-glob';
import pLimit from 'p-limit';
import type { Plugin } from 'vite';
import { dataToEsm } from '@rollup/pluginutils';
import type { AstroSettings } from '../@types/astro.js';
import { encodeName } from '../core/build/util.js';
import { AstroError, AstroErrorData } from '../core/errors/index.js';
Expand Down Expand Up @@ -97,12 +98,24 @@ export function astroContentVirtualModPlugin({
} satisfies AstroPluginMetadata,
};
}
if(id === RESOLVED_DATA_STORE_VIRTUAL_ID) {
if(!fs.existsSync(dataStoreFile)) {
return 'export default {}'
if (id === RESOLVED_DATA_STORE_VIRTUAL_ID) {
if (!fs.existsSync(dataStoreFile)) {
return 'export default {}';
}
const jsonData = await fs.promises.readFile(dataStoreFile, 'utf-8');
return `export default ${jsonData}`;

try {
const parsed = JSON.parse(jsonData);
return {
code: dataToEsm(parsed, {
compact: true,
}),
map: { mappings: '' },
};
} catch (err) {
const message = 'Could not parse JSON file';
this.error({ message, id, cause: err });
}
}
},
renderChunk(code, chunk) {
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 839838e

Please sign in to comment.