Skip to content

Commit

Permalink
js bundle creation
Browse files Browse the repository at this point in the history
  • Loading branch information
drewvolz committed May 26, 2024
1 parent d67030b commit 64a64ef
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 4 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,7 @@ jobs:
cache: 'npm'
- run: npm ci
- run: npm run build
- run: |
cd ./dist/source && zip -r bundle.zip .
mv bundle.zip ../../ && cd ../../
- run: npm run bundle

- name: Set sanitized branch name
id: sanitize_branch
Expand All @@ -136,5 +134,5 @@ jobs:
uses: actions/upload-artifact@v4
with:
name: ${{ steps.sanitize_branch.outputs.sanitized }}
path: bundle.zip
path: dist/bundle.js
overwrite: true
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"scripts": {
"build": "rm -rf ./dist ./.tsbuildinfo && tsc",
"build:watch": "tsc --watch",
"bundle": "node ./scripts/bundle.js",
"p": "npm run pretty -- --no-write --check",
"pretty": "prettier --write source",
"lint": "eslint --cache --max-warnings=0 .",
Expand Down
73 changes: 73 additions & 0 deletions scripts/bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import {readdirSync, writeFileSync, rmSync, existsSync, mkdirSync} from 'fs'

Check warning on line 1 in scripts/bundle.js

View workflow job for this annotation

GitHub Actions / lint (22.x)

'mkdirSync' is defined but never used
import {fileURLToPath} from 'url'
import path, {join} from 'path'
import {execSync} from 'child_process'

// Shims
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)

// Paths
const BUNDLE_PATH = join(__dirname, '../dist/bundle')
const FINAL_BUNDLE = join(__dirname, '../dist/bundle.js')
const ENTRY_FILE = join(BUNDLE_PATH, 'entry.js')

// Args
const commonFlags = ' --minify --platform=node --format=esm'
const commonDependencies =
'--alias:lodash-es=lodash --alias:jsdom=jsdom/lib/api.js --alias:koa-router=koa-router/lib/router.js --external:turndown'
const commonArgs = [commonFlags, commonDependencies].join(' ')

const runCommand = (command) => {
try {
execSync(command, {stdio: 'inherit'})
} catch (error) {
console.error(`Error executing command: ${command}`, error)
process.exit(1)
}
}

// Clean the bundle directory
const cleanBundle = ({startup}) => {
if (existsSync(BUNDLE_PATH)) {
rmSync(BUNDLE_PATH, {recursive: true, force: true})
}
if (existsSync(FINAL_BUNDLE) && startup === true) {
rmSync(FINAL_BUNDLE)
}
console.log('Bundle directory cleaned.')
}

// Transform the files
const transformFiles = () => {
runCommand(`esbuild dist/source/**/**/*.js --bundle --outdir=dist/bundle ${commonArgs}`)
console.log('Files transformed.')
}

// Create the entry file
const createEntryFile = () => {
const files = readdirSync(BUNDLE_PATH, {recursive: true})
const imports = files
.filter((file) => file.endsWith('.js'))
.map((file) => `import '${join('../bundle', file)}'`)
.join('\n')

writeFileSync(ENTRY_FILE, imports)
console.log('entry.js has been created successfully.')
}

// Combine the bundles
const combineBundles = () => {
runCommand(`esbuild dist/bundle/entry.js --bundle --outfile=dist/bundle.js ${commonArgs}`)
console.log('Final bundle has been created successfully.')
}

const run = () => {
cleanBundle({startup: true})
transformFiles()
createEntryFile()
combineBundles()
cleanBundle({startup: false})
}

run()

0 comments on commit 64a64ef

Please sign in to comment.