generated from Himenon/template-js
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: support dual package * fix: webpack load esm
- Loading branch information
Showing
7 changed files
with
98 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
}, {}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters