Skip to content

Commit

Permalink
a lot of changes
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed May 19, 2023
1 parent 7eaed20 commit 15b1443
Show file tree
Hide file tree
Showing 19 changed files with 218 additions and 386 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ Any BREAKING CHANGE between minor versions will be documented here in upper case
(Previouly, each plugin had it's own instance).
- Cache the remote files using Web Cache API.

### Changed
- `lightningcss` plugin bundlers the CSS code by default (set `includes: false` option to only transform it).
- BREAKING CHANGE: Removed the sync template loader for nunjucks. Use `asyncEach / endeach` and `ifAsync / endif` instead of `for / endfor` and `if / endif` if you need to include templates between these tags.

### Removed
- The `site.includesLoader` class.

### Fixed
- Ignore `/.git` folder by the watcher.
- Don't show the full path of the files in the output.
Expand Down
75 changes: 0 additions & 75 deletions core/includes_loader.ts

This file was deleted.

41 changes: 23 additions & 18 deletions core/renderer.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
import { concurrent, isGenerator } from "./utils.ts";
import { concurrent, isGenerator, resolveInclude } from "./utils.ts";
import { Exception } from "./errors.ts";
import { Page } from "./filesystem.ts";
import { posix } from "../deps/path.ts";
import { getDate, getUrl, mergeData } from "./source.ts";

import type {
Content,
Data,
Formats,
IncludesLoader,
Processors,
} from "../core.ts";
import type { Content, Data, Formats, FS, Processors } from "../core.ts";

export interface Options {
includesLoader: IncludesLoader;
includes: string;
prettyUrls: boolean;
preprocessors: Processors;
formats: Formats;
fs: FS;
}

/**
* The renderer is responsible for rendering the site pages
* in the right order and using the right template engine.
*/
export default class Renderer {
/** To load the includes files (layouts) */
includesLoader: IncludesLoader;
/** The default folder to include the layouts */
includes: string;

/** The filesystem instance used to read the layouts */
fs: FS;

/** To convert the urls to pretty /example.html => /example/ */
prettyUrls: boolean;
Expand All @@ -40,10 +38,11 @@ export default class Renderer {
helpers = new Map<string, [Helper, HelperOptions]>();

constructor(options: Options) {
this.includesLoader = options.includesLoader;
this.includes = options.includes;
this.prettyUrls = options.prettyUrls;
this.preprocessors = options.preprocessors;
this.formats = options.formats;
this.fs = options.fs;
}

/** Register a new helper used by the template engines */
Expand Down Expand Up @@ -241,20 +240,26 @@ export default class Renderer {
);
}

const result = await this.includesLoader.load(layout, format, path);
const includesPath = format.includesPath || this.includes;
const layoutPath = resolveInclude(
layout,
includesPath,
posix.dirname(path),
);
const entry = this.fs.entries.get(layoutPath);

if (!result) {
if (!entry) {
throw new Exception(
"Couldn't load this layout",
{ layout },
"The layout file doesn't exist",
{ layoutPath },
);
}

const layoutData = await entry.getContent(format.pageLoader);

delete data.layout;
delete data.templateEngine;

const [layoutPath, layoutData] = result;

data = {
...layoutData,
...data,
Expand Down
39 changes: 3 additions & 36 deletions core/site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { Exception } from "./errors.ts";
import FS from "./fs.ts";
import ComponentLoader from "./component_loader.ts";
import DataLoader from "./data_loader.ts";
import IncludesLoader from "./includes_loader.ts";
import Source from "./source.ts";
import Scopes from "./scopes.ts";
import Processors from "./processors.ts";
Expand All @@ -17,7 +16,6 @@ import Searcher from "./searcher.ts";
import Scripts from "./scripts.ts";
import Writer from "./writer.ts";
import textLoader from "./loaders/text.ts";
import binaryLoader from "./loaders/binary.ts";

import type {
Component,
Expand Down Expand Up @@ -87,9 +85,6 @@ export default class Site {
/** To load all _data files */
dataLoader: DataLoader;

/** To load all _includes files (layouts, templates, etc) */
includesLoader: IncludesLoader;

/** To load reusable components */
componentLoader: ComponentLoader;

Expand Down Expand Up @@ -154,7 +149,6 @@ export default class Site {
const formats = new Formats();

const dataLoader = new DataLoader({ formats });
const includesLoader = new IncludesLoader({ fs, includes });
const componentLoader = new ComponentLoader({ formats });
const source = new Source({
fs,
Expand All @@ -171,10 +165,11 @@ export default class Site {
const processors = new Processors();
const preprocessors = new Processors();
const renderer = new Renderer({
includesLoader,
prettyUrls,
preprocessors,
formats,
fs,
includes,
});

// Other stuff
Expand All @@ -199,7 +194,6 @@ export default class Site {
this.formats = formats;
this.componentLoader = componentLoader;
this.dataLoader = dataLoader;
this.includesLoader = includesLoader;
this.source = source;
this.scopes = scopes;
this.processors = processors;
Expand Down Expand Up @@ -768,7 +762,7 @@ export default class Site {
const basePath = this.src();

if (file.startsWith(basePath)) {
file = file.slice(basePath.length);
file = normalizePath(file.slice(basePath.length));
}

// It's a page
Expand All @@ -787,33 +781,6 @@ export default class Site {
| Uint8Array;
}

// Search in includes
const format = this.formats.search(file);
const pageLoader = format?.pageLoader;

if (pageLoader) {
const resolvedPath = this.includesLoader.resolve(file, format);

if (resolvedPath) {
const entry = this.fs.entries.get(resolvedPath);

if (entry) {
try {
if (pageLoader === textLoader || pageLoader === binaryLoader) {
return (await entry.getContent(pageLoader)).content as
| string
| Uint8Array;
}
return (await entry.getContent(loader)).content as
| string
| Uint8Array;
} catch {
// Ignore error
}
}
}
}

// Read the source files directly
try {
const entry = this.fs.entries.get(file);
Expand Down
44 changes: 42 additions & 2 deletions core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,17 +229,28 @@ export function isPlainObject(obj: unknown): obj is Record<string, unknown> {
* to Posix paths (with the separator "/")
* and ensure it starts with "/".
*/
export function normalizePath(path: string) {
export function normalizePath(path: string, rootToRemove?: string) {
if (rootToRemove) {
path = path.replace(rootToRemove, "");
}

if (SEP !== "/") {
path = path.replaceAll(SEP, "/");

// Is absolute Windows path (C:/...)
if (path.includes(":/")) {
if (rootToRemove && path.startsWith(rootToRemove)) {
return posix.join("/", path.replace(rootToRemove, ""));
}

return path;
}
}

return posix.join("/", path);
const absolute = posix.join("/", path);
return rootToRemove && absolute.startsWith(rootToRemove)
? posix.join("/", absolute.replace(rootToRemove, ""))
: absolute;
}

/** Convert an HTMLDocument instance to a string */
Expand Down Expand Up @@ -480,3 +491,32 @@ export function isGenerator(
const name = content.constructor.name;
return (name === "GeneratorFunction" || name === "AsyncGeneratorFunction");
}

/**
* Resolve the path of an included file
* Used in the template engines and processors
*/
export function resolveInclude(
path: string,
includesDir: string,
fromDir?: string,
rootToRemove?: string,
): string {
if (isUrl(path)) {
return path;
}

if (path.startsWith(".")) {
if (!fromDir) {
throw new Error(`Cannot load "${path}" without a base directory`);
}

return normalizePath(posix.join(fromDir, path), rootToRemove);
}

const normalized = normalizePath(path, rootToRemove);

return normalized.startsWith(normalizePath(posix.join(includesDir, "/")))
? normalized
: normalizePath(posix.join(includesDir, normalized));
}
3 changes: 1 addition & 2 deletions deps/lightningcss.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from "npm:[email protected]";
export { default as init } from "npm:[email protected]";
export * from "npm:[email protected]";
3 changes: 2 additions & 1 deletion plugins/esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { build, BuildOptions, OutputFile, stop } from "../deps/esbuild.ts";
import { extname, fromFileUrl, posix, toFileUrl } from "../deps/path.ts";
import { prepareAsset, saveAsset } from "./source_maps.ts";
import { Page } from "../core/filesystem.ts";
import textLoader from "../core/loaders/text.ts";

import type { DenoConfig, Site } from "../core.ts";

Expand Down Expand Up @@ -178,7 +179,7 @@ export default function (userOptions?: Partial<Options>) {
if (namespace === "deno") {
if (path.startsWith(prefix)) {
const file = path.replace(prefix, "");
const content = await site.getContent(file);
const content = await site.getContent(file, textLoader);

if (content) {
return {
Expand Down
Loading

0 comments on commit 15b1443

Please sign in to comment.