diff --git a/packages/webpack-plugin/lib/style-compiler/strip-conditional-loader.js b/packages/webpack-plugin/lib/style-compiler/strip-conditional-loader.js index 7e0fb8fb28..f361a0511f 100644 --- a/packages/webpack-plugin/lib/style-compiler/strip-conditional-loader.js +++ b/packages/webpack-plugin/lib/style-compiler/strip-conditional-loader.js @@ -29,7 +29,8 @@ function tokenize(cssString) { // match[2] 为条件(如果存在) tokens.push({ type: match[1], // 'if'、'elif'、'else' 或 'endif' - condition: match[2] ? match[2].trim() : null + condition: match[2] ? match[2].trim() : null, + rawValue: match[0] }) lastIndex = regex.lastIndex } @@ -54,6 +55,7 @@ function parse(cssString) { currentChildren.push(node) } else if (token.type === 'if') { const node = new Node('If', token.condition) + node.rawValue = token.rawValue || '' currentChildren.push(node) nodeStack.push(currentChildren) currentChildren = node.children @@ -63,6 +65,7 @@ function parse(cssString) { } currentChildren = nodeStack[nodeStack.length - 1] const node = new Node('ElseIf', token.condition) + node.rawValue = token.rawValue || '' currentChildren.push(node) currentChildren = node.children } else if (token.type === 'else') { @@ -71,12 +74,16 @@ function parse(cssString) { } currentChildren = nodeStack[nodeStack.length - 1] const node = new Node('Else') + node.rawValue = token.rawValue || '' currentChildren.push(node) currentChildren = node.children } else if (token.type === 'endif') { + const node = new Node('EndIf') + node.rawValue = token.rawValue || '' if (nodeStack.length > 0) { currentChildren = nodeStack.pop() } + currentChildren.push(node) } }) return ast @@ -105,17 +112,22 @@ function traverseAndEvaluate(ast, defs) { } else if (node.type === 'If') { // 直接判断 If 节点 batchedIf = false + output += node.rawValue || '' if (evaluateCondition(node.condition, defs)) { traverse(node.children) batchedIf = true } } else if (node.type === 'ElseIf' && !batchedIf) { + output += node.rawValue || '' if (evaluateCondition(node.condition, defs)) { traverse(node.children) batchedIf = true } } else if (node.type === 'Else' && !batchedIf) { + output += node.rawValue || '' traverse(node.children) + } else if (node.type === 'EndIf') { + output += node.rawValue || '' } } } diff --git a/packages/webpack-plugin/test/platform/common/fixtures/css-condition/at-import-2/output.md b/packages/webpack-plugin/test/platform/common/fixtures/css-condition/at-import-2/output.md index c52acec841..4ef3b09465 100644 --- a/packages/webpack-plugin/test/platform/common/fixtures/css-condition/at-import-2/output.md +++ b/packages/webpack-plugin/test/platform/common/fixtures/css-condition/at-import-2/output.md @@ -61,17 +61,17 @@ .selector3 color green /** comment 5 */ - +/* @mpx-if (!isMobile) *//* @mpx-else */ .layout-nested-else background #fff color blue - +/* @mpx-endif */ .text-ellipsis { - + /* @mpx-if (__mpx_mode__ !== 'ios' && __mpx_mode__ !== 'android' && __mpx_mode__ !== 'harmony') *//* @mpx-endif */ } - +/* @mpx-if (__mpx_mode__ !== 'ios' && __mpx_mode__ !== 'android' && __mpx_mode__ !== 'harmony') *//* @mpx-endif */ .layout background red diff --git a/packages/webpack-plugin/test/platform/common/fixtures/css-condition/at-import/output.md b/packages/webpack-plugin/test/platform/common/fixtures/css-condition/at-import/output.md index 33986ddf0d..e6f40a3916 100644 --- a/packages/webpack-plugin/test/platform/common/fixtures/css-condition/at-import/output.md +++ b/packages/webpack-plugin/test/platform/common/fixtures/css-condition/at-import/output.md @@ -16,19 +16,19 @@ .layout background red - + /* @mpx-if (isMobile) */ color yellow .driver color blue - + /* @mpx-endif */ .wrapper background red - + /* @mpx-if (isMobile) */ color yellow .child color blue - + /* @mpx-endif */ ``` \ No newline at end of file diff --git a/packages/webpack-plugin/test/platform/common/fixtures/css-condition/multiple_condition/output.md b/packages/webpack-plugin/test/platform/common/fixtures/css-condition/multiple_condition/output.md index 6f509055ef..51c0f88d22 100644 --- a/packages/webpack-plugin/test/platform/common/fixtures/css-condition/multiple_condition/output.md +++ b/packages/webpack-plugin/test/platform/common/fixtures/css-condition/multiple_condition/output.md @@ -14,16 +14,16 @@ ## Result ```css - +/*@mpx-if(isMobile)*/ .mobile { display: block; } +/*@mpx-endif*/ - - +/*@mpx-if(showHeader)*/ .header { height: 100px; } - +/*@mpx-endif*/ ``` \ No newline at end of file diff --git a/packages/webpack-plugin/test/platform/common/fixtures/css-condition/nested_condition/output.md b/packages/webpack-plugin/test/platform/common/fixtures/css-condition/nested_condition/output.md index ca0a66ec7b..578bfd4815 100644 --- a/packages/webpack-plugin/test/platform/common/fixtures/css-condition/nested_condition/output.md +++ b/packages/webpack-plugin/test/platform/common/fixtures/css-condition/nested_condition/output.md @@ -15,13 +15,13 @@ ```css body { margin: 0; } - +/*@mpx-if(isMobile)*/ .mobile { display: block; - + /*@mpx-if(hasFeature)*/ .feature { color: red; } - + /*@mpx-endif*/ } - +/*@mpx-endif*/ header { color: red } ``` \ No newline at end of file diff --git a/packages/webpack-plugin/test/platform/common/fixtures/css-condition/seq_condition/output.md b/packages/webpack-plugin/test/platform/common/fixtures/css-condition/seq_condition/output.md index 2ae3fc4038..9cde7530e6 100644 --- a/packages/webpack-plugin/test/platform/common/fixtures/css-condition/seq_condition/output.md +++ b/packages/webpack-plugin/test/platform/common/fixtures/css-condition/seq_condition/output.md @@ -15,8 +15,8 @@ ```css header {} - +/*@mpx-if(isMobile)*/ .mobile { display: block; } - +/*@mpx-endif */ body {} ``` \ No newline at end of file diff --git a/packages/webpack-plugin/test/platform/common/fixtures/css-condition/styl/output.condition-else.md b/packages/webpack-plugin/test/platform/common/fixtures/css-condition/styl/output.condition-else.md index 7c4e709089..cc01ed7608 100644 --- a/packages/webpack-plugin/test/platform/common/fixtures/css-condition/styl/output.condition-else.md +++ b/packages/webpack-plugin/test/platform/common/fixtures/css-condition/styl/output.condition-else.md @@ -14,13 +14,13 @@ ```stylus - +/* @mpx-if (isMobile) *//* @mpx-else */ .desktop { display: block; } +/* @mpx-endif */ - - +/*@mpx-if(!isMobile)*/ .mobile { display: block; } - +/* @mpx-endif */ ``` \ No newline at end of file diff --git a/packages/webpack-plugin/test/platform/common/fixtures/css-condition/styl/output.md b/packages/webpack-plugin/test/platform/common/fixtures/css-condition/styl/output.md index 6a2c8a0b63..f6e4578863 100644 --- a/packages/webpack-plugin/test/platform/common/fixtures/css-condition/styl/output.md +++ b/packages/webpack-plugin/test/platform/common/fixtures/css-condition/styl/output.md @@ -14,13 +14,13 @@ ```stylus - +/* @mpx-if (isMobile) */ .mobile { display: block; } +/* @mpx-endif */ - - +/*@mpx-if(!isMobile)*//* @mpx-else */ .desktop { display: block; } - +/* @mpx-endif */ ``` \ No newline at end of file diff --git a/packages/webpack-plugin/test/platform/common/fixtures/css-condition/stylus/empty-selector/output.md b/packages/webpack-plugin/test/platform/common/fixtures/css-condition/stylus/empty-selector/output.md index cb90cce630..e53e81ffd4 100644 --- a/packages/webpack-plugin/test/platform/common/fixtures/css-condition/stylus/empty-selector/output.md +++ b/packages/webpack-plugin/test/platform/common/fixtures/css-condition/stylus/empty-selector/output.md @@ -15,5 +15,5 @@ ```stylus .selector - + /* @mpx-if (isMobile) *//* @mpx-endif */ ``` \ No newline at end of file