Skip to content

Commit

Permalink
Split date adapters into sub packages to help with peerDependencies r…
Browse files Browse the repository at this point in the history
…esolution

```diff
- import { AdapterDateFns } from "@salt-ds/date-adapters";
- import { AdapterDayjs } from "@salt-ds/date-adapters";
- import { AdapterLuxon } from "@salt-ds/date-adapters";
- import { AdapterMoment } from "@salt-ds/date-adapters";
+ import { AdapterDateFns } from "@salt-ds/date-adapters/date-fns";
+ import { AdapterDayjs } from "@salt-ds/date-adapters/dayjs";
+ import { AdapterLuxon } from "@salt-ds/date-adapters/luxon";
+ import { AdapterMoment } from "@salt-ds/date-adapters/moment";
```
  • Loading branch information
mark-tate committed Dec 24, 2024
1 parent 3c4f2c5 commit 2709996
Show file tree
Hide file tree
Showing 45 changed files with 1,474 additions and 87 deletions.
8 changes: 4 additions & 4 deletions docs/decorators/withLocalization.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { Decorator } from "@storybook/react";
import "dayjs/locale/en";
import { AdapterDateFns } from "@salt-ds/date-adapters";
import { AdapterDayjs } from "@salt-ds/date-adapters";
import { AdapterLuxon } from "@salt-ds/date-adapters";
import { AdapterMoment } from "@salt-ds/date-adapters";
import { AdapterDateFns } from "@salt-ds/date-adapters/date-fns";
import { AdapterDayjs } from "@salt-ds/date-adapters/dayjs";
import { AdapterLuxon } from "@salt-ds/date-adapters/luxon";
import { AdapterMoment } from "@salt-ds/date-adapters/moment";
import { LocalizationProvider } from "@salt-ds/lab";
import { enUS as dateFnsEnUs } from "date-fns/locale";

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
"get-tsconfig": "^4.7.5",
"rollup": "^4.24.2",
"rollup-plugin-esbuild": "^6.1.1",
"rollup-plugin-postcss": "^4.0.2"
"rollup-plugin-postcss": "^4.0.2",
"vite-tsconfig-paths": "^4.2.0"
}
}
2 changes: 1 addition & 1 deletion packages/date-adapters/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@
"provenance": true
},
"scripts": {
"build": "yarn node ../../scripts/build.mjs"
"build": "yarn node ./scripts/build.mjs"
}
}
138 changes: 138 additions & 0 deletions packages/date-adapters/scripts/build.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import path from "node:path";

Check failure on line 1 in packages/date-adapters/scripts/build.mjs

View workflow job for this annotation

GitHub Actions / lint

format

File content differs from formatting output
import commonjs from "@rollup/plugin-commonjs";
import json from "@rollup/plugin-json";
import { nodeResolve } from "@rollup/plugin-node-resolve";
import browserslistToEsbuild from "browserslist-to-esbuild";
import fs from "fs-extra";
import { rollup } from "rollup";
import esbuild from "rollup-plugin-esbuild";
import { makeTypings } from "./../../../scripts/makeTypings.mjs";
import { transformWorkspaceDeps } from "./../../../scripts/transformWorkspaceDeps.mjs";
import { distinct } from "./../../../scripts/utils.mjs";

const cwd = process.cwd();

const packageJson = (
await import(path.join("file://", cwd, "package.json"), {
with: { type: "json" },
})
).default;

const FILES_TO_COPY = ["README.md", "LICENSE", "CHANGELOG.md"].concat(
packageJson.files ?? [],
);

const packageName = packageJson.name;
const outputDir = path.join(packageJson.publishConfig.directory);

console.log(`Building ${packageName}`);

await fs.mkdirp(outputDir);
await fs.emptyDir(outputDir);

// Define entry points for each adapter
const entryPoints = {
types: path.join(cwd, "src/types/index.ts"),
moment: path.join(cwd, "src/moment-adapter/index.ts"),
luxon: path.join(cwd, "src/luxon-adapter/index.ts"),
dayjs: path.join(cwd, "src/dayjs-adapter/index.ts"),
"date-fns": path.join(cwd, "src/date-fns-adapter/index.ts"),
};

for (const [adapterName, inputPath] of Object.entries(entryPoints)) {

const entryFolder = path.basename(path.dirname(inputPath));

await makeTypings(outputDir, path.dirname(inputPath));

const bundle = await rollup({
input: inputPath,
external: (id) => {
if (id === "babel-plugin-transform-async-to-promises/helpers") {
return false;
}
return !id.startsWith(".") && !path.isAbsolute(id);
},
treeshake: {
propertyReadSideEffects: false,
},
plugins: [
nodeResolve({
extensions: [".ts", ".tsx", ".js", ".jsx"],
browser: true,
mainFields: ["module", "main", "browser"],
}),
commonjs({ include: /\/node_modules\// }),
esbuild({
target: browserslistToEsbuild(),
minify: false,
sourceMap: true,
}),
json(),
],
});

const transformSourceMap = (relativeSourcePath, sourceMapPath) => {
const absoluteSourcepath = path.resolve(
path.dirname(sourceMapPath),
relativeSourcePath,
);
const packageRelativeSourcePath = path.relative(cwd, absoluteSourcepath);

return `../${packageRelativeSourcePath}`;
};

await bundle.write({
freeze: false,
sourcemap: true,
preserveModules: false,
dir: path.join(outputDir, `dist-cjs/${adapterName}`),
format: "cjs",
exports: "auto",
sourcemapPathTransform: transformSourceMap,
});

await bundle.write({
freeze: false,
sourcemap: true,
preserveModules: false,
dir: path.join(outputDir, `dist-es/${adapterName}`),
format: "es",
exports: "auto",
sourcemapPathTransform: transformSourceMap,
});

await bundle.close();
}

await fs.writeJSON(
path.join(outputDir, "package.json"),
{
...packageJson,
dependencies: await transformWorkspaceDeps(packageJson.dependencies),
main: "dist-cjs/index.js",
module: "dist-es/index.js",
typings: "dist-types/types/index.d.ts",
files: distinct([
...(packageJson.files ?? []),
"dist-cjs",
"dist-es",
"dist-types",
"CHANGELOG.md",
]),
},
{ spaces: 2 },
);

for (const file of FILES_TO_COPY) {
const filePath = path.join(cwd, file);
try {
await fs.copy(filePath, path.join(outputDir, file));
} catch (error) {
if (error.code !== "ENOENT") {
throw error;
}
}
}

console.log(`Built ${packageName} into ${outputDir}`);
1 change: 1 addition & 0 deletions packages/date-adapters/src/__tests__/date-fns.spec.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
1 change: 1 addition & 0 deletions packages/date-adapters/src/__tests__/dayjs.spec.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
1 change: 1 addition & 0 deletions packages/date-adapters/src/__tests__/luxon.spec.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
1 change: 1 addition & 0 deletions packages/date-adapters/src/__tests__/moment.spec.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "moment-timezone";
208 changes: 208 additions & 0 deletions packages/date-adapters/src/date-fns-adapter/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
import { type Locale } from "date-fns";

Check failure on line 1 in packages/date-adapters/src/date-fns-adapter/index.d.ts

View workflow job for this annotation

GitHub Actions / lint

lint/style/useImportType

All these imports are only used as types.

Check failure on line 1 in packages/date-adapters/src/date-fns-adapter/index.d.ts

View workflow job for this annotation

GitHub Actions / lint

format

File content differs from formatting output
import { type AdapterOptions, type ParserResult, type RecommendedFormats, type SaltDateAdapter, type TimeFields, type Timezone } from "../types";

Check failure on line 2 in packages/date-adapters/src/date-fns-adapter/index.d.ts

View workflow job for this annotation

GitHub Actions / lint

lint/style/useImportType

All these imports are only used as types.
declare module "@salt-ds/date-adapters" {
interface DateFrameworkTypeMap {
"date-fns": Date;
}
}
/**
* Adapter for date-fns library, implementing the SaltDateAdapter interface.
* Provides methods for date manipulation and formatting using date-fns.
*/
export declare class AdapterDateFns implements SaltDateAdapter<Date, Locale> {
/**
* The locale used for date formatting.
*/
locale: Locale;
/**
* The name of the date library.
*/
lib: string;
/**
* Mapping of format tokens from other libraries to date-fns format tokens.
*/
formats: {
[key: string]: string;
};
/**
* Creates an instance of AdapterDateFns.
* @param options - Adapter options including locale.
*/
constructor({ locale }?: AdapterOptions<Locale, typeof Date>);
/**
* Maps format tokens from other libraries to date-fns format tokens.
* @param adapterFormat - The format string to map.
* @returns The mapped format string.
*/
private mapToDateFnsFormat;
/**
* Creates a Date object from a string or returns an invalid date.
* @param value - The date string to parse.
* @param timezone - The timezone to use (default is "default").
* @param locale - The locale to use for parsing.
* @returns The parsed Date object or an invalid date object.
*/
date: <T extends string | undefined>(value?: T | undefined, _timezone?: Timezone, _locale?: Locale | undefined) => Date;
/**
* Formats a Date object using the specified format string.
* Returns an empty string when null or undefined date is given.
* @param date - The Date object to format.
* @param format - The format string to use.
* @param locale - The locale to use for formatting.
* @returns The formatted date string.
*/
format(date: Date | null | undefined, format?: RecommendedFormats, locale?: Locale): string;
/**
* Compares two Date objects.
* @param dateA - The first Date object.
* @param dateB - The second Date object.
* @returns 0 if equal, 1 if dateA is after dateB, -1 if dateA is before dateB.
*/
compare(dateA: Date, dateB: Date): number;
/**
* Parses a date string using the specified format.
* @param value - The date string to parse.
* @param format - The format string to use.
* @param locale - The locale to use for parsing.
* @returns A DateDetail object containing the parsed date and any errors.
*/
parse(value: string, format: string, locale?: Locale): ParserResult<Date>;
/**
* Checks if a Date object is valid.
* @param date - The Date object to check.
* @returns True if the date is valid date object, false otherwise.
*/
isValid(date: any): date is Date;
/**
* Subtracts time from a Date object.
* @param date - The Date object to subtract from.
* @param duration - The duration to subtract.
* @returns The resulting Date object.
*/
subtract(date: Date, { days, weeks, months, years, hours, minutes, seconds, milliseconds, }: {
days?: number;
weeks?: number;
months?: number;
years?: number;
hours?: number;
minutes?: number;
seconds?: number;
milliseconds?: number;
}): Date;
/**
* Adds time to a Date object.
* @param date - The Date object to add to.
* @param duration - The duration to add.
* @returns The resulting Date object.
*/
add(date: Date, { days, weeks, months, years, hours, minutes, seconds, milliseconds, }: {
days?: number;
weeks?: number;
months?: number;
years?: number;
hours?: number;
minutes?: number;
seconds?: number;
milliseconds?: number;
}): Date;
/**
* Sets specific components of a Date object.
* @param date - The Date object to modify.
* @param components - The components to set, the month is a number (1-12).
* @returns The resulting Date object.
*/
set(date: Date, { day, month, year, hour, minute, second, millisecond, }: {
day?: number;
month?: number;
year?: number;
hour?: number;
minute?: number;
second?: number;
millisecond?: number;
}): Date;
/**
* Checks if two Date objects are the same based on the specified granularity.
* @param dateA - The first Date object.
* @param dateB - The second Date object.
* @param granularity - The granularity to compare by ("day", "month", "year").
* @returns True if the dates are the same, false otherwise.
*/
isSame(dateA: Date, dateB: Date, granularity?: "day" | "month" | "year"): boolean;
/**
* Gets the start of a specified time period for a Date object.
* @param date - The Date object.
* @param offset - The time period ("day", "week", "month", "year").
* @param locale - The locale to use.
* @returns The Date object representing the start of the period.
*/
startOf(date: Date, offset: "day" | "week" | "month" | "year", locale?: Locale): Date;
/**
* Gets the end of a specified time period for a Date object.
* @param date - The Date object.
* @param offset - The time period ("day", "week", "month", "year").
* @param locale - The locale to use.
* @returns The Date object representing the end of the period.
*/
endOf(date: Date, offset: "day" | "week" | "month" | "year", locale?: Locale): Date;
/**
* Gets the current date with the time set to the start of the day.
* @param _locale - The locale to use.
* @returns The current date at the start of the day.
*/
today(_locale?: Locale): Date;
/**
* Gets the current date and time.
* @param locale - The locale to use.
* @returns The current date and time.
*/
now(locale?: Locale): Date;
/**
* Gets the day of the week for a Date object.
* @param date - The Date object.
* @param locale - The locale to use.
* @returns The day of the week as a number (0-6).
*/
getDayOfWeek(date: Date, locale?: Locale): number;
/**
* Gets the name of the day of the week.
* @param dow - The day of the week as a number (0-6).
* @param format - The format for the day name ("long", "short", "narrow").
* @param locale - The locale to use.
* @returns The name of the day of the week.
*/
getDayOfWeekName(dow: number, format: "long" | "short" | "narrow", locale: Locale): string;
/**
* Gets the day of the month for a Date object.
* @param date - The Date object.
* @returns The day of the month as a number (1-31).
*/
getDay(date: Date): number;
/**
* Gets the month for a Date object.
* @param date - The Date object.
* @returns The month as a number (1-12).
*/
getMonth(date: Date): number;
/**
* Gets the year for a Date object.
* @param date - The Date object.
* @returns The year as a number.
*/
getYear(date: Date): number;
/**
* Gets the time components for a Date object.
* @param date - The Date object.
* @returns An object containing the hour, minute, second, and millisecond.
*/
getTime(date: Date): TimeFields;
/**
* Validate date string so it can be parsed
* @param value
*/
isValidDateString(value: string): boolean;
/**
* Clone the date object
* @param date
*/
clone(date: Date): Date;
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import {
type Timezone,
} from "../types";

declare module "../types" {
declare module "@salt-ds/date-adapters" {
interface DateFrameworkTypeMap {
"date-fns": Date;
}
Expand Down
Loading

0 comments on commit 2709996

Please sign in to comment.