Skip to content

Commit

Permalink
Update build & tests systems (#736)
Browse files Browse the repository at this point in the history
* use rollup esbuild plugin

* bundle sentry separately

* bundle rita separately

* remove log

* update configs

* remove leftover log

* move build scripts to typescript

* replace ts-node with esbuild-runner

* replace ts-jest

* convert install-compiler to typescript

* remove obsolete babel config
  • Loading branch information
Glinkis committed Apr 10, 2022
1 parent 1d25fc6 commit d6fbbbf
Show file tree
Hide file tree
Showing 10 changed files with 311 additions and 244 deletions.
6 changes: 0 additions & 6 deletions .babelrc

This file was deleted.

2 changes: 0 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import './sentry'

export * from './alchemist/_common'
export * from './alchemist/alchemistCustomers'
export * from './alchemist/alchemistData'
Expand Down
2 changes: 2 additions & 0 deletions main.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
<script async src="https://unpkg.com/core-js-bundle@3.0.1/minified.js"></script>

<!-- Bundle -->
<script defer src="sentry.js"></script>
<script defer src="rita.js"></script>
<script defer src="main.js"></script>

<!-- Global site tag (gtag.js) - Google Analytics -->
Expand Down
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
},
"homepage": "https://github.com/ryceg/Eigengrau-s-Essential-Establishment-Generator/blob/master/README.md",
"scripts": {
"install-compiler": "node scripts/install-compiler",
"build": "cross-env NODE_ENV=production node scripts/build",
"start": "node scripts/build --watch",
"install-compiler": "node -r esbuild-runner/register scripts/install-compiler",
"build": "cross-env NODE_ENV=production node -r esbuild-runner/register scripts/build",
"start": "node -r esbuild-runner/register scripts/build --watch",
"test": "jest",
"lint": "eslint \"**/*.{js,ts}\"",
"type": "tsc"
Expand All @@ -31,27 +31,28 @@
"*.{js,ts}": "eslint --fix"
},
"jest": {
"preset": "ts-jest"
"transform": {
"\\.ts$": "esbuild-runner/jest"
}
},
"dependencies": {
"@babel/preset-typescript": "^7.16.7",
"@rollup/plugin-babel": "^5.3.1",
"@rollup/plugin-commonjs": "^21.0.3",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^13.1.3",
"@rollup/plugin-replace": "^4.0.0",
"@sentry/browser": "^6.19.4",
"@sentry/integrations": "^6.19.4",
"@sentry/tracing": "^6.19.4",
"@types/jest": "^27.4.1",
"@types/jquery": "^3.5.5",
"@types/twine-sugarcube": "^2.33.1",
"@types/yauzl": "^2.9.2",
"@typescript-eslint/eslint-plugin": "^5.18.0",
"@typescript-eslint/parser": "^5.18.0",
"chalk": "^4.0.0",
"chokidar": "^3.5.1",
"cpy": "^8.1.1",
"cross-env": "^7.0.3",
"esbuild": "^0.14.34",
"esbuild-runner": "^2.2.1",
"eslint": "^8.12.0",
"eslint-config-standard": "^16.0.2",
"eslint-plugin-function-call-context": "^1.0.2",
Expand All @@ -65,8 +66,7 @@
"lint-staged": "^12.3.7",
"rita": "^2.4.91",
"rollup": "^2.36.1",
"rollup-plugin-terser": "^7.0.2",
"ts-jest": "^27.1.4",
"rollup-plugin-esbuild-transform": "^1.3.2",
"typescript": "^4.6.3",
"yauzl": "^2.10.0"
},
Expand Down
38 changes: 19 additions & 19 deletions scripts/build.js → scripts/build.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const fs = require('fs')
const path = require('path')
const spawn = require('child_process').spawn
const utils = require('./utils')
const chokidar = require('chokidar')
const cpy = require('cpy')
const rollup = require('rollup')
import { spawn } from 'child_process'
import fs from 'fs'
import path from 'path'
import chokidar from 'chokidar'
import cpy from 'cpy'
import { OutputOptions } from 'rollup'
import * as rollup from 'rollup'
import utils from './utils'

const tweego = path.resolve(utils.twineFolder, 'tweego')

Expand Down Expand Up @@ -41,13 +41,13 @@ const verifyInstall = () => {
}
}

const runTweego = (args) => {
const runTweego = (args: string[]) => {
// Run tweego with arguments.
utils.logAction('Running tweego!')
const tweegoProcess = spawn(tweego, args)

// Log messages from the tweego process.
tweegoProcess.stderr.on('data', data => {
tweegoProcess.stderr.on('data', (data) => {
const messages = data.toString().split('\n')

for (const message of messages) {
Expand All @@ -66,7 +66,7 @@ const runTweego = (args) => {
})
}

const copyFiles = async (args) => {
const copyFiles = async (args: string[]) => {
const watch = args.includes('--watch')
const source = path.resolve(__dirname, '../src/Resources')
const destination = path.resolve(__dirname, '../gh-pages/src/Resources')
Expand All @@ -88,21 +88,21 @@ const copyFiles = async (args) => {
}
}

const bundleJS = async (args) => {
const bundleJS = async (args: string[]) => {
const watch = args.includes('--watch')
const configs = require('./rollup.config')
const { configs } = await import('./rollup.config')
if (watch) {
for (const config of configs) {
const watcher = rollup.watch(config)
watcher.on('event', async ({ code, result, error }) => {
switch (code) {
watcher.on('event', async (event) => {
switch (event.code) {
case 'BUNDLE_END': {
await result.write(config.output)
result.close()
await event.result.write(config.output as OutputOptions)
event.result.close()
break
}
case 'ERROR': {
utils.logError(error)
utils.logError(event.error)
break
}
}
Expand All @@ -112,7 +112,7 @@ const bundleJS = async (args) => {
} else {
for (const { output, ...input } of configs) {
const bundled = await rollup.rollup(input)
await bundled.write(output)
await bundled.write(output as OutputOptions)
}
}
}
Expand Down
49 changes: 27 additions & 22 deletions scripts/install-compiler.js → scripts/install-compiler.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const path = require('path')
const http = require('https')
const fs = require('fs')
const yauzl = require('yauzl')
const utils = require('./utils')
import path from 'path'
import http from 'http'
import https from 'https'
import fs from 'fs'
import yauzl from 'yauzl'
import utils from './utils'

fs.mkdirSync(utils.twineFolder, { recursive: true })

Expand All @@ -19,19 +19,19 @@ downloadAndExtract(tweegoLink, utils.tweegoZip).then(() => {
downloadAndExtract(utils.links.storyFormats, utils.formatsZip, utils.formatsFolder).then(() => {
utils.logSuccess('All done!')
if (process.platform === 'win32') {
logWarning('If you receive an error about SugarCube not being available, rename the folder ' + '"sugarcube-2" to "SugarCube" and try again.')
utils.logWarning('If you receive an error about SugarCube not being available, rename the folder ' + '"sugarcube-2" to "SugarCube" and try again.')
}
})
})

function downloadAndExtract (link, filePath, outFolder) {
function downloadAndExtract (link: string, filePath: string, outFolder?: string) {
return new Promise((resolve, reject) => {
http.get(link, request).on('error', utils.logError)
https.get(link, request).on('error', utils.logError)

function request (response) {
function request (response: http.IncomingMessage) {
// Recursively follow potential redirects.
if (response.statusCode === 302) {
http.get(response.headers.location, request)
if (response.statusCode === 302 && response.headers.location) {
https.get(response.headers.location, request)
return
}

Expand All @@ -57,16 +57,17 @@ function downloadAndExtract (link, filePath, outFolder) {
yauzl.open(filePath, { lazyEntries: true }, (error, zip) => {
if (error) throw error

utils.logAction('Unzipping...\n')

zip.on('entry', entry => isFolder(entry) ? zip.readEntry() : unzipEntry(zip, entry))
zip.on('end', resolve)
zip.readEntry()
if (zip) {
utils.logAction('Unzipping...\n')
zip.on('entry', entry => isFolder(entry) ? zip.readEntry() : unzipEntry(zip, entry))
zip.on('end', resolve)
zip.readEntry()
}
})
}
})

function unzipEntry (zip, entry) {
function unzipEntry (zip: yauzl.ZipFile, entry: yauzl.Entry) {
zip.openReadStream(entry, (error, stream) => {
if (error) throw error

Expand All @@ -77,21 +78,25 @@ function downloadAndExtract (link, filePath, outFolder) {
const filePath = path.resolve(folder, entry.fileName)
const writeStream = fs.createWriteStream(filePath)

stream.on('end', () => zip.readEntry())
stream.pipe(writeStream)
if (stream) {
stream.on('end', () => zip.readEntry())
stream.pipe(writeStream)
}
})
}
}

function getTweegoLink () {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const platform = utils.links.tweego[process.platform]
return platform[process.arch] || platform.x86
}

function isFolder (entry) {
function isFolder (entry: yauzl.Entry) {
return /\/$/.test(entry.fileName)
}

function getFileDirectory (file) {
function getFileDirectory (file: string) {
return file.substring(0, file.lastIndexOf('/'))
}
40 changes: 0 additions & 40 deletions scripts/rollup.config.js

This file was deleted.

51 changes: 51 additions & 0 deletions scripts/rollup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Plugin, RollupOptions } from 'rollup'
// TODO: Replace this when "rollup-plugin-esbuild" fixes their sourcemap issues.
import esbuild, { Options } from 'rollup-plugin-esbuild-transform'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'

const env = process.env.NODE_ENV
const isProduction = env === 'production'

const sharedOptions: Options = {
minify: isProduction,
target: 'es2016',
define: {
// Avoids issues with the Node-specific variable `process`.
'process.env.NODE_ENV': JSON.stringify(env)
}
}

const plugins: Plugin[] = [
esbuild([
{ loader: 'json', ...sharedOptions },
{ loader: 'ts', ...sharedOptions }
]),
nodeResolve({ browser: true }),
commonjs({ extensions: ['.js', '.json'] })
]

export const configs: RollupOptions[] = [
{
input: 'src/main.ts',
plugins,
external: ['@lib'],
output: { format: 'iife', file: 'src/init.js', globals: { '@lib': 'lib' }, sourcemap: 'inline' }
},
{
input: 'lib/index.ts',
external: ['rita'],
plugins,
output: { format: 'iife', file: './gh-pages/main.js', globals: { rita: 'RiTa' }, name: 'lib', sourcemap: true }
},
{
input: 'lib/sentry.ts',
plugins,
output: { format: 'iife', file: './gh-pages/sentry.js', sourcemap: false }
},
{
input: 'rita',
plugins,
output: { format: 'iife', file: './gh-pages/rita.js', name: 'RiTa', sourcemap: false }
}
]
17 changes: 8 additions & 9 deletions scripts/utils.js → scripts/utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/* eslint-disable no-console */
/* eslint-disable @typescript-eslint/no-var-requires */
const path = require('path')
const chalk = require('chalk')
import path from 'path'
import chalk from 'chalk'

// Folder to store the twine-related dependencies in.
const twineFolder = path.resolve(__dirname, '..', '.twine')

module.exports = {
export default {
// Useful paths.
twineFolder,
tweegoZip: path.resolve(twineFolder, 'tweego.zip'),
Expand Down Expand Up @@ -36,19 +35,19 @@ module.exports = {
logClear () {
console.clear()
},
logError (...args) {
logError (...args: unknown[]) {
console.log(chalk.red(...args))
},
logWarning (...args) {
logWarning (...args: unknown[]) {
console.log(chalk.yellow(...args))
},
logAction (...args) {
logAction (...args: unknown[]) {
console.log(chalk.blue(...args))
},
logInfo (...args) {
logInfo (...args: unknown[]) {
console.log(chalk.grey(...args))
},
logSuccess (...args) {
logSuccess (...args: unknown[]) {
console.log(chalk.green(...args))
}
}
Loading

0 comments on commit d6fbbbf

Please sign in to comment.