-
Notifications
You must be signed in to change notification settings - Fork 108
feat: postcss-loader #1787
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
Merged
Merged
feat: postcss-loader #1787
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cd91704
feat: 支持postcss-loader
YufJi 7f3a83c
chore: 合并postcss到options传给render
YufJi 955b462
chore: 更新.gitignore以排除less.postcss的node_modules目录
YufJi 72f6110
feat: 添加PostCSS插件支持并更新相关类型定义
YufJi 0a6efbb
refactor: 简化Less和Sass插件中的选项传递
YufJi d179f9c
docs: 添加postcss支持的配置说明
YufJi fc239b9
feat: 添加对PostCSS的支持,更新相关插件以集成postLoaders
YufJi 93bef9a
feat: 更新Less和Sass插件以移除postLoaders,增强PostCSS支持
YufJi 72dc41b
test: 补充postcss测试用例
YufJi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,21 @@ | ||
| const assert = require("assert"); | ||
|
|
||
| const { parseBuildResult } = require("../../../scripts/test-utils"); | ||
| const { files } = parseBuildResult(__dirname); | ||
|
|
||
| assert.match( | ||
| files["index.css"], | ||
| new RegExp(`.foo .bar { | ||
| width: 100vw; | ||
| }`), | ||
| "less width is not expected" | ||
| ); | ||
|
|
||
| assert.match( | ||
| files["index.css"], | ||
| new RegExp(`.a .b { | ||
| width: 100vw; | ||
| }`), | ||
| "css width is not expected" | ||
| ); | ||
|
|
This file contains hidden or 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 @@ | ||
| { | ||
| "postcss": true | ||
| } |
This file contains hidden or 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,5 @@ | ||
| { | ||
| "devDependencies": { | ||
| "postcss-px-to-viewport-8-plugin": "1.2.5" | ||
| } | ||
| } |
This file contains hidden or 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,9 @@ | ||
| module.exports = { | ||
| plugins: [ | ||
| require('postcss-px-to-viewport-8-plugin')({ | ||
| unitToConvert: 'px', | ||
| viewportWidth: 375, | ||
| propList: ['*'], | ||
| }), | ||
| ], | ||
| }; |
This file contains hidden or 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 @@ | ||
| .a .b { | ||
| width: 375px; | ||
| } |
This file contains hidden or 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,5 @@ | ||
| .foo { | ||
| .bar { | ||
| width: 375px; | ||
| } | ||
| } |
This file contains hidden or 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 "./index.less"; | ||
| import "./index.css"; | ||
| console.log(1); |
This file contains hidden or 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,8 @@ | ||
| h2 { | ||
| color: red; | ||
| } | ||
|
|
||
| h2 .blue { | ||
| color: blue; | ||
| font-size: 24px; | ||
| } |
This file contains hidden or 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,8 @@ | ||
| h1 { | ||
| color: red; | ||
|
|
||
| .blue { | ||
| color: blue; | ||
| font-size: 24px; | ||
| } | ||
| } |
This file contains hidden or 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,19 @@ | ||
| import React from 'react'; | ||
| import ReactDOM from 'react-dom/client'; | ||
| import './index.less'; | ||
| import './index.css'; | ||
|
|
||
| function App() { | ||
| return ( | ||
| <div> | ||
| <h1> | ||
| Hello, <span className="blue">Less</span> | ||
| </h1> | ||
| <h2> | ||
| Hello, <span className="blue">Css</span> | ||
| </h2> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| ReactDOM.createRoot(document.getElementById('root')!).render(<App />); |
This file contains hidden or 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 @@ | ||
| { | ||
| "postcss": true | ||
| } |
This file contains hidden or 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,9 @@ | ||
| { | ||
| "dependencies": { | ||
| "react": "18.2.0", | ||
| "react-dom": "18.2.0" | ||
| }, | ||
| "devDependencies": { | ||
| "postcss-px-to-viewport-8-plugin": "^1.2.5" | ||
| } | ||
| } |
This file contains hidden or 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,12 @@ | ||
| module.exports = { | ||
| plugins: [ | ||
| require('postcss-px-to-viewport-8-plugin')({ | ||
| unitToConvert: 'px', | ||
| viewportWidth: 375, | ||
| propList: ['*'], | ||
| landscape: true, | ||
| landscapeUnit: 'vw', | ||
| landscapeWidth: 812, | ||
| }), | ||
| ], | ||
| }; |
This file contains hidden or 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,10 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <link rel="stylesheet" type="text/css" href="index.css"> | ||
| </head> | ||
| <body> | ||
| <div id="root"></div> | ||
| <script src="index.js"></script> | ||
| </body> | ||
| </html> |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,81 @@ | ||
| import path from 'path'; | ||
| import url from 'url'; | ||
| import { BuildParams } from '../../'; | ||
| import * as binding from '../../../binding'; | ||
| import { RunLoadersOptions, createParallelLoader } from '../../runLoaders'; | ||
|
|
||
| export class PostcssPlugin implements binding.JsHooks { | ||
| name: string; | ||
| params: BuildParams & { resolveAlias: Record<string, string> }; | ||
| extOpts: RunLoadersOptions; | ||
| parallelLoader: ReturnType<typeof createParallelLoader> | undefined; | ||
|
|
||
| constructor(params: BuildParams & { resolveAlias: Record<string, string> }) { | ||
| this.name = 'postcss'; | ||
| this.params = params; | ||
| this.extOpts = { | ||
| alias: params.resolveAlias, | ||
| root: params.root, | ||
| }; | ||
| } | ||
|
|
||
| transform = async ( | ||
| content: string, | ||
| filename: string, | ||
| ): Promise<{ content: string; type: 'css' | 'js' } | void> => { | ||
| if (!isTargetFile(filename)) { | ||
| return; | ||
| } | ||
|
|
||
| this.parallelLoader ||= createParallelLoader( | ||
| path.resolve(__dirname, './render.js'), | ||
| ); | ||
|
|
||
| const result = await this.parallelLoader.run({ | ||
| filename, | ||
| content, | ||
| extOpts: this.extOpts, | ||
| }); | ||
|
|
||
| let css: string = ''; | ||
|
|
||
| if (result.result) { | ||
| const buf = result.result[0]; | ||
| if (Buffer.isBuffer(buf)) { | ||
| css = buf.toString('utf-8'); | ||
| } else { | ||
| css = buf ?? ''; | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| content: css, | ||
| type: 'css', | ||
| }; | ||
| }; | ||
| } | ||
|
|
||
| function getFilename(filePath: string) { | ||
| let filename = ''; | ||
| try { | ||
| filename = decodeURIComponent(url.parse(filePath).pathname || ''); | ||
| } catch (e) { | ||
| return ''; | ||
| } | ||
|
|
||
| return filename; | ||
| } | ||
|
|
||
| function isTargetFile(filePath: string) { | ||
| let filename = getFilename(filePath); | ||
|
|
||
| if ( | ||
| filename?.endsWith('.css') || | ||
| filename?.endsWith('.less') || | ||
| filename?.endsWith('.scss') | ||
| ) { | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.