8000 refactor: re-organize utils · unjs/unplugin@378aac1 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 378aac1

Browse files
committed
refactor: re-organize utils
1 parent c980632 commit 378aac1

File tree

18 files changed

+72
-89
lines changed

18 files changed

+72
-89
lines changed

src/esbuild/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import type {
1010
} from '../types'
1111
import fs from 'fs'
1212
import path from 'path'
13+
import { toArray } from '../utils/general'
1314
import {
1415
combineSourcemaps,
1516
createBuildContext,
1617
createPluginContext,
1718
guessLoader,
1819
processCodeWithSourceMap,
19-
toArray,
2020
unwrapLoader,
2121
} from './utils'
2222

src/esbuild/utils.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ import { Buffer } from 'buffer'
77
import fs from 'fs'
88
import path from 'path'
99
import remapping from '@ampproject/remapping'
10-
import { Parser } from 'acorn'
11-
12< 10000 code class="diff-text syntax-highlighted-line deletion">-
export * from '../utils'
10+
import { parse } from '../utils/context'
1311

1412
const ExtToLoader: Record<string, Loader> = {
1513
'.js': 'js',
@@ -116,14 +114,7 @@ export function createBuildContext(build: EsbuildPluginBuild): UnpluginBuildCont
116114
const watchFiles: string[] = []
117115
const { initialOptions } = build
118116
return {
119-
parse(code: string, opts: any = {}) {
120-
return Parser.parse(code, {
121-
sourceType: 'module',
122-
ecmaVersion: 'latest',
123-
locations: true,
124-
...opts,
125-
})
126-
},
117+
parse,
127118
addWatchFile() {
128119
throw new Error('unplugin/esbuild: addWatchFile outside supported hooks (resolveId, load, transform)')
129120
},

src/farm/context.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,14 @@ import type { CompilationContext } from '@farmfe/core'
22
import type { UnpluginBuildContext, UnpluginContext } from '../types'
33
import { Buffer } from 'buffer'
44
import { extname } from 'path'
5-
import { Parser } from 'acorn'
5+
import { parse } from '../utils/context'
66

77
export function createFarmContext(
88
context: CompilationContext,
99
currentResolveId?: string,
1010
): UnpluginBuildContext {
1111
return {
12-
parse(code: string, opts: any = {}) {
13-
return Parser.parse(code, {
14-
sourceType: 'module',
15-
ecmaVersion: 'latest',
16-
locations: true,
17-
...opts,
18-
})
19-
},
12+
parse,
2013

2114
addWatchFile(id: string) {
2215
context.addWatchFile(currentResolveId || id, id)

src/farm/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import type {
1616
} from '../types'
1717
import type { JsPluginExtended, WatchChangeEvents } from './utils'
1818
import path from 'path'
19-
import { toArray } from '../utils'
19+
import { toArray } from '../utils/general'
2020
import { createFarmContext, unpluginContext } from './context'
2121

2222
import {

src/farm/utils.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import type { TransformResult } from '../types'
33
import path from 'path'
44
import * as querystring from 'querystring'
55

6-
export * from '../utils'
7-
86
export type WatchChangeEvents = 'create' | 'update' | 'delete'
97

108
const ExtToLoader: Record<string, string> = {

src/rolldown/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { RolldownPlugin, UnpluginContextMeta, UnpluginFactory, UnpluginInstance } from '../types'
22
import { toRollupPlugin } from '../rollup'
3-
import { toArray } from '../utils'
3+
import { toArray } from '../utils/general'
44

55
export function getRolldownPlugin<UserOptions = Record<string, never>, Nested extends boolean = boolean>(
66
factory: UnpluginFactory<UserOptions, Nested>,

src/rollup/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { RollupPlugin, UnpluginContextMeta, UnpluginFactory, UnpluginInstance, UnpluginOptions } from '../types'
2-
import { toArray } from '../utils'
2+
import { toArray } from '../utils/general'
33

44
export function getRollupPlugin<UserOptions = Record<string, never>, Nested extends boolean = boolean>(
55
factory: UnpluginFactory<UserOptions, Nested>,

src/rspack/context.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Compilation, Compiler, LoaderContext } from '@rspack/core'
22
import type { UnpluginBuildContext, UnpluginContext, UnpluginMessage } from '../types'
33
import { Buffer } from 'buffer'
44
import { resolve } from 'path'
5-
import { Parser } from 'acorn'
5+
import { parse } from '../utils/context'
66

77
export function createBuildContext(compiler: Compiler, compilation: Compilation, loaderContext?: LoaderContext): UnpluginBuildContext {
88
return {
@@ -20,14 +20,7 @@ export function createBuildContext(compiler: Compiler, compilation: Compilation,
2020
getWatchFiles() {
2121
return Array.from(compilation.fileDependencies)
2222
},
23-
parse(code: string, opts: any = {}) {
24-
return Parser.parse(code, {
25-
sourceType: 'module',
26-
ecmaVersion: 'latest',
27-
locations: true,
28-
...opts,
29-
})
30-
},
23+
parse,
3124
emitFile(emittedFile) {
3225
const outFileName = emittedFile.fileName || emittedFile.name
3326
if (emittedFile.source && outFileName) {

src/rspack/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import type {
88
} from '../types'
99
import fs from 'fs'
1010
import { resolve } from 'path'
11-
import { normalizeAbsolutePath, toArray, transformUse } from '../utils'
11+
import { toArray } from '../utils/general'
12+
import { normalizeAbsolutePath, transformUse } from '../utils/webpack-like'
1213
import { createBuildContext, normalizeMessage } from './context'
1314
import { decodeVirtualModuleId, encodeVirtualModuleId, FakeVirtualModulesPlugin, isVirtualModuleId } from './utils'
1415

src/rspack/loaders/load.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { LoaderContext } from '@rspack/core'
22
import type { ResolvedUnpluginOptions } from '../../types'
3-
import { normalizeAbsolutePath } from '../../utils'
3+
import { normalizeAbsolutePath } from '../../utils/webpack-like'
44
import { createBuildContext, createContext } from '../context'
55
import { decodeVirtualModuleId, isVirtualModuleId } from '../utils'
66

0 commit comments

Comments
 (0)
0