Skip to content

Commit

Permalink
fix(types): fix types and pkg info for modules with CDN specifier
Browse files Browse the repository at this point in the history
  • Loading branch information
hatemhosny committed Oct 12, 2023
1 parent 398c7a0 commit 7cdd9b2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
19 changes: 13 additions & 6 deletions src/livecodes/compiler/import-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,20 @@ export const importsPattern =

export const dynamicImportsPattern = /(import\s*?\(\s*?((?:".*?")|(?:'.*?'))\s*?\))/g;

export const getImports = (code: string) =>
export const getImports = (code: string, removeSpecifier = false) =>
[
...[...removeComments(code).matchAll(new RegExp(importsPattern))],
...[...removeComments(code).matchAll(new RegExp(dynamicImportsPattern))],
].map((arr) => arr[2].replace(/"/g, '').replace(/'/g, ''));
]
.map((arr) => arr[2].replace(/"/g, '').replace(/'/g, ''))
.map((mod) => {
if (!removeSpecifier || !isBare(mod) || !mod.includes(':')) {
return mod;
}
return mod.split(':')[1];
});

const needsBundler = (mod: string) =>
const needsBundler = /* @__PURE__ */ (mod: string) =>
!mod.startsWith('https://deno.bundlejs.com/') &&
!mod.startsWith('https://edge.bundlejs.com/') &&
!mod.endsWith('#nobundle') &&
Expand All @@ -36,23 +43,23 @@ const needsBundler = (mod: string) =>
mod.endsWith('.jsx') ||
mod.endsWith('.tsx'));

const isBare = (mod: string) =>
export const isBare = /* @__PURE__ */ (mod: string) =>
!mod.startsWith('https://') &&
!mod.startsWith('http://') &&
!mod.startsWith('.') &&
!mod.startsWith('/') &&
!mod.startsWith('data:') &&
!mod.startsWith('blob:');

const isStylesheet = (mod: string) =>
const isStylesheet = /* @__PURE__ */ (mod: string) =>
(mod.endsWith('.css') ||
mod.endsWith('.scss') ||
mod.endsWith('.sass') ||
mod.endsWith('.less') ||
mod.endsWith('.styl')) &&
!mod.startsWith('./style');

export const findImportMapKey = (mod: string, importmap: Record<string, string>) =>
export const findImportMapKey = /* @__PURE__ */ (mod: string, importmap: Record<string, string>) =>
Object.keys(importmap).find((key) => key === mod || mod.startsWith(key + '/'));

export const createImportMap = (code: string, config: Config, fallbackToCdn = true) =>
Expand Down
4 changes: 1 addition & 3 deletions src/livecodes/editor/monaco/monaco.ts
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ export const createEditor = async (options: EditorOptions): Promise<CodeEditor>
const npmPackageHoverProvider: Monaco.languages.HoverProvider = {
provideHover(model, position) {
const content = model.getLineContent(position.lineNumber);
let pkg = getImports(content)[0];
let pkg = getImports(content, /* removeSpecifier= */ true)[0];
if (!pkg) return;

if (
Expand All @@ -668,8 +668,6 @@ export const createEditor = async (options: EditorOptions): Promise<CodeEditor>
pkg.startsWith('blob:')
) {
return;
} else if (/^(skypack|unpkg|jsdelivr|esm|esm\.run|esm\.sh|bundle\.run)\:/.test(pkg)) {
pkg = pkg.replace(/^(skypack|unpkg|jsdelivr|esm|esm\.run|esm\.sh|bundle\.run)\:/, '');
}
// remove version
// pkg = pkg.replace(/(^@?([^@])+)(.*)/g, `$1`);
Expand Down
6 changes: 5 additions & 1 deletion src/livecodes/services/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
/* eslint-disable import/no-internal-modules */
import { isBare } from '../compiler/import-map';
import type { Types } from '../models';

export const typesService = {
getTypeUrls: async (types: string[]) => {
const fetchedTypes: Types = {};
await Promise.all(
types.map(async (type) => {
if (!isBare(type)) return;
try {
const res = await fetch('https://esm.sh/' + type);
const mod = type.includes(':') ? type.split(':')[1] : type;
const res = await fetch('https://esm.sh/' + mod);
if (!res.ok) return;
const typesUrl = res.headers.get('X-Typescript-Types');
if (!typesUrl) return;
Expand Down

0 comments on commit 7cdd9b2

Please sign in to comment.