Skip to content

Commit

Permalink
Merge pull request #503 from xmtp/rygine/wasm-update
Browse files Browse the repository at this point in the history
Refactor WASM integration for web
  • Loading branch information
rygine authored Dec 15, 2023
2 parents 72ff335 + a18f1bc commit f0558f7
Show file tree
Hide file tree
Showing 13 changed files with 455 additions and 268 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
},
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
Expand Down
58 changes: 58 additions & 0 deletions build/esbuild-plugin-resolve-extensions/index.ts
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
})
},
}
}
48 changes: 48 additions & 0 deletions build/esbuild-plugin-resolve-extensions/utils.ts
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)
}
Loading

0 comments on commit f0558f7

Please sign in to comment.