Skip to content

Commit

Permalink
Remove dynamic import from browser bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
pomber committed Jul 13, 2023
1 parent a790ab9 commit 24563e6
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 30 deletions.
5 changes: 5 additions & 0 deletions .changeset/serious-teachers-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@code-hike/lighter": patch
---

Remove dynamic import from browser bundle
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ jobs:
- name: Checkout Repo
uses: actions/checkout@v3

- name: Setup Node.js 16.x
- name: Setup Node.js 18.x
uses: actions/setup-node@v3
with:
node-version: 16.x
node-version: 18.x

- name: Install Dependencies
run: yarn
Expand Down
29 changes: 2 additions & 27 deletions lib/dist/browser.esm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,8 @@ async function readJSON(folder, filename) {
// /grammars/${name}.json
// /themes/${name}.json
async function fetchJSON(endpoint) {
if (typeof fetch === "function") {
// console.log(`using fetch`, `https://lighter.codehike.org/${endpoint}.json`);
const r = await fetch(`https://lighter.codehike.org/${endpoint}.json`);
return await r.json();
}
// console.log(`using https`, `https://lighter.codehike.org/${endpoint}.json`);
const https = await import('https');
const options = {
host: "lighter.codehike.org",
path: `/${endpoint}.json`,
method: "GET",
};
return new Promise((resolve, reject) => {
const req = https.request(options, (res) => {
let data = "";
res.on("data", (chunk) => {
data += chunk;
});
res.on("end", () => {
resolve(JSON.parse(data));
});
});
req.on("error", (error) => {
reject(error);
});
req.end();
});
const r = await fetch(`https://lighter.codehike.org/${endpoint}.json`);
return await r.json();
}

// from https://stackoverflow.com/a/53936623/1325646
Expand Down
7 changes: 6 additions & 1 deletion lib/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ export default [
input: "src/index.ts",
output: [{ file: "dist/browser.esm.mjs", format: "esm" }],
plugins: [
replace({ "file-system": "file-system.fake", preventAssignment: false }),
replace({
"./file-system": "./file-system.browser",
"./network": "./network.browser",
delimiters: ["", "\\b"],
preventAssignment: false,
}),
...plugins,
],
},
Expand Down
File renamed without changes.
10 changes: 10 additions & 0 deletions lib/src/network.browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// this will be replaced at build time with the version from package json
const LIGHTER_VERSION = "__LIGHTER_VERSION__";

// endpoints:
// /grammars/${name}.json
// /themes/${name}.json
export async function fetchJSON(endpoint: string) {
const r = await fetch(`https://lighter.codehike.org/${endpoint}.json`);
return await r.json();
}

1 comment on commit 24563e6

@vercel
Copy link

@vercel vercel bot commented on 24563e6 Jul 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

lighter – ./

lighter-codehike.vercel.app
lighter-git-main-codehike.vercel.app
lighter.codehike.org

Please sign in to comment.