Skip to content

Commit

Permalink
put all imports in top-level imports field
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Oct 19, 2023
1 parent a99fbac commit bae0075
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 294 deletions.
45 changes: 0 additions & 45 deletions src/add-paths-to-tsconfig.ts

This file was deleted.

33 changes: 14 additions & 19 deletions src/built-imports.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
// merge tshy.imports with package.json imports

import { Package, TshyConfig } from './types.js'
import { Package } from './types.js'

// strip the ./src/ and turn ts extension into js
const stripSrc = (ti: TshyConfig['imports']): Package['imports'] => {
if (!ti) return undefined
// strip the ./src/ and turn ts extension into js for built imports
// leave unbuilt imports alone, they'll be symlinked
export default (pkg: Package): Package['imports'] => {
const { imports } = pkg
if (!imports) return undefined
return Object.fromEntries(
Object.entries(ti).map(([k, v]) => [
Object.entries(imports).map(([k, v]) => [
k,
'./' +
v
.substring('./src/'.length)
.replace(/\.([cm]?)ts$/, '.$1js')
.replace(/\.tsx$/, '.js'),
typeof v === 'string' && v.startsWith('./src/')
? './' +
v
.substring('./src/'.length)
.replace(/\.([cm]?)ts$/, '.$1js')
.replace(/\.tsx$/, '.js')
: v,
])
)
}

export default (pkg: Package): Package['imports'] => {
const { tshy } = pkg
if (!tshy?.imports && !pkg.imports) return undefined
return {
...pkg.imports,
...stripSrc(tshy?.imports),
}
}
18 changes: 16 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// get the config and package and stuff

import chalk from 'chalk'
import * as console from './console.js'
import fail from './fail.js'
import pkg from './package.js'
import sources from './sources.js'
Expand All @@ -23,14 +25,26 @@ const validConfig = (e: any): e is TshyConfig =>
(e.dialects === undefined || validDialects(e.dialects)) &&
validExtraDialects(e) &&
validBoolean(e, 'selfLink') &&
validBoolean(e, 'main') &&
validImports(e, pkg)
validBoolean(e, 'main')

const getConfig = (
pkg: Package,
sources: Set<string>
): TshyConfig => {
const tshy: TshyConfig = validConfig(pkg.tshy) ? pkg.tshy : {}
const ti = tshy as TshyConfig & { imports?: any }
if (ti.imports) {
console.debug(
chalk.cyan.dim('imports') +
' moving from tshy config to top level'
)
pkg.imports = {
...pkg.imports,
...ti.imports,
}
delete ti.imports
}
validImports(pkg)
if (tshy.exports) return tshy
const e: Exclude<TshyConfig['exports'], undefined> = {
'./package.json': './package.json',
Expand Down
6 changes: 2 additions & 4 deletions src/tsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { join } from 'node:path/posix'
import * as console from './console.js'

// the commonjs build needs to exclude anything that will be polyfilled
import { addToFile, addToObject } from './add-paths-to-tsconfig.js'
import config from './config.js'
import polyfills from './polyfills.js'

Expand All @@ -21,7 +20,7 @@ const {
commonjsDialects = [],
} = config

const recommended: Record<string, any> = addToObject({
const recommended: Record<string, any> = {
compilerOptions: {
declaration: true,
declarationMap: true,
Expand All @@ -38,7 +37,7 @@ const recommended: Record<string, any> = addToObject({
strict: true,
target: 'es2022',
},
})
}

const build: Record<string, any> = {
extends: '../tsconfig.json',
Expand Down Expand Up @@ -101,7 +100,6 @@ if (!existsSync('tsconfig.json')) {
writeConfig('../tsconfig', recommended)
} else {
console.debug('using existing tsconfig.json')
addToFile()
}
for (const f of readdirSync('.tshy')) {
unlinkSync(resolve('.tshy', f))
Expand Down
1 change: 0 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import type {

export type TshyConfig = {
exports?: Record<string, TshyExport>
imports?: Record<string, string>
dialects?: Dialect[]
selfLink?: boolean
main?: boolean
Expand Down
34 changes: 16 additions & 18 deletions src/valid-imports.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
import fail from './fail.js'
import { Package, TshyConfig } from './types.js'
import { Package } from './types.js'
import validExternalExport from './valid-external-export.js'

// this validates the tshy.imports field.
export default ({ imports }: TshyConfig, pkg: Package) => {
// validate the package.imports field
export default (pkg: Package) => {
const { imports } = pkg
if (imports === undefined) return true
const { imports: pkgImports = {} } = pkg
if (typeof imports !== 'object' || Array.isArray(imports)) {
fail('tshy.imports must be an object if specified')
if (Array.isArray(imports) || typeof imports !== 'object') {
fail(
'invalid imports object, must be Record<string, Import>, ' +
`got: ${JSON.stringify(imports)}`
)
return process.exit(1)
}

for (const [i, v] of Object.entries(imports)) {
if (i in pkgImports) {
fail(
'tshy.imports keys must not appear in top-level imports, ' +
'found in both: ' +
JSON.stringify(i)
)
return process.exit(1)
}
if (!i.startsWith('#') || i === '#' || i.startsWith('#/')) {
fail('invalid tshy.imports module specifier: ' + i)
fail('invalid imports module specifier: ' + i)
return process.exit(1)
}
if (typeof v !== 'string' || !v.startsWith('./src/')) {
if (typeof v === 'string') continue
if (!validExternalExport(v)) {
fail(
'tshy.imports values must start with "./src/", ' +
'got: ' +
`unbuilt package.imports ${i} must not be in ./src, ` +
'and imports in ./src must be string values. got: ' +
JSON.stringify(v)
)
return process.exit(1)
Expand Down
4 changes: 2 additions & 2 deletions tap-snapshots/test/config.ts.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ tshy.exports ./blah unbuilt exports must not be in ./src, and exports in src mus
`

exports[`test/config.ts > TAP > {"config":{"imports":"blah"},"sources":[],"ok":false} > must match snapshot 1`] = `
tshy.imports must be an object if specified
invalid imports module specifier: 0
`

exports[`test/config.ts > TAP > {"config":{"imports":["blah"]},"sources":[],"ok":false} > must match snapshot 1`] = `
tshy.imports must be an object if specified
invalid imports module specifier: 0
`

exports[`test/config.ts > TAP > {"config":{"main":"blah"},"sources":[],"ok":false} > must match snapshot 1`] = `
Expand Down
28 changes: 10 additions & 18 deletions tap-snapshots/test/valid-imports.ts.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,22 @@
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
exports[`test/valid-imports.ts > TAP > {"config":{"imports":"asdf"},"pkg":{}} > failure message 1`] = `
tshy.imports must be an object if specified
exports[`test/valid-imports.ts > TAP > {"pkg":{"imports":"asdf"}} > failure message 1`] = `
invalid imports object, must be Record<string, Import>, got: "asdf"
`

exports[`test/valid-imports.ts > TAP > {"config":{"imports":[]},"pkg":{}} > failure message 1`] = `
tshy.imports must be an object if specified
exports[`test/valid-imports.ts > TAP > {"pkg":{"imports":[]}} > failure message 1`] = `
invalid imports object, must be Record<string, Import>, got: []
`

exports[`test/valid-imports.ts > TAP > {"config":{"imports":{"#":"y"}},"pkg":{}} > failure message 1`] = `
invalid tshy.imports module specifier: #
exports[`test/valid-imports.ts > TAP > {"pkg":{"imports":{"#":"y"}}} > failure message 1`] = `
invalid imports module specifier: #
`

exports[`test/valid-imports.ts > TAP > {"config":{"imports":{"#x":"./src/x"}},"pkg":{"imports":{"#x":{}}}} > failure message 1`] = `
tshy.imports keys must not appear in top-level imports, found in both: "#x"
exports[`test/valid-imports.ts > TAP > {"pkg":{"imports":{"#x":["./src/x"]}}} > failure message 1`] = `
unbuilt package.imports #x must not be in ./src, and imports in ./src must be string values. got: ["./src/x"]
`

exports[`test/valid-imports.ts > TAP > {"config":{"imports":{"#x":"y"}},"pkg":{}} > failure message 1`] = `
tshy.imports values must start with "./src/", got: "y"
`

exports[`test/valid-imports.ts > TAP > {"config":{"imports":{"#x":{}}},"pkg":{}} > failure message 1`] = `
tshy.imports values must start with "./src/", got: {}
`

exports[`test/valid-imports.ts > TAP > {"config":{"imports":{"x":"y"}},"pkg":{}} > failure message 1`] = `
invalid tshy.imports module specifier: x
exports[`test/valid-imports.ts > TAP > {"pkg":{"imports":{"x":"y"}}} > failure message 1`] = `
invalid imports module specifier: x
`
Loading

0 comments on commit bae0075

Please sign in to comment.