From 48d38f9c8576de5e8433b9dd67272a456b218a7c Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Sun, 7 Apr 2019 16:24:10 +0900 Subject: [PATCH 01/14] refactor: use import() to load assets --- .babelrc | 4 + .eslintrc.json | 1 + package-lock.json | 235 +++++++++++++++++++- package.json | 4 + src/core/algorithms.js | 16 +- src/core/best-practices.js | 13 +- src/core/caniuse.js | 21 +- src/core/examples.js | 15 +- src/core/highlight-vars.js | 14 +- src/core/highlight.js | 18 +- src/core/issues-notes.js | 13 +- src/core/style.js | 29 ++- src/core/ui.js | 24 +- src/core/webidl-clipboard.js | 24 +- src/core/webidl.js | 16 +- src/core/worker.js | 29 ++- src/jsconfig.json | 5 +- src/type-helper.d.ts | 4 + tools/babel-plugin-import-meta-requirejs.js | 54 +++++ tools/builder.js | 6 + 20 files changed, 485 insertions(+), 60 deletions(-) create mode 100644 tools/babel-plugin-import-meta-requirejs.js diff --git a/.babelrc b/.babelrc index 3097139d3e..97615bd82f 100644 --- a/.babelrc +++ b/.babelrc @@ -7,5 +7,9 @@ "modules": "amd" } ] + ], + "plugins": [ + "./tools/babel-plugin-import-meta-requirejs", + "dynamic-import-amd" ] } diff --git a/.eslintrc.json b/.eslintrc.json index 633a658a71..7560bdfa8e 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -8,6 +8,7 @@ "worker": true }, "extends": ["eslint:recommended", "plugin:prettier/recommended"], + "parser": "babel-eslint", "parserOptions": { "sourceType": "module", "ecmaVersion": 2019 diff --git a/package-lock.json b/package-lock.json index d47b873087..2b22eabd75 100644 --- a/package-lock.json +++ b/package-lock.json @@ -428,6 +428,15 @@ "@babel/helper-plugin-utils": "^7.0.0" } }, + "@babel/plugin-syntax-import-meta": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/@babel/plugin-syntax-import-meta/-/plugin-syntax-import-meta-7.2.0.tgz", + "integrity": "sha512-Hq6kFSZD7+PHkmBN8bCpHR6J8QEoCuEV/B38AIQscYjgMZkGlXB7cHNFzP5jR4RCh5545yP1ujHdmO7hAgKtBA==", + "dev": true, + "requires": { + "@babel/helper-plugin-utils": "^7.0.0" + } + }, "@babel/plugin-syntax-json-strings": { "version": "7.2.0", "resolved": "https://registry.npmjs.org/@babel/plugin-syntax-json-strings/-/plugin-syntax-json-strings-7.2.0.tgz", @@ -1451,6 +1460,189 @@ "integrity": "sha512-Wm6ukoaOGJi/73p/cl2GvLjTI5JM1k/O14isD73YML8StrH/7/lRFgmg8nICZgD3bZZvjwCGxtMOD3wWNAu8cg==", "dev": true }, + "babel-code-frame": { + "version": "6.26.0", + "resolved": "https://registry.npmjs.org/babel-code-frame/-/babel-code-frame-6.26.0.tgz", + "integrity": "sha1-Y/1D99weO7fONZR9uP42mj9Yx0s=", + "dev": true, + "requires": { + "chalk": "^1.1.3", + "esutils": "^2.0.2", + "js-tokens": "^3.0.2" + }, + "dependencies": { + "ansi-regex": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.1.1.tgz", + "integrity": "sha1-w7M6te42DYbg5ijwRorn7yfWVN8=", + "dev": true + }, + "ansi-styles": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-2.2.1.tgz", + "integrity": "sha1-tDLdM1i2NM914eRmQ2gkBTPB3b4=", + "dev": true + }, + "chalk": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-1.1.3.tgz", + "integrity": "sha1-qBFcVeSnAv5NFQq9OHKCKn4J/Jg=", + "dev": true, + "requires": { + "ansi-styles": "^2.2.1", + "escape-string-regexp": "^1.0.2", + "has-ansi": "^2.0.0", + "strip-ansi": "^3.0.0", + "supports-color": "^2.0.0" + } + }, + "js-tokens": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-3.0.2.tgz", + "integrity": "sha1-mGbfOVECEw449/mWvOtlRDIJwls=", + "dev": true + }, + "strip-ansi": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz", + "integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=", + "dev": true, + "requires": { + "ansi-regex": "^2.0.0" + } + }, + "supports-color": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-2.0.0.tgz", + "integrity": "sha1-U10EXOa2Nj+kARcIRimZXp3zJMc=", + "dev": true + } + } + }, + "babel-eslint": { + "version": "10.0.1", + "resolved": "https://registry.npmjs.org/babel-eslint/-/babel-eslint-10.0.1.tgz", + "integrity": "sha512-z7OT1iNV+TjOwHNLLyJk+HN+YVWX+CLE6fPD2SymJZOZQBs+QIexFjhm4keGTm8MW9xr4EC9Q0PbaLB24V5GoQ==", + "dev": true, + "requires": { + "@babel/code-frame": "^7.0.0", + "@babel/parser": "^7.0.0", + "@babel/traverse": "^7.0.0", + "@babel/types": "^7.0.0", + "eslint-scope": "3.7.1", + "eslint-visitor-keys": "^1.0.0" + }, + "dependencies": { + "eslint-scope": { + "version": "3.7.1", + "resolved": "https://registry.npmjs.org/eslint-scope/-/eslint-scope-3.7.1.tgz", + "integrity": "sha1-PWPD7f2gLgbgGkUq2IyqzHzctug=", + "dev": true, + "requires": { + "esrecurse": "^4.1.0", + "estraverse": "^4.1.1" + } + } + } + }, + "babel-messages": { + "version": "6.23.0", + "resolved": "https://registry.npmjs.org/babel-messages/-/babel-messages-6.23.0.tgz", + "integrity": "sha1-8830cDhYA1sqKVHG7F7fbGLyYw4=", + "dev": true, + "requires": { + "babel-runtime": "^6.22.0" + } + }, + "babel-plugin-dynamic-import-amd": { + "version": "github:holyzfy/babel-plugin-dynamic-import-amd#1bbaf86d4472c2eef0fc01f3199d637a4b6c51e2", + "from": "github:holyzfy/babel-plugin-dynamic-import-amd", + "dev": true, + "requires": { + "babel-plugin-syntax-dynamic-import": "^6.18.0", + "babel-template": "^6.22.0" + } + }, + "babel-plugin-syntax-dynamic-import": { + "version": "6.18.0", + "resolved": "https://registry.npmjs.org/babel-plugin-syntax-dynamic-import/-/babel-plugin-syntax-dynamic-import-6.18.0.tgz", + "integrity": "sha1-jWomIpyDdFqZgqRBBRVyyqF5sdo=", + "dev": true + }, + "babel-runtime": { + "version": "6.26.0", + "resolved": "https://registry.npmjs.org/babel-runtime/-/babel-runtime-6.26.0.tgz", + "integrity": "sha1-llxwWGaOgrVde/4E/yM3vItWR/4=", + "dev": true, + "requires": { + "core-js": "^2.4.0", + "regenerator-runtime": "^0.11.0" + } + }, + "babel-template": { + "version": "6.26.0", + "resolved": "https://registry.npmjs.org/babel-template/-/babel-template-6.26.0.tgz", + "integrity": "sha1-3gPi0WOWsGn0bdn/+FIfsaDjXgI=", + "dev": true, + "requires": { + "babel-runtime": "^6.26.0", + "babel-traverse": "^6.26.0", + "babel-types": "^6.26.0", + "babylon": "^6.18.0", + "lodash": "^4.17.4" + } + }, + "babel-traverse": { + "version": "6.26.0", + "resolved": "https://registry.npmjs.org/babel-traverse/-/babel-traverse-6.26.0.tgz", + "integrity": "sha1-RqnL1+3MYsjlwGTi0tjQ9ANXZu4=", + "dev": true, + "requires": { + "babel-code-frame": "^6.26.0", + "babel-messages": "^6.23.0", + "babel-runtime": "^6.26.0", + "babel-types": "^6.26.0", + "babylon": "^6.18.0", + "debug": "^2.6.8", + "globals": "^9.18.0", + "invariant": "^2.2.2", + "lodash": "^4.17.4" + }, + "dependencies": { + "globals": { + "version": "9.18.0", + "resolved": "https://registry.npmjs.org/globals/-/globals-9.18.0.tgz", + "integrity": "sha512-S0nG3CLEQiY/ILxqtztTWH/3iRRdyBLw6KMDxnKMchrtbj2OFmehVh0WUCfW3DUrIgx/qFrJPICrq4Z4sTR9UQ==", + "dev": true + } + } + }, + "babel-types": { + "version": "6.26.0", + "resolved": "https://registry.npmjs.org/babel-types/-/babel-types-6.26.0.tgz", + "integrity": "sha1-o7Bz+Uq0nrb6Vc1lInozQ4BjJJc=", + "dev": true, + "requires": { + "babel-runtime": "^6.26.0", + "esutils": "^2.0.2", + "lodash": "^4.17.4", + "to-fast-properties": "^1.0.3" + }, + "dependencies": { + "to-fast-properties": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/to-fast-properties/-/to-fast-properties-1.0.3.tgz", + "integrity": "sha1-uDVx+k2MJbguIxsG46MFXeTKGkc=", + "dev": true + } + } + }, + "babylon": { + "version": "6.18.0", + "resolved": "https://registry.npmjs.org/babylon/-/babylon-6.18.0.tgz", + "integrity": "sha512-q/UEjfGJ2Cm3oKV71DJz9d25TPnq5rhBVL2Q4fA5wcC3jcrdn7+SssEybFIxwAvvP+YCsCYNKughoF33GxgycQ==", + "dev": true + }, "backo2": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/backo2/-/backo2-1.0.2.tgz", @@ -1688,7 +1880,7 @@ }, "browserify-aes": { "version": "1.2.0", - "resolved": "https://registry.npmjs.org/browserify-aes/-/browserify-aes-1.2.0.tgz", + "resolved": "http://registry.npmjs.org/browserify-aes/-/browserify-aes-1.2.0.tgz", "integrity": "sha512-+7CHXqGuspUn/Sl5aO7Ea0xWGAtETPXNSAjHo48JfLdPWcMng33Xe4znFvQweqc/uzk5zSOI3H52CYnjCfb5hA==", "dev": true, "requires": { @@ -1733,7 +1925,7 @@ }, "browserify-rsa": { "version": "4.0.1", - "resolved": "https://registry.npmjs.org/browserify-rsa/-/browserify-rsa-4.0.1.tgz", + "resolved": "http://registry.npmjs.org/browserify-rsa/-/browserify-rsa-4.0.1.tgz", "integrity": "sha1-IeCr+vbyApzy+vsTNWenAdQTVSQ=", "dev": true, "requires": { @@ -1778,7 +1970,7 @@ }, "buffer": { "version": "4.9.1", - "resolved": "https://registry.npmjs.org/buffer/-/buffer-4.9.1.tgz", + "resolved": "http://registry.npmjs.org/buffer/-/buffer-4.9.1.tgz", "integrity": "sha1-bRu2AbB6TvztlwlBMgkwJ8lbwpg=", "dev": true, "requires": { @@ -2508,7 +2700,7 @@ }, "create-hash": { "version": "1.2.0", - "resolved": "https://registry.npmjs.org/create-hash/-/create-hash-1.2.0.tgz", + "resolved": "http://registry.npmjs.org/create-hash/-/create-hash-1.2.0.tgz", "integrity": "sha512-z00bCGNHDG8mHAkP7CtT1qVu+bFQUPjYq/4Iv3C3kWjTFV10zIjfSoeqXo9Asws8gwSHDGj/hl2u4OGIjapeCg==", "dev": true, "requires": { @@ -2521,7 +2713,7 @@ }, "create-hmac": { "version": "1.1.7", - "resolved": "https://registry.npmjs.org/create-hmac/-/create-hmac-1.1.7.tgz", + "resolved": "http://registry.npmjs.org/create-hmac/-/create-hmac-1.1.7.tgz", "integrity": "sha512-MJG9liiZ+ogc4TzUwuvbER1JRdgvUFSB5+VR/g5h82fGaIRWMWddtKBHi7/sVhfjQZ6SehlyhvQYrcYkaUIpLg==", "dev": true, "requires": { @@ -2762,7 +2954,7 @@ }, "diffie-hellman": { "version": "5.0.3", - "resolved": "https://registry.npmjs.org/diffie-hellman/-/diffie-hellman-5.0.3.tgz", + "resolved": "http://registry.npmjs.org/diffie-hellman/-/diffie-hellman-5.0.3.tgz", "integrity": "sha512-kqag/Nl+f3GwyK25fhUMYj81BUOrZ9IuJsjIcDE5icNM9FJHAVm3VcUDxdLPoQtTuUylWm6ZIknYJwwaPxsUzg==", "dev": true, "requires": { @@ -4693,7 +4885,7 @@ }, "got": { "version": "6.7.1", - "resolved": "https://registry.npmjs.org/got/-/got-6.7.1.tgz", + "resolved": "http://registry.npmjs.org/got/-/got-6.7.1.tgz", "integrity": "sha1-JAzQV4WpoY5WHcG0S0HHY+8ejbA=", "requires": { "create-error-class": "^3.0.0", @@ -4749,6 +4941,23 @@ "function-bind": "^1.1.1" } }, + "has-ansi": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/has-ansi/-/has-ansi-2.0.0.tgz", + "integrity": "sha1-NPUEnOHs3ysGSa8+8k5F7TVBbZE=", + "dev": true, + "requires": { + "ansi-regex": "^2.0.0" + }, + "dependencies": { + "ansi-regex": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.1.1.tgz", + "integrity": "sha1-w7M6te42DYbg5ijwRorn7yfWVN8=", + "dev": true + } + } + }, "has-binary2": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/has-binary2/-/has-binary2-1.0.3.tgz", @@ -7606,6 +7815,12 @@ "regenerate": "^1.4.0" } }, + "regenerator-runtime": { + "version": "0.11.1", + "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.11.1.tgz", + "integrity": "sha512-MguG95oij0fC3QV3URf4V2SDYGJhJnJGqvIIgdECeODCT98wSWDAJ94SSuVpYQUoTcGUIL6L4yNB7j1DFFHSBg==", + "dev": true + }, "regenerator-transform": { "version": "0.13.4", "resolved": "https://registry.npmjs.org/regenerator-transform/-/regenerator-transform-0.13.4.tgz", @@ -9170,7 +9385,7 @@ }, "through": { "version": "2.3.8", - "resolved": "http://registry.npmjs.org/through/-/through-2.3.8.tgz", + "resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz", "integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=" }, "through2": { @@ -9981,7 +10196,7 @@ }, "wrap-ansi": { "version": "2.1.0", - "resolved": "http://registry.npmjs.org/wrap-ansi/-/wrap-ansi-2.1.0.tgz", + "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-2.1.0.tgz", "integrity": "sha1-2Pw9KE3QV5T+hJc8rs3Rz4JP3YU=", "requires": { "string-width": "^1.0.1", @@ -10003,7 +10218,7 @@ }, "string-width": { "version": "1.0.2", - "resolved": "http://registry.npmjs.org/string-width/-/string-width-1.0.2.tgz", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-1.0.2.tgz", "integrity": "sha1-EYvfW4zcUaKn5w0hHgfisLmxB9M=", "requires": { "code-point-at": "^1.0.0", diff --git a/package.json b/package.json index 3092b221b0..704c0f78e3 100644 --- a/package.json +++ b/package.json @@ -23,8 +23,12 @@ "devDependencies": { "@babel/cli": "^7.4.3", "@babel/core": "^7.4.3", + "@babel/plugin-syntax-import-meta": "^7.2.0", "@babel/preset-env": "^7.4.3", + "@babel/template": "^7.4.0", "@types/pluralize": "0.0.29", + "babel-eslint": "^10.0.1", + "babel-plugin-dynamic-import-amd": "github:holyzfy/babel-plugin-dynamic-import-amd", "chai": "^4.2.0", "clipboard": "^2.0.4", "domReady": "github:requirejs/domReady#d85bdc38a6df868ead52ab3e80715aaf60e68765", diff --git a/src/core/algorithms.js b/src/core/algorithms.js index dff03f0ad9..354af83308 100644 --- a/src/core/algorithms.js +++ b/src/core/algorithms.js @@ -1,11 +1,21 @@ +// @ts-check /** Currently used only for adding 'assert' class to algorithm lists */ -import css from "text!../../assets/algorithms.css"; - export const name = "core/algorithms"; +async function loadStyle() { + try { + return (await import("text!../../assets/algorithms.css")).default; + } catch { + const res = await fetch( + new URL("../../assets/algorithms.css", import.meta.url) + ); + return await res.text(); + } +} + export async function run() { const elements = Array.from(document.querySelectorAll("ol.algorithm li")); elements @@ -13,7 +23,7 @@ export async function run() { .forEach(li => li.classList.add("assert")); if (document.querySelector(".assert")) { const style = document.createElement("style"); - style.textContent = css; + style.textContent = await loadStyle(); document.head.appendChild(style); } } diff --git a/src/core/best-practices.js b/src/core/best-practices.js index 03b90e0889..82f1ea49d8 100644 --- a/src/core/best-practices.js +++ b/src/core/best-practices.js @@ -4,13 +4,21 @@ // The summary is generated if there is a section in the document with ID bp-summary. // Best practices are marked up with span.practicelab. import { addId } from "./utils"; -import css from "text!../../assets/bp.css"; import hyperHTML from "hyperhtml"; import { pub } from "./pubsubhub"; export const name = "core/best-practices"; -export function run() { +async function loadStyle() { + try { + return (await import("text!../../assets/bp.css")).default; + } catch { + const res = await fetch(new URL("../../assets/bp.css", import.meta.url)); + return await res.text(); + } +} + +export async function run() { let num = 0; const bps = document.querySelectorAll("span.practicelab"); const ul = document.createElement("ul"); @@ -27,6 +35,7 @@ export function run() { } const bpSummary = document.getElementById("bp-summary"); if (bps.length) { + const css = await loadStyle(); document.head.insertBefore( hyperHTML``, document.head.querySelector("link") diff --git a/src/core/caniuse.js b/src/core/caniuse.js index ee4d06f184..1c473e7b51 100644 --- a/src/core/caniuse.js +++ b/src/core/caniuse.js @@ -4,7 +4,6 @@ * Usage options: https://github.com/w3c/respec/wiki/caniuse */ import { pub, sub } from "./pubsubhub"; -import caniuseCss from "text!../../assets/caniuse.css"; import { createResourceHint } from "./utils"; import hyperHTML from "hyperhtml"; @@ -42,6 +41,17 @@ const supportTitles = new Map([ ["d", "Disabled by default (needs to enabled)."], ]); +async function loadStyle() { + try { + return (await import("text!../../assets/caniuse.css")).default; + } catch { + const res = await fetch( + new URL("../../assets/caniuse.css", import.meta.url) + ); + return await res.text(); + } +} + export async function run(conf) { if (!conf.caniuse) { return; // nothing to do. @@ -56,9 +66,12 @@ export async function run(conf) { hint: "preconnect", href: "https://respec.org", }); - document.head.appendChild(link); - document.head.appendChild(hyperHTML` - `); + const caniuseCss = await loadStyle(); + document.head.append( + link, + hyperHTML` + ` + ); const headDlElem = document.querySelector(".head dl"); const contentPromise = new Promise(async resolve => { diff --git a/src/core/examples.js b/src/core/examples.js index d3187b936b..502bf1e254 100644 --- a/src/core/examples.js +++ b/src/core/examples.js @@ -7,7 +7,6 @@ // be used by a containing shell to extract all examples. import { addId } from "./utils"; -import css from "text!../../assets/examples.css"; import { lang as defaultLang } from "../core/l10n"; import html from "hyperhtml"; import { pub } from "./pubsubhub"; @@ -58,13 +57,25 @@ function makeTitle(conf, elem, num, report) { `; } -export function run(conf) { +async function loadStyle() { + try { + return (await import("text!../../assets/examples.css")).default; + } catch { + const res = await fetch( + new URL("../../assets/examples.css", import.meta.url) + ); + return await res.text(); + } +} + +export async function run(conf) { /** @type {NodeListOf} */ const examples = document.querySelectorAll( "pre.example, pre.illegal-example, aside.example" ); if (!examples.length) return; + const css = await loadStyle(); document.head.insertBefore( html`