-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
can build ts scripts into a single js file
- Loading branch information
1 parent
0acddee
commit 1589e55
Showing
8 changed files
with
112,489 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,21 @@ | ||
# How to develop a script | ||
|
||
## Separate build for scripts | ||
|
||
You may want to enjoy TypeScript within scripts, and also reuse | ||
handy helpers you created in the app, like the Mongoose connection code. | ||
|
||
To do so, you need a build-step outside of Next, that mimicks most of the same feature | ||
but can run independently. | ||
|
||
To achieve this, you need a mix between Webpack and TypeScript. Webpack is responsible | ||
for creating one bundled .js file, TypeScript for transpiling the code. | ||
|
||
Hopefully, Vercel got us covered already for the config: see https://github.com/vercel/ncc | ||
|
||
## Step by step | ||
|
||
- Code your script in "./ts-sources" | ||
- Build the scripts using `yarn run build:scripts` => you end up with a few .js file at the root. | ||
- Commit the built JS file to the git repository. Note: usually, we don't commit built files, | ||
but it's ok for scripts because they don't change often. |
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,16 @@ | ||
#! /bin/bash | ||
# Utility to run NCC on multiple files | ||
# Will transform ./ts-sources/foobar.ts into a built ./foobar.js | ||
# See https://github.com/vercel/ncc | ||
# NOTE: you are expected to run "yarn run build:scripts" from the project root for path to be correct | ||
vn_scripts_dir=./.vn/scripts | ||
for f in "$vn_scripts_dir/ts-sources"/* | ||
do | ||
echo "Build $f" | ||
fname=`basename "$f"` | ||
fname_no_ext=`echo "$fname" | cut -d. -f1` | ||
echo "Filename $fname, without ext $fname_no_ext" | ||
yarn run ncc build "$f" --no-cache --out "$vn_scripts_dir/dist" | ||
mv "$vn_scripts_dir/dist/index.js" "$vn_scripts_dir/$fname_no_ext".js | ||
done | ||
rm -R "$vn_scripts_dir/dist" |
Oops, something went wrong.