Skip to content

Commit b113b5a

Browse files
authored
Merge pull request #73 from Vahor/add-esbuild-options-to-plugin-options
add esbuildOptions param to getConfig
2 parents 2a0405c + 8609c18 commit b113b5a

File tree

8 files changed

+42
-16
lines changed

8 files changed

+42
-16
lines changed

packages/@contentlayer/cli/src/commands/BuildCommand.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class BuildCommand extends BaseCommand {
2121
executeSafe = () =>
2222
pipe(
2323
this.clearCacheIfNeeded(),
24-
T.chain(() => core.getConfig({ configPath: this.configPath })),
24+
T.chain(() => core.getConfig({ configPath: this.configPath, esbuildOptions: { external: this.external } })),
2525
T.tap((config) => (config.source.options.disableImportAliasWarning ? T.unit : T.fork(core.validateTsconfig))),
2626
T.chain((config) => core.generateDotpkg({ config, verbose: this.verbose })),
2727
T.tap(core.logGenerateInfo),

packages/@contentlayer/cli/src/commands/DevCommand.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class DevCommand extends BaseCommand {
2222
executeSafe = () =>
2323
pipe(
2424
S.fromEffect(this.clearCacheIfNeeded()),
25-
S.chain(() => core.getConfigWatch({ configPath: this.configPath })),
25+
S.chain(() => core.getConfigWatch({ configPath: this.configPath, esbuildOptions: { external: this.external } })),
2626
S.tapSkipFirstRight(() => T.log(`Contentlayer config change detected. Updating type definitions and data...`)),
2727
S.tapRight((config) =>
2828
config.source.options.disableImportAliasWarning ? T.unit : T.fork(core.validateTsconfig),

packages/@contentlayer/cli/src/commands/PostInstallCommand.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@ export class PostInstallCommand extends BaseCommand {
1010
static paths = [['postinstall']]
1111

1212
executeSafe = () => {
13-
const { configPath } = this
13+
const { configPath, external } = this
1414
return pipe(
1515
T.gen(function* ($) {
1616
const artifactsDirPath = yield* $(core.ArtifactsDir.mkdir)
1717

18-
yield* $(generateTypes({ artifactsDirPath, moduleName: 'generated', configPath }))
18+
yield* $(
19+
generateTypes({
20+
artifactsDirPath,
21+
moduleName: 'generated',
22+
configOptions: { configPath, esbuildOptions: { external } },
23+
}),
24+
)
1925

2026
yield* $(addToplevelDotpkgToGitignore())
2127
}),
@@ -30,11 +36,11 @@ export class PostInstallCommand extends BaseCommand {
3036
const generateTypes = ({
3137
artifactsDirPath,
3238
moduleName,
33-
configPath,
39+
configOptions,
3440
}: {
3541
artifactsDirPath: string
3642
moduleName: string
37-
configPath?: string
43+
configOptions: Parameters<typeof core.getConfig>[0]
3844
}) =>
3945
T.gen(function* ($) {
4046
const dirPath = path.join(artifactsDirPath, moduleName)
@@ -51,7 +57,7 @@ const generateTypes = ({
5157

5258
if (indexDtsFileExists && typesDtsFileExists) return
5359

54-
const sourceEither = yield* $(pipe(core.getConfig({ configPath }), T.either))
60+
const sourceEither = yield* $(pipe(core.getConfig(configOptions), T.either))
5561
if (sourceEither._tag === 'Left') {
5662
if (sourceEither.left._tag === 'NoConfigFoundError') {
5763
yield* $(fs.writeFile(indexDtsFilePath, moduleStubFileIndexDts))

packages/@contentlayer/cli/src/commands/_BaseCommand.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ export abstract class BaseCommand extends Command {
2121
description: 'More verbose logging and error stack traces',
2222
})
2323

24+
external = Option.String('--external', {
25+
description: 'External dependencies to exclude from the config bundle',
26+
validator: t.isArray(t.isString()),
27+
required: false,
28+
})
29+
2430
abstract executeSafe: () => T.Effect<OT.HasTracer & HasClock & HasCwd & HasConsole & fs.HasFs, unknown, void>
2531

2632
execute = (): Promise<void> =>

packages/@contentlayer/core/src/getConfig/esbuild.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export abstract class EsbuildWatcher {
1111

1212
export type BuildResult = esbuild.BuildResult
1313
export type Plugin = esbuild.Plugin
14+
export type BuildOptions = esbuild.BuildOptions
1415

1516
export type EsbuildError = UnknownEsbuildError | KnownEsbuildError
1617

packages/@contentlayer/core/src/getConfig/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@ export type Config = {
3434

3535
export const getConfig = ({
3636
configPath,
37+
esbuildOptions,
3738
}: {
3839
/** Contentlayer config source path */
3940
configPath?: string
41+
esbuildOptions?: esbuild.BuildOptions
4042
}): T.Effect<OT.HasTracer & HasCwd & fs.HasFs, GetConfigError, Config> =>
4143
pipe(
42-
getConfigWatch({ configPath }),
44+
getConfigWatch({ configPath, esbuildOptions }),
4345
S.take(1),
4446
S.runCollect,
4547
T.map(Chunk.unsafeHead),
@@ -49,8 +51,10 @@ export const getConfig = ({
4951

5052
export const getConfigWatch = ({
5153
configPath: configPath_,
54+
esbuildOptions,
5255
}: {
5356
configPath?: string
57+
esbuildOptions?: esbuild.BuildOptions
5458
}): S.Stream<OT.HasTracer & HasCwd & fs.HasFs, never, E.Either<GetConfigError, Config>> => {
5559
const resolveParams = pipe(
5660
T.structPar({
@@ -66,6 +70,7 @@ export const getConfigWatch = ({
6670
S.chainMapEitherRight(({ configPath, outfilePath, cwd }) =>
6771
pipe(
6872
esbuild.makeAndSubscribe({
73+
...esbuildOptions,
6974
entryPoints: [configPath],
7075
entryNames: '[name]-[hash]',
7176
outfile: outfilePath,

packages/contentlayer-stackbit-yaml-generator/src/cli/DefaultCommand.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ export class DefaultCommand extends Command {
3535
validator: t.isString(),
3636
})
3737

38+
external = Option.String('--external', {
39+
description: 'External dependencies to exclude from the config bundle',
40+
validator: t.isArray(t.isString()),
41+
required: false,
42+
})
43+
3844
// TODO refactor similar to `@contentlayer2/cli`
3945
async execute() {
4046
try {
@@ -55,7 +61,7 @@ export class DefaultCommand extends Command {
5561

5662
executeSafe = (): T.Effect<OT.HasTracer & HasCwd & HasConsole & fs.HasFs, unknown, void> =>
5763
pipe(
58-
getConfig({ configPath: this.configPath }),
64+
getConfig({ configPath: this.configPath, esbuildOptions: { external: this.external } }),
5965
T.chain((config) =>
6066
T.struct({ source: T.succeed(config.source), schema: config.source.provideSchema(config.esbuildHash) }),
6167
),

packages/next-contentlayer/src/plugin.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,25 @@ import '@contentlayer2/utils/effect/Tracing/Enable'
33
import * as core from '@contentlayer2/core'
44
import { errorToString } from '@contentlayer2/utils'
55
import { E, OT, pipe, S, T } from '@contentlayer2/utils/effect'
6+
import type * as esbuild from 'esbuild'
67
import type { WebpackOptionsNormalized } from 'webpack'
78

89
import { checkConstraints } from './check-constraints.js'
910

1011
export type NextPluginOptions = {
1112
configPath?: string | undefined
13+
esbuildOptions?: Pick<esbuild.BuildOptions, 'external'> | undefined
1214
}
1315

1416
/** Seems like the next.config.js export function might be executed multiple times, so we need to make sure we only run it once */
1517
let contentlayerInitialized = false
1618

17-
const runContentlayerDev = async ({ configPath }: NextPluginOptions) => {
19+
const runContentlayerDev = async ({ configPath, esbuildOptions }: NextPluginOptions) => {
1820
if (contentlayerInitialized) return
1921
contentlayerInitialized = true
2022

2123
await pipe(
22-
core.getConfigWatch({ configPath }),
24+
core.getConfigWatch({ configPath, esbuildOptions }),
2325
S.tapSkipFirstRight(() => T.log(`Contentlayer config change detected. Updating type definitions and data...`)),
2426
S.tapRight((config) => (config.source.options.disableImportAliasWarning ? T.unit : T.fork(core.validateTsconfig))),
2527
S.chainSwitchMapEitherRight((config) => core.generateDotpkgStream({ config, verbose: false, isDev: true })),
@@ -29,12 +31,12 @@ const runContentlayerDev = async ({ configPath }: NextPluginOptions) => {
2931
)
3032
}
3133

32-
const runContentlayerBuild = async ({ configPath }: NextPluginOptions) => {
34+
const runContentlayerBuild = async ({ configPath, esbuildOptions }: NextPluginOptions) => {
3335
if (contentlayerInitialized) return
3436
contentlayerInitialized = true
3537

3638
await pipe(
37-
core.getConfig({ configPath }),
39+
core.getConfig({ configPath, esbuildOptions }),
3840
T.chain((config) => core.generateDotpkg({ config, verbose: false })),
3941
T.tap(core.logGenerateInfo),
4042
OT.withSpan('next-contentlayer:runContentlayerBuild'),
@@ -56,14 +58,14 @@ export const runBeforeWebpackCompile = async ({
5658
const isNextDev = mode === 'development'
5759
const isBuild = mode === 'production'
5860

59-
const { configPath } = pluginOptions
61+
const { configPath, esbuildOptions } = pluginOptions
6062

6163
if (isBuild) {
6264
checkConstraints()
63-
await runContentlayerBuild({ configPath })
65+
await runContentlayerBuild({ configPath, esbuildOptions })
6466
} else if (isNextDev && !devServerStartedRef.current) {
6567
devServerStartedRef.current = true
6668
// TODO also block here until first Contentlayer run is complete
67-
runContentlayerDev({ configPath })
69+
runContentlayerDev({ configPath, esbuildOptions })
6870
}
6971
}

0 commit comments

Comments
 (0)