Skip to content

Commit

Permalink
feat(datepicker): bundle locales for ESM build
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurdenner committed Jan 17, 2022
1 parent 2c4c6b4 commit ef135da
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 8 deletions.
6 changes: 6 additions & 0 deletions dts.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const copy = require('rollup-plugin-copy');
const postcss = require('rollup-plugin-postcss');
const replace = require('@rollup/plugin-replace');
const autoprefixer = require('autoprefixer');
const cssnano = require('cssnano');

Expand All @@ -18,6 +19,11 @@ module.exports = {
extract: options.writeMeta
? 'react-semantic-ui-datepickers.css'
: false,
}),
replace({
// This allows tree-shaking based on the output format
'process.env.FORMAT': JSON.stringify(options.format),
preventAssignment: true,
})
);

Expand Down
26 changes: 18 additions & 8 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { BasicDatePicker, RangeDatePicker } from './pickers';
import { Locale, SemanticDatepickerProps } from './types';
import Calendar from './components/calendar';
import Input from './components/input';
import allLocales from './locales';

const style: React.CSSProperties = {
display: 'inline-block',
Expand Down Expand Up @@ -167,16 +168,25 @@ class SemanticDatepicker extends React.Component<
get locale() {
const { locale } = this.props;

let localeJson: Locale;
if (process.env.FORMAT === 'cjs') {
let localeJson: Locale;

try {
localeJson = require(`./locales/${locale}.json`);
} catch (e) {
console.warn(`"${locale}" is not a valid locale`);
localeJson = require('./locales/en-US.json');
}
try {
localeJson = require(`./locales/${locale}.json`);
} catch (e) {
console.warn(`"${locale}" is not a valid locale`);
localeJson = require('./locales/en-US.json');
}

return localeJson;
} else {
if (!allLocales.has(locale)) {
console.warn(`"${locale}" is not a valid locale`);
return allLocales.get('en-US');
}

return localeJson;
return allLocales.get(locale);
}
}

get weekdays() {
Expand Down
49 changes: 49 additions & 0 deletions src/locales/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import bg_BG from './bg-BG';
import ca_ES from './ca-ES';
import cs_CZ from './cs-CZ';
import de_DE from './de-DE';
import el_GR from './el-GR';
import en_US from './en-US';
import es_ES from './es-ES';
import et_EE from './et-EE';
import fi_FI from './fi-FI';
import fr_FR from './fr-FR';
import he_IL from './he-IL';
import it_IT from './it-IT';
import ja_JP from './ja-JP';
import ko_KR from './ko-KR';
import nb_NO from './nb-NO';
import nn_NO from './nn-NO';
import pl_PL from './pl-PL';
import pt_BR from './pt-BR';
import ru_RU from './ru-RU';
import sk_SK from './sk-SK';
import sv_SE from './sv-SE';
import tr_TR from './tr-TR';
import zh_CN from './zh-CN';

export default new Map([
['bg-BG', bg_BG],
['ca-ES', ca_ES],
['cs-CZ', cs_CZ],
['de-DE', de_DE],
['el-GR', el_GR],
['en-US', en_US],
['es-ES', es_ES],
['et-EE', et_EE],
['fi-FI', fi_FI],
['fr-FR', fr_FR],
['he-IL', he_IL],
['it-IT', it_IT],
['ja-JP', ja_JP],
['ko-KR', ko_KR],
['nb-NO', nb_NO],
['nn-NO', nn_NO],
['pl-PL', pl_PL],
['pt-BR', pt_BR],
['ru-RU', ru_RU],
['sk-SK', sk_SK],
['sv-SE', sv_SE],
['tr-TR', tr_TR],
['zh-CN', zh_CN],
]);

0 comments on commit ef135da

Please sign in to comment.