-
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.
Merge pull request #17 from iFixit/add-tsup-bundler
- Loading branch information
Showing
22 changed files
with
1,164 additions
and
46 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 |
---|---|---|
|
@@ -2,4 +2,8 @@ node_modules | |
.git | ||
.gitignore | ||
*.md | ||
dist | ||
dist | ||
bin | ||
.env | ||
lh-config.js | ||
urls.json |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -35,4 +35,5 @@ urls.json | |
lh-config.js | ||
|
||
# TSC Output | ||
dist | ||
dist | ||
bin |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { build as tsupBuild } from 'tsup'; | ||
import { replaceTscAliasPaths } from 'tsc-alias'; | ||
import yargs from 'yargs'; | ||
|
||
const CORE_DIR = 'dist'; | ||
const SCRIPTS_DIR = 'bin'; | ||
const isDocker = process.env.DOCKER === 'true'; | ||
|
||
const argv = yargs(process.argv.slice(2)).options({ | ||
target: { | ||
alias: 't', | ||
type: 'string', | ||
description: 'Build target', | ||
choices: ['all', 'core', 'scripts'], | ||
default: 'all' | ||
}, | ||
silent: { | ||
alias: 's', | ||
type: 'boolean', | ||
description: 'Silent mode', | ||
default: false | ||
}, | ||
clean: { | ||
alias: 'c', | ||
type: 'boolean', | ||
description: 'Clean out files before build', | ||
default: false | ||
}, | ||
}).strict().parseSync(); | ||
|
||
const config = { | ||
splitting: false, // don't split code into chunks | ||
platform: 'node', // environment to build for | ||
target: 'node20', | ||
format: 'esm', | ||
silent: argv.silent, | ||
clean: argv.clean, | ||
external: isDocker ? ['@config'] : [], // Don't resolve @config modules when building in docker | ||
} | ||
|
||
const CORE_BUILD = { | ||
...config, | ||
outDir: CORE_DIR, | ||
entry: { | ||
index: 'src/core/index.ts', | ||
}, | ||
} | ||
|
||
const SCRIPTS_BUILD = { | ||
...config, | ||
outDir: SCRIPTS_DIR, | ||
entry: ['src/scripts/*'], | ||
} | ||
|
||
/** | ||
* Resolve paths after the build process because ESBuild only supports path | ||
* resolution natively during bundling. | ||
* | ||
* "@config/" paths are replaced with relative-path "./" to allow for | ||
* non-bundling scenarios like Docker. | ||
*/ | ||
async function replaceConfigAliasPaths(outDir) { | ||
await replaceTscAliasPaths({ | ||
configFile: 'tsconfig.json', | ||
outDir: outDir, | ||
watch: false, | ||
replacers: ['build.config-alias-replacer.cjs'] | ||
}); | ||
} | ||
|
||
async function build(buildConfig, buildTarget) { | ||
try { | ||
await tsupBuild(buildConfig); | ||
|
||
if (isDocker) { | ||
await replaceConfigAliasPaths(buildConfig.outDir); | ||
} | ||
} catch (e) { | ||
console.error(`Failed to build ${buildTarget}:`, e); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
switch(argv.target) { | ||
case 'all': | ||
await build(CORE_BUILD, 'core'); | ||
await build(SCRIPTS_BUILD, 'scripts'); | ||
break; | ||
case 'core': | ||
await build(CORE_BUILD, 'core'); | ||
break; | ||
case 'scripts': | ||
await build(SCRIPTS_BUILD, 'scripts'); | ||
break; | ||
} |
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,3 @@ | ||
# Do note wrap the values in quotes (single or double) | ||
DD_API_KEY= | ||
DD_APP_KEY= |
File renamed without changes.
File renamed without changes.
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,11 @@ | ||
#!/bin/bash | ||
|
||
root_dir=$(git rev-parse --show-toplevel) | ||
config_dir="$root_dir/src/config" | ||
|
||
# Run the docker image | ||
docker run --rm \ | ||
-v "$config_dir/lh-config.js":/app/dist/lh-config.js \ | ||
-v "$config_dir/urls.json":/app/dist/urls.json \ | ||
--env-file "$root_dir/.env" \ | ||
vigilo |
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,15 @@ | ||
#!/bin/bash | ||
|
||
set -ex | ||
|
||
root_dir=$(git rev-parse --show-toplevel) | ||
|
||
config_templates_dir="$root_dir/config-templates" | ||
config_dir="$root_dir/src/config" | ||
|
||
cp $config_templates_dir/.env.template $root_dir/.env | ||
|
||
cp $config_templates_dir/lh-config.template.js $config_dir/lh-config.js | ||
cp $config_templates_dir/urls.template.json $config_dir/urls.json | ||
|
||
pnpm install |
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 |
---|---|---|
@@ -1,20 +1,23 @@ | ||
{ | ||
"name": "vigilo", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Vigilo is a monitoring tool that automates Lighthouse audits and sends key metrics to Datadog. Designed to keep a vigilant eye on your web application's performance and accessibility.", | ||
"main": "dist/src/index.js", | ||
"main": "dist/index.js", | ||
"repository": "https://github.com/iFixit/vigilo.git", | ||
"type": "module", | ||
"engines": { | ||
"node": ">=18.0.0" | ||
}, | ||
"scripts": { | ||
"start": "node dist/src/index.js", | ||
"create-dashboard": "pnpm build && node dist/src/createDashboard.js", | ||
"update-metric-metadata": "node dist/src/updateDatadogMetricMetadata.js", | ||
"clean": "rm -rf dist", | ||
"build": "pnpm clean && tsc", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"setup": "dev-scripts/setup.sh", | ||
"start": "node dist/index.js", | ||
"start:docker": "dev-scripts/run-docker.sh", | ||
"create-dashboard": "pnpm build:scripts && node bin/createDashboard.js", | ||
"update-metric-metadata": "pnpm build:scripts && node bin/updateDatadogMetricMetadata.js", | ||
"clean": "rm -rf dist && rm -rf bin", | ||
"build": "pnpm clean && node build.js", | ||
"build:core": "node build.js --target=core", | ||
"build:scripts": "node build.js --target=scripts" | ||
}, | ||
"keywords": [], | ||
"author": "Angel de la Torre <[email protected]>", | ||
|
@@ -23,6 +26,8 @@ | |
"@tsconfig/node18": "^18.2.1", | ||
"@types/node": "^20.5.9", | ||
"@types/yargs": "^17.0.28", | ||
"tsc-alias": "^1.8.8", | ||
"tsup": "^8.0.1", | ||
"typescript": "^5.2.2" | ||
}, | ||
"dependencies": { | ||
|
Oops, something went wrong.