Skip to content

Commit

Permalink
build detectors as cjs files
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanhofer committed May 21, 2021
1 parent 5079c21 commit 9f662a6
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,6 +1,7 @@
node_modules
/adapters
/cjs
/detectors
/dist
/esm
/formatters
Expand Down
46 changes: 24 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions package.json
Expand Up @@ -25,9 +25,9 @@
"browser": "dist/i18n.min.js",
"types": "types/index.d.ts",
"exports": {
"./formatters": "./formatters/index.js",
"./formatters/*": "./formatters/*.js",
"./adapters/*": "./adapters/*.js",
"./detectors": "./detectors/index.js",
"./formatters": "./formatters/index.js",
"./react/react-context": "./react/react-context.js",
"./rollup/*": "./rollup/*.js",
"./svelte/svelte-store": "./svelte/svelte-store.js",
Expand Down Expand Up @@ -59,10 +59,11 @@
"dev:node": "rollup -c packages/node-process/rollup.config.ts -w",
"dev:rollup": "rollup -c packages/rollup-plugin/rollup.config.ts -w",
"dev:webpack": "rollup -c packages/webpack-plugin/rollup.config.ts -w",
"build": "npm run build:node && npm run build:rollup && npm run build:webpack && npm run build:formatters && npm run build:adapters && npm run build:esm && npm run build:cjs && npm run build:browser && npm run build:svelte && npm run build:react && npm run link-typedefinitions",
"build": "npm run build:node && npm run build:rollup && npm run build:webpack && npm run build:formatters && npm run build:adapters && npm run build:esm && npm run build:cjs && npm run build:detectors && npm run build:browser && npm run build:svelte && npm run build:react && npm run link-typedefinitions",
"build:adapters": "rollup -c packages/adapters/rollup.config.js && tsc -p packages/adapters/tsconfig.json --emitDeclarationOnly",
"build:browser": "rollup -c packages/browser/rollup.config.js",
"build:cjs": "tsc -p packages/core/tsconfig-cjs.json",
"build:detectors": "rollup -c packages/locale-detector/rollup.config.js && tsc -p packages/locale-detector/tsconfig.json --emitDeclarationOnly",
"build:esm": "tsc -p packages/core/tsconfig-esm.json",
"build:formatters": "rollup -c packages/formatters/rollup.config.js && tsc -p packages/formatters/tsconfig.json --emitDeclarationOnly",
"build:node": "rollup -c packages/node-process/rollup.config.js",
Expand All @@ -80,6 +81,8 @@
"test:size": "size-limit"
},
"dependencies": {
"@types/glob": "^7.1.3",
"glob": "^7.1.7",
"typesafe-utils": "^1.11.1"
},
"peerDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/formatters/rollup.config.js
Expand Up @@ -13,7 +13,7 @@ const getPath = (file) => path.resolve(__dirname, file)
const files = fs.readdirSync(getPath('./src'))

const config = files
.filter((file) => !file.includes('_types.ts'))
.filter((file) => !file.startsWith('_'))
.map((file) => ({
input: getPath(`./src/${file}`),
output: [
Expand Down
8 changes: 6 additions & 2 deletions packages/link-typedefinitions.ts
@@ -1,5 +1,6 @@
import { readdirSync, writeFileSync } from 'fs'
import { writeFileSync } from 'fs'
import { resolve } from 'path'
import { sync as globSync } from 'glob'

type FromWheretoImport = string
type OutputPath = string
Expand All @@ -12,12 +13,15 @@ const mappings: [FromWheretoImport, OutputPath?, FilterFunction?][] = [
['core', 'cjs'],
['core', 'esm'],
['formatters'],
['locale-detector', 'detectors'],
['rollup-plugin', 'rollup'],
['webpack-plugin', 'webpack'],
]

mappings.forEach(([fromWheretoImport, outputPath = fromWheretoImport, mapperFunction]) => {
const files = readdirSync(resolve(__dirname, `../types/${fromWheretoImport}/src`))
const files = globSync(resolve(__dirname, `../types/${fromWheretoImport}/src/**/*.d.ts`)).map((file) =>
resolve(file).substring(resolve(__dirname, `../types/${fromWheretoImport}/src/`).length + 1),
)

const filteredFiles = (mapperFunction && files.filter(mapperFunction)) || files

Expand Down
43 changes: 43 additions & 0 deletions packages/locale-detector/rollup.config.js
@@ -0,0 +1,43 @@
// @ts-check

import fs from 'fs'
import path from 'path'

import resolve from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import typescript from '@rollup/plugin-typescript'
import { terser } from 'rollup-plugin-terser'

const getPath = (file) => path.resolve(__dirname, file)

const isNotFolder = (fileName) => fileName.includes('.')

const files = [
...fs.readdirSync(getPath('./src')).filter(isNotFolder),
...fs.readdirSync(getPath('./src/detectors')).map((file) => `detectors/${file}`),
]

const config = files
.filter((file) => !file.startsWith('_'))
.map((file) => ({
input: getPath(`./src/${file}`),
output: [
{
file: getPath(`../../detectors/${file.replace('.ts', '.js')}`),
format: 'cjs',
},
],
plugins: [
resolve(),
commonjs(),
typescript({
tsconfig: getPath('./tsconfig.json'),
sourceMap: false,
declaration: false,
declarationDir: null,
}),
terser(),
],
}))

export default config
18 changes: 11 additions & 7 deletions packages/locale-detector/src/detect.ts
@@ -1,15 +1,19 @@
import { Locale } from 'packages/core/src/core'
import { Locale } from '../../core/src/core'
import { isTruthy } from 'typesafe-utils'

export type LocaleDetector = () => Locale[]
export type LocaleDetector = () => string[]

export const detectLocale = (baseLocale: Locale, availableLocales: Locale[], ...detectors: LocaleDetector[]): Locale =>
detectors.reduce<Locale | undefined>(
(prev, detector) => prev || findMatchingLocale(availableLocales, detector),
'',
export const detectLocale = <L extends Locale>(
baseLocale: L,
availableLocales: L[],
...detectors: LocaleDetector[]
): L =>
detectors.reduce<L | undefined>(
(prev, detector) => prev || findMatchingLocale<L>(availableLocales, detector),
'' as L,
) || baseLocale

const findMatchingLocale = (availableLocales: Locale[], detector: LocaleDetector): Locale | undefined => {
const findMatchingLocale = <L extends Locale>(availableLocales: L[], detector: LocaleDetector): L | undefined => {
const detectedLocales = detector().map((locale) => locale.toLowerCase())
// also include locles without country code e.g. if only 'en-US' is detected, we should also look for 'en'
const localesToMatch = Array.from(new Set(detectedLocales.flatMap((locale) => [locale, locale.split('-')[0]])))
Expand Down
7 changes: 7 additions & 0 deletions packages/locale-detector/tsconfig.json
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "../../detector"
},
"include": ["./src/**/*.ts"]
}
2 changes: 1 addition & 1 deletion tsconfig.json
Expand Up @@ -27,7 +27,7 @@
"jsx": "react"
},
"include": ["packages/**/*"],
"exclude": ["node_modules"],
"exclude": ["node_modules", "packages/**/test/generated/**/*"],
"ts-node": {
"compilerOptions": {
"module": "commonjs"
Expand Down

0 comments on commit 9f662a6

Please sign in to comment.