Skip to content

Commit

Permalink
can build ts scripts into a single js file
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-burel committed Sep 6, 2021
1 parent 0acddee commit 1589e55
Show file tree
Hide file tree
Showing 8 changed files with 112,489 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .vn/scripts/README.md
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.
16 changes: 16 additions & 0 deletions .vn/scripts/build-scripts.sh
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"
Loading

0 comments on commit 1589e55

Please sign in to comment.