Skip to content

Commit 002fb1b

Browse files
authored
try using esm for config (#10)
1 parent 45f515f commit 002fb1b

File tree

7 files changed

+31
-43
lines changed

7 files changed

+31
-43
lines changed

.changeset/twelve-peas-boil.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@effect/contentlayer": patch
3+
---
4+
5+
switch to esm builds for config

packages/core/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@
3939
},
4040
"dependencies": {
4141
"@parcel/watcher": "^2.5.1",
42-
"esbuild": "^0.25.2"
42+
"esbuild": "^0.25.2",
43+
"typescript": "^5.8.3"
44+
},
45+
"peerDependencies": {
46+
"effect": "^3.14"
4347
},
4448
"devDependencies": {
4549
"@effect/cli": "^0.59.10",
@@ -51,7 +55,6 @@
5155
"glob": "^11.0.1",
5256
"minimatch": "^10.0.1",
5357
"tsup": "^8.4.0",
54-
"typescript": "^5.8.3",
5558
"unist-util-remove": "^4.0.0",
5659
"vfile": "^6.0.3"
5760
}

packages/core/scripts/copy-package-json.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ const read = Effect.gen(function*() {
1010
const exports: Record<string, any> = {
1111
".": {
1212
types: "./index.d.ts",
13-
import: "./index.mjs",
14-
default: "./index.js"
13+
import: "./index.js",
14+
require: "./index.cjs"
1515
}
1616
}
1717
for (const entrypoint of entrypoints) {
1818
const name = entrypoint.replace(/\.ts$/, "")
1919
exports[`./${name}`] = {
2020
types: `./${name}.d.ts`,
21-
import: `./${name}.mjs`,
22-
default: `./${name}.js`
21+
import: `./${name}.js`,
22+
require: `./${name}.cjs`
2323
}
2424
}
2525

@@ -29,12 +29,13 @@ const read = Effect.gen(function*() {
2929
name: json.name,
3030
version: json.version,
3131
description: json.description,
32+
type: "module",
3233
bin: {
3334
contentlayer: "./bin.js"
3435
},
35-
main: "./index.js",
36+
main: "./index.cjs",
3637
types: "./index.d.ts",
37-
module: "./index.mjs",
38+
module: "./index.js",
3839
exports,
3940
engines: json.engines,
4041
repository: json.repository,
@@ -43,7 +44,9 @@ const read = Effect.gen(function*() {
4344
bugs: json.bugs,
4445
homepage: json.homepage,
4546
tags: json.tags,
46-
keywords: json.keywords
47+
keywords: json.keywords,
48+
dependencies: json.dependencies,
49+
peerDependencies: json.peerDependencies
4750
}
4851
})
4952

packages/core/src/Cli.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,10 @@ const command = Command.make("contentlayer", { configPath, watchMode }).pipe(
4444
),
4545
Layer.mergeAll(
4646
BuildOptions.Live({
47-
bundle: true,
48-
external: ["@effect/contentlayer", "effect"],
47+
bundle: false,
4948
entryNames: "[name]-[hash]",
5049
entryPoints: [configPath],
51-
format: "cjs",
50+
format: "esm",
5251
logLevel: "silent",
5352
metafile: true,
5453
outfile: ".contentlayer/compiled-contentlayer-config",

packages/core/src/ConfigBuilder.ts

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
/**
22
* @since 1.0.0
33
*/
4-
import * as NodeFileSystem from "@effect/platform-node/NodeFileSystem"
54
import * as NodePath from "@effect/platform-node/NodePath"
6-
import * as FileSystem from "@effect/platform/FileSystem"
75
import * as Path from "@effect/platform/Path"
86
import * as Arr from "effect/Array"
97
import * as Effect from "effect/Effect"
@@ -12,10 +10,8 @@ import * as Layer from "effect/Layer"
1210
import * as Option from "effect/Option"
1311
import * as Stream from "effect/Stream"
1412
import * as SubscriptionRef from "effect/SubscriptionRef"
15-
import * as Module from "node:module"
16-
import * as VM from "node:vm"
1713
import * as Config from "./Config.ts"
18-
import { ContentlayerError } from "./ContentlayerError.ts"
14+
import { ContentlayerError } from "./ContentlayerError.js"
1915
import type { EsbuildSuccess } from "./Esbuild.ts"
2016
import { Esbuild } from "./Esbuild.ts"
2117

@@ -61,7 +57,6 @@ export class ConfigBuilder extends Effect.Tag("@effect/contentlayer/ConfigBuilde
6157
>() {
6258
static Live = Layer.scoped(this, make).pipe(
6359
Layer.provide(Esbuild.Live),
64-
Layer.provide(NodeFileSystem.layer),
6560
Layer.provide(NodePath.layer)
6661
)
6762
}
@@ -74,7 +69,7 @@ const build = (
7469
): Effect.Effect<
7570
Option.Option<BuiltConfig>,
7671
ContentlayerError,
77-
FileSystem.FileSystem | Path.Path
72+
Path.Path
7873
> =>
7974
Effect.gen(function*() {
8075
const path = yield* Path.Path
@@ -100,24 +95,11 @@ export const fromPath = Effect.fnUntraced(function*(
10095
entrypoint: string,
10196
hash: string
10297
) {
103-
const fs = yield* FileSystem.FileSystem
10498
const path = yield* Path.Path
105-
106-
const content = yield* Effect.orDie(fs.readFileString(outPath))
10799
const resolvedPath = path.resolve(outPath)
108100

109-
const context = VM.createContext({
110-
...globalThis,
111-
process,
112-
__filename: resolvedPath,
113-
__dirname: path.dirname(resolvedPath)
114-
})
115-
context.require = Module.createRequire(entrypoint)
116-
context.module = { exports: {} }
117-
context.exports = context.module.exports
118-
119-
yield* Effect.try({
120-
try: () => VM.runInContext(content, context),
101+
const content = yield* Effect.tryPromise({
102+
try: () => import(new URL(`file://${resolvedPath}`) as any),
121103
catch: (cause) =>
122104
new ContentlayerError({
123105
module: "ConfigBuilder",
@@ -127,7 +109,7 @@ export const fromPath = Effect.fnUntraced(function*(
127109
})
128110
})
129111

130-
return configAsOption(context.module.exports.default).pipe(
112+
return configAsOption(content.default).pipe(
131113
Option.map((config): BuiltConfig => ({
132114
hash,
133115
config,

packages/core/tsup.config.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@ export default defineConfig({
44
entry: ["src/*.ts"],
55
format: ["esm", "cjs"],
66
tsconfig: "tsconfig.build.json",
7-
outExtension({ format }) {
8-
return {
9-
js: format === "cjs" ? ".js" : ".mjs"
10-
}
11-
},
7+
external: ["effect"],
128
shims: true,
139
splitting: true,
1410
sourcemap: true,

pnpm-lock.yaml

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)