From ac2e3625d74bcf68b90852a22c139853583f1e0f Mon Sep 17 00:00:00 2001 From: Alex Rock Ancelet Date: Mon, 18 Apr 2022 17:38:54 +0200 Subject: [PATCH] Fix @beyonk/svelte-datepicker local issue It's a hack to fix https://github.com/beyonk-adventures/svelte-datepicker/issues/56 But it's not the end fix. --- .../components/PaginatedTable/Filter.svelte | 2 +- src/lib/utils/date-utils.ts | 26 +++++++++++++++++++ svelte.config.js | 10 ++++--- 3 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 src/lib/utils/date-utils.ts diff --git a/src/lib/admin/components/PaginatedTable/Filter.svelte b/src/lib/admin/components/PaginatedTable/Filter.svelte index 301e1f4..c5b8e88 100644 --- a/src/lib/admin/components/PaginatedTable/Filter.svelte +++ b/src/lib/admin/components/PaginatedTable/Filter.svelte @@ -2,7 +2,7 @@ import ConfigFilter from '$lib/admin/ConfigFilter'; import FilterType from '$lib/admin/FilterType'; import DatePicker from '@beyonk/svelte-datepicker/src/components/DatePicker.svelte'; - import { dayjs } from '@beyonk/svelte-datepicker/src/components/lib/date-utils'; + import { dayjs } from '$lib/utils/date-utils'; export let filter: ConfigFilter; export let change_callback: Function; diff --git a/src/lib/utils/date-utils.ts b/src/lib/utils/date-utils.ts new file mode 100644 index 0000000..d38ab92 --- /dev/null +++ b/src/lib/utils/date-utils.ts @@ -0,0 +1,26 @@ +import * as baseDayJs from 'dayjs/esm/index.js'; +import localeData from 'dayjs/esm/plugin/localeData/index.js'; +import minMax from 'dayjs/esm/plugin/minMax/index.js'; +import isSameOrBefore from 'dayjs/esm/plugin/isSameOrBefore/index.js'; +import isSameOrAfter from 'dayjs/esm/plugin/isSameOrAfter/index.js'; + +let dayjs = baseDayJs; + +if (baseDayJs.default) { + dayjs = baseDayJs.default; +} + +if (dayjs && dayjs.extend) { + dayjs.extend(localeData); + dayjs.extend(minMax); + dayjs.extend(isSameOrBefore); + dayjs.extend(isSameOrAfter); +} else if (!dayjs) { + console.error('"dayjs" import is empty.'); +} else if (!dayjs.extend) { + console.error('"dayjs" import does not implement "extend".'); +} + +export { + dayjs +}; diff --git a/svelte.config.js b/svelte.config.js index 68eac21..8a474cb 100644 --- a/svelte.config.js +++ b/svelte.config.js @@ -3,7 +3,7 @@ import preprocess from 'svelte-preprocess'; import path from 'path'; import fs from 'fs'; -const copyPlugin = function (options) { +const copyFile = function (options) { return function () { const targetDir = path.dirname(options.target); if (!fs.existsSync(targetDir)){ @@ -29,14 +29,18 @@ const config = { vite: { prebundleSvelteLibraries: true, plugins: [ - copyPlugin({ + copyFile({ source: './node_modules/bootstrap/dist/js/bootstrap.bundle.min.js', target: './static/bootstrap.min.js', }), - copyPlugin({ + copyFile({ source: './node_modules/bootstrap/dist/js/bootstrap.bundle.min.js.map', target: './static/bootstrap.min.js.map', }), + copyFile({ + source: './src/lib/utils/date-utils.ts', + target: './node_modules/@beyonk/svelte-datepicker/src/components/lib/date-utils.js', + }), ], ssr: { noExternal: [ 'dayjs' ]