-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(webpack): support passing function as
postcssOptions
(#19495)
- Loading branch information
Showing
7 changed files
with
150 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { join } from 'node:path' | ||
import PostcssConfig from '../src/utils/postcss' | ||
|
||
describe('webpack: postcss', () => { | ||
const getConfigWithPostcssConfig = config => | ||
new PostcssConfig({ | ||
options: { | ||
dev: false, | ||
srcDir: join(__dirname), | ||
rootDir: join(__dirname), | ||
modulesDir: [] | ||
}, | ||
nuxt: { | ||
resolver: { | ||
requireModule: plugin => opts => [plugin, opts] | ||
} | ||
}, | ||
buildOptions: { | ||
postcss: config | ||
} | ||
}) | ||
|
||
test('should have the right default configuration', () => { | ||
// Use the default postcss config: stage 2 | ||
// https://cssdb.org/#staging-process | ||
const pluginConfig = Object.fromEntries( | ||
getConfigWithPostcssConfig({ postcssOptions: {} }).config().postcssOptions.plugins | ||
) | ||
expect(pluginConfig).toMatchInlineSnapshot(` | ||
{ | ||
"cssnano": { | ||
"preset": [ | ||
"default", | ||
{ | ||
"minifyFontValues": { | ||
"removeQuotes": false, | ||
}, | ||
}, | ||
], | ||
}, | ||
"postcss-import": { | ||
"resolve": [Function], | ||
}, | ||
"postcss-preset-env": {}, | ||
"postcss-url": {}, | ||
} | ||
`) | ||
}) | ||
test('can pass a function through', () => { | ||
// Use the default postcss config: stage 2 | ||
// https://cssdb.org/#staging-process | ||
const options = getConfigWithPostcssConfig({ postcssOptions: () => ({ preset: { stage: 2 } }) }).config().postcssOptions | ||
expect(typeof options).toBe('function') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { getPort, loadFixture, Nuxt } from '../utils' | ||
|
||
let port | ||
const url = route => 'http://localhost:' + port + route | ||
|
||
let nuxt = null | ||
|
||
describe('postcss configuration as function', () => { | ||
beforeAll(async () => { | ||
const options = await loadFixture('postcss-function') | ||
nuxt = new Nuxt(options) | ||
await nuxt.ready() | ||
|
||
port = await getPort() | ||
await nuxt.server.listen(port, '0.0.0.0') | ||
}) | ||
|
||
for (const path of ['/css', '/postcss']) { | ||
test(path, async () => { | ||
const window = await nuxt.server.renderAndGetWindow(url(path)) | ||
|
||
const headHtml = window.document.head.innerHTML | ||
expect(headHtml.replace(/\s+/g, '').replace(/;}/g, '}')).toContain('div.red{background-color:blue}.red{color:red}') | ||
|
||
const element = window.document.querySelector('.red') | ||
expect(element).not.toBe(null) | ||
expect(element.textContent).toContain('This is red') | ||
expect(element.className).toBe('red') | ||
// t.is(window.getComputedStyle(element).color, 'red') | ||
}) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const createData = async () => { | ||
await new Promise(resolve => setTimeout(resolve, 500)) | ||
return { | ||
build: { | ||
postcss: { | ||
postcssOptions: () => ({ | ||
plugins: [ | ||
['postcss-preset-env', { features: { 'custom-selectors': true } }] | ||
] | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
|
||
export default createData |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<template> | ||
<div class="red"> | ||
This is red | ||
</div> | ||
</template> | ||
|
||
<style> | ||
@custom-selector :--red div.red; | ||
:--red { | ||
background-color: blue; | ||
} | ||
.red { | ||
color: red; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<template> | ||
<div class="red"> | ||
This is red | ||
</div> | ||
</template> | ||
|
||
<style lang="postcss"> | ||
@custom-selector :--red div.red; | ||
:--red { | ||
background-color: blue; | ||
} | ||
.red { | ||
color: red; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { buildFixture } from '../../utils/build' | ||
|
||
buildFixture('postcss-function') |