Unverified Commit 26768571 authored by Sukka's avatar Sukka Committed by GitHub

test(unit): add cases for cssnano simple (#46862)

Continues from #46380. The PR migrates the unit test cases from the original repo into the Next.js repo.
parent a39df508
...@@ -180,6 +180,7 @@ ...@@ -180,6 +180,7 @@
"pixrem": "5.0.0", "pixrem": "5.0.0",
"playwright-chromium": "1.28.1", "playwright-chromium": "1.28.1",
"plop": "3.0.5", "plop": "3.0.5",
"postcss": "8.4.14",
"postcss-nested": "4.2.1", "postcss-nested": "4.2.1",
"postcss-pseudoelements": "5.0.0", "postcss-pseudoelements": "5.0.0",
"postcss-short-size": "4.0.0", "postcss-short-size": "4.0.0",
......
...@@ -140,6 +140,7 @@ importers: ...@@ -140,6 +140,7 @@ importers:
pixrem: 5.0.0 pixrem: 5.0.0
playwright-chromium: 1.28.1 playwright-chromium: 1.28.1
plop: 3.0.5 plop: 3.0.5
postcss: 8.4.14
postcss-nested: 4.2.1 postcss-nested: 4.2.1
postcss-pseudoelements: 5.0.0 postcss-pseudoelements: 5.0.0
postcss-short-size: 4.0.0 postcss-short-size: 4.0.0
...@@ -315,6 +316,7 @@ importers: ...@@ -315,6 +316,7 @@ importers:
pixrem: 5.0.0 pixrem: 5.0.0
playwright-chromium: 1.28.1 playwright-chromium: 1.28.1
plop: 3.0.5 plop: 3.0.5
postcss: 8.4.14
postcss-nested: 4.2.1 postcss-nested: 4.2.1
postcss-pseudoelements: 5.0.0 postcss-pseudoelements: 5.0.0
postcss-short-size: 4.0.0 postcss-short-size: 4.0.0
...@@ -5850,7 +5852,7 @@ packages: ...@@ -5850,7 +5852,7 @@ packages:
dependencies: dependencies:
'@mdx-js/mdx': 2.2.1 '@mdx-js/mdx': 2.2.1
source-map: 0.7.3 source-map: 0.7.3
webpack: 5.74.0 webpack: 5.74.0_@swc+core@1.2.203
transitivePeerDependencies: transitivePeerDependencies:
- supports-color - supports-color
...@@ -6506,7 +6508,6 @@ packages: ...@@ -6506,7 +6508,6 @@ packages:
cpu: [arm] cpu: [arm]
os: [android] os: [android]
requiresBuild: true requiresBuild: true
dev: true
optional: true optional: true
/@swc/core-android-arm64/1.2.203: /@swc/core-android-arm64/1.2.203:
...@@ -6515,7 +6516,6 @@ packages: ...@@ -6515,7 +6516,6 @@ packages:
cpu: [arm64] cpu: [arm64]
os: [android] os: [android]
requiresBuild: true requiresBuild: true
dev: true
optional: true optional: true
/@swc/core-darwin-arm64/1.2.203: /@swc/core-darwin-arm64/1.2.203:
...@@ -6524,7 +6524,6 @@ packages: ...@@ -6524,7 +6524,6 @@ packages:
cpu: [arm64] cpu: [arm64]
os: [darwin] os: [darwin]
requiresBuild: true requiresBuild: true
dev: true
optional: true optional: true
/@swc/core-darwin-x64/1.2.203: /@swc/core-darwin-x64/1.2.203:
...@@ -6533,7 +6532,6 @@ packages: ...@@ -6533,7 +6532,6 @@ packages:
cpu: [x64] cpu: [x64]
os: [darwin] os: [darwin]
requiresBuild: true requiresBuild: true
dev: true
optional: true optional: true
/@swc/core-freebsd-x64/1.2.203: /@swc/core-freebsd-x64/1.2.203:
...@@ -6542,7 +6540,6 @@ packages: ...@@ -6542,7 +6540,6 @@ packages:
cpu: [x64] cpu: [x64]
os: [freebsd] os: [freebsd]
requiresBuild: true requiresBuild: true
dev: true
optional: true optional: true
/@swc/core-linux-arm-gnueabihf/1.2.203: /@swc/core-linux-arm-gnueabihf/1.2.203:
...@@ -6551,7 +6548,6 @@ packages: ...@@ -6551,7 +6548,6 @@ packages:
cpu: [arm] cpu: [arm]
os: [linux] os: [linux]
requiresBuild: true requiresBuild: true
dev: true
optional: true optional: true
/@swc/core-linux-arm64-gnu/1.2.203: /@swc/core-linux-arm64-gnu/1.2.203:
...@@ -6560,7 +6556,6 @@ packages: ...@@ -6560,7 +6556,6 @@ packages:
cpu: [arm64] cpu: [arm64]
os: [linux] os: [linux]
requiresBuild: true requiresBuild: true
dev: true
optional: true optional: true
/@swc/core-linux-arm64-musl/1.2.203: /@swc/core-linux-arm64-musl/1.2.203:
...@@ -6569,7 +6564,6 @@ packages: ...@@ -6569,7 +6564,6 @@ packages:
cpu: [arm64] cpu: [arm64]
os: [linux] os: [linux]
requiresBuild: true requiresBuild: true
dev: true
optional: true optional: true
/@swc/core-linux-x64-gnu/1.2.203: /@swc/core-linux-x64-gnu/1.2.203:
...@@ -6578,7 +6572,6 @@ packages: ...@@ -6578,7 +6572,6 @@ packages:
cpu: [x64] cpu: [x64]
os: [linux] os: [linux]
requiresBuild: true requiresBuild: true
dev: true
optional: true optional: true
/@swc/core-linux-x64-musl/1.2.203: /@swc/core-linux-x64-musl/1.2.203:
...@@ -6587,7 +6580,6 @@ packages: ...@@ -6587,7 +6580,6 @@ packages:
cpu: [x64] cpu: [x64]
os: [linux] os: [linux]
requiresBuild: true requiresBuild: true
dev: true
optional: true optional: true
/@swc/core-win32-arm64-msvc/1.2.203: /@swc/core-win32-arm64-msvc/1.2.203:
...@@ -6596,7 +6588,6 @@ packages: ...@@ -6596,7 +6588,6 @@ packages:
cpu: [arm64] cpu: [arm64]
os: [win32] os: [win32]
requiresBuild: true requiresBuild: true
dev: true
optional: true optional: true
/@swc/core-win32-ia32-msvc/1.2.203: /@swc/core-win32-ia32-msvc/1.2.203:
...@@ -6605,7 +6596,6 @@ packages: ...@@ -6605,7 +6596,6 @@ packages:
cpu: [ia32] cpu: [ia32]
os: [win32] os: [win32]
requiresBuild: true requiresBuild: true
dev: true
optional: true optional: true
/@swc/core-win32-x64-msvc/1.2.203: /@swc/core-win32-x64-msvc/1.2.203:
...@@ -6614,7 +6604,6 @@ packages: ...@@ -6614,7 +6604,6 @@ packages:
cpu: [x64] cpu: [x64]
os: [win32] os: [win32]
requiresBuild: true requiresBuild: true
dev: true
optional: true optional: true
/@swc/core/1.2.203: /@swc/core/1.2.203:
...@@ -6635,7 +6624,6 @@ packages: ...@@ -6635,7 +6624,6 @@ packages:
'@swc/core-win32-arm64-msvc': 1.2.203 '@swc/core-win32-arm64-msvc': 1.2.203
'@swc/core-win32-ia32-msvc': 1.2.203 '@swc/core-win32-ia32-msvc': 1.2.203
'@swc/core-win32-x64-msvc': 1.2.203 '@swc/core-win32-x64-msvc': 1.2.203
dev: true
/@swc/helpers/0.4.11: /@swc/helpers/0.4.11:
resolution: {integrity: sha512-rEUrBSGIoSFuYxwBYtlUFMlE2CwGhmW+w9355/5oduSw8e5h2+Tj4UrAGNNgP9915++wj5vkQo0UuOBqOAq4nw==} resolution: {integrity: sha512-rEUrBSGIoSFuYxwBYtlUFMlE2CwGhmW+w9355/5oduSw8e5h2+Tj4UrAGNNgP9915++wj5vkQo0UuOBqOAq4nw==}
...@@ -23091,7 +23079,6 @@ packages: ...@@ -23091,7 +23079,6 @@ packages:
source-map: 0.6.1 source-map: 0.6.1
terser: 5.14.1 terser: 5.14.1
webpack: 5.74.0_@swc+core@1.2.203 webpack: 5.74.0_@swc+core@1.2.203
dev: true
/terser-webpack-plugin/5.2.4_webpack@5.74.0: /terser-webpack-plugin/5.2.4_webpack@5.74.0:
resolution: {integrity: sha512-E2CkNMN+1cho04YpdANyRrn8CyN4yMy+WdFKZIySFZrGXZxJwJP6PMNGGc/Mcr6qygQHUUqRxnAPmi0M9f00XA==} resolution: {integrity: sha512-E2CkNMN+1cho04YpdANyRrn8CyN4yMy+WdFKZIySFZrGXZxJwJP6PMNGGc/Mcr6qygQHUUqRxnAPmi0M9f00XA==}
...@@ -24489,7 +24476,6 @@ packages: ...@@ -24489,7 +24476,6 @@ packages:
- '@swc/core' - '@swc/core'
- esbuild - esbuild
- uglify-js - uglify-js
dev: true
/websocket-driver/0.7.3: /websocket-driver/0.7.3:
resolution: {integrity: sha512-bpxWlvbbB459Mlipc5GBzzZwhoZgGEZLuqPaR0INBGnPAY1vdBX6hPnoFXiw+3yWxDuHyQjO2oXTMyS8A5haFg==} resolution: {integrity: sha512-bpxWlvbbB459Mlipc5GBzzZwhoZgGEZLuqPaR0INBGnPAY1vdBX6hPnoFXiw+3yWxDuHyQjO2oXTMyS8A5haFg==}
......
import cssnanoPresetSimple from 'next/src/bundles/cssnano-simple/cssnano-preset-simple'
// https://github.com/Timer/cssnano-preset-simple/issues/1
describe('https://github.com/Timer/cssnano-preset-simple/issues/1', () => {
test('evaluates without error', () => {
cssnanoPresetSimple()
})
})
import postcss from 'postcss'
import mod from './plugin'
import css from '../noop-template'
describe('accepts plugin configuration', () => {
test('should not remove all comments', async () => {
const input = css`
p {
/*! heading */
color: yellow;
}
`
const res = await postcss([mod({ discardComments: {} })]).process(input, {
from: 'input.css',
to: 'output.css',
})
expect(res.css).not.toBe('p{color:#ff0}')
})
test('should remove all comments', async () => {
const input = css`
p {
/*! heading */
color: yellow;
}
`
const res = await postcss([
mod({ discardComments: { removeAll: true } }),
]).process(input, {
from: 'input.css',
to: 'output.css',
})
expect(res.css).toBe('p{color:#ff0}')
})
})
// https://github.com/Timer/cssnano-preset-simple/blob/master/test/plugin.js
import postcss from 'postcss'
import type { PluginCreator } from 'postcss'
// Since the cssnano-preset-simple.js will be bundled into cssnano-simple
// during pre-compilation, we need to test against the source file directly
import cssnanoPresetSimple from 'next/src/bundles/cssnano-simple/cssnano-preset-simple'
const cssnanoPlugin = (options = {}) => {
const plugins: any[] = []
const nanoPlugins = cssnanoPresetSimple(options).plugins
for (const nanoPlugin of nanoPlugins) {
if (Array.isArray(nanoPlugin)) {
let [processor, opts] = nanoPlugin
processor = processor.default || processor
if (
typeof opts === 'undefined' ||
(typeof opts === 'object' && !opts.exclude) ||
(typeof opts === 'boolean' && opts === true)
) {
plugins.push(processor(opts))
}
} else {
plugins.push(nanoPlugin)
}
}
return postcss(plugins)
}
cssnanoPlugin.postcss = true
export default cssnanoPlugin as PluginCreator<any>
import postcss from 'postcss'
import mod from './plugin'
import css from '../noop-template'
describe('property sorting', () => {
test('should result in correct border width', async () => {
const input = css`
p {
border: 1px solid var(--test);
border-radius: var(--test);
border-width: 0;
}
`
const res = await postcss([mod]).process(input, {
from: 'input.css',
to: 'output.css',
})
expect(res.css).toMatchInlineSnapshot(
`"p{border-width:1px;border-radius:var(--test);border:0 solid var(--test)}"`
)
})
})
import postcss from 'postcss'
import mod from './plugin'
describe('with escaped selector', () => {
test('should not misplace the backslash', async () => {
const input = `
.foo\\,2 {
background: blue;
}
`
const res = await postcss([mod]).process(input, {
from: 'input.css',
to: 'output.css',
})
expect(res.css).toMatchInlineSnapshot(`".foo\\\\,2{background:blue}"`)
})
})
import postcss from 'postcss'
import mod from 'next/src/bundles/cssnano-simple/index'
import css from '../noop-template'
describe('basic test', () => {
test('should minify css', async () => {
const input = css`
p {
color: yellow;
}
`
const res = await postcss([mod()]).process(input, {
from: 'input.css',
to: 'output.css',
})
expect(res.css).toBe('p{color:#ff0}')
})
})
import postcss from 'postcss'
import mod from 'next/src/bundles/cssnano-simple/index'
import css from '../noop-template'
describe('exclude all test', () => {
test('should not transform css', async () => {
const input = css`
p {
/* test */
color: yellow;
}
`
const res = await postcss([mod({ excludeAll: true })]).process(input, {
from: 'input.css',
to: 'output.css',
})
expect(res.css).toBe(input)
})
test('should strip comments and spaces from css', async () => {
const input = css`
p {
/* test */
color: yellow;
}
.empty {
}
`
const res = await postcss([
mod({
excludeAll: true,
discardComments: true,
normalizeWhitespace: { exclude: false },
}),
]).process(input, {
from: 'input.css',
to: 'output.css',
})
expect(res.css).toMatchInlineSnapshot(`"p{color:yellow}.empty{}"`)
})
test('should enable rule with empty object', async () => {
const input = css`
p {
/* test */
color: yellow;
}
.empty {
}
`
const res = await postcss([
mod({
excludeAll: true,
discardComments: true,
normalizeWhitespace: { exclude: false },
discardEmpty: {},
}),
]).process(input, {
from: 'input.css',
to: 'output.css',
})
expect(res.css).toMatchInlineSnapshot(`"p{color:yellow}"`)
})
})
import postcss from 'postcss'
import mod from 'next/src/bundles/cssnano-simple/index'
import css from '../noop-template'
describe('accepts plugin configuration', () => {
test('should not remove all comments', async () => {
const input = css`
p {
/*! heading */
color: yellow;
}
`
const res = await postcss([mod({ discardComments: {} })]).process(input, {
from: 'input.css',
to: 'output.css',
})
expect(res.css).not.toBe('p{color:#ff0}')
})
test('should remove all comments', async () => {
const input = css`
p {
/*! heading */
color: yellow;
}
`
const res = await postcss([
mod({ discardComments: { removeAll: true } }),
]).process(input, {
from: 'input.css',
to: 'output.css',
})
expect(res.css).toBe('p{color:#ff0}')
})
})
export default function noop(strings: TemplateStringsArray, ...keys: string[]) {
const lastIndex = strings.length - 1
return (
strings.slice(0, lastIndex).reduce((p, s, i) => p + s + keys[i], '') +
strings[lastIndex]
)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment