Skip to content

Commit b1985b7

Browse files
authored
fix(types): fix typescript definitions (#160)
1 parent aa06933 commit b1985b7

12 files changed

+253
-34
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ compiled
1010
.awcache
1111
.rpt2_cache
1212
.cache/
13+
temp

api-extractor.json

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// this the shared base config for all packages.
2+
{
3+
"$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
4+
5+
"apiReport": {
6+
"enabled": true,
7+
"reportFolder": "<projectFolder>/temp/"
8+
},
9+
10+
"docModel": {
11+
"enabled": true
12+
},
13+
14+
"dtsRollup": {
15+
"enabled": true
16+
},
17+
18+
"tsdocMetadata": {
19+
"enabled": false
20+
},
21+
22+
"messages": {
23+
"compilerMessageReporting": {
24+
"default": {
25+
"logLevel": "warning"
26+
}
27+
},
28+
29+
"extractorMessageReporting": {
30+
"default": {
31+
"logLevel": "warning",
32+
"addToApiReportFile": true
33+
},
34+
35+
"ae-missing-release-tag": {
36+
"logLevel": "none"
37+
}
38+
},
39+
40+
"tsdocMessageReporting": {
41+
"default": {
42+
"logLevel": "warning"
43+
},
44+
45+
"tsdoc-undefined-tag": {
46+
"logLevel": "none"
47+
}
48+
}
49+
}
50+
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"devDependencies": {
3535
"@ls-lint/ls-lint": "^1.9.0",
3636
"@rollup/plugin-commonjs": "^15.0.0",
37+
"@microsoft/api-extractor": "^7.9.2",
3738
"@rollup/plugin-json": "^4.1.0",
3839
"@rollup/plugin-node-resolve": "^9.0.0",
3940
"@rollup/plugin-replace": "^2.3.3",

packages/fluent-vue/__tests__/vue/messageOverride.spec.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import VueCompositionApi from '@vue/composition-api'
55
import { FluentBundle, FluentResource } from '@fluent/bundle'
66
import ftl from '@fluent/dedent'
77

8-
import { createFluentVue } from '../../src'
9-
import { FluentVue } from '../../src/interfaces'
8+
import { createFluentVue, FluentVue } from '../../src'
109

1110
Vue.use(VueCompositionApi)
1211

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "../../api-extractor.json",
3+
"mainEntryPointFilePath": "./dist/packages/<unscopedPackageName>/src/index.d.ts",
4+
"dtsRollup": {
5+
"publicTrimmedFilePath": "./dist/<unscopedPackageName>.d.ts"
6+
}
7+
}

packages/fluent-vue/package.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "fluent-vue",
33
"version": "3.0.0-beta.0",
4-
"description": "Fluent bindings for Vue.js",
4+
"description": "Project Fluent bindings for Vue.js",
55
"keywords": [
66
"localization",
77
"l10n",
@@ -16,11 +16,13 @@
1616
"format",
1717
"vue",
1818
"vuejs",
19-
"vue.js"
19+
"vue.js",
20+
"ProjectFluent",
21+
"Project Fluent"
2022
],
2123
"main": "dist/fluent-vue.cjs.prod.js",
2224
"module": "dist/fluent-vue.esm.js",
23-
"types": "src/types/index.d.ts",
25+
"types": "dist/fluent-vue.d.ts",
2426
"sideEffects": false,
2527
"files": [
2628
"dist",

packages/fluent-vue/src/TranslationContext.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { CachedSyncIterable } from 'cached-iterable'
22
import { mapBundleSync } from '@fluent/sequence'
33
import { warn } from './util/warn'
44
import { FluentBundle, FluentVariable } from '@fluent/bundle'
5-
import { Pattern } from '@fluent/bundle/esm/ast'
5+
import { Message, Pattern } from '@fluent/bundle/esm/ast'
66
import { computed, ComputedRef, Ref } from 'vue-demi'
77
import { getOrderedBundles } from './getOrderedBundles'
88

@@ -23,7 +23,7 @@ export class TranslationContext {
2323
return mapBundleSync(this.bundlesIterable.value, key)
2424
}
2525

26-
getMessage(bundle: FluentBundle | null, key: string) {
26+
getMessage(bundle: FluentBundle | null, key: string): Message | null {
2727
const message = bundle?.getMessage(key)
2828

2929
if (message === undefined) {

packages/fluent-vue/src/index.ts

+24-5
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,41 @@
1-
import { isVue3, ref, provide } from 'vue-demi'
2-
import { FluentVueOptions } from './interfaces'
1+
import { isVue3, ref, provide, Ref } from 'vue-demi'
32
import { TranslationContext } from './TranslationContext'
43
import { createVue2Directive, createVue3Directive } from './vue/directive'
54
import component from './vue/component'
65
import { getContext } from './composition'
76
import { RootContextSymbol } from './symbols'
8-
import { FluentVariable } from '@fluent/bundle'
7+
import { FluentBundle, FluentVariable } from '@fluent/bundle'
98

109
export { useFluent } from './composition'
1110

11+
export interface FluentVueOptions {
12+
/** Currently selected locale */
13+
locale: string | string[]
14+
/** List of bundles used in application */
15+
bundles: FluentBundle[]
16+
}
17+
18+
export interface FluentVue {
19+
/** Currently selected locale */
20+
locale: string | string[]
21+
/** List of bundles used in application */
22+
bundles: FluentBundle[]
23+
24+
format(key: string, value?: Record<string, FluentVariable>): string
25+
26+
formatAttrs(key: string, value?: Record<string, FluentVariable>): Record<string, string>
27+
28+
install(vue: any): void
29+
}
30+
1231
/**
1332
* Creates FluentVue instance that can bu used on a Vue app.
1433
*
1534
* @param options - {@link FluentVueOptions}
1635
*/
17-
export function createFluentVue(options: FluentVueOptions) {
36+
export function createFluentVue(options: FluentVueOptions): FluentVue {
1837
const locale = ref(options.locale)
19-
const bundles = ref(options.bundles)
38+
const bundles: Ref<FluentBundle[]> = ref(options.bundles)
2039

2140
const rootContext = new TranslationContext(locale, bundles)
2241

packages/fluent-vue/src/interfaces.ts

-16
This file was deleted.

scripts/build.js

+40
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const devOnly = args.devOnly || args.d
2929
const prodOnly = !devOnly && (args.prodOnly || args.p)
3030
const sourceMap = args.sourcemap || args.s
3131
const isRelease = args.release
32+
const buildTypes = args.t || args.types || isRelease
3233
const buildAllMatching = args.all || args.a
3334
const commit = execa.sync('git', ['rev-parse', 'HEAD']).stdout.slice(0, 7)
3435

@@ -75,6 +76,7 @@ async function build(target) {
7576
`NODE_ENV:${env}`,
7677
`TARGET:${target}`,
7778
formats ? `FORMATS:${formats}` : ``,
79+
buildTypes ? `TYPES:true` : ``,
7880
prodOnly ? `PROD_ONLY:true` : ``,
7981
sourceMap ? `SOURCE_MAP:true` : ``,
8082
]
@@ -83,6 +85,44 @@ async function build(target) {
8385
],
8486
{ stdio: 'inherit' }
8587
)
88+
89+
if (buildTypes && pkg.types) {
90+
console.log()
91+
console.log(chalk.bold(chalk.yellow(`Rolling up type definitions for ${target}...`)))
92+
93+
// build types
94+
const { Extractor, ExtractorConfig } = require('@microsoft/api-extractor')
95+
96+
const extractorConfigPath = path.resolve(pkgDir, `api-extractor.json`)
97+
const extractorConfig = ExtractorConfig.loadFileAndPrepare(extractorConfigPath)
98+
const result = Extractor.invoke(extractorConfig, {
99+
localBuild: true,
100+
showVerboseMessages: true,
101+
})
102+
103+
if (result.succeeded) {
104+
// concat additional d.ts to rolled-up dts (mostly for JSX)
105+
if (pkg.buildOptions && pkg.buildOptions.dts) {
106+
const dtsPath = path.resolve(pkgDir, pkg.types)
107+
const existing = await fs.readFile(dtsPath, 'utf-8')
108+
const toAdd = await Promise.all(
109+
pkg.buildOptions.dts.map((file) => {
110+
return fs.readFile(path.resolve(pkgDir, file), 'utf-8')
111+
})
112+
)
113+
await fs.writeFile(dtsPath, existing + '\n' + toAdd.join('\n'))
114+
}
115+
console.log(chalk.bold(chalk.green(`API Extractor completed successfully.`)))
116+
} else {
117+
console.error(
118+
`API Extractor completed with ${extractorResult.errorCount} errors` +
119+
` and ${extractorResult.warningCount} warnings`
120+
)
121+
process.exitCode = 1
122+
}
123+
124+
await fs.remove(`${pkgDir}/dist/packages`)
125+
}
86126
}
87127

88128
function checkAllSizes(targets) {

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"baseUrl": ".",
44
"outDir": "dist",
55
"sourceMap": false,
6-
"target": "ES2019",
6+
"target": "esnext",
77
"module": "esnext",
88
"moduleResolution": "node",
99
"allowJs": false,

0 commit comments

Comments
 (0)