Skip to content

Commit

Permalink
feat(remix-dev): deprecate serverBuildTarget
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDeBoey committed Jan 22, 2023
1 parent c08f30f commit 54ed18e
Show file tree
Hide file tree
Showing 5 changed files with 234 additions and 147 deletions.
64 changes: 55 additions & 9 deletions docs/file-conventions/remix-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ module.exports = {
});
},
serverBuildPath: "build/index.js",
serverBuildTarget: "node-cjs",
};
```

Expand Down Expand Up @@ -103,21 +102,28 @@ either a `.js` or `.ts` file extension.
## serverBuildDirectory

<docs-warning>This option is deprecated and will likely be removed in a future
stable release. Use [`serverBuildPath`][server-build-path] instead.</docs-warning>
stable release. Use [`serverBuildPath`][server-build-path]
instead.</docs-warning>

The path to the server build, relative to `remix.config.js`. Defaults to
"build". This needs to be deployed to your server.

## serverBuildPath

The path to the server build file, relative to `remix.config.js`. This file
should end in a `.js` extension and should be deployed to your server.

If omitted, the default build path will be based on your
[`serverBuildTarget`][server-build-target].
should end in a `.js` extension and should be deployed to your server. Defaults
to `"build/index.js"`.

## serverBuildTarget

<docs-warning>This option is deprecated and will likely be removed in a future
stable release. Use a combination of [`publicPath`][public-path],
[`serverBuildPath`][server-build-path], [`serverConditions`][server-conditions],
[`serverDependenciesToBundle`][server-dependencies-to-bundle]
[`serverMainFields`][server-main-fields], [`serverMinify`][server-minify],
[`serverModuleFormat`][server-module-format] and/or
[`serverPlatform`][server-platform] instead.</docs-warning>

The target of the server build. Defaults to `"node-cjs"`.

The `serverBuildTarget` can be one of the following:
Expand All @@ -130,11 +136,22 @@ The `serverBuildTarget` can be one of the following:
- [`"node-cjs"`][node-cjs]
- [`"vercel"`][vercel]

## serverConditions

The order of conditions to use when resolving server dependencies' `exports`
field in `package.json`.

## serverDependenciesToBundle

A list of regex patterns that determines if a module is transpiled and included in the server bundle. This can be useful when consuming ESM only packages in a CJS build, or when consuming packages with [CSS side-effect imports][css-side-effect-imports].
A list of regex patterns that determines if a module is transpiled and included
in the server bundle. This can be useful when consuming ESM only packages in a
CJS build, or when consuming packages with [CSS side effect
imports][css-side-effect-imports].

For example, the `unified` ecosystem is all ESM-only. Let's also say we're using a `@sindresorhus/slugify` which is ESM-only as well. Here's how you would be able to consume those packages in a CJS app without having to use dynamic imports:
For example, the `unified` ecosystem is all ESM-only. Let's also say we're using
a `@sindresorhus/slugify` which is ESM-only as well. Here's how you would be
able to consume those packages in a CJS app without having to use dynamic
imports:

```ts filename=remix.config.js lines=[8-13]
/** @type {import('@remix-run/dev').AppConfig} */
Expand All @@ -153,6 +170,29 @@ module.exports = {
};
```

If you want to bundle all server dependencies, you can
`serverDependenciesToBundle` to `"all"`.

## serverMainFields

The order of main fields to use when resolving server dependencies. Defaults to
`["main", "module"]` when `serverModuleFormat` is set to `"cjs"`. Defaults to
`["module", "main"]` when `serverModuleFormat` is set to `"esm"`.

## serverMinify

Whether to minify the server build in production or not. Defaults to `false`.

## serverModuleFormat

The output format of the server build, which can either be `"cjs"` or `"esm"`.
Defaults to `"cjs"`.

## serverPlatform

The platform the server build is targeting, which can either be `"neutral"` or
`"node"`. Defaults to `"node"`.

## watchPaths

An array, string, or async function that defines custom directories, relative to the project root, to watch while running [remix dev][remix-dev]. These directories are in addition to [`appDirectory`][app-directory].
Expand All @@ -173,8 +213,14 @@ There are a few conventions that Remix uses you should be aware of.
<docs-info>[Dilum Sanjaya][dilum-sanjaya] made [an awesome visualization][an-awesome-visualization] of how routes in the file system map to the URL in your app that might help you understand these conventions.</docs-info>

[minimatch]: https://www.npmjs.com/package/minimatch
[public-path]: #publicpath
[server-build-path]: #serverbuildpath
[server-build-target]: #serverbuildtarget
[server-conditions]: #serverconditions
[server-dependencies-to-bundle]: #serverdependenciestobundle
[server-main-fields]: #servermainfields
[server-minify]: #serverminify
[server-module-format]: #servermoduleformat
[server-platform]: #serverplatform
[arc]: https://arc.codes
[cloudflare-pages]: https://pages.cloudflare.com
[cloudflare-workers]: https://workers.cloudflare.com
Expand Down
6 changes: 6 additions & 0 deletions packages/remix-dev/__tests__/readConfig-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,14 @@ describe("readConfig", () => {
"serverBuildPath": Any<String>,
"serverBuildTarget": undefined,
"serverBuildTargetEntryModule": "export * from \\"@remix-run/dev/server-build\\";",
"serverConditions": undefined,
"serverDependenciesToBundle": Array [],
"serverEntryPoint": undefined,
"serverMainFields": Array [
"main",
"module",
],
"serverMinify": false,
"serverMode": "production",
"serverModuleFormat": "cjs",
"serverPlatform": "node",
Expand Down
19 changes: 3 additions & 16 deletions packages/remix-dev/compiler/compilerServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ const createEsbuildConfig = (
};
}

let isCloudflareRuntime = ["cloudflare-pages", "cloudflare-workers"].includes(
config.serverBuildTarget ?? ""
);
let isDenoRuntime = config.serverBuildTarget === "deno";

let { mode } = options;
let { rootDirectory } = config;
let outputCss = false;
Expand Down Expand Up @@ -84,11 +79,7 @@ const createEsbuildConfig = (
stdin,
entryPoints,
outfile: config.serverBuildPath,
conditions: isCloudflareRuntime
? ["worker"]
: isDenoRuntime
? ["deno", "worker"]
: undefined,
conditions: config.serverConditions,
platform: config.serverPlatform,
format: config.serverModuleFormat,
treeShaking: true,
Expand All @@ -100,12 +91,8 @@ const createEsbuildConfig = (
// PR makes dev mode behave closer to production in terms of dead
// code elimination / tree shaking is concerned.
minifySyntax: true,
minify: options.mode === "production" && isCloudflareRuntime,
mainFields: isCloudflareRuntime
? ["browser", "module", "main"]
: config.serverModuleFormat === "esm"
? ["module", "main"]
: ["main", "module"],
minify: options.mode === "production" && config.serverMinify,
mainFields: config.serverMainFields,
target: options.target,
loader: loaders,
bundle: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,8 @@ export function serverBareModulesPlugin(
}
}

switch (remixConfig.serverBuildTarget) {
// Always bundle everything for cloudflare.
case "cloudflare-pages":
case "cloudflare-workers":
case "deno":
return undefined;
if (remixConfig.serverDependenciesToBundle === "all") {
return undefined;
}

for (let pattern of remixConfig.serverDependenciesToBundle) {
Expand Down
Loading

0 comments on commit 54ed18e

Please sign in to comment.