Skip to content

Commit

Permalink
Update: add import as const feature
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Dec 19, 2020
1 parent e2f717d commit 4edd9e7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,23 @@ And then add this to `tsconfig.json`.
```

If you're using VSCode, [switch to workspace version](https://code.visualstudio.com/docs/typescript/typescript-compiling#_using-the-workspace-version-of-typescript).

## `.const.toml`

If the file name ends with `.const.toml`, the typings will become as if you are using `as const`.

For example it will be like below.

`test.toml` and `test.const.toml`
```toml
key = 'val'
```

`index.ts`
```ts
import test from './test.toml'
import testConst from './test.const.toml'

type Test = typeof test // { key: string }
type TestConst = typeof testConst // { readonly key: 'val' }
```
5 changes: 3 additions & 2 deletions src/createDts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { Logger } from './logger'
export const createDtsSnapshot = (
tsModule: typeof ts,
scriptSnapshot: ts.IScriptSnapshot,
logger: Logger
logger: Logger,
useAsConst: boolean
): ts.IScriptSnapshot => {
const text = scriptSnapshot.getText(0, scriptSnapshot.getLength())

Expand All @@ -20,7 +21,7 @@ export const createDtsSnapshot = (
const data = toml.parse(text)

dts = `
declare const data = ${JSON.stringify(data)}
declare const data = ${JSON.stringify(data)}${useAsConst ? ' as const' : ''}
export default data
`
} catch (e) {
Expand Down
16 changes: 13 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type * as ts from 'typescript/lib/tsserverlibrary'
import path from 'path'
import { createDtsSnapshot } from './createDts'
import { isToml } from './util'
import { isToml, isConstToml } from './util'
import { createLogger } from './logger'

function init({
Expand All @@ -21,7 +21,12 @@ function init({
): ts.SourceFile => {
if (isToml(fileName)) {
logger.log(`create ${fileName}`)
scriptSnapshot = createDtsSnapshot(tsModule, scriptSnapshot, logger)
scriptSnapshot = createDtsSnapshot(
tsModule,
scriptSnapshot,
logger,
isConstToml(fileName)
)
}
const sourceFile = _createLanguageServiceSourceFile(
fileName,
Expand All @@ -43,7 +48,12 @@ function init({
): ts.SourceFile => {
if (isToml(sourceFile.fileName)) {
logger.log(`update ${sourceFile.fileName}`)
scriptSnapshot = createDtsSnapshot(tsModule, scriptSnapshot, logger)
scriptSnapshot = createDtsSnapshot(
tsModule,
scriptSnapshot,
logger,
isConstToml(sourceFile.fileName)
)
}
sourceFile = _updateLanguageServiceSourceFile(
sourceFile,
Expand Down
2 changes: 2 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export const isToml = (filename: string): boolean => filename.endsWith('.toml')
export const isConstToml = (filename: string): boolean =>
filename.endsWith('.const.toml')

0 comments on commit 4edd9e7

Please sign in to comment.