Skip to content

Commit

Permalink
fix: add node: prefix to Node.js imports (fix #113) (#115)
Browse files Browse the repository at this point in the history
Co-authored-by: Sergey Sova <[email protected]>
  • Loading branch information
brillout and sergeysova authored Jul 2, 2024
1 parent 66dc10b commit f9a3015
Show file tree
Hide file tree
Showing 25 changed files with 97 additions and 88 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci/getTestJobs.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { cmd, isObject } from './utils.mjs'
import assert from 'assert'
import path from 'path'
import { createRequire } from 'module'
import assert from 'node:assert'
import path from 'node:path'
import { createRequire } from 'node:module'
const require = createRequire(import.meta.url)

const args = process.argv
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci/utils.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export { cmd }
export { isObject }

import { execSync } from 'child_process'
import { execSync } from 'node:child_process'

/** @type { (command: string, options?: { cwd?: string }) => string } */
function cmd(command, options = {}) {
Expand Down
11 changes: 10 additions & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,19 @@
"semicolons": "asNeeded",
"lineWidth": 120,
"quoteStyle": "single",
"trailingComma": "all"
"trailingCommas": "all"
}
},
"linter": {
"enabled": true,
"rules": {
"recommended": false,
"style": {
"useNodejsImportProtocol": "error"
}
}
},
"organizeImports": {
"enabled": false
},
"vcs": {
Expand Down
6 changes: 3 additions & 3 deletions examples/babel/server.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import express from 'express'
import { telefunc, config } from 'telefunc'
import { createRequire } from 'module'
import { createRequire } from 'node:module'

startServer()

Expand Down Expand Up @@ -39,8 +39,8 @@ function start(app) {

// https://stackoverflow.com/questions/46745014/alternative-for-dirname-in-node-js-when-using-es6-modules
async function getRoot() {
const { dirname } = await import('path')
const { fileURLToPath } = await import('url')
const { dirname } = await import('node:path')
const { fileURLToPath } = await import('node:url')
const __dirname = dirname(fileURLToPath(import.meta.url))
const root = __dirname
return root
Expand Down
2 changes: 1 addition & 1 deletion examples/cloudflare-workers/.testRun.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { page, test, expect, run, skip, fetchHtml, isCI, getServerUrl, isWindows, autoRetry } from '@brillout/test-e2e'
import assert from 'assert'
import assert from 'node:assert'

export { testRun }

Expand Down
2 changes: 1 addition & 1 deletion examples/next/pages/api/_telefunc.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getUser } from '../../auth/getUser'
import { telefunc, config } from 'telefunc'
import type { NextApiRequest, NextApiResponse } from 'next'
import assert from 'assert'
import assert from 'node:assert'

config.telefuncUrl = '/api/_telefunc'

Expand Down
2 changes: 1 addition & 1 deletion examples/react-native/server.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import express from 'express'
import cors from 'cors'
import { telefunc, config } from 'telefunc'
import { createRequire } from 'module'
import { createRequire } from 'node:module'

startServer()

Expand Down
6 changes: 3 additions & 3 deletions examples/svelte-kit/src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script>
import Counter from './Counter.svelte';
/** @type {import('./$types').PageData} */
export let data;
import Counter from './Counter.svelte'
/** @type {import('./$types').PageData} */
export let data
</script>

<h1>Welcome to SvelteKit</h1>
Expand Down
4 changes: 2 additions & 2 deletions examples/svelte-kit/src/routes/Counter.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import { onCounterIncrement } from './Counter.telefunc';
export let count: number;
import { onCounterIncrement } from './Counter.telefunc'
export let count: number
</script>

<div class="counter">
Expand Down
4 changes: 2 additions & 2 deletions examples/vite/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ function start(app) {

// https://stackoverflow.com/questions/46745014/alternative-for-dirname-in-node-js-when-using-es6-modules
async function getRoot() {
const { dirname } = await import('path')
const { fileURLToPath } = await import('url')
const { dirname } = await import('node:path')
const { fileURLToPath } = await import('node:url')
const __dirname = dirname(fileURLToPath(import.meta.url))
const root = __dirname
return root
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@
"========= Formatting": "",
"format": "pnpm run format:biome",
"format:prettier": "git ls-files | egrep '\\.(json|js|jsx|css|ts|tsx|vue|mjs|cjs)$' | grep --invert-match package.json | xargs pnpm exec prettier --write",
"format:biome": "biome format --write .",
"format:check": "biome format . || echo Fix formatting by running: $ pnpm -w run format",
"format:biome": "biome check --write --unsafe",
"format:check": "biome check || echo Fix formatting by running: $ pnpm -w run format",
"========= Reset": "",
"reset": "git clean -Xdf && pnpm install && pnpm run build",
"========= Only allow pnpm; forbid yarn & npm": "",
"preinstall": "npx only-allow pnpm"
},
"devDependencies": {
"@biomejs/biome": "^1.5.3",
"@biomejs/biome": "^1.8.3",
"@brillout/bump-dependencies": "^0.1.1",
"@brillout/dev-my-dep": "^0.0.10",
"@brillout/test-e2e": "^0.5.33",
Expand Down
98 changes: 49 additions & 49 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion telefunc/node/server/getContext/async.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export { getContext_async }
export { provideTelefuncContext_async }

import { AsyncLocalStorage } from 'async_hooks'
import { AsyncLocalStorage } from 'node:async_hooks'
import { assert, assertWarning, isObject, getGlobalObject, assertUsage } from '../../utils'
import { installAsyncMode } from '../getContext'
import type { Telefunc } from './TelefuncNamespace'
Expand Down
8 changes: 4 additions & 4 deletions telefunc/node/server/runTelefunc/assertNamingConvention.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ export { assertNamingConvention }

import { assert, assertWarning, isProduction, assertPosixPath } from '../../utils'
import type { Telefunction } from '../types'
import type * as fsType from 'fs'
import type * as pathType from 'path'
import type * as fsType from 'node:fs'
import type * as pathType from 'node:path'

function assertNamingConvention(
exportValue: unknown,
Expand Down Expand Up @@ -41,8 +41,8 @@ function assertCollocation(telefuncFilePath: string, appRootDir: string | null,
let path: typeof pathType
const req: NodeRequire = require
try {
fs = req('fs')
path = req('path')
fs = req('node:fs')
path = req('node:path')
} catch {
return
}
Expand Down
4 changes: 2 additions & 2 deletions telefunc/node/server/runTelefunc/getEtag.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export { getEtag }

import { assert } from '../../utils'
import type { createHash as createHashType } from 'crypto'
import type { createHash as createHashType } from 'node:crypto'

type CreateHash = typeof createHashType

Expand All @@ -11,7 +11,7 @@ async function getEtag(runContext: { disableEtag: boolean; httpResponseBody: str
}
let createHash: CreateHash
try {
createHash = (await import('crypto')).createHash
createHash = (await import('node:crypto')).createHash
} catch (err) {
/*
assertWarning(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export { loadTelefuncFilesFromConfig }

import { assert, assertPosixPath, assertUsage, isTelefuncFilePath } from '../../utils'
import { posix } from 'path'
import { posix } from 'node:path'
import type { TelefuncFiles } from '../types'
import { import_ } from '@brillout/import'
import pc from '@brillout/picocolors'
Expand Down
2 changes: 1 addition & 1 deletion telefunc/node/server/serverConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export { configUser as config }
export { getServerConfig }
export type { ConfigUser }

import { isAbsolute } from 'path'
import { isAbsolute } from 'node:path'
import { assertUsage, hasProp, toPosixPath, isTelefuncFilePath } from '../utils'

/** Telefunc Server Configuration */
Expand Down
4 changes: 2 additions & 2 deletions telefunc/node/server/shield/codegen/generateShield.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import {
unique,
assertPosixPath,
} from '../../../utils'
import fs from 'fs'
import path from 'path'
import fs from 'node:fs'
import path from 'node:path'
import pc from 'picocolors'

type GeneratedShield = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export { transformTelefuncFileClientSideSync }

import { posix } from 'path'
import { posix } from 'node:path'
import { assert, assertPosixPath, assertUsage, getTelefunctionKey } from '../utils'

function transformTelefuncFileClientSideSync(
Expand Down
2 changes: 1 addition & 1 deletion telefunc/node/vite/importGlob/toggle.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export { importGlobOff }
export { importGlobOn }

import { writeFileSync } from 'fs'
import { writeFileSync } from 'node:fs'
import { scriptFileExtensions } from '../utils'
const dir = __dirname + (() => '')() // trick to avoid `@vercel/ncc` to glob import
const telefuncFilesGlobPath = `${dir}/telefuncFilesGlob.js`
Expand Down
Loading

0 comments on commit f9a3015

Please sign in to comment.