Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"sideEffects": false,
"main": "lib/$cjs/index.js",
"module": "lib/$esm/index.js",
"browser": "lib/$esm/index.js",
"types": "lib/$types/index.d.ts",
"directories": {
"lib": "lib"
Expand Down
14 changes: 13 additions & 1 deletion scripts/build.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import "./clean";

import { copyPackageSet } from "./tools/copyPackageSet";
import { generateExportsField } from "./tools/dualPackageSupport";
import { shell } from "./tools/shell";

const main = async () => {
Expand All @@ -10,7 +11,18 @@ const main = async () => {
shell("yarn tsc -p tsconfig.esm.json"),
]);
await shell("cherry-pick --cwd ./lib --input-dir ../src --types-dir ./\\$types --cjs-dir ./\\$cjs --esm-dir ./\\$esm");
await copyPackageSet();

const exportsFiled = generateExportsField("./src", {
directory: {
import: "./$esm",
// require: "./$cjs", // OFFにするとwebpack 5でesmを読んでくれる
node: "./$cjs",
browser: "./$esm",
default: "./$cjs",
},
});

await copyPackageSet(exportsFiled);
};

main().catch(error => {
Expand Down
4 changes: 3 additions & 1 deletion scripts/tools/copyPackageSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ const pkg = require("../../package.json");
/**
* README, LICENCE, CHANGELOG.mdをlibディレクトリに出力する
*/
export const copyPackageSet = async (): Promise<void> => {
export const copyPackageSet = async (exportsField: {}): Promise<void> => {
const libDir = "lib";
const publishPackageJson = path.join(libDir, "package.json");
pkg.private = undefined;
pkg.main = path.posix.relative(libDir, pkg.main);
pkg.browser = path.posix.relative(libDir, pkg.browser);
pkg.module = path.posix.relative(libDir, pkg.module);
pkg.types = path.posix.relative(libDir, pkg.types);
pkg.directories = undefined;
pkg.files = undefined;
pkg.exports = exportsField;
fs.writeFileSync(publishPackageJson, JSON.stringify(pkg, null, 2), {
encoding: "utf-8",
});
Expand Down
64 changes: 64 additions & 0 deletions scripts/tools/dualPackageSupport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import * as fs from "fs";
import { posix as path } from "path";

export type SupportModuleType = "browser" | "node" | "require" | "import" | "default";

export type SupportModule = {
// eslint-disable-next-line no-unused-vars
[P in SupportModuleType]?: string;
};

export interface Option {
directory: Partial<SupportModule>;
}

export interface ExportsField {
[filepath: string]: SupportModule;
}

const isFile = (p: string) => {
return fs.existsSync(p) && fs.statSync(p).isFile();
};

const isTypeScriptFile = (p: string): boolean => {
return !!p.match(/tsx?$/);
};

const isIndexFile = (p: string) => {
return !!p.match(/index\.tsx?$/);
};

const convertNameTsToJs = (p: string): string => {
return p.replace(/\.tsx?$/, ".js");
};

const isSupportModuleType = (text: string | undefined): text is SupportModuleType => {
if (!text) {
return false;
}
return ["node", "browser", "require", "import", "default"].includes(text);
};

const trimExtension = (p: string): string => {
const ext = path.extname(p);
return p.replace(new RegExp(ext + "$"), "");
};

export const generateExportsField = (sourceRootDirectory: string, option: Option): ExportsField => {
const pathArray = fs.readdirSync(sourceRootDirectory);
const filteredPathArray = pathArray.filter(p => {
const pathName = path.join(sourceRootDirectory, p);
return isTypeScriptFile(pathName) && isFile(pathName);
});
return filteredPathArray.reduce<ExportsField>((exportsFields, pathName) => {
const filepath = isIndexFile(pathName) ? "." : "./" + trimExtension(pathName);
const jsFilename = convertNameTsToJs(pathName);
const supportModule: SupportModule = {};
Object.entries(option.directory).forEach(([key, exportBaseDirectory]) => {
if (isSupportModuleType(key) && typeof exportBaseDirectory === "string") {
supportModule[key] = "./" + path.join(exportBaseDirectory, jsFilename);
}
});
return { ...exportsFields, [filepath]: supportModule };
}, {});
};
2 changes: 1 addition & 1 deletion src/Validator/v3/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ export const validate = (openapiDoc: OpenApi.Document, option?: LogOption): void
validate(openapiDoc);
if (validate.errors) {
showLogs(validate.errors, option);
process.exit(1);
throw new Error("Validation Error");
}
};
6 changes: 6 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export * as TypeScriptCodeGenerator from "./CodeGenerator";
export * as Converter from "./Converter";
export * as DefaultCodeTemplate from "./DefaultCodeTemplate";
export { fileSystem } from "./FileSystem";
export * as ResolveReference from "./ResolveReference";
export * as Validator from "./Validator";
20 changes: 10 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4359,10 +4359,10 @@ globals@^12.1.0:
dependencies:
type-fest "^0.8.1"

globalyzer@^0.1.0:
version "0.1.4"
resolved "https://registry.yarnpkg.com/globalyzer/-/globalyzer-0.1.4.tgz#bc8e273afe1ac7c24eea8def5b802340c5cc534f"
integrity sha512-LeguVWaxgHN0MNbWC6YljNMzHkrCny9fzjmEUdnF1kQ7wATFD1RHFRqA1qxaX2tgxGENlcxjOflopBwj3YZiXA==
[email protected]:
version "0.1.0"
resolved "https://registry.yarnpkg.com/globalyzer/-/globalyzer-0.1.0.tgz#cb76da79555669a1519d5a8edf093afaa0bf1465"
integrity sha512-40oNTM9UfG6aBmuKxk/giHn5nQ8RVz/SS4Ir6zgzOv9/qC3kKZ9v4etGTcJbEl/NyVQH7FGU7d+X1egr57Md2Q==

[email protected]:
version "10.0.0"
Expand Down Expand Up @@ -4404,7 +4404,7 @@ globby@^9.2.0:
pify "^4.0.1"
slash "^2.0.0"

globrex@^0.1.1:
globrex@^0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/globrex/-/globrex-0.1.2.tgz#dd5d9ec826232730cd6793a5e33a9302985e6098"
integrity sha512-uHJgbwAMwNFf5mLst7IWLNg14x1CkeqglJb/K3doi4dw6q2IvAAmM/Y81kevy83wP+Sst+nutFTYOGg3d1lsxg==
Expand Down Expand Up @@ -8362,12 +8362,12 @@ through@2, "through@>=2.2.7 <3", through@^2.3.4, through@^2.3.6, through@^2.3.8:
integrity sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=

tiny-glob@^0.2.6:
version "0.2.6"
resolved "https://registry.yarnpkg.com/tiny-glob/-/tiny-glob-0.2.6.tgz#9e056e169d9788fe8a734dfa1ff02e9b92ed7eda"
integrity sha512-A7ewMqPu1B5PWwC3m7KVgAu96Ch5LA0w4SnEN/LbDREj/gAD0nPWboRbn8YoP9ISZXqeNAlMvKSKoEuhcfK3Pw==
version "0.2.8"
resolved "https://registry.yarnpkg.com/tiny-glob/-/tiny-glob-0.2.8.tgz#b2792c396cc62db891ffa161fe8b33e76123e531"
integrity sha512-vkQP7qOslq63XRX9kMswlby99kyO5OvKptw7AMwBVMjXEI7Tb61eoI5DydyEMOseyGS5anDN1VPoVxEvH01q8w==
dependencies:
globalyzer "^0.1.0"
globrex "^0.1.1"
globalyzer "0.1.0"
globrex "^0.1.2"

tmp@^0.0.33:
version "0.0.33"
Expand Down