Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: print deprecation warning only when plugin is used #1744

Merged
merged 3 commits into from May 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 15 additions & 13 deletions lib/postcss.js
Expand Up @@ -27,25 +27,27 @@ function postcss(...plugins) {
}

postcss.plugin = function plugin(name, initializer) {
// eslint-disable-next-line no-console
if (console && console.warn) {
let warningPrinted = false
function creator(...args) {
// eslint-disable-next-line no-console
console.warn(
name +
': postcss.plugin was deprecated. Migration guide:\n' +
'https://evilmartians.com/chronicles/postcss-8-plugin-migration'
)
if (process.env.LANG && process.env.LANG.startsWith('cn')) {
/* c8 ignore next 7 */
if (console && console.warn && !warningPrinted) {
warningPrinted = true
// eslint-disable-next-line no-console
console.warn(
name +
': 里面 postcss.plugin 被弃用. 迁移指南:\n' +
'https://www.w3ctech.com/topic/2226'
': postcss.plugin was deprecated. Migration guide:\n' +
'https://evilmartians.com/chronicles/postcss-8-plugin-migration'
)
if (process.env.LANG && process.env.LANG.startsWith('cn')) {
/* c8 ignore next 7 */
// eslint-disable-next-line no-console
console.warn(
name +
': 里面 postcss.plugin 被弃用. 迁移指南:\n' +
'https://www.w3ctech.com/topic/2226'
)
}
}
}
function creator(...args) {
let transformer = initializer(...args)
transformer.postcssPlugin = name
transformer.postcssVersion = new Processor().version
Expand Down
14 changes: 10 additions & 4 deletions test/postcss.test.ts
Expand Up @@ -121,12 +121,13 @@ test('has deprecated method to creat plugins', () => {
})
}
})
equal(warn.callCount, 1)
match(warn.calls[0][0], /postcss\.plugin was deprecated/)

equal(warn.callCount, 0)

let func1: any = postcss(plugin).plugins[0]
is(func1.postcssPlugin, 'test')
match(func1.postcssVersion, /\d+.\d+.\d+/)
equal(warn.callCount, 1)
zardoy marked this conversation as resolved.
Show resolved Hide resolved

let func2: any = postcss(plugin()).plugins[0]
equal(func2.postcssPlugin, func1.postcssPlugin)
Expand All @@ -137,6 +138,9 @@ test('has deprecated method to creat plugins', () => {

let result2 = postcss(plugin).process('a{ one: 1; two: 2 }')
is(result2.css, 'a{ one: 1 }')

equal(warn.callCount, 1)
match(warn.calls[0][0], /postcss\.plugin was deprecated/)
})

test('creates a shortcut to process css', async () => {
Expand All @@ -148,7 +152,6 @@ test('creates a shortcut to process css', async () => {
})
}
})
equal(warn.callCount, 1)

let result1 = plugin.process('a{value:foo}')
is(result1.css, 'a{value:bar}')
Expand All @@ -159,6 +162,8 @@ test('creates a shortcut to process css', async () => {
let result = await plugin.process('a{value:foo}', { from: 'a' }, 'baz')
equal(result.opts, { from: 'a' })
is(result.css, 'a{value:baz}')

equal(warn.callCount, 1)
})

test('does not call plugin constructor', () => {
Expand All @@ -169,13 +174,14 @@ test('does not call plugin constructor', () => {
return () => {}
})
is(calls, 0)
equal(warn.callCount, 1)

postcss(plugin).process('a{}')
is(calls, 1)

postcss(plugin()).process('a{}')
is(calls, 2)

equal(warn.callCount, 1)
})

test.run()
4 changes: 2 additions & 2 deletions test/processor.test.ts
Expand Up @@ -390,8 +390,8 @@ test('checks plugin compatibility', () => {
throw new Error('Er')
}
})
equal(warn.callCount, 1)
let func = plugin()
equal(warn.callCount, 1)
func.postcssVersion = '2.1.5'

function processBy(version: string): void {
Expand Down Expand Up @@ -562,9 +562,9 @@ test('supports plugins returning processors', () => {
let other: any = (postcss as any).plugin('test', () => {
return new Processor([a])
})
equal(warn.callCount, 1)
processor.use(other)
equal(processor.plugins, [a])
equal(warn.callCount, 1)
})

test('supports plugin creators returning processors', () => {
Expand Down