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 47e2f12 commit 0626cdc
Show file tree
Hide file tree
Showing 45 changed files with 1,811 additions and 92 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"
}
}
137 changes: 137 additions & 0 deletions packages/date-adapters/scripts/build.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
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: "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";
Loading

0 comments on commit 0626cdc

Please sign in to comment.