Skip to content

Commit

Permalink
style: Use explicit imports for process.env and Buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli committed Dec 30, 2024
1 parent 2c55723 commit 4354c42
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 9 deletions.
24 changes: 18 additions & 6 deletions config/rollup.browser-config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,35 @@ import babel from '@rollup/plugin-babel'
import replace from '@rollup/plugin-replace'
import typescript from '@rollup/plugin-typescript'

/** @type {import('rollup').RollupOptions} */
export default {
input: {
index: 'src/index.ts',
util: 'src/util.ts'
},
output: { dir: 'browser/dist', format: 'esm', preserveModules: true },
plugins: [
replace({
preventAssignment: true,
'process.env.LOG_TOKENS': String(!!process.env.LOG_TOKENS),
'process.env.LOG_STREAM': String(!!process.env.LOG_STREAM)
}),
replace({ preventAssignment: true }),
babel({
babelHelpers: 'bundled',
presets: [['@babel/env', { modules: false }]]
}),
typescript({ declaration: false, outDir: 'browser/dist' })
typescript({ declaration: false, outDir: 'browser/dist' }),
{
resolveId: source =>
['node:buffer', 'node:process'].includes(source) ? source : null,
load(id) {
switch (id) {
case 'node:buffer':
return 'export const Buffer = null;'
case 'node:process':
return (
'export const emitWarning = null;' +
`export const env = { LOG_STREAM: ${!!process.env.LOG_STREAM}, LOG_TOKENS: ${!!process.env.LOG_TOKENS} };`
)
}
}
}
],
treeshake: { moduleSideEffects: false, propertyReadSideEffects: false }
}
1 change: 1 addition & 0 deletions config/rollup.node-config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export default [
esModule: false,
preserveModules: true
},
external: ['node:buffer', 'node:process'],
plugins: [
typescript({
transformers: { afterDeclarations: [fixDeclarationImportPaths] }
Expand Down
5 changes: 3 additions & 2 deletions src/log.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { emitWarning } from 'node:process'

export type LogLevelId = 'silent' | 'error' | 'warn' | 'debug'

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

export function warn(logLevel: LogLevelId, warning: string | Error) {
if (logLevel === 'debug' || logLevel === 'warn') {
if (typeof process !== 'undefined' && process.emitWarning)
process.emitWarning(warning)
if (typeof emitWarning === 'function') emitWarning(warning)
else console.warn(warning)
}
}
3 changes: 2 additions & 1 deletion src/parse/parser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { env } from 'node:process'
import type {
SourceToken,
Token,
Expand Down Expand Up @@ -187,7 +188,7 @@ export class Parser {
*/
*next(source: string) {
this.source = source
if (process.env.LOG_TOKENS) console.log('|', prettyToken(source))
if (env.LOG_TOKENS) console.log('|', prettyToken(source))

if (this.atScalar) {
this.atScalar = false
Expand Down
1 change: 1 addition & 0 deletions src/schema/yaml-1.1/binary.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Buffer } from 'node:buffer'
import { Scalar } from '../../nodes/Scalar.ts'
import { stringifyString } from '../../stringify/stringifyString.ts'
import type { ScalarTag } from '../types.ts'
Expand Down

0 comments on commit 4354c42

Please # to comment.