Skip to content

Commit 0d2aa6e

Browse files
committed
chore: changing syntax for node 12.x support
1 parent 02d22df commit 0d2aa6e

File tree

5 files changed

+31
-13
lines changed

5 files changed

+31
-13
lines changed

src/core/utils.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,24 @@ export const transformCSS = (service: WindiPluginUtils, source: string, resource
2222
try {
2323
output = service.transformCSS(source, resource, { globaliseKeyframes: true })
2424
if (!output || output.length <= 0) {
25-
console.warn(`[WindiCSS] Failed to transform CSS for resource: ${resource}.`)
25+
console.warn(`[WindiCSS] Invalid response from windi core transforming resource: ${resource}.`)
2626
return source
2727
}
2828
debug.loader('Transformed CSS', resource)
2929
}
3030
catch (e) {
31-
console.warn(`[WindiCSS] Failed to transform CSS for resource: ${resource}.`)
31+
console.warn(`[WindiCSS] Exception when transforming CSS for resource: ${resource}.`, e)
3232
return source
3333
}
3434
return output
3535
}
36+
37+
/**
38+
* Default function. Take the value or the default
39+
*/
40+
export const def = (val: any, def: any) => {
41+
if (val)
42+
return val
43+
44+
return def
45+
}

src/interfaces.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type webpack from 'webpack'
22
import type { WindiPluginUtils, UserOptions } from '@windicss/plugin-utils'
33

44
export type Compiler = webpack.Compiler & {
5-
$windyCSSService?: WindiPluginUtils & {
5+
$windyCSSService: WindiPluginUtils & {
66
dirty: Set<string>
77
root: string
88
virtualModules: Map<string, string>

src/loaders/transform-template.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import defaults from 'lodash/defaults'
44
import loaderUtils from 'loader-utils'
55
import debug from '../core/debug'
66
import type { Compiler } from '../interfaces'
7-
import { isJsx, transformCSS } from '../core/utils'
7+
import { def, isJsx, transformCSS } from '../core/utils'
88

99
function TransformTemplate(
1010
this: webpack.loader.LoaderContext,
@@ -75,7 +75,7 @@ function TransformTemplate(
7575
}
7676
})
7777
}
78-
return transformedCSS ?? match
78+
return def(transformedCSS, match)
7979
}
8080
const transformedCSS = transformCSS(service, css, this.resource)
8181
return `<style${meta}>\n${transformedCSS}\n</style>`

src/loaders/virtual-module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { LayerName } from '@windicss/plugin-utils'
55
import type { Compiler } from '../interfaces'
66
import { MODULE_ID_VIRTUAL_TEST } from '../core/constants'
77
import debug from '../core/debug'
8+
import { def } from '../core/utils'
89

910
async function VirtualModule(
1011
this: webpack.loader.LoaderContext,
@@ -37,7 +38,7 @@ async function VirtualModule(
3738
service.options.enableScan = false
3839

3940
const css = (await service.generateCSS(layer)).replace('(boot)', '')
40-
service.virtualModules.set(layer ?? 'all', css)
41+
service.virtualModules.set(def(layer, 'all'), css)
4142
callback(null, css)
4243
}
4344
catch (e: any) {

src/plugin.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import VirtualModulesPlugin from 'webpack-virtual-modules'
55
import type { Compiler, WindiCSSWebpackPluginOptions } from './interfaces'
66
import { MODULE_ID, MODULE_ID_VIRTUAL_TEST, MODULE_ID_VIRTUAL_MODULES, NAME } from './core/constants'
77
import debug from './core/debug'
8+
import { def } from './core/utils'
89

910
const loadersPath = resolve(__dirname, 'loaders')
1011
const pitcher = resolve(loadersPath, 'pitcher.js')
@@ -28,7 +29,13 @@ class WindiCSSWebpackPlugin {
2829
}
2930

3031
apply(compiler: Compiler): void {
31-
const root = this.options.root ?? compiler.options.resolve?.alias?.['~'] ?? compiler.context
32+
// resolve the root working directory
33+
let root = compiler.context
34+
if (this.options.root)
35+
root = this.options.root
36+
else if (compiler.options.resolve && compiler.options.resolve.alias && compiler.options.resolve.alias['~'])
37+
root = compiler.options.resolve.alias['~']
38+
3239
// Fix possibly undefined issues
3340
if (!compiler.options.module || !compiler.options.module.rules)
3441
return
@@ -89,7 +96,7 @@ class WindiCSSWebpackPlugin {
8996
if (shouldExcludeResource(resource))
9097
return false
9198

92-
return Boolean(compiler.$windyCSSService?.isDetectTarget(resource))
99+
return Boolean(compiler.$windyCSSService.isDetectTarget(resource))
93100
},
94101
enforce: 'post',
95102
use: [{
@@ -107,7 +114,7 @@ class WindiCSSWebpackPlugin {
107114
if (shouldExcludeResource(resource))
108115
return false
109116

110-
return Boolean(compiler.$windyCSSService?.isDetectTarget(resource))
117+
return Boolean(compiler.$windyCSSService.isDetectTarget(resource))
111118
},
112119
use: [{
113120
loader: transformTemplateLoader,
@@ -119,7 +126,7 @@ class WindiCSSWebpackPlugin {
119126
if (shouldExcludeResource(resource))
120127
return false
121128

122-
return Boolean(compiler.$windyCSSService?.isCssTransformTarget(resource))
129+
return Boolean(compiler.$windyCSSService.isCssTransformTarget(resource))
123130
},
124131
use: [{
125132
loader: transformCSSLoader,
@@ -204,7 +211,7 @@ class WindiCSSWebpackPlugin {
204211
if (match) {
205212
const layer = (match[1] as LayerName | 'all') || 'all'
206213
if (compiler.$windyCSSService && compiler.$windyCSSService.virtualModules.has(layer))
207-
virtualModuleContent = compiler.$windyCSSService.virtualModules.get(layer) ?? ''
214+
virtualModuleContent = def(compiler.$windyCSSService.virtualModules.get(layer), '')
208215
}
209216
virtualModules.writeModule(
210217
join(this.options.virtualModulePath, virtualModulePath),
@@ -217,10 +224,10 @@ class WindiCSSWebpackPlugin {
217224
// Make windy service available to the loader
218225
const initWindyCSSService = async() => {
219226
if (!compiler.$windyCSSService) {
220-
const utils = this.options.utils ?? createUtils(this.options, {
227+
const utils = def(this.options.utils, createUtils(this.options, {
221228
root,
222229
name: NAME,
223-
})
230+
}))
224231

225232
compiler.$windyCSSService = Object.assign(
226233
utils,

0 commit comments

Comments
 (0)