-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #503 from xmtp/rygine/wasm-update
Refactor WASM integration for web
- Loading branch information
Showing
13 changed files
with
455 additions
and
268 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { basename, dirname, extname, format } from 'node:path' | ||
import { existsSync } from 'node:fs' | ||
import { getResolvedPath, loadCompilerOptions } from './utils' | ||
import { Plugin } from 'esbuild' | ||
|
||
type ResolveExtensionsPluginOptions = { | ||
extensions?: string[] | ||
tsconfigPath?: string | ||
} | ||
|
||
export const resolveExtensionsPlugin = ( | ||
options?: ResolveExtensionsPluginOptions | ||
): Plugin => { | ||
const { tsconfigPath, extensions = [] } = options ?? {} | ||
const compilerOptions = loadCompilerOptions(tsconfigPath) | ||
return { | ||
name: 'resolve-extensions', | ||
setup({ onResolve }) { | ||
onResolve({ filter: /.*/ }, async ({ kind, importer, path }) => { | ||
let result: null | { path: string } = null | ||
switch (kind) { | ||
case 'import-statement': | ||
case 'require-call': | ||
case 'dynamic-import': | ||
case 'require-resolve': | ||
{ | ||
const resolvedPath = getResolvedPath( | ||
path, | ||
importer, | ||
compilerOptions | ||
) | ||
if (resolvedPath) { | ||
const ext = extname(resolvedPath) | ||
const base = basename(resolvedPath, ext) | ||
const dir = dirname(resolvedPath) | ||
// check for extensions | ||
extensions.some((extension) => { | ||
const newPath = format({ | ||
dir, | ||
name: base, | ||
ext: `${extension}${ext}`, | ||
}) | ||
const exists = existsSync(newPath) | ||
if (exists) { | ||
result = { path: newPath } | ||
return true | ||
} | ||
return false | ||
}) | ||
} | ||
} | ||
break | ||
} | ||
return result | ||
}) | ||
}, | ||
} | ||
} |
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,48 @@ | ||
import { readFileSync, existsSync } from 'node:fs' | ||
import type { CompilerOptions } from 'typescript' | ||
import ts from 'typescript' | ||
import { findUpSync } from 'find-up' | ||
|
||
const { nodeModuleNameResolver, sys } = ts | ||
|
||
type TSConfig = { | ||
compilerOptions?: CompilerOptions | ||
} | ||
|
||
const loadJSON = (jsonPath: string) => | ||
JSON.parse(readFileSync(jsonPath, 'utf8')) as TSConfig | ||
|
||
export const loadCompilerOptions = (tsconfigPath?: string) => { | ||
let config: TSConfig = {} | ||
if (!tsconfigPath) { | ||
const configPath = findUpSync('tsconfig.json') | ||
if (configPath) { | ||
config = loadJSON(configPath) | ||
} | ||
} else { | ||
if (existsSync(tsconfigPath)) { | ||
config = loadJSON(tsconfigPath) | ||
} | ||
} | ||
return config?.compilerOptions ?? {} | ||
} | ||
|
||
export const getResolvedPath = ( | ||
path: string, | ||
importer: string, | ||
compilerOptions: CompilerOptions | ||
) => { | ||
const { resolvedModule } = nodeModuleNameResolver( | ||
path, | ||
importer, | ||
compilerOptions, | ||
sys | ||
) | ||
|
||
const resolvedFileName = resolvedModule?.resolvedFileName | ||
if (!resolvedFileName || resolvedFileName.endsWith('.d.ts')) { | ||
return null | ||
} | ||
|
||
return sys.resolvePath(resolvedFileName) | ||
} |
Oops, something went wrong.