Skip to content

Commit 4354c42

Browse files
committed
style: Use explicit imports for process.env and Buffer
1 parent 2c55723 commit 4354c42

5 files changed

Lines changed: 25 additions & 9 deletions

File tree

config/rollup.browser-config.mjs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,35 @@ import babel from '@rollup/plugin-babel'
22
import replace from '@rollup/plugin-replace'
33
import typescript from '@rollup/plugin-typescript'
44

5+
/** @type {import('rollup').RollupOptions} */
56
export default {
67
input: {
78
index: 'src/index.ts',
89
util: 'src/util.ts'
910
},
1011
output: { dir: 'browser/dist', format: 'esm', preserveModules: true },
1112
plugins: [
12-
replace({
13-
preventAssignment: true,
14-
'process.env.LOG_TOKENS': String(!!process.env.LOG_TOKENS),
15-
'process.env.LOG_STREAM': String(!!process.env.LOG_STREAM)
16-
}),
13+
replace({ preventAssignment: true }),
1714
babel({
1815
babelHelpers: 'bundled',
1916
presets: [['@babel/env', { modules: false }]]
2017
}),
21-
typescript({ declaration: false, outDir: 'browser/dist' })
18+
typescript({ declaration: false, outDir: 'browser/dist' }),
19+
{
20+
resolveId: source =>
21+
['node:buffer', 'node:process'].includes(source) ? source : null,
22+
load(id) {
23+
switch (id) {
24+
case 'node:buffer':
25+
return 'export const Buffer = null;'
26+
case 'node:process':
27+
return (
28+
'export const emitWarning = null;' +
29+
`export const env = { LOG_STREAM: ${!!process.env.LOG_STREAM}, LOG_TOKENS: ${!!process.env.LOG_TOKENS} };`
30+
)
31+
}
32+
}
33+
}
2234
],
2335
treeshake: { moduleSideEffects: false, propertyReadSideEffects: false }
2436
}

config/rollup.node-config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export default [
6262
esModule: false,
6363
preserveModules: true
6464
},
65+
external: ['node:buffer', 'node:process'],
6566
plugins: [
6667
typescript({
6768
transformers: { afterDeclarations: [fixDeclarationImportPaths] }

src/log.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { emitWarning } from 'node:process'
2+
13
export type LogLevelId = 'silent' | 'error' | 'warn' | 'debug'
24

35
export function debug(logLevel: LogLevelId, ...messages: any[]) {
@@ -6,8 +8,7 @@ export function debug(logLevel: LogLevelId, ...messages: any[]) {
68

79
export function warn(logLevel: LogLevelId, warning: string | Error) {
810
if (logLevel === 'debug' || logLevel === 'warn') {
9-
if (typeof process !== 'undefined' && process.emitWarning)
10-
process.emitWarning(warning)
11+
if (typeof emitWarning === 'function') emitWarning(warning)
1112
else console.warn(warning)
1213
}
1314
}

src/parse/parser.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { env } from 'node:process'
12
import type {
23
SourceToken,
34
Token,
@@ -187,7 +188,7 @@ export class Parser {
187188
*/
188189
*next(source: string) {
189190
this.source = source
190-
if (process.env.LOG_TOKENS) console.log('|', prettyToken(source))
191+
if (env.LOG_TOKENS) console.log('|', prettyToken(source))
191192

192193
if (this.atScalar) {
193194
this.atScalar = false

src/schema/yaml-1.1/binary.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Buffer } from 'node:buffer'
12
import { Scalar } from '../../nodes/Scalar.ts'
23
import { stringifyString } from '../../stringify/stringifyString.ts'
34
import type { ScalarTag } from '../types.ts'

0 commit comments

Comments
 (0)