-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(build): add logic for non-bundling builds
For Docker builds, we don't want to bundle the config files into the dist/index.js file. By bundling them, we lose the ability to change them without rebuilding the image. Rebuilding the image every time we want to change the config files is not ideal and would prevent us from using the image for other sites. Solving this problem is a bit tricky because we want to use the same build script for both Docker and non-Docker builds, keep the independent core and scripts bundling, and retain the path aliasing functionality. To achieve all of this, I used the following: - tsup's 'external' option to exclude the "@config" modules from the bundle - The tsc-alias package to resolve the "@config" paths to a relative path - The process.env.DOCKER variable to determine whether to use tsup's 'external' option or not (this variable is set in the Dockerfile). By doing all of this, we can now pass the config files into the container as volumes and change them without rebuilding the image. At the same time, we can still use the same build script for local development and retain the simplicity of standalone builds. The use of tsc-alias was discovered in this issue: evanw/esbuild#394 (comment). Without it, "@config/*" paths would remain in the bundle and would cause errors as there is no way for "@config" to be resolved as a local path.
- Loading branch information
Showing
4 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* File must be named with a .cjs extension to be recognized by tsc-alias and | ||
* work with the type: "module" setting in package.json. | ||
* @see https://github.com/justkey007/tsc-alias/discussions/73#discussioncomment-4416038 | ||
*/ | ||
const fs = require('fs'); | ||
|
||
/** | ||
* This script replaces '@config/' aliases with relative paths './' in the | ||
* provided file. | ||
* This is necessary for non-bundling scenarios like Docker, where we want to | ||
* use files from the host machine. | ||
* Without this, the referenced files would be bundled, which is not the | ||
* desired behavior in this case. | ||
*/ | ||
function configAliasReplace({ orig, file }) { | ||
const fileContents = fs.readFileSync(file, 'utf8'); | ||
const newContents = fileContents.replace(/@config\//g, './'); | ||
fs.writeFileSync(file, newContents, 'utf8'); | ||
return orig; | ||
} | ||
|
||
exports.default = configAliasReplace; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.