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 vue2 css match error #4065

Merged
merged 1 commit into from
Oct 14, 2024
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
33 changes: 20 additions & 13 deletions driver/js/packages/hippy-vue-css-loader/src/css-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,19 @@
* Convert the CSS text to AST that able to parse by selector parser.
*/
function hippyVueCSSLoader(this: any, source: any) {
const options = getOptions(this);

Check failure on line 33 in driver/js/packages/hippy-vue-css-loader/src/css-loader.ts

View workflow job for this annotation

GitHub Actions / frontend_build_tests (16.x)

'options' is assigned a value but never used. Allowed unused vars must match /^_.+/u

Check failure on line 33 in driver/js/packages/hippy-vue-css-loader/src/css-loader.ts

View workflow job for this annotation

GitHub Actions / frontend_build_tests (17.x)

'options' is assigned a value but never used. Allowed unused vars must match /^_.+/u
const parsed = parseCSS(source, { source: sourceId });
const hash = crypto.createHash('shake256', { outputLength: 3 });

const majorNodeVersion = parseInt(process.versions.node.split('.')[0], 10);
const hashType = majorNodeVersion >= 17 ? 'md5' : 'md4';
const hash = crypto.createHash(hashType);
const contentHash = hash.update(source).digest('hex');
sourceId += 1;
const rulesAst = parsed.stylesheet.rules.filter((n: any) => n.type === 'rule').map((n: any) => ([
contentHash,
n.selectors,
// filter comment declaration and empty declaration
n.declarations.filter(dec => dec.type !== 'comment').map((dec: any) => {
const rulesAst = parsed.stylesheet.rules.filter((n: any) => n.type === 'rule').map((n: any) => ({
hash: contentHash,
selectors: n.selectors,

declarations: n.declarations.map((dec: any) => {
let { value } = dec;
const isVariableColor = dec.property?.startsWith('-') && typeof value === 'string'
&& (
Expand All @@ -52,14 +55,18 @@
if (dec.property && (dec.property.toLowerCase().indexOf('color') > -1 || isVariableColor)) {
value = translateColor(value);
}
return [dec.property, value];
return {
type: dec.type,
property: dec.property,
value,
};
}),
])).filter(rule => rule[2].length > 0);
const code = `(function(n) {
if (!global[n]) {
global[n] = [];
}));
const code = `(function() {
if (!global['${GLOBAL_STYLE_NAME}']) {
global['${GLOBAL_STYLE_NAME}'] = [];
}
global[n] = global[n].concat(${JSON.stringify(rulesAst)});
global['${GLOBAL_STYLE_NAME}'] = global['${GLOBAL_STYLE_NAME}'].concat(${JSON.stringify(rulesAst)});

if(module.hot) {
module.hot.dispose(() => {
Expand All @@ -70,7 +77,7 @@
global['${GLOBAL_DISPOSE_STYLE_NAME}'] = global['${GLOBAL_DISPOSE_STYLE_NAME}'].concat('${contentHash}');
})
}
})('${GLOBAL_STYLE_NAME}')`;
})()`;
return `module.exports=${code}`;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ import { SelectorsMap } from './css-selectors-match';
import { parseSelector } from './parser';
import { HIPPY_GLOBAL_STYLE_NAME, HIPPY_GLOBAL_DISPOSE_STYLE_NAME } from './';

type Declaration = [property: string, value: string | number];
export type ASTRule = [hash: string, selectors: string[], declarations: Declaration[]];

// style load hook
const beforeLoadStyleHook: Function = (declaration: Function): Function => declaration;

Expand Down Expand Up @@ -73,7 +70,7 @@ function createSimpleSelectorFromAst(ast) {
? new AttributeSelector(ast.property, ast.test, ast.value)
: new AttributeSelector(ast.property);
default:
return new InvalidSelector(new Error('Unknown selector.'));;
return null;
}
}

Expand Down Expand Up @@ -128,23 +125,10 @@ function createSelector(sel) {
* @param beforeLoadStyle
*/
export function fromAstNodes(
astRules: Array<CssAttribute | ASTRule> = [],
astRules: CssAttribute[] = [],
beforeLoadStyle?: Function,
): RuleSet[] {
const rules = astRules.map(rule => {
if (!Array.isArray(rule)) return rule;
const [hash, selectors, declarations] = rule as ASTRule;
return {
hash,
selectors,
declarations: declarations.map(([property, value]) => ({
type: 'declaration',
property,
value,
})),
};
});
return rules.map((rule) => {
return astRules.map((rule) => {
const declarations = rule.declarations
.filter(isDeclaration)
// use default hook when there is no hook passed in
Expand Down
Loading