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

Replace node-sass with sass-embedded #1001

Merged
merged 1 commit into from
Sep 27, 2023

Conversation

AMoo-Miki
Copy link
Collaborator

@AMoo-Miki AMoo-Miki commented Aug 29, 2023

Description

Replace node-sass with sass-embedded

Also:

  • Implement sass variable extraction
  • Implement sass variable importing in typescript
  • Add guards to post-install cleanup
  • Make sources compatible with dart sass by wrapping divisions in calc() and adding units were necessary
  • Fold in @elastic/charts/dist/theme.scss to modify it for dart-sass compatibility
  • Break out compiling charts

closes #966

Check List

  • New functionality includes testing.
  • New functionality has been documented.
  • All tests pass
    • yarn lint
    • yarn test-unit
  • Update CHANGELOG.md
  • Commits are signed per the DCO using --signoff

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

For more information on following Developer Certificate of Origin and signing off your commits, please check here.

Copy link
Contributor

@BSFishy BSFishy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

initial review, still need to look at variable from scss when my brain isnt completely fried

@@ -16,7 +16,7 @@
position: absolute;
left: 0;
top: 50%;
margin-top: -($ouiRangeThumbHeight / 2);
margin-top: calc(-1 * $ouiRangeThumbHeight / 2);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be -1 * x? What about $ouiRangeThumbHeight / -2?

Comment on lines +24 to +25
$arrowPlusSize: (calc($arrowSize/2) + 1px) * -1; /* 1 */
$arrowMinusSize: (calc($arrowSize/2) - 1px) * -1; /* 1 */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like these equations could be optimized but that could be done in a followup

Copy link
Member

@joshuarrrr joshuarrrr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple questions, but looks good to me. The important part is that it works! 😁

@joshuarrrr joshuarrrr added the 1.3 label Aug 30, 2023
Copy link
Collaborator

@SergeyMyssak SergeyMyssak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job! I added a couple of minor comments on code style but they can be ignored.

I was wondering if you used prettier in the new files, I have Webstorm showing that the file can be formatted

Screenshot 2023-09-02 at 10 14 35

Comment on lines +5 to +8
const idKey = '!!variables-from-scss!!';
const keyLength = idKey.length;

const forbiddenKeyNames = [...Object.getOwnPropertyNames(Object.prototype), 'prototype'];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can write them in uppercase to note that these variables are constant and should not be changed:

const IMPORT_PREFIX = '!!variables-from-scss!!';
const FORBIDDEN_KEYS = [...Object.getOwnPropertyNames(Object.prototype), 'prototype'];

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reserve uppercased names for constants that get exported; here they are just local variables. Do you have a strong feeling for this? or do you think they should be exported?

Comment on lines +27 to +33
const { variables } = compileWithVariablesSync(importTarget);

// If no default specifier is used, reduce the variables to only those needed
const importedVariables = assignments.length === 0 ? usedVariableNames.reduce((acc, name) => {
if (!forbiddenKeyNames.includes(name)) acc[name] = variables[name];
return acc;
}, {}) : variables;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can put the logic in a separate function to improve readability:

const filterImportedVariables = (allVariables, usedVariableNames) => {
  return usedVariableNames.reduce((acc, name) => {
    if (!FORBIDDEN_KEYS.includes(name)) {
      acc[name] = allVariables[name];
    }
    return acc;
  }, {});
};
const importTarget = join(dirname(state.file.opts.filename), path.node.source.value.substring(IMPORT_PREFIX.length));
const { variables: allVariables } = compileWithVariablesSync(importTarget);
const importedVariables = assignments.length === 0 ? filterImportedVariables(allVariables, usedVariableNames) : allVariables;

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a feeling that these are called synchronously and recursively and was very worried about stack overflows; hence I inlined everything without looking at the depth.

Comment on lines +12 to +15
const varListMarker = ':֍෴֍߷';
const arrayMarker = '෴';
const objectMarker = '߷';
const forbiddenKeyNames = [...Object.getOwnPropertyNames(Object.prototype), 'prototype'];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can write them in uppercase to note that these variables are constant and should not be changed:

const VAR_LIST_MARKER = ':֍෴֍߷';
const ARRAY_MARKER = '෴';
const OBJECT_MARKER = '߷';
const FORBIDDEN_KEY_NAMES = [...Object.getOwnPropertyNames(Object.prototype), 'prototype'];

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since they are local variables and not exported, I didn't; do you have a strong feeling towards this?
PS, for the same reason, i didn't deepFreeze the forbidden key names.

Comment on lines +83 to +123
// originalKey is only used for displaying errors
const _set = (o, path, value, originalKey) => {
// To improve performance, since this is a private method, it is not validating `path`
const [key, type, ...rest] = path;
if (forbiddenKeyNames.includes(key)) return;
if (rest.length === 0) {
if (type) {
throw new Error(`Unexpected type ${type} for ${key} was found on ${originalKey}`);
}
// For bwc, 0px -> 0
o[key] = isFinite(value) ? parseFloat(value) : (value === '0px' ? 0 : value);
} else {
switch (type) {
case arrayMarker:
if (key in o) {
if (!Array.isArray(o[key]))
throw new Error(`${key} of ${originalKey} was found to be ${Object.prototype.toString.call(o[key])}`);
} else {
o[key] = [];
// Inserting to the beginning to make sure the inner references appear before the outer ones.
foundArrays.unshift({ node: o, key });
}
_set(o[key], rest, value);
break;

case objectMarker:
if (key in o) {
if (Object.prototype.toString.call(o[key]) !== '[object Object]')
throw new Error(`${key} of ${originalKey} was found to be ${Object.prototype.toString.call(o[key])}`);
} else {
o[key] = {};
knownKeys.add(key);
}
_set(o[key], rest, value);
break;

default:
throw new Error(`Unknown type [${type}] in ${originalKey}`);
}
}
};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can move this function somewhere else to avoid overloading this part of the code

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should; let's hold of though until OUI 2 when we remove all of those backward compatibility stuff or else we will have to pass foundArrays and knownKeys around.

@joshuarrrr joshuarrrr removed the 1.3 label Sep 5, 2023
@joshuarrrr
Copy link
Member

This will follow the 1.3.0 release, as it only affects dev dependencies.

@BSFishy BSFishy mentioned this pull request Sep 14, 2023
7 tasks
Comment on lines +196 to +205
compileScssFiles(path.join('src', 'theme_*.scss'), 'dist', ouiPackageName)
.catch((err) => {
console.error(err);
process.exit(2);
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about it again, this function shouldn't ever throw an error, I think. Everything that might is wrapped in a try-catch

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a re-throw in compileScssFiles.

Also:
* Implement sass variable extraction
* Implement sass variable importing in typescript
* Add guards to post-install cleanup
* Make sources compatible with dart sass by wrapping divisions in `calc()` and adding units were necessary
* Fold in `@elastic/charts/dist/theme.scss` to modify it for dart-sass compatibility
* Break out compiling `charts`

Signed-off-by: Miki <[email protected]>
@AMoo-Miki AMoo-Miki merged commit db94845 into opensearch-project:main Sep 27, 2023
opensearch-trigger-bot bot pushed a commit that referenced this pull request Sep 27, 2023
Also:
* Implement sass variable extraction
* Implement sass variable importing in typescript
* Add guards to post-install cleanup
* Make sources compatible with dart sass by wrapping divisions in `calc()` and adding units were necessary
* Fold in `@elastic/charts/dist/theme.scss` to modify it for dart-sass compatibility
* Break out compiling `charts`

Signed-off-by: Miki <[email protected]>
(cherry picked from commit db94845)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
@AMoo-Miki AMoo-Miki deleted the dart-experiment branch September 27, 2023 21:11
BSFishy pushed a commit that referenced this pull request Sep 29, 2023
Also:
* Implement sass variable extraction
* Implement sass variable importing in typescript
* Add guards to post-install cleanup
* Make sources compatible with dart sass by wrapping divisions in `calc()` and adding units were necessary
* Fold in `@elastic/charts/dist/theme.scss` to modify it for dart-sass compatibility
* Break out compiling `charts`


(cherry picked from commit db94845)

Signed-off-by: Miki <[email protected]>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
@BSFishy BSFishy mentioned this pull request Sep 29, 2023
7 tasks
ShatilKhan pushed a commit to ShatilKhan/oui that referenced this pull request Nov 17, 2023
Also:
* Implement sass variable extraction
* Implement sass variable importing in typescript
* Add guards to post-install cleanup
* Make sources compatible with dart sass by wrapping divisions in `calc()` and adding units were necessary
* Fold in `@elastic/charts/dist/theme.scss` to modify it for dart-sass compatibility
* Break out compiling `charts`

Signed-off-by: Miki <[email protected]>
Signed-off-by: ShatilKhan <[email protected]>
ShatilKhan pushed a commit to ShatilKhan/oui that referenced this pull request Nov 17, 2023
Also:
* Implement sass variable extraction
* Implement sass variable importing in typescript
* Add guards to post-install cleanup
* Make sources compatible with dart sass by wrapping divisions in `calc()` and adding units were necessary
* Fold in `@elastic/charts/dist/theme.scss` to modify it for dart-sass compatibility
* Break out compiling `charts`

Signed-off-by: Miki <[email protected]>
Signed-off-by: ShatilKhan <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Replace the patched node-sass when the original library upgrades their libsass
5 participants