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(mini-runner): 修复公共样式不能覆盖全局样式的问题 #11621

Merged
merged 1 commit into from
Apr 12, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@
"@types/autoprefixer": "9.7.0",
"@types/babel-types": "^7.0.7",
"@types/babel__traverse": "^7.0.7",
"@types/convert-source-map": "^1.5.2",
"@types/debug": "4.1.5",
"@types/detect-port": "1.3.0",
"@types/fs-extra": "^8.0.1",
Expand Down
2 changes: 0 additions & 2 deletions packages/taro-mini-runner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
"@tarojs/taro-loader": "3.4.4",
"babel-loader": "8.2.1",
"babel-preset-taro": "3.4.4",
"convert-source-map": "^1.8.0",
"copy-webpack-plugin": "5.1.2",
"css-loader": "3.4.2",
"csso-webpack-plugin": "2.0.0-beta.1",
Expand All @@ -69,7 +68,6 @@
"mini-css-extract-plugin": "0.9.0",
"miniprogram-simulate": "^1.1.5",
"mkdirp": "^1.0.4",
"offset-sourcemap-lines": "^1.0.1",
"ora": "4.0.3",
"postcss": "8.3.5",
"postcss-html-transform": "3.4.4",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1078,8 +1078,8 @@ require(\\"./taro\\");
{\\"pages\\":[\\"pages/index/index\\",\\"pages/about/index\\"],\\"window\\":{\\"backgroundTextStyle\\":\\"light\\",\\"navigationBarBackgroundColor\\":\\"#fff\\",\\"navigationBarTitleText\\":\\"WeChat\\",\\"navigationBarTextStyle\\":\\"black\\"}}

/** filePath: dist/app.wxss **/
@import \\"./common.wxss\\";
.body{background-color:#e8e8ed}
@import \\"./common.wxss\\";

/** filePath: dist/base.wxml **/
<wxs module=\\"xs\\" src=\\"./utils.wxs\\" />
Expand Down
46 changes: 10 additions & 36 deletions packages/taro-mini-runner/src/plugins/MiniPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import * as NaturalChunkOrderPlugin from 'webpack/lib/optimize/NaturalChunkOrder
import * as SplitChunksPlugin from 'webpack/lib/optimize/SplitChunksPlugin'
import * as RuntimeChunkPlugin from 'webpack/lib/optimize/RuntimeChunkPlugin'
import * as MiniCssExtractPlugin from 'mini-css-extract-plugin'
import * as convert from 'convert-source-map'
import offsetLines from 'offset-sourcemap-lines'
import { ConcatSource } from 'webpack-sources'
import { urlToRequest } from 'loader-utils'
import { minify } from 'html-minifier'
Expand Down Expand Up @@ -1161,46 +1159,22 @@ export default class TaroMiniPlugin {

if (!assets[appStyle]) return

const commonStyles: string[] = []
const originSource: string = assets[appStyle].source()
const source = new ConcatSource()
source.add(originSource)

// 组件公共样式需要放在 app 全局样式之后:https://github.com/NervJS/taro/pull/6125
Object.keys(assets).forEach(assetName => {
const fileName = path.basename(assetName, path.extname(assetName))
if ((REG_STYLE.test(assetName) || REG_STYLE_EXT.test(assetName)) && this.options.commonChunks.includes(fileName)) {
commonStyles.push(`@import ${JSON.stringify(urlToRequest(assetName))};\n`)
}
})

if (commonStyles.length > 0) {
const source = new ConcatSource()
let rawSource = assets[appStyle].source()
source.add(commonStyles.join(''))

const rawConvSourceMap = convert.fromSource(rawSource)

if (rawConvSourceMap) {
rawSource = convert.removeComments(rawSource)
source.add(rawSource)
const offsettedMap = offsetLines(rawConvSourceMap.toObject(), commonStyles.length)
source.add(convert.fromObject(offsettedMap).toComment())
} else {
source.add(rawSource)
const appStyleMap = appStyle + '.map'
if (assets[appStyleMap]) {
const mapContext = assets[appStyleMap].source()
const offsettedMap = offsetLines(JSON.parse(mapContext), commonStyles.length)
const offsettedMapText = JSON.stringify(offsettedMap)
assets[appStyleMap] = {
size: () => offsettedMapText.length,
source: () => offsettedMapText
}
source.add('\n')
source.add(`@import ${JSON.stringify(urlToRequest(assetName))};`)
assets[appStyle] = {
size: () => source.source().length,
source: () => source.source()
}
}

assets[appStyle] = {
size: () => source.source().length,
source: () => source.source()
}
}
})
}

addTarBarFilesToDependencies (compilation: webpack.compilation.Compilation) {
Expand Down
14 changes: 1 addition & 13 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5319,11 +5319,6 @@
dependencies:
"@types/node" "*"

"@types/convert-source-map@^1.5.2":
version "1.5.2"
resolved "https://registry.yarnpkg.com/@types/convert-source-map/-/convert-source-map-1.5.2.tgz#318dc22d476632a4855594c16970c6dc3ed086e7"
integrity sha512-tHs++ZeXer40kCF2JpE51Hg7t4HPa18B1b1Dzy96S0eCw8QKECNMYMfwa1edK/x8yCN0r4e6ewvLcc5CsVGkdg==

"@types/cors@^2.8.6":
version "2.8.12"
resolved "https://registry.yarnpkg.com/@types/cors/-/cors-2.8.12.tgz#6b2c510a7ad7039e98e7b8d3d6598f4359e5c080"
Expand Down Expand Up @@ -10894,7 +10889,7 @@ convert-source-map@^0.3.3:
resolved "https://registry.yarnpkg.com/convert-source-map/-/convert-source-map-0.3.5.tgz#f1d802950af7dd2631a1febe0596550c86ab3190"
integrity sha1-8dgClQr33SYxof6+BZZVDIarMZA=

convert-source-map@^1.1.0, convert-source-map@^1.4.0, convert-source-map@^1.5.0, convert-source-map@^1.5.1, convert-source-map@^1.6.0, convert-source-map@^1.7.0, convert-source-map@^1.8.0:
convert-source-map@^1.1.0, convert-source-map@^1.4.0, convert-source-map@^1.5.0, convert-source-map@^1.5.1, convert-source-map@^1.6.0, convert-source-map@^1.7.0:
version "1.8.0"
resolved "https://registry.yarnpkg.com/convert-source-map/-/convert-source-map-1.8.0.tgz#f3373c32d21b4d780dd8004514684fb791ca4369"
integrity sha512-+OQdjP49zViI/6i7nIJpA8rAl4sV/JdPfU9nZs3VqOwGIgizICvuN2ru6fMd+4llL0tar18UYJXfZ/TWtmhUjA==
Expand Down Expand Up @@ -21799,13 +21794,6 @@ obuf@^1.0.0, obuf@^1.1.2:
resolved "https://registry.yarnpkg.com/obuf/-/obuf-1.1.2.tgz#09bea3343d41859ebd446292d11c9d4db619084e"
integrity sha512-PX1wu0AmAdPqOL1mWhqmlOd8kOIZQwGZw6rh7uby9fTc5lhaOWFLX3I6R1hrF9k3zUY40e6igsLGkDXK92LJNg==

offset-sourcemap-lines@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/offset-sourcemap-lines/-/offset-sourcemap-lines-1.0.1.tgz#5854dff74b73fc06efcb61d7b721a8113d99be92"
integrity sha1-WFTf90tz/Abvy2HXtyGoET2ZvpI=
dependencies:
source-map "^0.5.0"

omggif@^1.0.10, omggif@^1.0.9:
version "1.0.10"
resolved "https://registry.yarnpkg.com/omggif/-/omggif-1.0.10.tgz#ddaaf90d4a42f532e9e7cb3a95ecdd47f17c7b19"
Expand Down