Skip to content

Commit

Permalink
move project to native esm module
Browse files Browse the repository at this point in the history
Build as native ESM module and only provide commonjs as a fallback
for legacy node versions. Typescript unfortunetly does not support
compiling to import versions with file extension. The project itself
suggests to add the js extension within the typescript files itself.
This neither does look right nor feelds right, but they do not provide
another solution.

See https://www.typescriptlang.org/docs/handbook/esm-node.html for more
information.

Fixes cibernox/svelte-intl-precompile#65

Signed-off-by: Tobias Kohlbau <[email protected]>
  • Loading branch information
tobiaskohlbau committed Aug 7, 2023
1 parent cdaee6e commit 5c7fc67
Show file tree
Hide file tree
Showing 26 changed files with 95 additions and 87 deletions.
4 changes: 2 additions & 2 deletions babel.config.js → babel.config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
{
presets: [
[
"@babel/preset-env",
Expand All @@ -9,4 +9,4 @@ module.exports = {
}
]
]
};
}
2 changes: 1 addition & 1 deletion dist/modules/configs.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import { ConfigureOptions } from './types/index';
import { ConfigureOptions } from './types/index.js';
export declare function init(opts: ConfigureOptions): void;
4 changes: 2 additions & 2 deletions dist/modules/configs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { $locale } from './stores/locale';
import { getOptions } from './includes/utils';
import { $locale } from './stores/locale.js';
import { getOptions } from './includes/utils.js';
export function init(opts) {
const { formats, ...rest } = opts;
const initialLocale = opts.initialLocale || opts.fallbackLocale;
Expand Down
2 changes: 1 addition & 1 deletion dist/modules/includes/formatters.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MemoizedIntlFormatter } from '../types/index';
import { MemoizedIntlFormatter } from '../types/index.js';
export declare const getNumberFormatter: MemoizedIntlFormatter<Intl.NumberFormat, Intl.NumberFormatOptions>;
export declare const getDateFormatter: MemoizedIntlFormatter<Intl.DateTimeFormat, Intl.DateTimeFormatOptions>;
export declare const getTimeFormatter: MemoizedIntlFormatter<Intl.DateTimeFormat, Intl.DateTimeFormatOptions>;
4 changes: 2 additions & 2 deletions dist/modules/includes/formatters.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getOptions, getCurrentLocale } from './utils';
import { monadicMemoize } from './memoize';
import { getOptions, getCurrentLocale } from './utils.js';
import { monadicMemoize } from './memoize.js';
const getIntlFormatterOptions = (type, name) => {
const { formats } = getOptions();
if (type in formats && name in formats[type]) {
Expand Down
2 changes: 1 addition & 1 deletion dist/modules/includes/loaderQueue.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MessagesLoader } from '../types/index';
import { MessagesLoader } from '../types/index.js';
export declare function resetQueues(): void;
export declare function hasLocaleQueue(locale: string): boolean;
export declare function flush(locale: string): Promise<void>;
Expand Down
6 changes: 3 additions & 3 deletions dist/modules/includes/loaderQueue.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { hasLocaleDictionary, $dictionary, addMessages, } from '../stores/dictionary';
import { getPossibleLocales, getOptions } from '../includes/utils';
import { $isLoading } from '../stores/loading';
import { hasLocaleDictionary, $dictionary, addMessages, } from '../stores/dictionary.js';
import { getPossibleLocales, getOptions } from '../includes/utils.js';
import { $isLoading } from '../stores/loading.js';
const loaderQueue = {};
export function resetQueues() {
Object.keys(loaderQueue).forEach(key => {
Expand Down
4 changes: 2 additions & 2 deletions dist/modules/includes/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getMessageFromDictionary } from '../stores/dictionary';
import { getPossibleLocales } from './utils';
import { getMessageFromDictionary } from '../stores/dictionary.js';
import { getPossibleLocales } from './utils.js';
export const lookupCache = {};
const addToCache = (path, locale, message) => {
if (!message)
Expand Down
18 changes: 9 additions & 9 deletions dist/modules/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
export * from './includes/localeGetters';
export * from './includes/utils';
export * from './includes/localeGetters.js';
export * from './includes/utils.js';
export declare function waitLocale(locale?: string): Promise<void>;
export { init } from './configs';
export { $locale as locale } from './stores/locale';
export { $dictionary as dictionary, $locales as locales, addMessages, } from './stores/dictionary';
export { registerLocaleLoader as register } from './includes/loaderQueue';
export { $isLoading as isLoading } from './stores/loading';
export { formatMessage, $format as format, $format as _, $format as t, $formatDate as date, $formatNumber as number, $formatTime as time, $getJSON as json, } from './stores/formatters';
export { getDateFormatter, getNumberFormatter, getTimeFormatter, } from './includes/formatters';
export { init } from './configs.js';
export { $locale as locale } from './stores/locale.js';
export { $dictionary as dictionary, $locales as locales, addMessages, } from './stores/dictionary.js';
export { registerLocaleLoader as register } from './includes/loaderQueue.js';
export { $isLoading as isLoading } from './stores/loading.js';
export { formatMessage, $format as format, $format as _, $format as t, $formatDate as date, $formatNumber as number, $formatTime as time, $getJSON as json, } from './stores/formatters.js';
export { getDateFormatter, getNumberFormatter, getTimeFormatter, } from './includes/formatters.js';
declare type PluralRule = "zero" | "one" | "two" | "few" | "many" | "other" | number;
declare type PluralOptions = Record<PluralRule, string>;
export declare function __interpolate(value: any): any;
Expand Down
24 changes: 12 additions & 12 deletions dist/modules/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { flush } from './includes/loaderQueue';
import { getOptions, getCurrentLocale } from './includes/utils';
export * from './includes/localeGetters';
export * from './includes/utils';
import { flush } from './includes/loaderQueue.js';
import { getOptions, getCurrentLocale } from './includes/utils.js';
export * from './includes/localeGetters.js';
export * from './includes/utils.js';
export function waitLocale(locale) {
return flush(locale || getCurrentLocale() || getOptions().initialLocale);
}
export { init } from './configs';
export { $locale as locale } from './stores/locale';
export { $dictionary as dictionary, $locales as locales, addMessages, } from './stores/dictionary';
export { registerLocaleLoader as register } from './includes/loaderQueue';
export { $isLoading as isLoading } from './stores/loading';
import { formatTime, formatDate, formatNumber } from './stores/formatters';
export { formatMessage, $format as format, $format as _, $format as t, $formatDate as date, $formatNumber as number, $formatTime as time, $getJSON as json, } from './stores/formatters';
export { init } from './configs.js';
export { $locale as locale } from './stores/locale.js';
export { $dictionary as dictionary, $locales as locales, addMessages, } from './stores/dictionary.js';
export { registerLocaleLoader as register } from './includes/loaderQueue.js';
export { $isLoading as isLoading } from './stores/loading.js';
import { formatTime, formatDate, formatNumber } from './stores/formatters.js';
export { formatMessage, $format as format, $format as _, $format as t, $formatDate as date, $formatNumber as number, $formatTime as time, $getJSON as json, } from './stores/formatters.js';
// low-level
export { getDateFormatter, getNumberFormatter, getTimeFormatter, } from './includes/formatters';
export { getDateFormatter, getNumberFormatter, getTimeFormatter, } from './includes/formatters.js';
export function __interpolate(value) {
return value === 0 ? 0 : value || '';
}
Expand Down
2 changes: 1 addition & 1 deletion dist/modules/stores/dictionary.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LocaleDictionary, DeepDictionary, Dictionary } from '../types/index';
import { LocaleDictionary, DeepDictionary, Dictionary } from '../types/index.js';
declare const $dictionary: import("svelte/store").Writable<Dictionary>;
export declare function getLocaleDictionary(locale: string): LocaleDictionary;
export declare function getDictionary(): Dictionary;
Expand Down
2 changes: 1 addition & 1 deletion dist/modules/stores/dictionary.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @ts-ignore
import { writable, derived } from 'svelte/store';
import { getPossibleLocales } from '../includes/utils';
import { getPossibleLocales } from '../includes/utils.js';
let dictionary;
const $dictionary = writable({});
export function getLocaleDictionary(locale) {
Expand Down
4 changes: 2 additions & 2 deletions dist/modules/stores/formatters.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { MessageFormatter, TimeFormatter, DateFormatter, NumberFormatter, JsonGetter } from '../types/index';
import { MessageFormatter, TimeFormatter, DateFormatter, NumberFormatter, JsonGetter } from '../types/index.js';
export declare const formatMessage: MessageFormatter;
export declare const getJSON: JsonGetter;
export declare const formatTime: TimeFormatter;
export declare const formatDate: DateFormatter;
export declare const formatNumber: NumberFormatter;
export declare const $format: import("svelte/store").Readable<(id: string | import("../types/index").MessageObjectWithId, options?: import("../types/index").MessageObject | undefined) => string>;
export declare const $format: import("svelte/store").Readable<(id: string | import("../types/index.js").MessageObjectWithId, options?: import("../types/index.js").MessageObject | undefined) => string>;
export declare const $formatTime: import("svelte/store").Readable<(d: number | Date, options?: (Intl.DateTimeFormatOptions & {
format?: string | undefined;
locale?: string | undefined;
Expand Down
12 changes: 6 additions & 6 deletions dist/modules/stores/formatters.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { derived } from "svelte/store";
import { lookup } from '../includes/lookup';
import { hasLocaleQueue } from '../includes/loaderQueue';
import { getTimeFormatter, getDateFormatter, getNumberFormatter, } from '../includes/formatters';
import { getOptions, getCurrentLocale, getPossibleLocales } from '../includes/utils';
import { $dictionary } from './dictionary';
import { $locale } from './locale';
import { lookup } from '../includes/lookup.js';
import { hasLocaleQueue } from '../includes/loaderQueue.js';
import { getTimeFormatter, getDateFormatter, getNumberFormatter, } from '../includes/formatters.js';
import { getOptions, getCurrentLocale, getPossibleLocales } from '../includes/utils.js';
import { $dictionary } from './dictionary.js';
import { $locale } from './locale.js';
export const formatMessage = (currentLocale, optionsOrId, maybeOptions = {}) => {
const id = typeof optionsOrId === 'string' ? optionsOrId : optionsOrId.id;
const options = typeof optionsOrId === 'string' ? maybeOptions : optionsOrId;
Expand Down
6 changes: 3 additions & 3 deletions dist/modules/stores/locale.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { writable } from 'svelte/store';
import { flush, hasLocaleQueue } from '../includes/loaderQueue';
import { getCurrentLocale, setCurrentLocale } from '../includes/utils';
import { getClosestAvailableLocale } from './dictionary';
import { flush, hasLocaleQueue } from '../includes/loaderQueue.js';
import { getCurrentLocale, setCurrentLocale } from '../includes/utils.js';
import { getClosestAvailableLocale } from './dictionary.js';
const $locale = writable('');
$locale.subscribe((newLocale) => {
setCurrentLocale(newLocale);
Expand Down
4 changes: 2 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module.exports = {
export default {
bail: false,
moduleFileExtensions: ["js", "json"],
transform: {
"^.+\\.js$": "babel-jest",
},
verbose: true
};
}
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
"main": "dist/commonjs/index.js",
"module": "dist/modules/index.js",
"types": "dist/modules/index.d.ts",
"type": "module",
"exports": {
".": {
"import": "./dist/modules/index.js",
"require": "./dist/commonjs/index.js",
"default": "./dist/modules/index.js"
}
},
"peerDependencies": {
"svelte": "^3.37.0 || ^4.0.0"
},
Expand Down
6 changes: 3 additions & 3 deletions src/configs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ConfigureOptions } from './types/index'
import { $locale } from './stores/locale';
import { getOptions } from './includes/utils';
import { ConfigureOptions } from './types/index.js'
import { $locale } from './stores/locale.js';
import { getOptions } from './includes/utils.js';

export function init(opts: ConfigureOptions) {
const { formats, ...rest } = opts
Expand Down
6 changes: 3 additions & 3 deletions src/includes/formatters.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MemoizedIntlFormatter } from '../types/index'
import { getOptions, getCurrentLocale } from './utils';
import { monadicMemoize } from './memoize'
import { MemoizedIntlFormatter } from '../types/index.js'
import { getOptions, getCurrentLocale } from './utils.js';
import { monadicMemoize } from './memoize.js'

const getIntlFormatterOptions = (
type: 'time' | 'number' | 'date',
Expand Down
8 changes: 4 additions & 4 deletions src/includes/loaderQueue.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { MessagesLoader } from '../types/index'
import { MessagesLoader } from '../types/index.js'
import {
hasLocaleDictionary,
$dictionary,
addMessages,
} from '../stores/dictionary'
import { getPossibleLocales, getOptions } from '../includes/utils';
import { $isLoading } from '../stores/loading'
} from '../stores/dictionary.js'
import { getPossibleLocales, getOptions } from '../includes/utils.js';
import { $isLoading } from '../stores/loading.js'

type Queue = Set<MessagesLoader>
const loaderQueue: Record<string, Queue> = {}
Expand Down
4 changes: 2 additions & 2 deletions src/includes/lookup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getMessageFromDictionary } from '../stores/dictionary';
import { getPossibleLocales } from './utils';
import { getMessageFromDictionary } from '../stores/dictionary.js';
import { getPossibleLocales } from './utils.js';

export const lookupCache: {
[locale: string]: {
Expand Down
2 changes: 1 addition & 1 deletion src/includes/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GetClientLocaleOptions } from "../types/index";
import { GetClientLocaleOptions } from "../types/index.js";
interface Formats {
number: Record<string, any>
date: Record<string, any>
Expand Down
24 changes: 12 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
import { flush } from './includes/loaderQueue'
import { getOptions, getCurrentLocale } from './includes/utils'
export * from './includes/localeGetters';
export * from './includes/utils';
import { flush } from './includes/loaderQueue.js'
import { getOptions, getCurrentLocale } from './includes/utils.js'
export * from './includes/localeGetters.js';
export * from './includes/utils.js';

export function waitLocale(locale?: string) {
return flush(locale || getCurrentLocale() || getOptions().initialLocale)
}

export { init } from './configs'
export { init } from './configs.js'

export { $locale as locale } from './stores/locale'
export { $locale as locale } from './stores/locale.js'

export {
$dictionary as dictionary,
$locales as locales,
addMessages,
} from './stores/dictionary'
export { registerLocaleLoader as register } from './includes/loaderQueue'
} from './stores/dictionary.js'
export { registerLocaleLoader as register } from './includes/loaderQueue.js'

export { $isLoading as isLoading } from './stores/loading'
export { $isLoading as isLoading } from './stores/loading.js'

import {
formatTime,
formatDate,
formatNumber
} from './stores/formatters';
} from './stores/formatters.js';
export {
formatMessage,
$format as format,
Expand All @@ -34,14 +34,14 @@ export {
$formatNumber as number,
$formatTime as time,
$getJSON as json,
} from './stores/formatters'
} from './stores/formatters.js'

// low-level
export {
getDateFormatter,
getNumberFormatter,
getTimeFormatter,
} from './includes/formatters'
} from './includes/formatters.js'


type PluralRule = "zero" | "one" | "two" | "few" | "many" | "other" | number
Expand Down
4 changes: 2 additions & 2 deletions src/stores/dictionary.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// @ts-ignore
import { writable, derived } from 'svelte/store';
import { LocaleDictionary, LocaleDictionaryValue, DeepDictionary, Dictionary }
from '../types/index';
import { getPossibleLocales } from '../includes/utils';
from '../types/index.js';
import { getPossibleLocales } from '../includes/utils.js';

let dictionary: Dictionary
const $dictionary = writable<Dictionary>({})
Expand Down
14 changes: 7 additions & 7 deletions src/stores/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import {
DateFormatter,
NumberFormatter,
JsonGetter,
} from '../types/index'
import { lookup } from '../includes/lookup'
import { hasLocaleQueue } from '../includes/loaderQueue'
} from '../types/index.js'
import { lookup } from '../includes/lookup.js'
import { hasLocaleQueue } from '../includes/loaderQueue.js'
import {
getTimeFormatter,
getDateFormatter,
getNumberFormatter,
} from '../includes/formatters'
import { getOptions, getCurrentLocale, getPossibleLocales } from '../includes/utils';
} from '../includes/formatters.js'
import { getOptions, getCurrentLocale, getPossibleLocales } from '../includes/utils.js';

import { $dictionary } from './dictionary'
import { $locale } from './locale'
import { $dictionary } from './dictionary.js'
import { $locale } from './locale.js'

export const formatMessage: MessageFormatter = (currentLocale, optionsOrId, maybeOptions= {}) => {
const id = typeof optionsOrId === 'string' ? optionsOrId : optionsOrId.id;
Expand Down
6 changes: 3 additions & 3 deletions src/stores/locale.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { writable } from 'svelte/store'

import { flush, hasLocaleQueue } from '../includes/loaderQueue'
import { getCurrentLocale, setCurrentLocale } from '../includes/utils';
import { getClosestAvailableLocale } from './dictionary'
import { flush, hasLocaleQueue } from '../includes/loaderQueue.js'
import { getCurrentLocale, setCurrentLocale } from '../includes/utils.js';
import { getClosestAvailableLocale } from './dictionary.js'

const $locale = writable('')

Expand Down

0 comments on commit 5c7fc67

Please sign in to comment.