Skip to content

Commit

Permalink
refactor: enable isolatedDeclarations
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Dec 3, 2024
1 parent a2505d9 commit 58cb8aa
Show file tree
Hide file tree
Showing 13 changed files with 34 additions and 33 deletions.
14 changes: 7 additions & 7 deletions src/define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,43 +41,43 @@ export function createUnplugin<UserOptions, Nested extends boolean = boolean>(

export function createEsbuildPlugin<UserOptions, Nested extends boolean = boolean>(
factory: UnpluginFactory<UserOptions, Nested>,
) {
): UnpluginInstance<UserOptions>['esbuild'] {
return getEsbuildPlugin(factory)
}

export function createRollupPlugin<UserOptions, Nested extends boolean = boolean>(
factory: UnpluginFactory<UserOptions, Nested>,
) {
): UnpluginInstance<UserOptions>['rollup'] {
return getRollupPlugin(factory)
}

export function createVitePlugin<UserOptions, Nested extends boolean = boolean>(
factory: UnpluginFactory<UserOptions, Nested>,
) {
): UnpluginInstance<UserOptions>['vite'] {
return getVitePlugin(factory)
}

/** @experimental do not use it in production */
export function createRolldownPlugin<UserOptions, Nested extends boolean = boolean>(
factory: UnpluginFactory<UserOptions, Nested>,
) {
): UnpluginInstance<UserOptions>['rolldown'] {
return getRolldownPlugin(factory)
}

export function createWebpackPlugin<UserOptions, Nested extends boolean = boolean>(
factory: UnpluginFactory<UserOptions, Nested>,
) {
): UnpluginInstance<UserOptions>['webpack'] {
return getWebpackPlugin(factory)
}

export function createRspackPlugin<UserOptions, Nested extends boolean = boolean>(
factory: UnpluginFactory<UserOptions, Nested>,
) {
): UnpluginInstance<UserOptions>['rspack'] {
return getRspackPlugin(factory)
}

export function createFarmPlugin<UserOptions, Nested extends boolean = boolean>(
factory: UnpluginFactory<UserOptions, Nested>,
) {
): UnpluginInstance<UserOptions>['farm'] {
return getFarmPlugin(factory)
}
8 changes: 6 additions & 2 deletions src/esbuild/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,11 @@ export function createBuildContext(build: EsbuildPluginBuild): UnpluginBuildCont
}
}

export function createPluginContext(context: UnpluginBuildContext) {
export function createPluginContext(context: UnpluginBuildContext): {
errors: PartialMessage[]
warnings: PartialMessage[]
mixedContext: UnpluginContext & UnpluginBuildContext
} {
const errors: PartialMessage[] = []
const warnings: PartialMessage[] = []
const pluginContext: UnpluginContext = {
Expand Down Expand Up @@ -183,7 +187,7 @@ function normalizeMessage(message: string | UnpluginMessage): Message {
}
}

export function processCodeWithSourceMap(map: SourceMap | null | undefined, code: string) {
export function processCodeWithSourceMap(map: SourceMap | null | undefined, code: string): string {
if (map) {
if (!map.sourcesContent || map.sourcesContent.length === 0)
map.sourcesContent = [code]
Expand Down
5 changes: 0 additions & 5 deletions src/farm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ export function getFarmPlugin<
}) as UnpluginInstance<UserOptions>['farm']
}

export const resolveIdFilters = {
sources: ['!node_modules'],
importers: ['!node_modules'],
}

export function toFarmPlugin(plugin: UnpluginOptions, options?: Record<string, any> | undefined): JsPlugin {
const farmPlugin: JsPlugin = {
name: plugin.name,
Expand Down
10 changes: 5 additions & 5 deletions src/farm/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function guessIdLoader(id: string): string {
return ExtToLoader[path.extname(id).toLowerCase()] || 'js'
}

export function transformQuery(context: any) {
export function transformQuery(context: any): void {
const queryParamsObject: Record<string, string | boolean> = {}
context.query.forEach(([param, value]: string[]) => {
queryParamsObject[param] = value
Expand All @@ -35,7 +35,7 @@ export function transformQuery(context: any) {
context.resolvedPath = `${context.resolvedPath}?${transformQuery}`
}

export function convertEnforceToPriority(value: 'pre' | 'post' | undefined) {
export function convertEnforceToPriority(value: 'pre' | 'post' | undefined): number {
const defaultPriority = 100
const enforceToPriority = {
pre: 101,
Expand Down Expand Up @@ -71,17 +71,17 @@ export function isObject(variable: unknown): variable is object {
return typeof variable === 'object' && variable !== null
}

export function customParseQueryString(url: string | null) {
export function customParseQueryString(url: string | null): [string, string][] {
if (!url)
return []

const queryString = url.split('?')[1]

const parsedParams = querystring.parse(queryString)
const paramsArray = []
const paramsArray: [string, string][] = []

for (const key in parsedParams)
paramsArray.push([key, parsedParams[key]])
paramsArray.push([key, parsedParams[key] as string])

return paramsArray
}
Expand Down
2 changes: 1 addition & 1 deletion src/rspack/loaders/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { normalizeAbsolutePath } from '../../utils/webpack-like'
import { createBuildContext, createContext } from '../context'
import { decodeVirtualModuleId, isVirtualModuleId } from '../utils'

export default async function load(this: LoaderContext, source: string, map: any) {
export default async function load(this: LoaderContext, source: string, map: any): Promise<void> {
const callback = this.async()
const { plugin } = this.query as { plugin: ResolvedUnpluginOptions }

Expand Down
2 changes: 1 addition & 1 deletion src/rspack/loaders/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default async function transform(
this: LoaderContext,
source: string,
map: any,
) {
): Promise<void> {
const callback = this.async()
const { plugin } = this.query as { plugin: ResolvedUnpluginOptions }
if (!plugin?.transform)
Expand Down
4 changes: 2 additions & 2 deletions src/rspack/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class FakeVirtualModulesPlugin {
name = 'FakeVirtualModulesPlugin'
constructor(private plugin: ResolvedUnpluginOptions) {}

apply(compiler: Compiler) {
apply(compiler: Compiler): void {
const dir = this.plugin.__virtualModulePrefix
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true })
Expand All @@ -29,7 +29,7 @@ export class FakeVirtualModulesPlugin {
})
}

async writeModule(file: string) {
async writeModule(file: string): Promise<string> {
const path = encodeVirtualModuleId(file, this.plugin)
await fs.promises.writeFile(path, '')
return path
Expand Down
4 changes: 2 additions & 2 deletions src/utils/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Parser } from 'acorn'
import { Parser, type Program } from 'acorn'

export function parse(code: string, opts: any = {}) {
export function parse(code: string, opts: any = {}): Program {
return Parser.parse(code, {
sourceType: 'module',
ecmaVersion: 'latest',
Expand Down
5 changes: 3 additions & 2 deletions src/utils/webpack-like.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { RuleSetUseItem } from '@rspack/core'
import type { ResolvedUnpluginOptions } from '../types'
import { isAbsolute, normalize } from 'path'

export function transformUse(
data: { resource?: string, resourceQuery?: string },
plugin: ResolvedUnpluginOptions,
transformLoader: string,
) {
): RuleSetUseItem[] {
if (data.resource == null)
return []

Expand All @@ -32,7 +33,7 @@ export function transformUse(
* @param path - Path to normalize.
* @returns a new normalized path.
*/
export function normalizeAbsolutePath(path: string) {
export function normalizeAbsolutePath(path: string): string {
if (isAbsolute(path))
return normalize(path)
else
Expand Down
2 changes: 1 addition & 1 deletion src/webpack/loaders/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { ResolvedUnpluginOptions } from '../../types'
import { normalizeAbsolutePath } from '../../utils/webpack-like'
import { createBuildContext, createContext } from '../context'

export default async function load(this: LoaderContext<any>, source: string, map: any) {
export default async function load(this: LoaderContext<any>, source: string, map: any): Promise<void> {
const callback = this.async()
const { plugin } = this.query as { plugin: ResolvedUnpluginOptions }
let id = this.resource
Expand Down
2 changes: 1 addition & 1 deletion src/webpack/loaders/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { LoaderContext } from 'webpack'
import type { ResolvedUnpluginOptions } from '../../types'
import { createBuildContext, createContext } from '../context'

export default async function transform(this: LoaderContext<any>, source: string, map: any) {
export default async function transform(this: LoaderContext<any>, source: string, map: any): Promise<void> {
const callback = this.async()

const { plugin } = this.query as { plugin: ResolvedUnpluginOptions }
Expand Down
8 changes: 4 additions & 4 deletions test/unit-tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import * as webpack from 'webpack'

export * from '../../src/utils/general'

export const viteBuild = vite.build
export const rollupBuild = rollup.rollup
export const esbuildBuild = esbuild.build
export const viteBuild: typeof vite.build = vite.build
export const rollupBuild: typeof rollup.rollup = rollup.rollup
export const esbuildBuild: typeof esbuild.build = esbuild.build
export const webpackBuild: typeof webpack.webpack = webpack.webpack || (webpack as any).default || webpack
export const rspackBuild: typeof rspack.rspack = rspack.rspack

export const webpackVersion = ((webpack as any).default || webpack).version
export const webpackVersion: string = ((webpack as any).default || webpack).version

export const build: {
webpack: typeof webpack.webpack
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"declaration": true,
"noEmit": true,
"esModuleInterop": true,
"isolatedDeclarations": true,
"isolatedModules": true,
"verbatimModuleSyntax": true,
"skipLibCheck": true
Expand Down

0 comments on commit 58cb8aa

Please sign in to comment.