Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing local json in main.js in Svelte

Tags:

json

svelte

What is the pattern for importing JSON into Svelte's main.js file?

I'm trying:

import App from './App.html';
const dataset = require('./../posts.json');

console.log(dataset);

const app = new App({
  target: document.body,
  data: dataset
});

export default app;

But this does not resolve as JSON cannot be imported as an es6 module.

like image 251
Union find Avatar asked Aug 28 '18 17:08

Union find


1 Answers

Svelte isn't involved in this process — it's entirely up to your bundler. If you're using Rollup, you'll need to add the rollup-plugin-json plugin to your rollup.config.js file.

like image 122
Rich Harris Avatar answered Oct 06 '22 11:10

Rich Harris