Skip to content

Commit d247bc4

Browse files
committed
Merge branch 'master' into madlittlemods/686-682-local-friendly-development-and-commonjs
Conflicts: package.json scripts/sdk/base-manifest.json
2 parents 0023ab3 + 66304ed commit d247bc4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+1061
-371
lines changed

CONTRIBUTING.md

+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
Contributing code to hydrogen-web
2+
==================================
3+
4+
Everyone is welcome to contribute code to hydrogen-web, provided that they are
5+
willing to license their contributions under the same license as the project
6+
itself. We follow a simple 'inbound=outbound' model for contributions: the act
7+
of submitting an 'inbound' contribution means that the contributor agrees to
8+
license the code under the same terms as the project's overall 'outbound'
9+
license - in this case, Apache Software License v2 (see
10+
[LICENSE](LICENSE)).
11+
12+
How to contribute
13+
-----------------
14+
15+
The preferred and easiest way to contribute changes to the project is to fork
16+
it on github, and then create a pull request to ask us to pull your changes
17+
into our repo (https://help.github.com/articles/using-pull-requests/)
18+
19+
We use GitHub's pull request workflow to review the contribution, and either
20+
ask you to make any refinements needed or merge it and make them ourselves.
21+
22+
Things that should go into your PR description:
23+
* References to any bugs fixed by the change (in GitHub's `Fixes` notation)
24+
* Describe the why and what is changing in the PR description so it's easy for
25+
onlookers and reviewers to onboard and context switch.
26+
* If your PR makes visual changes, include both **before** and **after** screenshots
27+
to easily compare and discuss what's changing.
28+
* Include a step-by-step testing strategy so that a reviewer can check out the
29+
code locally and easily get to the point of testing your change.
30+
* Add comments to the diff for the reviewer that might help them to understand
31+
why the change is necessary or how they might better understand and review it.
32+
33+
We use continuous integration, and all pull requests get automatically tested:
34+
if your change breaks the build, then the PR will show that there are failed
35+
checks, so please check back after a few minutes.
36+
37+
Tests
38+
-----
39+
If your PR is a feature then we require that the PR also includes tests.
40+
These need to test that your feature works as expected and ideally test edge cases too.
41+
42+
Tests are written as unit tests by exporting a `tests` function from the file to be tested.
43+
The function returns an object where the key is the test label, and the value is a
44+
function that accepts an [assert](https://nodejs.org/api/assert.html) object, and return a Promise or nothing.
45+
46+
Note that there is currently a limitation that files that are not indirectly included from `src/platform/web/main.js` won't be found by the runner.
47+
48+
You can run the tests by running `yarn test`.
49+
This uses the [impunity](https://github.com/bwindels/impunity) runner.
50+
51+
We don't require tests for bug fixes.
52+
53+
In the future we may formalise this more.
54+
55+
Code style
56+
----------
57+
The js-sdk aims to target TypeScript/ES6. All new files should be written in
58+
TypeScript and existing files should use ES6 principles where possible.
59+
60+
Please disable any automatic formatting tools you may have active.
61+
If present, you'll be asked to undo any unrelated whitespace changes during code review.
62+
63+
Members should not be exported as a default export in general.
64+
In general, avoid using `export default`.
65+
66+
The remaining code-style for hydrogen is [in the process of being documented](codestyle.md), but
67+
contributors are encouraged to read the
68+
[code style document for matrix-react-sdk](https://github.com/matrix-org/matrix-react-sdk/blob/master/code_style.md)
69+
and follow the principles set out there.
70+
71+
Please ensure your changes match the cosmetic style of the existing project,
72+
and ***never*** mix cosmetic and functional changes in the same commit, as it
73+
makes it horribly hard to review otherwise.
74+
75+
Attribution
76+
-----------
77+
If you change or create a file, feel free to add yourself to the copyright holders
78+
in the license header of that file.
79+
80+
Sign off
81+
--------
82+
In order to have a concrete record that your contribution is intentional
83+
and you agree to license it under the same terms as the project's license, we've
84+
adopted the same lightweight approach that the Linux Kernel
85+
(https://www.kernel.org/doc/Documentation/SubmittingPatches), Docker
86+
(https://github.com/docker/docker/blob/master/CONTRIBUTING.md), and many other
87+
projects use: the DCO (Developer Certificate of Origin:
88+
http://developercertificate.org/). This is a simple declaration that you wrote
89+
the contribution or otherwise have the right to contribute it to Matrix:
90+
91+
```
92+
Developer Certificate of Origin
93+
Version 1.1
94+
95+
Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
96+
660 York Street, Suite 102,
97+
San Francisco, CA 94110 USA
98+
99+
Everyone is permitted to copy and distribute verbatim copies of this
100+
license document, but changing it is not allowed.
101+
102+
Developer's Certificate of Origin 1.1
103+
104+
By making a contribution to this project, I certify that:
105+
106+
(a) The contribution was created in whole or in part by me and I
107+
have the right to submit it under the open source license
108+
indicated in the file; or
109+
110+
(b) The contribution is based upon previous work that, to the best
111+
of my knowledge, is covered under an appropriate open source
112+
license and I have the right under that license to submit that
113+
work with modifications, whether created in whole or in part
114+
by me, under the same open source license (unless I am
115+
permitted to submit under a different license), as indicated
116+
in the file; or
117+
118+
(c) The contribution was provided directly to me by some other
119+
person who certified (a), (b) or (c) and I have not modified
120+
it.
121+
122+
(d) I understand and agree that this project and the contribution
123+
are public and that a record of the contribution (including all
124+
personal information I submit with it, including my sign-off) is
125+
maintained indefinitely and may be redistributed consistent with
126+
this project or the open source license(s) involved.
127+
```
128+
129+
If you agree to this for your contribution, then all that's needed is to
130+
include the line in your commit or pull request comment:
131+
132+
```
133+
Signed-off-by: Your Name <your@email.example.org>
134+
```
135+
136+
We accept contributions under a legally identifiable name, such as your name on
137+
government documentation or common-law names (names claimed by legitimate usage
138+
or repute). Unfortunately, we cannot accept anonymous contributions at this
139+
time.
140+
141+
Git allows you to add this signoff automatically when using the `-s` flag to
142+
`git commit`, which uses the name and email set in your `user.name` and
143+
`user.email` git configs.
144+
145+
If you forgot to sign off your commits before making your pull request and are
146+
on Git 2.17+ you can mass signoff using rebase:
147+
148+
```
149+
git rebase --signoff origin/develop
150+
```

package.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"lint-ts": "eslint src/ -c .ts-eslintrc.js --ext .ts",
1111
"lint-ci": "eslint src/",
1212
"test": "impunity --entry-point src/platform/web/main.js src/platform/web/Platform.js --force-esm-dirs lib/ src/ --root-dir src/",
13+
"test:postcss": "impunity --entry-point scripts/postcss/test.js ",
1314
"start": "vite --port 3000",
1415
"build": "vite build",
1516
"build:sdk": "./scripts/sdk/build.sh"
@@ -30,6 +31,7 @@
3031
"acorn": "^8.6.0",
3132
"acorn-walk": "^8.2.0",
3233
"aes-js": "^3.1.2",
34+
"bs58": "^4.0.1",
3335
"core-js": "^3.6.5",
3436
"es6-promise": "https://github.com/bwindels/es6-promise.git#bwindels/expose-flush",
3537
"escodegen": "^2.0.0",
@@ -45,13 +47,14 @@
4547
"text-encoding": "^0.7.0",
4648
"typescript": "^4.3.5",
4749
"vite": "todo: wait for next Vite release",
48-
"xxhashjs": "^0.2.2",
49-
"bs58": "^4.0.1"
50+
"xxhashjs": "^0.2.2"
5051
},
5152
"dependencies": {
5253
"@matrix-org/olm": "https://gitlab.matrix.org/api/v4/projects/27/packages/npm/@matrix-org/olm/-/@matrix-org/olm-3.2.3.tgz",
5354
"another-json": "^0.2.0",
5455
"base64-arraybuffer": "^0.2.0",
55-
"dompurify": "^2.3.0"
56+
"dompurify": "^2.3.0",
57+
"off-color": "^2.0.0",
58+
"postcss-value-parser": "^4.2.0"
5659
}
5760
}

scripts/.eslintrc.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module.exports = {
2+
"env": {
3+
"node": true,
4+
"es6": true
5+
},
6+
"extends": "eslint:recommended",
7+
"parserOptions": {
8+
"ecmaVersion": 2020,
9+
"sourceType": "module"
10+
},
11+
"rules": {
12+
"no-console": "off",
13+
"no-empty": "off",
14+
"no-prototype-builtins": "off",
15+
"no-unused-vars": "warn"
16+
},
17+
};
18+

scripts/postcss/color.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
Copyright 2021 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
const offColor = require("off-color").offColor;
18+
19+
module.exports.derive = function (value, operation, argument) {
20+
const argumentAsNumber = parseInt(argument);
21+
switch (operation) {
22+
case "darker": {
23+
const newColorString = offColor(value).darken(argumentAsNumber / 100).hex();
24+
return newColorString;
25+
}
26+
case "lighter": {
27+
const newColorString = offColor(value).lighten(argumentAsNumber / 100).hex();
28+
return newColorString;
29+
}
30+
}
31+
}
+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
Copyright 2021 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
const valueParser = require("postcss-value-parser");
18+
19+
/**
20+
* This plugin derives new css variables from a given set of base variables.
21+
* A derived css variable has the form --base--operation-argument; meaning that the derived
22+
* variable has a value that is generated from the base variable "base" by applying "operation"
23+
* with given "argument".
24+
*
25+
* eg: given the base variable --foo-color: #40E0D0, --foo-color--darker-20 is a css variable
26+
* derived from foo-color by making it 20% more darker.
27+
*
28+
* All derived variables are added to the :root section.
29+
*
30+
* The actual derivation is done outside the plugin in a callback.
31+
*/
32+
33+
let aliasMap;
34+
let resolvedMap;
35+
let baseVariables;
36+
37+
function getValueFromAlias(alias) {
38+
const derivedVariable = aliasMap.get(alias);
39+
return baseVariables.get(derivedVariable) ?? resolvedMap.get(derivedVariable);
40+
}
41+
42+
function parseDeclarationValue(value) {
43+
const parsed = valueParser(value);
44+
const variables = [];
45+
parsed.walk(node => {
46+
if (node.type !== "function" && node.value !== "var") {
47+
return;
48+
}
49+
const variable = node.nodes[0];
50+
variables.push(variable.value);
51+
});
52+
return variables;
53+
}
54+
55+
function resolveDerivedVariable(decl, derive) {
56+
const RE_VARIABLE_VALUE = /--((.+)--(.+)-(.+))/;
57+
const variableCollection = parseDeclarationValue(decl.value);
58+
for (const variable of variableCollection) {
59+
const matches = variable.match(RE_VARIABLE_VALUE);
60+
if (matches) {
61+
const [, wholeVariable, baseVariable, operation, argument] = matches;
62+
const value = baseVariables.get(baseVariable) ?? getValueFromAlias(baseVariable);
63+
if (!value) {
64+
throw new Error(`Cannot derive from ${baseVariable} because it is neither defined in config nor is it an alias!`);
65+
}
66+
const derivedValue = derive(value, operation, argument);
67+
resolvedMap.set(wholeVariable, derivedValue);
68+
}
69+
}
70+
}
71+
72+
function extract(decl) {
73+
if (decl.variable) {
74+
// see if right side is of form "var(--foo)"
75+
const wholeVariable = decl.value.match(/var\(--(.+)\)/)?.[1];
76+
// remove -- from the prop
77+
const prop = decl.prop.substring(2);
78+
if (wholeVariable) {
79+
aliasMap.set(prop, wholeVariable);
80+
// Since this is an alias, we shouldn't store it in baseVariables
81+
return;
82+
}
83+
baseVariables.set(prop, decl.value);
84+
}
85+
}
86+
87+
function addResolvedVariablesToRootSelector(root, {Rule, Declaration}) {
88+
const newRule = new Rule({ selector: ":root", source: root.source });
89+
// Add derived css variables to :root
90+
resolvedMap.forEach((value, key) => {
91+
const declaration = new Declaration({prop: `--${key}`, value});
92+
newRule.append(declaration);
93+
});
94+
root.append(newRule);
95+
}
96+
97+
/**
98+
* @callback derive
99+
* @param {string} value - The base value on which an operation is applied
100+
* @param {string} operation - The operation to be applied (eg: darker, lighter...)
101+
* @param {string} argument - The argument for this operation
102+
*/
103+
/**
104+
*
105+
* @param {Object} opts - Options for the plugin
106+
* @param {derive} opts.derive - The callback which contains the logic for resolving derived variables
107+
*/
108+
module.exports = (opts = {}) => {
109+
aliasMap = new Map();
110+
resolvedMap = new Map();
111+
baseVariables = new Map();
112+
return {
113+
postcssPlugin: "postcss-compile-variables",
114+
115+
Once(root, {Rule, Declaration}) {
116+
/*
117+
Go through the CSS file once to extract all aliases and base variables.
118+
We use these when resolving derived variables later.
119+
*/
120+
root.walkDecls(decl => extract(decl));
121+
root.walkDecls(decl => resolveDerivedVariable(decl, opts.derive));
122+
addResolvedVariablesToRootSelector(root, {Rule, Declaration});
123+
},
124+
};
125+
};
126+
127+
module.exports.postcss = true;

0 commit comments

Comments
 (0)