Skip to content

Instantly share code, notes, and snippets.

View developit's full-sized avatar
🦊
write, the codes

Jason Miller developit

🦊
write, the codes
View GitHub Profile

Proposal: Importable Constructable Stylesheets

We're getting Constructable Stylesheets. This seems like an intuitive value to obtain when importing CSS from JavaScript, since it's the DOM's representation of a Stylesheet:

import stylesheet from './style.css';
console.log(stylesheet);  // CSSStyleSheet

No such system is in place to allow this to work (see [whatwg/loader]), however frontend build tooling has congregated around this approach as a mechanism for bringing CSS assets into the JavaScript module graph. There are many benefits to be obtained from moving CSS into this graph, however the most important is that imported CSS can be attributed to the consuming JS Module. This allows it to be bundled, optimized, and potentially dead-code-eliminated leveraging static analysis performed on the surrounding module graph.

@developit
developit / ie-cors-polyfill.js
Created January 9, 2019 21:00
A polyfill for CORS in older versions of IE.
var xhr2 = false;
try { xhr2 = 'useCredentials' in new XMLHttpRequest; } catch (e) {}
var old = window.XMLHttpRequest;
function XMLHttpRequest() {
if (xhr2) return new old();
}
XMLHttpRequest.prototype.open = function(method, url, async, user, pass) {
var pageOrigin = location.protocol + ':' + location.host;
var self = this;
function wrap(m) {
if (typeof WeakMap !== 'function') {
let c = 0;
WeakMap = function() {
let id = typeof Symbol === 'function' ? Symbol() : `__weak$${++c}`;
this.set = (key, val) => { key[id] = val };
this.get = key => key[id];
};
}
export function loadConfigModule(filename) {
// sub your own
return require(filename);
}
/**
* Modify a Webpack configuration by applying a mutator module/function.
* @param {Object} options
* @param {Object} options.config Webpack configuration object to modify in-place
module.exports = {
format(parts) {
return new URL(parts).href;
},
parse(url) {
const parsed = new URL(url);
parsed.query = parsed.search ? parsed.search.substring(1) : null;
parsed.auth = parsed.username + ':' + parsed.password;
return parsed;
},
(function() {
const { name, description, setup, teardown, tests, tags } = window['view-bench']._component.state.bench;
name = name.replace(/(?:\s*\((\d+)\))?$/, (s,d) => ` (${(d||0)+1})`);
history.replaceState(null, null, '/bench');
dispatchEvent(new Event('popstate'));
setTimeout(() => {
window['view-bench']._component.setState({ bench: { name, description, setup, teardown, tests, tags } });
window['view-bench']._component.save();
}, 100);
}());
import hljs from 'highlight.js/lib/highlight';
import javascript from 'highlight.js/lib/languages/javascript';
import json from 'highlight.js/lib/languages/json';
import xml from 'highlight.js/lib/languages/xml';
import 'highlight.js/styles/tomorrow-night-eighties.css';
const LANGUAGES = { javascript, json, xml };
Object.keys(LANGUAGES).forEach( key => hljs.registerLanguage(key, LANGUAGES[key]) );
export function syntaxHighlight(code, lang) {
node_modules
package-lock.json

suspense-loader

Installation (sorry)

npm i THIS_URL

Usage