Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip #2

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ scratchpad/
.direnv/
.idea/
.contentlayer/
contentlayer.config.ts
3 changes: 3 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- tweak .d.ts files to export all modules
- module resolution
- setup content watchers in fs source
86 changes: 35 additions & 51 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,66 +22,50 @@
"changeset-version": "changeset version && node scripts/version.mjs",
"changeset-publish": "pnpm build && TEST_DIST= pnpm vitest && changeset publish"
},
"resolutions": {
"dependency-tree": "^10.0.9",
"detective-amd": "^5.0.2",
"detective-cjs": "^5.0.1",
"detective-es6": "^4.0.1",
"detective-less": "^1.0.2",
"detective-postcss": "^6.1.3",
"detective-sass": "^5.0.3",
"detective-scss": "^4.0.3",
"detective-stylus": "^4.0.0",
"detective-typescript": "^11.1.0"
},
"devDependencies": {
"@babel/cli": "^7.24.8",
"@babel/core": "^7.25.2",
"@babel/plugin-transform-export-namespace-from": "^7.24.7",
"@babel/plugin-transform-modules-commonjs": "^7.24.8",
"@babel/cli": "^7.26.4",
"@babel/core": "^7.26.0",
"@babel/plugin-transform-export-namespace-from": "^7.25.9",
"@babel/plugin-transform-modules-commonjs": "^7.26.3",
"@changesets/changelog-github": "^0.5.0",
"@changesets/cli": "^2.27.7",
"@edge-runtime/vm": "^4.0.0",
"@effect/build-utils": "^0.7.7",
"@effect/docgen": "^0.4.3",
"@effect/dtslint": "^0.1.0",
"@changesets/cli": "^2.27.11",
"@edge-runtime/vm": "^5.0.0",
"@effect/build-utils": "^0.7.8",
"@effect/docgen": "^0.5.2",
"@effect/dtslint": "^0.1.2",
"@effect/eslint-plugin": "^0.2.0",
"@effect/experimental": "^0.21.1",
"@effect/language-service": "^0.1.0",
"@effect/platform": "^0.61.1",
"@effect/platform-node": "^0.56.1",
"@types/node": "^22.0.1",
"@typescript-eslint/eslint-plugin": "^8.0.0",
"@typescript-eslint/parser": "^8.0.0",
"@vitest/browser": "^2.0.5",
"@vitest/coverage-v8": "^2.0.5",
"@vitest/expect": "^2.0.5",
"@vitest/web-worker": "^2.0.5",
"babel-plugin-annotate-pure-calls": "^0.4.0",
"effect": "^3.6.0",
"eslint": "^8.57.0",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-codegen": "^0.28.0",
"@effect/experimental": "^0.39.1",
"@effect/language-service": "^0.2.0",
"@effect/platform": "^0.75.1",
"@effect/platform-node": "^0.71.1",
"@types/node": "^22.10.7",
"@typescript-eslint/eslint-plugin": "^8.21.0",
"@typescript-eslint/parser": "^8.21.0",
"@vitest/browser": "^3.0.3",
"@vitest/coverage-v8": "^3.0.3",
"@vitest/expect": "^3.0.3",
"@vitest/web-worker": "^3.0.3",
"babel-plugin-annotate-pure-calls": "^0.5.0",
"effect": "^3.12.7",
"eslint": "^8.57.1",
"eslint-import-resolver-typescript": "^3.7.0",
"eslint-plugin-codegen": "^0.29.0",
"eslint-plugin-deprecation": "^3.0.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-import": "^2.31.0",
"eslint-plugin-simple-import-sort": "^12.1.1",
"eslint-plugin-sort-destructure-keys": "^2.0.0",
"fast-check": "^3.20.0",
"glob": "^11.0.0",
"madge": "^7.0.0",
"playwright": "^1.45.1",
"prettier": "^3.3.3",
"fast-check": "^3.23.2",
"glob": "^11.0.1",
"madge": "^8.0.0",
"playwright": "^1.49.1",
"prettier": "^3.4.2",
"rimraf": "^6.0.1",
"tsx": "^4.16.3",
"typescript": "^5.5.4",
"vite": "^5.3.5",
"vitest": "^2.0.5"
"tsx": "^4.19.2",
"typescript": "^5.7.3",
"vite": "^6.0.11",
"vitest": "^3.0.3"
},
"pnpm": {
"patchedDependencies": {
"@changesets/[email protected]": "patches/@[email protected]",
"@changesets/[email protected]": "patches/@[email protected]"
},
"updateConfig": {
"ignoreDependencies": [
"eslint"
Expand Down
12 changes: 5 additions & 7 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"directory": "packages/cli"
},
"scripts": {
"codegen": "build-utils prepare-v2",
"build": "pnpm build-esm && pnpm build-annotate && pnpm build-cjs && build-utils pack-v2",
"build-esm": "tsc -b tsconfig.build.json",
"build-cjs": "babel build/esm --plugins @babel/transform-export-namespace-from --plugins @babel/transform-modules-commonjs --out-dir build/cjs --source-maps",
Expand All @@ -21,12 +20,11 @@
"coverage": "vitest --coverage"
},
"dependencies": {
"@effect/cli": "^0.40.1",
"@effect/cli": "^0.54.1",
"@effect/contentlayer-core": "workspace:^",
"@effect/experimental": "^0.21.1",
"@effect/platform": "^0.61.1",
"@effect/platform-node": "^0.56.1",
"@effect/schema": "^0.70.0",
"effect": "^3.6.0"
"@effect/experimental": "^0.39.1",
"@effect/platform": "^0.75.1",
"@effect/platform-node": "^0.71.1",
"effect": "^3.12.7"
}
}
6 changes: 3 additions & 3 deletions packages/cli/src/Cli.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as Command from "@effect/cli/Command"
import * as Options from "@effect/cli/Options"
import { ConfigBuilder } from "@effect/contentlayer-core/ConfigBuilder"
import { DocumentBuilder } from "@effect/contentlayer-core/DocumentBuilder"
import { BuildOptions } from "@effect/contentlayer-core/Esbuild"
import * as Config from "effect/Config"
import * as Effect from "effect/Effect"
Expand Down Expand Up @@ -31,9 +31,9 @@ const command = Command.make("contentlayer", { configPath, watchMode }).pipe(
)
),
Command.provide(({ configPath }) =>
ConfigBuilder.Live.pipe(
DocumentBuilder.Live.pipe(
Layer.provide(BuildOptions.Live({
bundle: true,
bundle: false,
entryNames: "[name]-[hash]",
entryPoints: [configPath],
format: "cjs",
Expand Down
22 changes: 19 additions & 3 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,24 @@
"coverage": "vitest --coverage"
},
"dependencies": {
"@effect/schema": "^0.70.0",
"effect": "^3.6.0",
"esbuild": "^0.23.0"
"effect": "^3.12.7",
"esbuild": "^0.24.2",
"glob": "^11.0.1",
"rehype-format": "^5.0.1",
"rehype-stringify": "^10.0.1",
"remark-rehype": "^11.1.1",
"typescript": "^5.7.3",
"unified": "^11.0.5",
"unist": "^0.0.1",
"unist-util-remove": "^4.0.0",
"vfile": "^6.0.3"
},
"devDependencies": {
"@types/unist": "^3.0.3",
"remark": "^15.0.1",
"remark-frontmatter": "^5.0.0",
"remark-parse": "^11.0.0",
"remark-parse-frontmatter": "^1.0.3",
"remark-stringify": "^11.0.0"
}
}
3 changes: 3 additions & 0 deletions packages/core/src/Config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @since 1.0.0
*/
import * as Predicate from "effect/Predicate"
import type { Document } from "./Document.js"

Expand Down
71 changes: 45 additions & 26 deletions packages/core/src/ConfigBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
/**
* @since 1.0.0
*/
import * as NodeFileSystem from "@effect/platform-node/NodeFileSystem"
import * as NodePath from "@effect/platform-node/NodePath"
import * as FileSystem from "@effect/platform/FileSystem"
import type * as Path from "@effect/platform/Path"
import * as Array from "effect/Array"
import * as Console from "effect/Console"
import * as Path from "@effect/platform/Path"
import * as Effect from "effect/Effect"
import { identity } from "effect/Function"
import * as Layer from "effect/Layer"
import * as Option from "effect/Option"
import * as Queue from "effect/Queue"
import * as Stream from "effect/Stream"
import * as SubscriptionRef from "effect/SubscriptionRef"
import * as Module from "node:module"
import * as VM from "node:vm"
import * as Config from "./Config.js"
import { ContentlayerError } from "./ContentlayerError.js"
import type { EsbuildSuccess } from "./Esbuild.js"
import { Esbuild } from "./Esbuild.js"

Expand Down Expand Up @@ -56,65 +60,80 @@ import { Esbuild } from "./Esbuild.js"
// compiled-contentlayer-config-[HASH].mjs

export const make = Effect.gen(function*() {
const results = yield* Esbuild.results
const esbuild = yield* Esbuild
const config = yield* SubscriptionRef.make(Option.none<Config.Config>())

yield* Queue.take(results).pipe(
yield* esbuild.results.take.pipe(
Effect.flatten,
Effect.flatMap(build),
Effect.flatMap((latest) => SubscriptionRef.set(config, latest)),
// TODO: log nice messages for the user
Effect.catchAllCause(Effect.logError),
Effect.forever,
Effect.forkScoped
)

return {
config: config.changes.pipe(
Stream.filterMap(identity)
Stream.filterMap(identity),
Stream.debounce("200 millis")
)
} as const
})

export class ConfigBuilder extends Effect.Tag("@effect/content/core/ConfigBuilder")<
export class ConfigBuilder extends Effect.Tag("@effect/contentlayer-core/ConfigBuilder")<
ConfigBuilder,
Effect.Effect.Success<typeof make>
>() {
static Live = Layer.scoped(this, make).pipe(
Layer.provide(Esbuild.Live)
Layer.provide(Esbuild.Live),
Layer.provide(NodeFileSystem.layer),
Layer.provide(NodePath.layer)
)
}

const ESBUILD_HASH_REGEX = /compiled-contentlayer-config-(.+)$/
const COMPILED_CONTENTLAYER_CONFIG_REGEX = /compiled-contentlayer-config-.+$/
// const ESBUILD_HASH_REGEX = /compiled-contentlayer-config-(.+)$/
const configAsOption = Option.liftPredicate(Config.isConfig)

const build = (
result: EsbuildSuccess
): Effect.Effect<Option.Option<Config.Config>, never, FileSystem.FileSystem | Path.Path> =>
): Effect.Effect<
Option.Option<Config.Config>,
ContentlayerError,
FileSystem.FileSystem | Path.Path
> =>
Effect.gen(function*() {
const fs = yield* FileSystem.FileSystem
const path = yield* Path.Path

const outfilePath = yield* Option.fromNullable(result.metafile).pipe(
Option.map((metafile) => Object.keys(metafile.outputs)),
Option.flatMap(Array.findFirst((filePath) => COMPILED_CONTENTLAYER_CONFIG_REGEX.test(filePath))),
const [outfilePath, output] = yield* Option.fromNullable(result.metafile).pipe(
Option.flatMapNullable((metafile) => Object.entries(metafile.outputs)[0]),
Effect.orDie
)

const esbuildHash = yield* Option.fromNullable(outfilePath.match(ESBUILD_HASH_REGEX)).pipe(
Option.flatMap(Array.get(1)),
Effect.orDie
)
// const esbuildHash = yield* Option.fromNullable(outfilePath.match(ESBUILD_HASH_REGEX)).pipe(
// Option.flatMap(Array.get(1)),
// Effect.orDie
// )

const content = yield* Effect.orDie(fs.readFileString(outfilePath))

const context = VM.createContext(globalThis)
const context = VM.createContext({
...globalThis
})
context.require = Module.createRequire(path.resolve(output.entryPoint!))
context.module = { exports: {} }
context.exports = context.module.exports

yield* Effect.sync(() => VM.runInContext(content, context))

yield* Console.dir(context.module.exports.default, { depth: null, colors: true })
yield* Console.log(esbuildHash)
yield* Effect.try({
try: () => VM.runInContext(content, context),
catch: (cause) =>
new ContentlayerError({
module: "ConfigBuilder",
method: "build",
description: "Error evaluating config",
cause
})
})

return Option.some(Config.make({ documents: [] }))
return configAsOption(context.module.exports.default)
})
40 changes: 40 additions & 0 deletions packages/core/src/ContentlayerError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @since 1.0.0
*/
import * as Schema from "effect/Schema"

/**
* @since 1.0.0
* @category type ids
*/
export const TypeId: unique symbol = Symbol.for("@effect/contentlayer-core/ContentlayerError")

/**
* @since 1.0.0
* @category type ids
*/
export type TypeId = typeof TypeId

/**
* @since 1.0.0
* @category models
*/
export class ContentlayerError
extends Schema.TaggedError<ContentlayerError>("@effect/contentlayer-core/ContentlayerError")("ContentlayerError", {
module: Schema.String,
method: Schema.String,
description: Schema.String,
cause: Schema.optional(Schema.Defect)
})
{
/**
* @since 1.0.0
*/
readonly [TypeId]: TypeId = TypeId
/**
* @since 1.0.0
*/
get message() {
return `${this.module}.${this.method}: ${this.description}`
}
}
Loading