NekitCorp / chrome-extension-svelte-typescript-boilerplate

Boilerplate for Chrome Extension Svelte Typescript project
237 stars 50 forks source link

Add custom files to the bundle #11

Closed avi12 closed 1 year ago

avi12 commented 1 year ago

I wish to add some files that interface directly with background/index.ts, however they're not supposed to be imported Is it possible in this boilerplate?

NekitCorp commented 1 year ago

Maybe I didn’t quite understand the problem, but why not put the reusable functions into separate modules and import them in background/index.ts and in some files, like storage.ts https://github.com/NekitCorp/chrome-extension-svelte-typescript-boilerplate/blob/main/src/background/index.ts?

avi12 commented 1 year ago

The thing is that this line expects a fixed path So I was wondering, is there a way that I could have a predetermined path to a file?

NekitCorp commented 1 year ago

Yes, you can:

  1. import

src/background/index.ts:

import data from "../data.json";

chrome.runtime.onInstalled.addListener(() => {
    console.log(data);
});
image
  1. public folder

Create public folder in root project directory, and create data.json inside public folder.

src/background/index.ts:

chrome.runtime.onInstalled.addListener(() => {
    const url = chrome.runtime.getURL("data.json");

    fetch(url)
        .then((res) => res.json())
        .then(console.log);
});

the result is the same as in option 1

Output:

image