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

Implement loading of CSS Modules. Resolves #1991. #1997

Merged
merged 7 commits into from
Aug 25, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ The minimal [1.5KB s.js loader](dist/s.min.js) provides a workflow where code wr
Since the ES module semantics such as live bindings, circular references, contextual metadata, dynamic import and top-level await [can all be fully supported this way](docs/system-register.md#semantics), while supporting CSP and cross-origin support, this workflow can be relied upon as a polyfill-like path.

* Loads and resolves modules as URLs, throwing for bare specifier names (eg `import 'lodash'`) like the native module loader.
* Loads System.register and JSON modules.
* Loads System.register, JSON, and CSS modules.
* Core hookable extensible loader supporting [custom extensions](docs/hooks.md).

#### 2. system.js loader
Expand Down Expand Up @@ -61,6 +61,8 @@ npm install systemjs
* [API](docs/api.md)
* [System.register](docs/system-register.md)
* [Loader Hooks](docs/hooks.md)
* [JSON modules](docs/json-modules.md)
* [CSS modules](docs/css-modules.md)
joeldenning marked this conversation as resolved.
Show resolved Hide resolved

## Example Usage

Expand Down
22 changes: 22 additions & 0 deletions docs/css-modules.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# CSS Modules

[CSS Modules](https://github.com/w3c/webcomponents/blob/gh-pages/proposals/css-modules-v1-explainer.md) are supported in both
joeldenning marked this conversation as resolved.
Show resolved Hide resolved
s.js and system.js.

Note that the term CSS Modules refers to two separate things: (1) the browser spec, or (2) the webpack / postcss plugin.
The CSS modules implemented by SystemJS are the browser spec.

## Example
```css
/* file.css */
.brown {
color: brown;
}
```

```js
System.import('file.css').then(function (module) {
const styleSheet = module.default; // A CSSStyleSheet object
document.adoptedStyleSheets = [...document.adoptedStyleSheets, styleSheet]; // now your css is available to be used.
});
```
joeldenning marked this conversation as resolved.
Show resolved Hide resolved
18 changes: 18 additions & 0 deletions docs/json-modules.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# JSON Modules

[JSON modules](https://github.com/whatwg/html/pull/4407) are supported in both s.js and system.js.

## Example

**file.json**
```json
{
"some": "json value"
}
```

```js
System.import('file.json').then(function (module) {
console.log(module.default); // The json as a js object.
});
```
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
],
"devDependencies": {
"bluebird": "^3.5.1",
"construct-style-sheets-polyfill": "^2.1.0",
"esm": "^3.2.25",
"mocha": "^5.2.0",
"rollup": "^0.64.1",
Expand Down
17 changes: 16 additions & 1 deletion src/features/script-load.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,25 @@ systemJSPrototype.instantiate = function (url, firstParentUrl) {
return fetch(url).then(function (resp) {
return resp.text();
}).then(function (source) {
return [[], function(_export) {
return [[], function (_export) {
return {execute: function() {_export('default', JSON.parse(source))}};
}];
});
} else if (url.substr(-4) === '.css') {
joeldenning marked this conversation as resolved.
Show resolved Hide resolved
return fetch(url).then(function (resp) {
return resp.text();
}).then(function (source) {
return [[], function (_export) {
return {
execute: function () {
// Relies on a Constructable Stylesheet polyfill
const stylesheet = new CSSStyleSheet();
stylesheet.replaceSync(source);
_export('default', stylesheet);
}
};
}];
});
joeldenning marked this conversation as resolved.
Show resolved Hide resolved
} else {
return new Promise(function (resolve, reject) {
let err;
Expand Down
6 changes: 6 additions & 0 deletions test/browser/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,10 @@ suite('SystemJS Standard Tests', function() {
assert.equal(m.addTwo(1, 1), 2);
});
});

test('should load a css module', async function () {
const m = await System.import('./css-modules/a.css')
assert.ok(m);
assert.ok(m.default instanceof CSSStyleSheet);
});
});
3 changes: 3 additions & 0 deletions test/fixtures/css-modules/a.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.hello {
background-color: peru;
}
1 change: 1 addition & 0 deletions test/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
if (typeof fetch === 'undefined')
document.write('<script src="../node_modules/whatwg-fetch/fetch.js"><\/script>');
</script>
<script src="../node_modules/construct-style-sheets-polyfill/adoptedStyleSheets.js"></script>
<script>
// TODO IE11 URL polyfill testing
// if (typeof URL === 'undefined')
Expand Down