Skip to content

Commit

Permalink
Attempt to make the library tree-shakeable by esbuild
Browse files Browse the repository at this point in the history
See [evanw/esbuild#1420](evanw/esbuild#1420).

Signed-off-by: William So <polyipseity@gmail.com>
  • Loading branch information
polyipseity committed Aug 17, 2023
1 parent 36f4b47 commit 77e138d
Show file tree
Hide file tree
Showing 3 changed files with 287 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/dirty-frogs-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@polyipseity/obsidian-plugin-library": patch
---

Attempt to make the library tree-shakeable by esbuild. See [evanw/esbuild#1420](https://github.com/evanw/esbuild/issues/1420).
41 changes: 41 additions & 0 deletions sources/.index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Await https://github.com/evanw/esbuild/issues/1420
import { readFile, writeFile } from "node:fs/promises"

function generateNamedExports(filename, code) {
return `export {
${[...new Set(
[...code.matchAll(/^export[^]+?(class|const|function|interface|let|namespace|type|var)[ \n]+([^ \n(<]+)/gmu)]
.map(([, keyword, name]) =>
`${["interface", "type"].includes(keyword) ? "type " : ""}${name}`),
)]
.sort((left, right) => {
const left2 = left.replace(/^type /u, "")
const right2 = right.replace(/^type /u, "")
return left2 > right2 ? 1 : left2 < right2 ? -1 : 0
})
.concat([""])
.join(",\n")}} from "${filename}"`
}

const regex = /^export[^]+?from[^"]+"([^"]*)"$/gmu,
index = await readFile("index.ts", { encoding: "utf-8" }),
files = Object.fromEntries(
await Promise.all(
[...index.matchAll(regex)]
.map(async ([, filename]) => [
filename,
generateNamedExports(
filename,
await readFile(
filename.replace(/\.js$/u, ".ts"),
{ encoding: "utf-8" },
),
),
]),
),
)
await writeFile(
"index.ts",
index.replace(regex, (substring, filename) => files[filename] ?? substring),
{ encoding: "utf-8" },
)
267 changes: 241 additions & 26 deletions sources/index.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,241 @@
export * from "../assets/locales.js"
export * from "./@types/lib.es5.js"
// eslint-disable-next-line import/export
export * from "./@types/obsidian.js"
export * from "./components/index.js"
export * from "./documentation.js"
export * from "./fixers.js"
export * from "./i18n.js"
export * from "./icons.js"
export * from "./import.js"
export * from "./magic.js"
export * from "./modals.js"
export * from "./obsidian.js"
export * from "./patch.js"
export * from "./platform.js"
export * from "./plugin.js"
export * from "./private.js"
export * from "./rules.js"
export * from "./settings-tab.js"
export * from "./settings-widgets.js"
export * from "./settings.js"
export * from "./source-maps.js"
export * from "./status-bar.js"
export * from "./typeof.js"
export * from "./types.js"
export * from "./util.js"
// Await https://github.com/evanw/esbuild/issues/1420
export {
type AwaitResources,
LibraryLocales,
type MergeNamespaces,
type MergeResources,
type NormalizeLocale,
mergeResources,
syncLocale,
} from "../assets/locales.js"
// eslint-disable-next-line @typescript-eslint/no-useless-empty-export
export {
} from "./@types/lib.es5.js"
export {
} from "./@types/obsidian.js"
export {
} from "./components/index.js"
export {
DocumentationMarkdownView,
} from "./documentation.js"
export {
type Fixed,
type Fixer,
fixArray,
fixInSet,
fixTyped,
markFixed,
} from "./fixers.js"
export {
type I18nFormatters,
type I18nNamespaces,
type I18nResources,
LanguageManager,
type TranslationKey,
createI18n,
} from "./i18n.js"
export {
addIcon,
registerIcon,
registerLucideIcon,
} from "./icons.js"
export {
type Bundle,
dynamicRequire,
dynamicRequireLazy,
dynamicRequireSync,
importable,
} from "./import.js"
export {
DOMClasses,
FileExtensions,
LibraryUUIDs,
} from "./magic.js"
export {
DialogModal,
EditDataModal,
ListModal,
makeModalDynamicWidth,
} from "./modals.js"
export {
LambdaComponent,
ResourceComponent,
type StatusUI,
UnnamespacedID,
UpdatableUI,
addCommand,
addRibbonIcon,
awaitCSS,
cleanFrontmatterCache,
commandNamer,
newCollabrativeState,
notice,
notice2,
printError,
printMalformedData,
readStateCollabratively,
recordViewStateHistory,
saveFileAs,
statusUI,
updateView,
useSettings,
useSubsettings,
writeStateCollabratively,
} from "./obsidian.js"
export {
patchWindows,
} from "./patch.js"
export {
Platform,
} from "./platform.js"
export type {
PluginContext,
} from "./plugin.js"
export {
type HasPrivate,
type Private,
type PrivateKeys,
type PrivateKeys$,
type RevealPrivate,
revealPrivate,
revealPrivateAsync,
} from "./private.js"
export {
type Rule,
Rules,
SettingRules,
rulesList,
} from "./rules.js"
export {
AdvancedSettingTab,
} from "./settings-tab.js"
export {
type ComponentAction,
closeSetting,
composeSetters,
dropdownSelect,
linkSetting,
resetButton,
setTextToEnum,
setTextToNumber,
} from "./settings-widgets.js"
export {
SettingsManager,
registerSettingsCommands,
} from "./settings.js"
export {
attachFunctionSourceMap,
attachSourceMap,
generateFunctionSourceMap,
generateSourceMap,
} from "./source-maps.js"
export {
StatusBarHider,
getStatusBar,
} from "./status-bar.js"
export {
type InverseTypeofMap,
type InverseTypeofMapE,
PRIMITIVE_TYPES,
PRIMITIVE_TYPES_E,
type PrimitiveOf,
type PrimitiveOfE,
type PrimitiveType,
type PrimitiveTypeE,
type TypeofMap,
type TypeofMapE,
genericTypeofGuard,
genericTypeofGuardE,
primitiveOf,
primitiveOfE,
typeofE,
} from "./typeof.js"
export {
type AnyObject,
type AsyncFunctionConstructor,
type Base64String,
type CodePoint,
type Deopaque,
type DistributeKeys,
type DistributeValues,
type Evaluate,
type IsExact,
NULL_SEM_VER_STRING,
type ReadonlyTuple,
type SemVerString,
type Unchecked,
codePoint,
contravariant,
correctType,
deopaque,
launderUnchecked,
opaqueOrDefault,
semVerString,
simplifyType,
} from "./types.js"
export {
EventEmitterLite,
Functions,
type KeyModifier,
type PromisePromise,
acquireConditionally,
activeSelf,
alternativeRegExp,
anyToError,
aroundIdentityFactory,
assignExact,
asyncDebounce,
asyncFunction,
base64ToBytes,
base64ToString,
basename,
bigIntReplacer,
bracket,
bytesToBase64,
capitalize,
cartesianProduct,
clear,
clearProperties,
cloneAsFrozen,
cloneAsWritable,
consumeEvent,
copyOnWrite,
copyOnWriteAsync,
createChildElement,
createDocumentFragment,
deepFreeze,
destroyWithOutro,
escapeJavaScriptString,
escapeQuerySelectorAttribute,
extname,
getKeyModifiers,
inSet,
insertAt,
instanceOf,
isHomogenousArray,
isNonNil,
lazyInit,
lazyProxy,
logFormat,
mapFirstCodePoint,
multireplace,
onResize,
onVisible,
openExternal,
promisePromise,
randomNotIn,
rangeCodePoint,
remove,
removeAt,
replaceAllRegex,
sleep2,
splitLines,
startCase,
stringToBase64,
swap,
typedIn,
typedKeys,
typedOwnKeys,
uncapitalize,
unexpected,
} from "./util.js"

0 comments on commit 77e138d

Please sign in to comment.