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 25, 2024
1 parent 47e2f12 commit bfc7fc3
Show file tree
Hide file tree
Showing 29 changed files with 281 additions and 95 deletions.
5 changes: 0 additions & 5 deletions cypress.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ async function getViteConfig(config: UserConfig) {
__dirname,
"./dist/salt-ds-countries",
),
"@salt-ds/date-adapters": path.resolve(
__dirname,
"./dist/salt-ds-date-adapters",
),
"@salt-ds/data-grid": path.resolve(
__dirname,
"./dist/salt-ds-data-grid",
Expand All @@ -73,7 +69,6 @@ async function getViteConfig(config: UserConfig) {
optimizeDeps: {
include: [
"@salt-ds/core",
"@salt-ds/data-adapters",
"@salt-ds/data-grid",
"@salt-ds/lab",
"@salt-ds/icons",
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"
}
}
164 changes: 164 additions & 0 deletions packages/date-adapters/scripts/build.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import path from "node:path";
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: "named",
sourcemapPathTransform: transformSourceMap,
});

await bundle.write({
freeze: false,
sourcemap: true,
preserveModules: false,
dir: path.join(outputDir, `dist-es/${adapterName}`),
format: "es",
exports: "named",
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",
exports: {
".": {
import: "./dist-es/index.js",
require: "./dist-cjs/index.js",
types: "./dist-types/types/index.d.ts",
},
"./date-fns": {
import: "./dist-es/date-fns/index.js",
require: "./dist-cjs/date-fns/index.js",
types: "./dist-types/date-fns-adapter/index.d.ts",
},
"./dayjs": {
import: "./dist-es/dayjs/index.js",
require: "./dist-cjs/dayjs/index.js",
types: "./dist-types/dayjs-adapter/index.d.ts",
},
"./luxon": {
import: "./dist-es/luxon/index.js",
require: "./dist-cjs/luxon/index.js",
types: "./dist-types/luxon-adapter/index.d.ts",
},
"./moment": {
import: "./dist-es/moment/index.js",
require: "./dist-cjs/moment/index.js",
types: "./dist-types/moment-adapter/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}`);
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Constructor = {
utc?: (value?: Parameters<typeof defaultDayjs>[0]) => Dayjs;
};

declare module "../types" {
declare module "@salt-ds/date-adapters" {
interface DateFrameworkTypeMap {
dayjs: Dayjs;
}
Expand Down
21 changes: 4 additions & 17 deletions packages/date-adapters/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
/**
* To add a new adapter, then, add the adapter's date object to `DateFrameworkTypeMap` interface
*
* declare module "./types" {
* interface DateFrameworkTypeMap {
* luxon: DateTime;
* }
* }
*/
// biome-ignore lint/complexity/noBannedTypes: type augmented by configured adapters
export type DateFrameworkTypeMap = {};

export * from "./date-fns";
export * from "./dayjs";
export * from "./luxon";
export * from "./moment";

export * from "./date-fns-adapter";
export * from "./dayjs-adapter";
export * from "./luxon-adapter";
export * from "./moment-adapter";
export * from "./types";
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
type Timezone,
} from "../types";

declare module "../types" {
declare module "@salt-ds/date-adapters" {
interface DateFrameworkTypeMap {
luxon: DateTime;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
type Timezone,
} from "../types";

declare module "../types" {
declare module "@salt-ds/date-adapters" {
export interface DateFrameworkTypeMap {
moment: Moment;
}
Expand Down
12 changes: 12 additions & 0 deletions packages/date-adapters/src/types/DateFrameworkTypeMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* To add a new adapter, then, add the adapter's date object to `DateFrameworkTypeMap` interface
*
* declare module "./types" {
* interface DateFrameworkTypeMap {
* luxon: DateTime;
* }
* }
*/

// biome-ignore lint/complexity/noBannedTypes: type augmented by configured adapters
export type DateFrameworkTypeMap = {};
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { DateFrameworkTypeMap } from "./DateFrameworkTypeMap";
/**
* Represents the date object of a date framework.
*
Expand Down
14 changes: 14 additions & 0 deletions packages/date-adapters/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"baseUrl": "./src",
"declaration": false,
"outDir": "./dist",
"module": "commonjs",
"target": "es2016",
"moduleResolution": "node",
"strict": true,
"skipLibCheck": true,
"esModuleInterop": true
},
"include": ["src/**/*"]
}
10 changes: 6 additions & 4 deletions packages/lab/src/__tests__/__e2e__/calendar/Calendar.a11y.cy.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
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,
AdapterDayjs,
AdapterLuxon,
AdapterMoment,
} from "@salt-ds/date-adapters";

import * as calendarStories from "@stories/calendar/calendar.stories";
import { composeStories } from "@storybook/react";
Expand Down
14 changes: 7 additions & 7 deletions packages/lab/src/__tests__/__e2e__/calendar/Calendar.cy.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type {
DateFrameworkType,
SaltDateAdapter,
import {
AdapterDateFns,
AdapterDayjs,
AdapterLuxon,
AdapterMoment,
type DateFrameworkType,
type SaltDateAdapter,
} from "@salt-ds/date-adapters";
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 {
Calendar,
CalendarGrid,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type {
DateFrameworkType,
SaltDateAdapter,
import {
AdapterDateFns,
AdapterDayjs,
AdapterLuxon,
AdapterMoment,
type DateFrameworkType,
type SaltDateAdapter,
} from "@salt-ds/date-adapters";
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 {
Calendar,
CalendarGrid,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type {
DateFrameworkType,
SaltDateAdapter,
import {
AdapterDateFns,
AdapterDayjs,
AdapterLuxon,
AdapterMoment,
type DateFrameworkType,
type SaltDateAdapter,
} from "@salt-ds/date-adapters";
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 {
Calendar,
CalendarGrid,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type {
DateFrameworkType,
SaltDateAdapter,
import {
AdapterDateFns,
AdapterDayjs,
AdapterLuxon,
AdapterMoment,
type DateFrameworkType,
type SaltDateAdapter,
} from "@salt-ds/date-adapters";
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 {
Calendar,
CalendarGrid,
Expand Down
10 changes: 6 additions & 4 deletions packages/lab/src/__tests__/__e2e__/date-input/DateInput.cy.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
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,
AdapterDayjs,
AdapterLuxon,
AdapterMoment,
} from "@salt-ds/date-adapters";
import * as dateInputStories from "@stories/date-input/date-input.stories";
import { composeStories } from "@storybook/react";
import { checkAccessibility } from "../../../../../../cypress/tests/checkAccessibility";
Expand Down
Loading

0 comments on commit bfc7fc3

Please sign in to comment.