Skip to content

Commit dfdd610

Browse files
authored
Merge pull request #48 from Coder-Spirit/fix-symbols-import-on-deno
fix: suport nominal-symbols in deno "pkg"
2 parents 1f69194 + dd0614c commit dfdd610

File tree

5 files changed

+30
-18
lines changed

5 files changed

+30
-18
lines changed

nominal/deno/internal/Symbols.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
export const __BaseType: unique symbol = Symbol('__BaseType')
2-
export const __Properties: unique symbol = Symbol('__Properties')
3-
export const __Brand: unique symbol = Symbol('__Brand')
4-
5-
export const __PropertyName: unique symbol = Symbol('__PropertyName')
6-
export const __PropertyValues: unique symbol = Symbol('__PropertyValues')
7-
8-
// Some Nominal tricks rely on the fact that this symbol and its type won't be
9-
// exported as public to the library's users.
10-
const __ImpossibleSymbol: unique symbol = Symbol('__Impossible')
11-
export type __Impossible = typeof __ImpossibleSymbol
1+
// We re-export, instead of directly importing from the package, for
2+
// compatibility reasons. Typescript and NodeJS are still in very
3+
// early stages when it comes to ES modules support.
4+
export {
5+
__BaseType,
6+
__Properties,
7+
__Brand,
8+
__PropertyName,
9+
__PropertyValues,
10+
type __Impossible
11+
} from 'https://deno.land/x/nominal_symbols@1.0.2/nominal-symbols/deno/index.ts'

nominal/scripts/adaptDeno.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,9 @@ run()
1313

1414
async function run(): Promise<void> {
1515
const esmDir = join(__dirname, '..', 'deno')
16-
await processDirectory(esmDir, '.ts')
16+
await processDirectory(
17+
esmDir,
18+
'.ts',
19+
[['@coderspirit/nominal-symbols', 'https://deno.land/x/nominal_symbols@1.0.2/nominal-symbols/deno/index.ts']]
20+
)
1721
}

nominal/scripts/build_deno.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@
33
mkdir -p ./deno \
44
&& cp -r ./src/* ./deno/ \
55
&& rm -rf ./deno/__tests__ \
6-
&& rm ./deno/internal/Symbols.ts \
7-
&& mv ./deno/internal/Symbols.ts.esm ./deno/internal/Symbols.ts \
6+
&& rm ./deno/internal/Symbols.ts.esm \
87
&& ts-node ./scripts/adaptDeno.ts

nominal/scripts/lib/processFiles.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { join } from 'path'
55
export async function processDirectory(
66
directoryPath: string,
77
ext: string,
8+
exactReplacements: ([string, string])[] = []
89
): Promise<void> {
910
const fileNames = await readdir(directoryPath)
1011

@@ -13,16 +14,17 @@ export async function processDirectory(
1314
const fileStats = await lstat(filePath)
1415

1516
if (fileStats.isDirectory()) {
16-
await processDirectory(filePath, ext)
17+
await processDirectory(filePath, ext, exactReplacements)
1718
} else {
18-
await processFile(filePath, ext)
19+
await processFile(filePath, ext, exactReplacements)
1920
}
2021
}
2122
}
2223

2324
export async function processFile(
2425
filePath: string,
2526
ext: string,
27+
exactReplacements: ([string, string])[] = []
2628
): Promise<void> {
2729
const thePattern = /^(.*}\s+from\s+'\.[A-Za-z0-9_./]+)';?\n?$/s
2830
const hasExtension = /\.(js|ts|d\.ts)$/s
@@ -42,7 +44,14 @@ export async function processFile(
4244
transformedLines.push(`${theMatch[1] as string}${ext}';`)
4345
transformed = true
4446
} else {
45-
transformedLines.push(line)
47+
let replaced = line
48+
for (const [pattern, replacement] of exactReplacements) {
49+
if (replaced.includes(pattern)) {
50+
replaced = replaced.replace(pattern, replacement)
51+
transformed = true
52+
}
53+
}
54+
transformedLines.push(replaced)
4655
}
4756
}
4857

nominal/src/internal/Symbols.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// We re-export, instead of directly importing from the package, for
2-
// compatibility with ES modules & Deno. Typescript and NodeJS are still in very
2+
// compatibility reasons. Typescript and NodeJS are still in very
33
// early stages when it comes to ES modules support.
44
export {
55
__BaseType,

0 commit comments

Comments
 (0)