Skip to content

Commit c3ce8b0

Browse files
committed
Support gh: specifiers
1 parent ab6b87a commit c3ce8b0

File tree

6 files changed

+36
-15
lines changed

6 files changed

+36
-15
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ Go to the `v2` branch to see the changelog of Lume 2.
77
Go to the `v1` branch to see the changelog of Lume 1.
88

99
## [3.0.11] - Unreleased
10+
### Added
11+
- Support for `gh:` specifiers in Lightningcss, Postcss and Tailwindcss.
12+
1013
### Fixed
1114
- `tailwind` plugin: Support to import styles from npm. (i.e. `@import "npm:tw-animate-css"`)
1215
- `tailwind` plugin: Imports with relative paths don't work.

core/site.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { env, setEnv } from "./utils/env.ts";
55
import { log } from "./utils/log.ts";
66
import { filter404page } from "./utils/page_url.ts";
77
import { insertContent } from "./utils/page_content.ts";
8-
import { getFiles } from "./utils/cdn.ts";
8+
import { getFile, getFiles, isFromCdn } from "./utils/cdn.ts";
99

1010
import FS from "./fs.ts";
1111
import { compileCSS, compileJS, ComponentLoader } from "./components.ts";
@@ -553,8 +553,8 @@ export default class Site {
553553
return;
554554
}
555555

556-
// Remote files
557-
if (from.startsWith("npm:") || from.startsWith("gh:")) {
556+
// Remote files from NPM or GitHub CDN
557+
if (isFromCdn(from)) {
558558
// It's a pattern
559559
if (from.includes("*")) {
560560
const specifier = from;
@@ -571,11 +571,7 @@ export default class Site {
571571
}
572572

573573
// Copy only the main file
574-
if (from.startsWith("npm:")) {
575-
from = from.replace("npm:", "https://cdn.jsdelivr.net/npm/");
576-
} else {
577-
from = from.replace("gh:", "https://cdn.jsdelivr.net/gh/");
578-
}
574+
from = getFile(from);
579575
}
580576

581577
if (isUrl(from)) {

core/utils/cdn.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,25 @@ export async function getFiles(
4949
return fileMap;
5050
}
5151

52+
export function isFromCdn(specifier: string): boolean {
53+
return specifier.startsWith("npm:") || specifier.startsWith("gh:");
54+
}
55+
56+
export function getFile(specifier: string): string {
57+
const result = parseNpm(specifier) || parseGh(specifier);
58+
if (!result) {
59+
throw new Error(`Invalid specifier: ${specifier}`);
60+
}
61+
62+
const [type, name, version, filename] = result;
63+
64+
if (filename.includes("*")) {
65+
throw new Error(`Specifier must not contain glob pattern: ${specifier}`);
66+
}
67+
68+
return `https://cdn.jsdelivr.net/${type}/${name}@${version}${filename}`;
69+
}
70+
5271
export async function getVersion(
5372
type: PackageType,
5473
name: string,

plugins/lightningcss.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { warnUntil } from "../core/utils/log.ts";
99
import { bytes } from "../core/utils/format.ts";
1010
import { log } from "../core/utils/log.ts";
1111
import { browsers, version } from "../core/utils/browsers.ts";
12+
import { getFile, isFromCdn } from "../core/utils/cdn.ts";
1213

1314
import type { Item } from "../deps/debugbar.ts";
1415
import type Site from "../core/site.ts";
@@ -164,8 +165,8 @@ export function lightningCSS(userOptions?: Options) {
164165
...options.options,
165166
resolver: {
166167
resolve(id: string, from: string) {
167-
if (id.startsWith("npm:")) {
168-
return id.replace("npm:", "https://cdn.jsdelivr.net/npm/");
168+
if (isFromCdn(id)) {
169+
return getFile(id);
169170
}
170171
return resolveInclude(id, includes, posix.dirname(from));
171172
},

plugins/postcss.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { prepareAsset, saveAsset } from "./source_maps.ts";
88
import { warnUntil } from "../core/utils/log.ts";
99
import { bytes } from "../core/utils/format.ts";
1010
import { browsers, versionString } from "../core/utils/browsers.ts";
11+
import { getFile, isFromCdn } from "../core/utils/cdn.ts";
1112

1213
import type Site from "../core/site.ts";
1314
import type { SourceMap } from "./source_maps.ts";
@@ -149,7 +150,7 @@ function configureImport(site: Site, includes: string) {
149150
return postcssImport({
150151
/** Resolve the import path */
151152
resolve(id: string, basedir: string) {
152-
if (id.startsWith("npm:")) {
153+
if (isFromCdn(id)) {
153154
return "/" + id;
154155
}
155156

@@ -158,8 +159,8 @@ function configureImport(site: Site, includes: string) {
158159

159160
/** Load the content (using the Lume reader) */
160161
async load(file: string) {
161-
if (file.startsWith("/npm:")) {
162-
const url = file.replace("/npm:", "https://cdn.jsdelivr.net/npm/");
162+
if (file.startsWith("/") && isFromCdn(file.slice(1))) {
163+
const url = getFile(file.slice(1));
163164
return await readFile(url);
164165
}
165166

plugins/tailwindcss.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { readFile } from "../core/utils/read.ts";
1111
import { resolveInclude } from "../core/utils/path.ts";
1212
import { prepareAsset, saveAsset } from "./source_maps.ts";
1313
import { Features, transform } from "../deps/lightningcss.ts";
14+
import { getFile, isFromCdn } from "../core/utils/cdn.ts";
1415

1516
import type { ChangedContent } from "../deps/tailwindcss.ts";
1617
import type Site from "../core/site.ts";
@@ -115,8 +116,8 @@ export function tailwindCSS(userOptions?: Options) {
115116
}
116117

117118
// Support npm: prefix to load from npm CDN (ex: npm:tw-animate-css)
118-
if (id.startsWith("npm:")) {
119-
id = id.replace("npm:", "https://cdn.jsdelivr.net/npm/");
119+
if (isFromCdn(id)) {
120+
id = getFile(id);
120121
const content = await readFile(id);
121122
return { content, path: id, base };
122123
}

0 commit comments

Comments
 (0)