From dadeaa2e7fa0d0447135f76a5c70273238fc1da0 Mon Sep 17 00:00:00 2001 From: Christian Kaisermann Date: Thu, 23 Jan 2020 10:11:04 -0300 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20add=20runtime=20typings?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ✅ Closes: Closes #43 --- .gitignore | 3 +- package.json | 16 +++-- rollup.config.js | 2 +- src/client/types/index.ts | 66 ------------------- src/{client => runtime}/configs.ts | 0 .../includes/formatters.ts | 0 .../includes/loaderQueue.ts | 0 src/{client => runtime}/includes/lookup.ts | 0 src/{client => runtime}/includes/memoize.ts | 0 src/{client => runtime}/includes/utils.ts | 0 src/{client => runtime}/index.ts | 0 src/{client => runtime}/stores/dictionary.ts | 0 src/{client => runtime}/stores/format.ts | 0 src/{client => runtime}/stores/loading.ts | 0 src/{client => runtime}/stores/locale.ts | 0 src/runtime/tsconfig.json | 8 +++ test/{client => runtime}/configs.test.ts | 4 +- .../includes/formatters.test.ts | 2 +- .../includes/loaderQueue.test.ts | 8 +-- .../includes/lookup.test.ts | 4 +- .../includes/utils.test.ts | 2 +- test/{client => runtime}/index.test.ts | 10 +-- .../stores/dictionary.test.ts | 2 +- .../{client => runtime}/stores/format.test.ts | 10 +-- .../{client => runtime}/stores/locale.test.ts | 10 +-- tsconfig.json | 3 +- 26 files changed, 49 insertions(+), 101 deletions(-) delete mode 100644 src/client/types/index.ts rename src/{client => runtime}/configs.ts (100%) rename src/{client => runtime}/includes/formatters.ts (100%) rename src/{client => runtime}/includes/loaderQueue.ts (100%) rename src/{client => runtime}/includes/lookup.ts (100%) rename src/{client => runtime}/includes/memoize.ts (100%) rename src/{client => runtime}/includes/utils.ts (100%) rename src/{client => runtime}/index.ts (100%) rename src/{client => runtime}/stores/dictionary.ts (100%) rename src/{client => runtime}/stores/format.ts (100%) rename src/{client => runtime}/stores/loading.ts (100%) rename src/{client => runtime}/stores/locale.ts (100%) create mode 100644 src/runtime/tsconfig.json rename test/{client => runtime}/configs.test.ts (93%) rename test/{client => runtime}/includes/formatters.test.ts (99%) rename test/{client => runtime}/includes/loaderQueue.test.ts (89%) rename test/{client => runtime}/includes/lookup.test.ts (88%) rename test/{client => runtime}/includes/utils.test.ts (98%) rename test/{client => runtime}/index.test.ts (88%) rename test/{client => runtime}/stores/dictionary.test.ts (98%) rename test/{client => runtime}/stores/format.test.ts (89%) rename test/{client => runtime}/stores/locale.test.ts (92%) diff --git a/.gitignore b/.gitignore index ee1bb96..be0303b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ node_modules *.log dist/ -coverage/ \ No newline at end of file +coverage/ +types/ \ No newline at end of file diff --git a/package.json b/package.json index 4ee237b..39ebf99 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,9 @@ { "name": "svelte-i18n", "version": "2.2.4", - "main": "dist/i18n.cjs.js", - "module": "dist/i18n.esm.js", + "main": "dist/runtime.cjs.js", + "module": "dist/runtime.esm.js", + "types": "types/runtime/index.d.ts", "bin": { "svelte-i18n": "dist/cli.js" }, @@ -21,21 +22,26 @@ "node": ">= 11.15.0" }, "scripts": { + "clean": "rm -rf dist/ types/", "build": "rollup -c", + "build:types": "tsc -p src/runtime --emitDeclarationOnly", "dev": "rollup -c -w", - "pretest": "npm run build", "test": "jest", "test:ci": "jest --silent", "test:watch": "jest --verbose --watchAll", "lint": "eslint \"src/**/*.ts\"", "format": "prettier --loglevel silent --write \"src/**/*.ts\" && eslint --fix \"src/**/*.ts\"", - "prerelease": "conventional-changelog -p angular -i CHANGELOG.md -s -r 1 && git add CHANGELOG.md", "release": " git add package.json && git commit -m \"chore(release): v$npm_package_version :tada:\"", + "pretest": "npm run build", + "prebuild": "yarn clean", + "postbuild": "yarn build:types", + "prerelease": "conventional-changelog -p angular -i CHANGELOG.md -s -r 1 && git add CHANGELOG.md", "postrelease": "git tag -a v$npm_package_version -m 'Release v$npm_package_version'", "prepublishOnly": "run-s test:ci build release" }, "files": [ - "dist/" + "dist/", + "types/" ], "jest": { "collectCoverage": true, diff --git a/rollup.config.js b/rollup.config.js index 5500583..205b89a 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -9,7 +9,7 @@ const PROD = !process.env.ROLLUP_WATCH export default [ { - input: 'src/client/index.ts', + input: 'src/runtime/index.ts', external: [ ...Object.keys(pkg.dependencies), ...Object.keys(pkg.peerDependencies), diff --git a/src/client/types/index.ts b/src/client/types/index.ts deleted file mode 100644 index ad0aace..0000000 --- a/src/client/types/index.ts +++ /dev/null @@ -1,66 +0,0 @@ -import { Formats } from 'intl-messageformat' - -export interface DeepDictionary { - [key: string]: DeepDictionary | string | string[] -} -export type LocaleDictionary = Record -export type Dictionary = Record - -export interface MessageObject { - id?: string - locale?: string - format?: string - default?: string - values?: Record -} - -interface FormatterFn { - (id: string | MessageObject, options?: MessageObject): string -} - -type IntlFormatterOptions = T & { - format?: string - locale?: string -} - -export interface MemoizedIntlFormatter { - (options?: IntlFormatterOptions): T -} - -export interface Formatter extends FormatterFn { - time: ( - d: Date | number, - options?: IntlFormatterOptions - ) => string - date: ( - d: Date | number, - options?: IntlFormatterOptions - ) => string - number: ( - d: number, - options?: IntlFormatterOptions - ) => string - capital: FormatterFn - title: FormatterFn - upper: FormatterFn - lower: FormatterFn -} - -export interface MessagesLoader { - (): Promise -} - -export interface GetClientLocaleOptions { - navigator?: boolean - hash?: string - search?: string - pathname?: RegExp - hostname?: RegExp -} - -export interface ConfigureOptions { - fallbackLocale: string - initialLocale?: string | GetClientLocaleOptions - formats?: Partial - loadingDelay?: number -} diff --git a/src/client/configs.ts b/src/runtime/configs.ts similarity index 100% rename from src/client/configs.ts rename to src/runtime/configs.ts diff --git a/src/client/includes/formatters.ts b/src/runtime/includes/formatters.ts similarity index 100% rename from src/client/includes/formatters.ts rename to src/runtime/includes/formatters.ts diff --git a/src/client/includes/loaderQueue.ts b/src/runtime/includes/loaderQueue.ts similarity index 100% rename from src/client/includes/loaderQueue.ts rename to src/runtime/includes/loaderQueue.ts diff --git a/src/client/includes/lookup.ts b/src/runtime/includes/lookup.ts similarity index 100% rename from src/client/includes/lookup.ts rename to src/runtime/includes/lookup.ts diff --git a/src/client/includes/memoize.ts b/src/runtime/includes/memoize.ts similarity index 100% rename from src/client/includes/memoize.ts rename to src/runtime/includes/memoize.ts diff --git a/src/client/includes/utils.ts b/src/runtime/includes/utils.ts similarity index 100% rename from src/client/includes/utils.ts rename to src/runtime/includes/utils.ts diff --git a/src/client/index.ts b/src/runtime/index.ts similarity index 100% rename from src/client/index.ts rename to src/runtime/index.ts diff --git a/src/client/stores/dictionary.ts b/src/runtime/stores/dictionary.ts similarity index 100% rename from src/client/stores/dictionary.ts rename to src/runtime/stores/dictionary.ts diff --git a/src/client/stores/format.ts b/src/runtime/stores/format.ts similarity index 100% rename from src/client/stores/format.ts rename to src/runtime/stores/format.ts diff --git a/src/client/stores/loading.ts b/src/runtime/stores/loading.ts similarity index 100% rename from src/client/stores/loading.ts rename to src/runtime/stores/loading.ts diff --git a/src/client/stores/locale.ts b/src/runtime/stores/locale.ts similarity index 100% rename from src/client/stores/locale.ts rename to src/runtime/stores/locale.ts diff --git a/src/runtime/tsconfig.json b/src/runtime/tsconfig.json new file mode 100644 index 0000000..bfa3577 --- /dev/null +++ b/src/runtime/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "declaration": true, + "declarationDir": "../../types/runtime" + }, + "include": ["."] +} diff --git a/test/client/configs.test.ts b/test/runtime/configs.test.ts similarity index 93% rename from test/client/configs.test.ts rename to test/runtime/configs.test.ts index 0915e6e..af8c896 100644 --- a/test/client/configs.test.ts +++ b/test/runtime/configs.test.ts @@ -5,8 +5,8 @@ import { getOptions, defaultOptions, defaultFormats, -} from '../../src/client/configs' -import { $locale } from '../../src/client/stores/locale' +} from '../../src/runtime/configs' +import { $locale } from '../../src/runtime/stores/locale' beforeEach(() => { init(defaultOptions) diff --git a/test/client/includes/formatters.test.ts b/test/runtime/includes/formatters.test.ts similarity index 99% rename from test/client/includes/formatters.test.ts rename to test/runtime/includes/formatters.test.ts index f467552..5a6bc77 100644 --- a/test/client/includes/formatters.test.ts +++ b/test/runtime/includes/formatters.test.ts @@ -4,7 +4,7 @@ import { getTimeFormatter, getMessageFormatter, init -} from '../../../src/client' +} from '../../../src/runtime' beforeEach(() => { init({ fallbackLocale: undefined }) diff --git a/test/client/includes/loaderQueue.test.ts b/test/runtime/includes/loaderQueue.test.ts similarity index 89% rename from test/client/includes/loaderQueue.test.ts rename to test/runtime/includes/loaderQueue.test.ts index adbc3f5..e3f2eff 100644 --- a/test/client/includes/loaderQueue.test.ts +++ b/test/runtime/includes/loaderQueue.test.ts @@ -5,10 +5,10 @@ import { flush, registerLocaleLoader, resetQueues, -} from '../../../src/client/includes/loaderQueue' -import { getMessageFromDictionary } from '../../../src/client/stores/dictionary' -import { $isLoading } from '../../../src/client/stores/loading' -import { getOptions } from '../../../src/client/configs' +} from '../../../src/runtime/includes/loaderQueue' +import { getMessageFromDictionary } from '../../../src/runtime/stores/dictionary' +import { $isLoading } from '../../../src/runtime/stores/loading' +import { getOptions } from '../../../src/runtime/configs' beforeEach(() => { resetQueues() diff --git a/test/client/includes/lookup.test.ts b/test/runtime/includes/lookup.test.ts similarity index 88% rename from test/client/includes/lookup.test.ts rename to test/runtime/includes/lookup.test.ts index bb647fc..affa8e4 100644 --- a/test/client/includes/lookup.test.ts +++ b/test/runtime/includes/lookup.test.ts @@ -1,5 +1,5 @@ -import { lookup, lookupCache } from '../../../src/client/includes/lookup' -import { $dictionary, addMessages } from '../../../src/client/stores/dictionary' +import { lookup, lookupCache } from '../../../src/runtime/includes/lookup' +import { $dictionary, addMessages } from '../../../src/runtime/stores/dictionary' beforeEach(() => { $dictionary.set({}) diff --git a/test/client/includes/utils.test.ts b/test/runtime/includes/utils.test.ts similarity index 98% rename from test/client/includes/utils.test.ts rename to test/runtime/includes/utils.test.ts index 1d34aa1..a5b45c2 100644 --- a/test/client/includes/utils.test.ts +++ b/test/runtime/includes/utils.test.ts @@ -5,7 +5,7 @@ import { upper, lower, flatObj, -} from '../../../src/client/includes/utils' +} from '../../../src/runtime/includes/utils' describe('getting client locale', () => { beforeEach(() => { diff --git a/test/client/index.test.ts b/test/runtime/index.test.ts similarity index 88% rename from test/client/index.test.ts rename to test/runtime/index.test.ts index 36a27df..c92fc89 100644 --- a/test/client/index.test.ts +++ b/test/runtime/index.test.ts @@ -1,11 +1,11 @@ -import { defineMessages, waitLocale, register, init } from '../../src/client' -import { $locale } from '../../src/client/stores/locale' -import { hasLocaleQueue } from '../../src/client/includes/loaderQueue' +import { defineMessages, waitLocale, register, init } from '../../src/runtime' +import { $locale } from '../../src/runtime/stores/locale' +import { hasLocaleQueue } from '../../src/runtime/includes/loaderQueue' import { getLocaleDictionary, $dictionary, -} from '../../src/client/stores/dictionary' -import { $format } from '../../src/client/stores/format' +} from '../../src/runtime/stores/dictionary' +import { $format } from '../../src/runtime/stores/format' test('defineMessages returns the identity of its first argument', () => { const obj = {} diff --git a/test/client/stores/dictionary.test.ts b/test/runtime/stores/dictionary.test.ts similarity index 98% rename from test/client/stores/dictionary.test.ts rename to test/runtime/stores/dictionary.test.ts index 9db5ba3..13b17b0 100644 --- a/test/client/stores/dictionary.test.ts +++ b/test/runtime/stores/dictionary.test.ts @@ -9,7 +9,7 @@ import { $dictionary, $locales, getLocaleDictionary, -} from '../../../src/client/stores/dictionary' +} from '../../../src/runtime/stores/dictionary' beforeEach(() => { $dictionary.set({}) diff --git a/test/client/stores/format.test.ts b/test/runtime/stores/format.test.ts similarity index 89% rename from test/client/stores/format.test.ts rename to test/runtime/stores/format.test.ts index 7d99eb7..7d2831b 100644 --- a/test/client/stores/format.test.ts +++ b/test/runtime/stores/format.test.ts @@ -1,8 +1,8 @@ -import { Formatter } from '../../../src/client/types/index' -import { $format } from '../../../src/client/stores/format' -import { init } from '../../../src/client/configs' -import { addMessages } from '../../../src/client/stores/dictionary' -import { $locale } from '../../../src/client/stores/locale' +import { Formatter } from '../../../src/runtime/types/index' +import { $format } from '../../../src/runtime/stores/format' +import { init } from '../../../src/runtime/configs' +import { addMessages } from '../../../src/runtime/stores/dictionary' +import { $locale } from '../../../src/runtime/stores/locale' let format: Formatter $format.subscribe(f => (format = f)) diff --git a/test/client/stores/locale.test.ts b/test/runtime/stores/locale.test.ts similarity index 92% rename from test/client/stores/locale.test.ts rename to test/runtime/stores/locale.test.ts index bd0f923..dbf9f97 100644 --- a/test/client/stores/locale.test.ts +++ b/test/runtime/stores/locale.test.ts @@ -1,6 +1,6 @@ import { get } from 'svelte/store' -import { lookup } from '../../../src/client/includes/lookup' +import { lookup } from '../../../src/runtime/includes/lookup' import { isFallbackLocaleOf, getFallbackOf, @@ -8,10 +8,10 @@ import { getCurrentLocale, $locale, isRelatedLocale, -} from '../../../src/client/stores/locale' -import { getOptions, init } from '../../../src/client/configs' -import { register } from '../../../src/client' -import { hasLocaleQueue } from '../../../src/client/includes/loaderQueue' +} from '../../../src/runtime/stores/locale' +import { getOptions, init } from '../../../src/runtime/configs' +import { register } from '../../../src/runtime' +import { hasLocaleQueue } from '../../../src/runtime/includes/loaderQueue' beforeEach(() => { init({ fallbackLocale: undefined }) diff --git a/tsconfig.json b/tsconfig.json index e5b261a..6c72373 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,6 +11,5 @@ "lib": ["es2018", "dom", "esnext"], "outDir": "dist", "types": ["svelte", "jest"] - }, - "exclude": ["node_modules/**/*", "dist"] + } }