Skip to content

Commit

Permalink
refactor: use @rollup/plugin-replace for name and version
Browse files Browse the repository at this point in the history
  • Loading branch information
Septh committed Oct 4, 2024
1 parent c85ab2d commit 7c1808f
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 18 deletions.
23 changes: 23 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"devDependencies": {
"@rollup/plugin-commonjs": "^28.0.0",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-replace": "^6.0.1",
"@rollup/plugin-terser": "^0.4.4",
"@types/node": "^22.7.2",
"rimraf": "^6.0.1",
Expand Down
32 changes: 26 additions & 6 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import path from 'node:path'
import { readFile } from 'fs/promises'
import { createRequire } from 'node:module'
import { nodeExternals } from 'rollup-plugin-node-externals'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import _commonsJS from '@rollup/plugin-commonjs'
import _commonsJs from '@rollup/plugin-commonjs'
import _replace from '@rollup/plugin-replace'
import { sucrase } from 'rollup-plugin-fast-typescript'
import _terser from '@rollup/plugin-terser'
import { defineConfig } from 'rollup'
Expand All @@ -13,9 +15,25 @@ import { defineConfig } from 'rollup'
* @see {@link https://github.com/rollup/plugins/issues/1541#issuecomment-1837153165}
*/
const fix = (f) => /** @type {T} */(f)
const commonJS = fix(_commonsJS)
const commonJs = fix(_commonsJs)
const replace = fix(_replace)
const terser = fix(_terser)

/** @type {import('#package.json')} */
const { name, version } = createRequire(import.meta.url)('#package.json')

/** @type {import('@rollup/plugin-replace').RollupReplaceOptions} */
const replacements = {
include: [ '**/index.js', '**/register.js' ],
preventAssignment: true,
sourceMap: true,
delimiters: [ '', '' ],
values: {
__TSRUN_NAME__: name.split('/')[1],
__TSRUN_VERSION__: version
}
}

export default defineConfig([

// bin/index.js
Expand All @@ -25,11 +43,12 @@ export default defineConfig([
file: 'bin/index.js',
format: 'esm',
sourcemap: true,
sourcemapExcludeSources: true
sourcemapExcludeSources: true,
},
plugins: [
nodeExternals(),
sucrase()
sucrase(),
replace(replacements)
],
external: /\/register.js$/
},
Expand All @@ -49,8 +68,9 @@ export default defineConfig([
plugins: [
nodeExternals(),
sucrase(),
replace(replacements),
{
name: 'emit-esm-declaration-file',
name: 'emit-dst',
async generateBundle() {
this.emitFile({
type: 'asset',
Expand Down Expand Up @@ -79,7 +99,7 @@ export default defineConfig([
plugins: [
nodeExternals(),
nodeResolve(),
commonJS(),
commonJs(),
sucrase(),
terser(),

Expand Down
16 changes: 5 additions & 11 deletions source/bin/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env node
import { createRequire } from 'node:module'
import { resolve } from 'node:path'
import { pathToFileURL } from 'node:url'

Expand All @@ -12,15 +11,10 @@ if (argv.length >= 3 && !argv[2].startsWith('-')) {
await import('../lib/register.js')
await import(pathToFileURL(argv[1]).href)
}
else {
const { name, version } = createRequire(import.meta.url)('#package.json') as typeof import('#package.json')
const shortName = name.split('/').pop()!

else if (argv.length === 3 && /^-v|--version$/.test(argv[2])) {
// Respond to `ts-run [-v | --version]` or print usage.
if (argv.length === 3 && /^-v|--version$/.test(argv[2])) {
console.log(`Node.js v${process.versions.node}, ${shortName} v${version}`)
}
else {
console.log(`Usage: ${shortName} -v | <script.ts>`)
}
console.log(`Node.js v${process.versions.node}, __TSRUN_NAME__ v__TSRUN_VERSION__`)
}
else {
console.log('Usage: __TSRUN_NAME__ -v | <script.ts>')
}
2 changes: 1 addition & 1 deletion source/lib/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { installCjsHooks } from './cjs-hooks.js'
const [ major, minor, patch ] = process.versions.node.split('.').map(Number)
assert(
major >= 21 || (major === 20 && minor >= 6) || (major === 18 && minor >= 19),
`Unsupported NodeJS version ${major}.${minor}.${patch}. ts-run requires Node 18.19.0+, Node 20.6.0+ or Node 21+.`
`Unsupported NodeJS version ${major}.${minor}.${patch}. __TSRUN_NAME__ requires Node 18.19.0+, Node 20.6.0+ or Node 21+.`
)

// Enable source map support.
Expand Down

0 comments on commit 7c1808f

Please sign in to comment.