Skip to content

Commit 11a0b8b

Browse files
committed
fix(types): prefer webpack augmentation
Fixes #116
1 parent 660029b commit 11a0b8b

File tree

7 files changed

+31
-31
lines changed

7 files changed

+31
-31
lines changed

src/loaders/dev-tools.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { readFileSync } from 'fs'
2-
import type webpack from 'webpack'
32
import { resolve } from 'pathe'
3+
import type { Compiler, loader } from 'webpack'
44
import { isDev, isWebCompilerTarget } from '../core/utils'
55
import { DEVTOOLS_POST_PATH } from '../core/constants'
6-
import type { Compiler } from '../types'
76

87
const DEVTOOLS_CLIENT_PATH = resolve(__dirname, '../runtime/client.cjs')
98

@@ -21,7 +20,7 @@ style.innerHTML = ${JSON.stringify(comment + css)}
2120
document.head.prepend(style)
2221
`
2322
}
24-
async function devtoolsLoader(this: webpack.loader.LoaderContext, source: string): Promise<void> {
23+
async function devtoolsLoader(this: loader.LoaderContext, source: string): Promise<void> {
2524
const callback = this.async()!
2625

2726
if (!this._compiler) {
@@ -31,13 +30,13 @@ async function devtoolsLoader(this: webpack.loader.LoaderContext, source: string
3130

3231
this.cacheable(true)
3332

34-
const { port, host } = await (this._compiler as Compiler).$windi.server.ensureStart()
33+
const { port, host } = await this._compiler.$windi.server.ensureStart()
3534

3635
if (isWebCompilerTarget(this._compiler.options.target) && isDev()) {
3736
const clientContent = readFileSync(DEVTOOLS_CLIENT_PATH, 'utf-8')
3837
.replace('__POST_PATH__', `http://${host}:${port}${DEVTOOLS_POST_PATH}`)
3938

40-
const mockClasses = getMockClassesInjector(this._compiler as Compiler)
39+
const mockClasses = getMockClassesInjector(this._compiler)
4140

4241
callback(null, `${clientContent}\n${mockClasses}`)
4342
}

src/loaders/virtual-module.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import fs, { readFileSync } from 'fs'
2-
import type webpack from 'webpack'
2+
import type { loader } from 'webpack'
33
import { defaultConfigureFiles } from '@windicss/plugin-utils'
44
import type { LayerName } from '@windicss/plugin-utils'
5-
import type { Compiler } from '../types'
65
import { MODULE_ID_VIRTUAL_TEST } from '../core/constants'
76
import debug from '../core/debug'
87
import { def } from '../core/utils'
98

109
async function VirtualModule(
11-
this: webpack.loader.LoaderContext,
10+
this: loader.LoaderContext,
1211
source: string,
1312
): Promise<void> {
1413
const callback = this.async()!
@@ -17,7 +16,7 @@ async function VirtualModule(
1716
return
1817
}
1918
this.cacheable(false)
20-
const service = (this._compiler as Compiler).$windi
19+
const service = this._compiler.$windi
2120
const match = this.resource.match(MODULE_ID_VIRTUAL_TEST)
2221
if (!service || !match) {
2322
const error = new Error(`Failed to match the resource "${this.resource}" to a WindiCSS virtual module.`)

src/loaders/windicss-css.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
import type webpack from 'webpack'
2-
import type { Compiler } from '../types'
1+
import type { loader } from 'webpack'
32
import { transformCSS } from '../core/utils'
43

54
function WindicssCss(
6-
this: webpack.loader.LoaderContext,
5+
this: loader.LoaderContext,
76
source: string,
87
): string {
98
if (!this._compiler)
109
return source
1110

1211
this.cacheable(true)
13-
const service = (this._compiler as Compiler).$windi
12+
const service = this._compiler.$windi
1413

1514
if (!service)
1615
return source

src/loaders/windicss-style-pitcher.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type webpack from 'webpack'
1+
import type { loader } from 'webpack'
22

33
type LoaderTest = (l: { path: string; ident?: string }) => boolean
44

@@ -12,7 +12,7 @@ const cssLoaderTest: LoaderTest = l => /(\/|\\|@)css-loader/.test(l.path)
1212
*
1313
* We move it just after the PostCSS loader
1414
*/
15-
export const pitch = function (this: webpack.loader.LoaderContext, remainingRequest: string) {
15+
export const pitch = function (this: loader.LoaderContext, remainingRequest: string) {
1616
const findLoaderIndex = (test: LoaderTest) => this.loaders.findIndex((loader) => {
1717
return test(loader) && !loader.normalExecuted
1818
})

src/loaders/windicss-template.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
import type webpack from 'webpack'
21
import compileTemplate from 'lodash/template'
32
import defaults from 'lodash/defaults'
43
import loaderUtils from 'loader-utils'
4+
import type { loader } from 'webpack'
55
import debug from '../core/debug'
6-
import type { Compiler } from '../types'
76
import { def, isJsx, transformCSS } from '../core/utils'
87

98
function WindicssTemplate(
10-
this: webpack.loader.LoaderContext,
9+
this: loader.LoaderContext,
1110
source: string,
1211
): string {
1312
if (!this._compiler)
1413
return source
1514

1615
this.cacheable(true)
17-
const service = (this._compiler as Compiler).$windi
16+
const service = this._compiler.$windi
1817

1918
if (!service)
2019
return source

src/plugin.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import type { LayerName } from '@windicss/plugin-utils'
22
import { createUtils } from '@windicss/plugin-utils'
33
import { join, resolve } from 'pathe'
44
import VirtualModulesPlugin from 'webpack-virtual-modules'
5-
import type { Compiler, WindiCSSWebpackPluginOptions } from './types'
5+
import type { Compiler } from 'webpack'
6+
import { Plugin } from 'webpack'
7+
import type { WindiCSSWebpackPluginOptions } from './types'
68
import { DEVTOOLS_MODULE_ID, DEVTOOLS_VIRTUAL_MODULE, DEVTOOLS_VIRTUAL_MODULE_ID, MODULE_ID, MODULE_ID_VIRTUAL_MODULES, MODULE_ID_VIRTUAL_TEST, NAME } from './core/constants'
79
import debug from './core/debug'
810
import { def } from './core/utils'
@@ -17,10 +19,11 @@ const transformTemplateLoader = resolve(loadersPath, 'windicss-template.cjs')
1719
const virtualModuleLoader = resolve(loadersPath, 'virtual-module.cjs')
1820
const devtoolsLoader = resolve(loadersPath, 'dev-tools.cjs')
1921

20-
class WindiCSSWebpackPlugin {
22+
class WindiCSSWebpackPlugin extends Plugin {
2123
options
2224

2325
constructor(options: Partial<WindiCSSWebpackPluginOptions> = {}) {
26+
super()
2427
// @todo validate options
2528
this.options = {
2629
// default options
@@ -32,7 +35,7 @@ class WindiCSSWebpackPlugin {
3235
} as WindiCSSWebpackPluginOptions
3336
}
3437

35-
apply(compiler: Compiler): void {
38+
apply(compiler: Compiler) {
3639
// resolve the root working directory
3740
let root = compiler.context
3841
if (this.options.root)

src/types.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type webpack from 'webpack'
21
import type { UserOptions, WindiPluginUtils } from '@windicss/plugin-utils'
32
import type Server from './core/server'
43

@@ -22,14 +21,16 @@ declare module 'windi-components.css' {}
2221
// @ts-expect-error virtual module
2322
declare module 'windi-utilities.css' {}
2423

25-
export type Compiler = webpack.Compiler & {
26-
$windi: WindiPluginUtils & {
27-
dirty: Set<string>
28-
root: string
29-
virtualModules: Map<string, string>
30-
initException?: Error
31-
invalidateCssModules: (resource: string, modules: string[]) => void
32-
server: Server
24+
declare module 'webpack' {
25+
interface Compiler {
26+
$windi: WindiPluginUtils & {
27+
dirty: Set<string>
28+
root: string
29+
virtualModules: Map<string, string>
30+
initException?: Error
31+
invalidateCssModules: (resource: string, modules: string[]) => void
32+
server: Server
33+
}
3334
}
3435
}
3536

0 commit comments

Comments
 (0)