@jsenv/core 27.4.0 → 27.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/dist/js/autoreload.js +359 -0
  2. package/dist/js/execute_using_dynamic_import.js +1 -1
  3. package/dist/js/html_supervisor_installer.js +221 -73
  4. package/dist/js/html_supervisor_setup.js +3 -4
  5. package/dist/js/new_stylesheet.js +26 -58
  6. package/dist/js/server_events_client.js +307 -0
  7. package/dist/main.js +7483 -7281
  8. package/package.json +11 -10
  9. package/{README.md → readme.md} +8 -9
  10. package/src/build/build.js +12 -16
  11. package/src/build/start_build_server.js +24 -28
  12. package/src/dev/start_dev_server.js +30 -94
  13. package/src/execute/execute.js +17 -35
  14. package/src/omega/errors.js +20 -18
  15. package/src/omega/kitchen.js +7 -6
  16. package/src/omega/omega_server.js +96 -127
  17. package/src/omega/server/file_service.js +247 -46
  18. package/src/omega/url_graph.js +33 -20
  19. package/src/plugins/autoreload/client/autoreload.js +201 -0
  20. package/src/plugins/autoreload/{dev_sse/client → client}/autoreload_preference.js +0 -0
  21. package/src/plugins/autoreload/{dev_sse/client → client}/reload.js +29 -10
  22. package/src/plugins/autoreload/{dev_sse/client → client}/url_helpers.js +0 -0
  23. package/src/plugins/autoreload/jsenv_plugin_autoreload.js +4 -4
  24. package/src/plugins/autoreload/{dev_sse/jsenv_plugin_dev_sse_client.js → jsenv_plugin_autoreload_client.js} +8 -8
  25. package/src/plugins/autoreload/jsenv_plugin_autoreload_server.js +196 -0
  26. package/src/{dev/plugins → plugins}/explorer/client/explorer.html +0 -0
  27. package/src/{dev/plugins → plugins}/explorer/client/jsenv.png +0 -0
  28. package/src/{dev/plugins → plugins}/explorer/jsenv_plugin_explorer.js +1 -3
  29. package/src/plugins/html_supervisor/client/{error_in_document.js → error_overlay.js} +73 -17
  30. package/src/plugins/html_supervisor/client/html_supervisor_installer.js +127 -54
  31. package/src/plugins/html_supervisor/client/html_supervisor_setup.js +3 -4
  32. package/src/plugins/html_supervisor/jsenv_plugin_html_supervisor.js +19 -12
  33. package/src/plugins/inline/jsenv_plugin_html_inline_content.js +97 -117
  34. package/src/plugins/node_esm_resolution/jsenv_plugin_node_esm_resolution.js +66 -58
  35. package/src/plugins/plugin_controller.js +102 -67
  36. package/src/plugins/plugins.js +10 -8
  37. package/src/{helpers/event_source/event_source.js → plugins/server_events/client/event_source_connection.js} +102 -31
  38. package/src/plugins/server_events/client/server_events_client.js +17 -0
  39. package/src/plugins/server_events/jsenv_plugin_server_events_client_injection.js +48 -0
  40. package/src/plugins/server_events/server_events_dispatcher.js +69 -0
  41. package/src/{dev/plugins → plugins}/toolbar/client/animation/toolbar_animation.js +0 -0
  42. package/src/{dev/plugins → plugins}/toolbar/client/eventsource/eventsource.css +0 -0
  43. package/src/{dev/plugins → plugins}/toolbar/client/eventsource/toolbar_eventsource.js +0 -0
  44. package/src/{dev/plugins → plugins}/toolbar/client/execution/execution.css +0 -0
  45. package/src/{dev/plugins → plugins}/toolbar/client/execution/toolbar_execution.js +0 -0
  46. package/src/{dev/plugins → plugins}/toolbar/client/focus/focus.css +0 -0
  47. package/src/{dev/plugins → plugins}/toolbar/client/focus/toolbar_focus.js +0 -0
  48. package/src/{dev/plugins → plugins}/toolbar/client/jsenv_logo.svg +0 -0
  49. package/src/{dev/plugins → plugins}/toolbar/client/notification/toolbar_notification.js +0 -0
  50. package/src/{dev/plugins → plugins}/toolbar/client/responsive/overflow_menu.css +0 -0
  51. package/src/{dev/plugins → plugins}/toolbar/client/responsive/toolbar_responsive.js +0 -0
  52. package/src/{dev/plugins → plugins}/toolbar/client/settings/settings.css +0 -0
  53. package/src/{dev/plugins → plugins}/toolbar/client/settings/toolbar_settings.js +0 -0
  54. package/src/{dev/plugins → plugins}/toolbar/client/theme/jsenv_theme.css +0 -0
  55. package/src/{dev/plugins → plugins}/toolbar/client/theme/light_theme.css +0 -0
  56. package/src/{dev/plugins → plugins}/toolbar/client/theme/toolbar_theme.js +0 -0
  57. package/src/{dev/plugins → plugins}/toolbar/client/toolbar.html +0 -0
  58. package/src/{dev/plugins → plugins}/toolbar/client/toolbar_injector.js +0 -0
  59. package/src/{dev/plugins → plugins}/toolbar/client/toolbar_main.css +0 -0
  60. package/src/{dev/plugins → plugins}/toolbar/client/toolbar_main.js +0 -0
  61. package/src/{dev/plugins → plugins}/toolbar/client/tooltip/tooltip.css +0 -0
  62. package/src/{dev/plugins → plugins}/toolbar/client/tooltip/tooltip.js +0 -0
  63. package/src/{dev/plugins → plugins}/toolbar/client/util/animation.js +0 -0
  64. package/src/{dev/plugins → plugins}/toolbar/client/util/dom.js +0 -0
  65. package/src/{dev/plugins → plugins}/toolbar/client/util/fetch_using_xhr.js +0 -0
  66. package/src/{dev/plugins → plugins}/toolbar/client/util/fetching.js +0 -0
  67. package/src/{dev/plugins → plugins}/toolbar/client/util/iframe_to_parent_href.js +0 -0
  68. package/src/{dev/plugins → plugins}/toolbar/client/util/jsenv_logger.js +0 -0
  69. package/src/{dev/plugins → plugins}/toolbar/client/util/preferences.js +0 -0
  70. package/src/{dev/plugins → plugins}/toolbar/client/util/responsive.js +0 -0
  71. package/src/{dev/plugins → plugins}/toolbar/client/util/util.js +0 -0
  72. package/src/{dev/plugins → plugins}/toolbar/client/variant/variant.js +0 -0
  73. package/src/{dev/plugins → plugins}/toolbar/jsenv_plugin_toolbar.js +0 -0
  74. package/src/plugins/transpilation/as_js_classic/jsenv_plugin_as_js_classic_html.js +4 -3
  75. package/src/plugins/transpilation/babel/new_stylesheet/client/new_stylesheet.js +25 -55
  76. package/src/plugins/transpilation/import_assertions/jsenv_plugin_import_assertions.js +44 -24
  77. package/src/plugins/transpilation/jsenv_plugin_transpilation.js +6 -1
  78. package/src/plugins/url_analysis/html/html_urls.js +8 -8
  79. package/src/test/execute_plan.js +36 -54
  80. package/src/test/execute_test_plan.js +2 -2
  81. package/dist/js/event_source_client.js +0 -549
  82. package/src/helpers/event_source/sse_service.js +0 -53
  83. package/src/plugins/autoreload/dev_sse/client/event_source_client.js +0 -193
  84. package/src/plugins/autoreload/dev_sse/jsenv_plugin_dev_sse_server.js +0 -192
@@ -19,16 +19,17 @@ import { jsenvPluginMinification } from "./minification/jsenv_plugin_minificatio
19
19
  import { jsenvPluginImportMetaHot } from "./import_meta_hot/jsenv_plugin_import_meta_hot.js"
20
20
  import { jsenvPluginAutoreload } from "./autoreload/jsenv_plugin_autoreload.js"
21
21
  import { jsenvPluginCacheControl } from "./cache_control/jsenv_plugin_cache_control.js"
22
+ // dev only
23
+ import { jsenvPluginExplorer } from "./explorer/jsenv_plugin_explorer.js"
22
24
 
23
25
  export const getCorePlugins = ({
24
26
  rootDirectoryUrl,
25
- urlGraph,
26
27
  scenario,
27
28
  runtimeCompat,
28
29
 
29
30
  urlAnalysis = {},
30
31
  htmlSupervisor,
31
- nodeEsmResolution,
32
+ nodeEsmResolution = true,
32
33
  fileSystemMagicResolution,
33
34
  directoryReferenceAllowed,
34
35
  transpilation = true,
@@ -38,6 +39,7 @@ export const getCorePlugins = ({
38
39
  clientAutoreload = false,
39
40
  clientFileChangeCallbackList,
40
41
  clientFilesPruneCallbackList,
42
+ explorer,
41
43
  } = {}) => {
42
44
  if (htmlSupervisor === true) {
43
45
  htmlSupervisor = {}
@@ -45,9 +47,13 @@ export const getCorePlugins = ({
45
47
  if (nodeEsmResolution === true) {
46
48
  nodeEsmResolution = {}
47
49
  }
50
+ if (fileSystemMagicResolution === true) {
51
+ fileSystemMagicResolution = {}
52
+ }
48
53
  if (clientAutoreload === true) {
49
54
  clientAutoreload = {}
50
55
  }
56
+
51
57
  return [
52
58
  jsenvPluginUrlAnalysis({ rootDirectoryUrl, ...urlAnalysis }),
53
59
  jsenvPluginTranspilation(transpilation),
@@ -63,12 +69,7 @@ export const getCorePlugins = ({
63
69
  jsenvPluginHttpUrls(),
64
70
  jsenvPluginLeadingSlash(),
65
71
  // before url resolution to handle "js_import_export" resolution
66
- jsenvPluginNodeEsmResolution({
67
- rootDirectoryUrl,
68
- urlGraph,
69
- runtimeCompat,
70
- ...nodeEsmResolution,
71
- }),
72
+ jsenvPluginNodeEsmResolution(nodeEsmResolution),
72
73
  jsenvPluginUrlResolution(),
73
74
  jsenvPluginUrlVersion(),
74
75
  jsenvPluginCommonJsGlobals(),
@@ -90,5 +91,6 @@ export const getCorePlugins = ({
90
91
  ]
91
92
  : []),
92
93
  jsenvPluginCacheControl(),
94
+ ...(explorer ? [jsenvPluginExplorer(explorer)] : []),
93
95
  ]
94
96
  }
@@ -1,5 +1,3 @@
1
- /* eslint-env browser */
2
-
3
1
  const STATUSES = {
4
2
  CONNECTING: "connecting",
5
3
  CONNECTED: "connected",
@@ -8,7 +6,12 @@ const STATUSES = {
8
6
 
9
7
  export const createEventSourceConnection = (
10
8
  eventSourceUrl,
11
- { retryMaxAttempt = Infinity, retryAllocatedMs = Infinity, lastEventId } = {},
9
+ {
10
+ retryMaxAttempt = Infinity,
11
+ retryAllocatedMs = Infinity,
12
+ lastEventId,
13
+ useEventsToManageConnection = true,
14
+ } = {},
12
15
  ) => {
13
16
  const { EventSource } = window
14
17
  if (typeof EventSource !== "function") {
@@ -16,25 +19,85 @@ export const createEventSourceConnection = (
16
19
  }
17
20
 
18
21
  let eventSource
19
- const events = {}
22
+ const listenersMap = new Map()
23
+ const callbacksMap = new Map()
20
24
  const eventSourceOrigin = new URL(eventSourceUrl).origin
21
- const addEventCallbacks = (eventCallbacks) => {
22
- Object.keys(eventCallbacks).forEach((eventName) => {
23
- const eventCallback = eventCallbacks[eventName]
24
- events[eventName] = (e) => {
25
- if (e.origin === eventSourceOrigin) {
26
- if (e.lastEventId) {
27
- lastEventId = e.lastEventId
25
+ const addEventCallbacks = (namedCallbacks) => {
26
+ let listenersMapSize = listenersMap.size
27
+ Object.keys(namedCallbacks).forEach((eventName) => {
28
+ const callback = namedCallbacks[eventName]
29
+ const existingCallbacks = callbacksMap.get(eventName)
30
+ let callbacks
31
+ if (existingCallbacks) {
32
+ callbacks = existingCallbacks
33
+ } else {
34
+ callbacks = []
35
+ callbacksMap.set(eventName, callbacks)
36
+ }
37
+ if (callbacks.length === 0) {
38
+ const eventListener = (e) => {
39
+ if (e.origin === eventSourceOrigin) {
40
+ if (e.lastEventId) {
41
+ lastEventId = e.lastEventId
42
+ }
43
+ callbacks.forEach((eventCallback) => {
44
+ eventCallback(e)
45
+ })
28
46
  }
29
- eventCallback(e)
47
+ }
48
+ listenersMap.set(eventName, eventListener)
49
+ if (eventSource) {
50
+ eventSource.addEventListener(eventName, eventListener)
30
51
  }
31
52
  }
32
- if (eventSource) {
33
- eventSource.addEventListener(eventName, events[eventName])
34
- }
53
+ callbacks.push(callback)
35
54
  })
55
+ if (
56
+ useEventsToManageConnection &&
57
+ listenersMapSize === 0 &&
58
+ listenersMap.size > 0 &&
59
+ status.value !== STATUSES.CONNECTING &&
60
+ status.value !== STATUSES.CONNECTED
61
+ ) {
62
+ _connect()
63
+ }
64
+
65
+ let removed = false
66
+ return () => {
67
+ if (removed) return
68
+ removed = true
69
+ listenersMapSize = listenersMap.size
70
+ Object.keys(namedCallbacks).forEach((eventName) => {
71
+ const callback = namedCallbacks[eventName]
72
+ const callbacks = callbacksMap.get(eventName)
73
+ if (callbacks) {
74
+ const index = callbacks.indexOf(callback)
75
+ if (index > -1) {
76
+ callbacks.splice(index, 1)
77
+ if (callbacks.length === 0) {
78
+ const listener = listenersMap.get(eventName)
79
+ if (listener) {
80
+ listenersMap.delete(listener)
81
+ if (eventSource) {
82
+ eventSource.removeEventListener(eventName, listener)
83
+ }
84
+ }
85
+ }
86
+ }
87
+ }
88
+ })
89
+ namedCallbacks = null // allow garbage collect
90
+ if (
91
+ useEventsToManageConnection &&
92
+ listenersMapSize > 0 &&
93
+ listenersMap.size === 0 &&
94
+ (status.value === STATUSES.CONNECTING ||
95
+ status.value === STATUSES.CONNECTED)
96
+ ) {
97
+ _disconnect()
98
+ }
99
+ }
36
100
  }
37
- addEventCallbacks(events)
38
101
 
39
102
  const status = {
40
103
  value: "default",
@@ -50,6 +113,12 @@ export const createEventSourceConnection = (
50
113
  let _disconnect = () => {}
51
114
 
52
115
  const attemptConnection = (url) => {
116
+ if (
117
+ status.value === STATUSES.CONNECTING ||
118
+ status.value === STATUSES.CONNECTED
119
+ ) {
120
+ return
121
+ }
53
122
  eventSource = new EventSource(url, {
54
123
  withCredentials: true,
55
124
  })
@@ -63,11 +132,13 @@ export const createEventSourceConnection = (
63
132
  )
64
133
  return
65
134
  }
66
- eventSource.onerror = undefined
67
- eventSource.close()
68
- Object.keys(events).forEach((eventName) => {
69
- eventSource.removeEventListener(eventName, events[eventName])
70
- })
135
+ if (eventSource) {
136
+ eventSource.onerror = undefined
137
+ eventSource.close()
138
+ listenersMap.forEach((listener, eventName) => {
139
+ eventSource.removeEventListener(eventName, listener)
140
+ })
141
+ }
71
142
  eventSource = null
72
143
  status.goTo(STATUSES.DISCONNECTED)
73
144
  }
@@ -107,22 +178,20 @@ export const createEventSourceConnection = (
107
178
  eventSource.onopen = () => {
108
179
  status.goTo(STATUSES.CONNECTED)
109
180
  }
110
- Object.keys(events).forEach((eventName) => {
111
- eventSource.addEventListener(eventName, events[eventName])
181
+ listenersMap.forEach((listener, eventName) => {
182
+ eventSource.addEventListener(eventName, listener)
112
183
  })
113
- if (!events.hasOwnProperty("welcome")) {
114
- eventSource.addEventListener("welcome", (e) => {
115
- if (e.origin === eventSourceOrigin && e.lastEventId) {
116
- lastEventId = e.lastEventId
117
- }
184
+ if (!listenersMap.has("welcome")) {
185
+ addEventCallbacks({
186
+ welcome: () => {}, // to update lastEventId
118
187
  })
119
188
  }
120
189
  status.goTo(STATUSES.CONNECTING)
121
190
  }
122
191
 
123
- let connect = () => {
192
+ let _connect = () => {
124
193
  attemptConnection(eventSourceUrl)
125
- connect = () => {
194
+ _connect = () => {
126
195
  attemptConnection(
127
196
  lastEventId
128
197
  ? addLastEventIdIntoUrlSearchParams(eventSourceUrl, lastEventId)
@@ -143,11 +212,13 @@ export const createEventSourceConnection = (
143
212
  const destroy = () => {
144
213
  removePageUnloadListener()
145
214
  _disconnect()
215
+ listenersMap.clear()
216
+ callbacksMap.clear()
146
217
  }
147
218
 
148
219
  return {
149
220
  status,
150
- connect,
221
+ connect: () => _connect(),
151
222
  addEventCallbacks,
152
223
  disconnect: () => _disconnect(),
153
224
  destroy,
@@ -0,0 +1,17 @@
1
+ import { createEventSourceConnection } from "./event_source_connection.js"
2
+
3
+ const eventsourceConnection = createEventSourceConnection(
4
+ document.location.href,
5
+ {
6
+ retryMaxAttempt: Infinity,
7
+ retryAllocatedMs: 20 * 1000,
8
+ },
9
+ )
10
+ const { status, connect, addEventCallbacks, disconnect } = eventsourceConnection
11
+ window.__server_events__ = {
12
+ addEventCallbacks,
13
+ status,
14
+ connect,
15
+ disconnect,
16
+ }
17
+ connect()
@@ -0,0 +1,48 @@
1
+ /*
2
+ * This plugin is very special because it is here
3
+ * to provide "serverEvents" used by other plugins
4
+ */
5
+
6
+ import {
7
+ parseHtmlString,
8
+ stringifyHtmlAst,
9
+ injectScriptNodeAsEarlyAsPossible,
10
+ createHtmlNode,
11
+ } from "@jsenv/ast"
12
+
13
+ const serverEventsClientFileUrl = new URL(
14
+ "./client/server_events_client.js",
15
+ import.meta.url,
16
+ ).href
17
+
18
+ export const jsenvPluginServerEventsClientInjection = () => {
19
+ return {
20
+ name: "jsenv:server_events_client_injection",
21
+ appliesDuring: "*",
22
+ transformUrlContent: {
23
+ html: (htmlUrlInfo, context) => {
24
+ const htmlAst = parseHtmlString(htmlUrlInfo.content)
25
+ const [serverEventsClientFileReference] = context.referenceUtils.inject(
26
+ {
27
+ type: "script_src",
28
+ expectedType: "js_module",
29
+ specifier: serverEventsClientFileUrl,
30
+ },
31
+ )
32
+ injectScriptNodeAsEarlyAsPossible(
33
+ htmlAst,
34
+ createHtmlNode({
35
+ "tagName": "script",
36
+ "type": "module",
37
+ "src": serverEventsClientFileReference.generatedSpecifier,
38
+ "injected-by": "jsenv:server_events",
39
+ }),
40
+ )
41
+ const htmlModified = stringifyHtmlAst(htmlAst)
42
+ return {
43
+ content: htmlModified,
44
+ }
45
+ },
46
+ },
47
+ }
48
+ }
@@ -0,0 +1,69 @@
1
+ import { createSSERoom } from "@jsenv/server"
2
+ import { createCallbackListNotifiedOnce } from "@jsenv/abort"
3
+
4
+ export const createServerEventsDispatcher = () => {
5
+ const destroyCallbackList = createCallbackListNotifiedOnce()
6
+ const rooms = []
7
+ const sseRoomLimit = 100
8
+
9
+ destroyCallbackList.add(() => {
10
+ rooms.forEach((room) => {
11
+ room.close()
12
+ })
13
+ })
14
+
15
+ return {
16
+ addRoom: (request) => {
17
+ const existingRoom = rooms.find(
18
+ (roomCandidate) =>
19
+ roomCandidate.request.ressource === request.ressource,
20
+ )
21
+ if (existingRoom) {
22
+ return existingRoom
23
+ }
24
+ const room = createSSERoom({
25
+ retryDuration: 2000,
26
+ historyLength: 100,
27
+ welcomeEventEnabled: true,
28
+ effect: () => {
29
+ rooms.push(room)
30
+ if (rooms.length >= sseRoomLimit) {
31
+ const firstRoom = rooms.shift()
32
+ firstRoom.close()
33
+ }
34
+ return () => {
35
+ // when the last client leaves the room it is closed and removed from the list
36
+ room.close()
37
+ const index = rooms.indexOf(room)
38
+ if (index > -1) {
39
+ rooms.splice(index, 1)
40
+ }
41
+ }
42
+ },
43
+ })
44
+ room.request = request
45
+ return room
46
+ },
47
+ dispatch: ({ type, data }) => {
48
+ rooms.forEach((room) =>
49
+ room.sendEventToAllClients({
50
+ type,
51
+ data: JSON.stringify(data),
52
+ }),
53
+ )
54
+ },
55
+ dispatchToRoomsMatching: ({ type, data }, predicate) => {
56
+ rooms.forEach((room) => {
57
+ if (predicate(room)) {
58
+ room.sendEventToAllClients({
59
+ type,
60
+ data: JSON.stringify(data),
61
+ })
62
+ }
63
+ })
64
+ },
65
+ destroy: () => {
66
+ destroyCallbackList.notify()
67
+ },
68
+ }
69
+ }
@@ -11,6 +11,7 @@ import {
11
11
  getHtmlNodePosition,
12
12
  setHtmlNodeAttributes,
13
13
  setHtmlNodeText,
14
+ analyzeScriptNode,
14
15
  injectScriptNodeAsEarlyAsPossible,
15
16
  createHtmlNode,
16
17
  } from "@jsenv/ast"
@@ -49,12 +50,12 @@ export const jsenvPluginAsJsClassicHtml = ({
49
50
  }
50
51
  }
51
52
  const visitScriptNodes = (node) => {
52
- const type = getHtmlNodeAttribute(node, "type")
53
- if (type === "module") {
53
+ const { type } = analyzeScriptNode(node)
54
+ if (type === "js_module") {
54
55
  moduleScriptNodes.push(node)
55
56
  return
56
57
  }
57
- if (type === undefined || type === "text/javascript") {
58
+ if (type === "js_classic") {
58
59
  classicScriptNodes.push(node)
59
60
  return
60
61
  }
@@ -1,6 +1,7 @@
1
1
  /* eslint-disable */
2
- // copy paste to help finding the file (npm and node modules would make it complex)
2
+ // construct-style-sheets-polyfill@3.1.0
3
3
  // to keep in sync with https://github.com/calebdwilliams/construct-style-sheets
4
+ // copy pasted into jsenv codebase to inject this code with more ease
4
5
  ;(function () {
5
6
  "use strict"
6
7
 
@@ -15,29 +16,6 @@
15
16
  var defineProperty = Object.defineProperty
16
17
  var forEach = Array.prototype.forEach
17
18
 
18
- var hasBrokenRules = (function () {
19
- var style = bootstrapper.createElement("style")
20
- style.textContent = '.x{content:"y"}'
21
- bootstrapper.body.appendChild(style)
22
- return style.sheet.cssRules[0].style.content !== '"y"'
23
- })()
24
- var brokenRulePatterns = [/content:\s*["']/gm]
25
- function fixBrokenRules(content) {
26
- return brokenRulePatterns.reduce(function (acc, pattern) {
27
- return acc.replace(pattern, "$&%%%")
28
- }, content)
29
- }
30
- var placeholderPatterns = [/(content:\s*["'])%%%/gm]
31
- var getCssText = hasBrokenRules
32
- ? function (rule) {
33
- return placeholderPatterns.reduce(function (acc, pattern) {
34
- return acc.replace(pattern, "$1")
35
- }, rule.cssText)
36
- }
37
- : function (rule) {
38
- return rule.cssText
39
- }
40
-
41
19
  var importPattern = /@import.+?;?$/gm
42
20
  function rejectImports(contents) {
43
21
  var _contents = contents.replace(importPattern, "")
@@ -48,16 +26,6 @@
48
26
  }
49
27
  return _contents.trim()
50
28
  }
51
- function clearRules(sheet) {
52
- for (var i = 0; i < sheet.cssRules.length; i++) {
53
- sheet.deleteRule(0)
54
- }
55
- }
56
- function insertAllRules(from, to) {
57
- forEach.call(from.cssRules, function (rule, i) {
58
- to.insertRule(getCssText(rule), i)
59
- })
60
- }
61
29
  function isElementConnected(element) {
62
30
  return "isConnected" in element
63
31
  ? element.isConnected
@@ -111,9 +79,10 @@
111
79
  ? nonConstructedProto.isPrototypeOf(instance)
112
80
  : false
113
81
  }
114
- var $basicStyleSheet = new WeakMap()
82
+ var $basicStyleElement = new WeakMap()
115
83
  var $locations = new WeakMap()
116
84
  var $adoptersByLocation = new WeakMap()
85
+ var $appliedMethods = new WeakMap()
117
86
  function addAdopterLocation(sheet, location) {
118
87
  var adopter = document.createElement("style")
119
88
  $adoptersByLocation.get(sheet).set(location, adopter)
@@ -134,12 +103,14 @@
134
103
  }
135
104
  function restyleAdopter(sheet, adopter) {
136
105
  requestAnimationFrame(function () {
137
- clearRules(adopter.sheet)
138
- insertAllRules($basicStyleSheet.get(sheet), adopter.sheet)
106
+ adopter.textContent = $basicStyleElement.get(sheet).textContent
107
+ $appliedMethods.get(sheet).forEach(function (command) {
108
+ return adopter.sheet[command.method].apply(adopter.sheet, command.args)
109
+ })
139
110
  })
140
111
  }
141
112
  function checkInvocationCorrectness(self) {
142
- if (!$basicStyleSheet.has(self)) {
113
+ if (!$basicStyleElement.has(self)) {
143
114
  throw new TypeError("Illegal invocation")
144
115
  }
145
116
  }
@@ -147,9 +118,10 @@
147
118
  var self = this
148
119
  var style = document.createElement("style")
149
120
  bootstrapper.body.appendChild(style)
150
- $basicStyleSheet.set(self, style.sheet)
121
+ $basicStyleElement.set(self, style)
151
122
  $locations.set(self, [])
152
123
  $adoptersByLocation.set(self, new WeakMap())
124
+ $appliedMethods.set(self, [])
153
125
  }
154
126
  var proto$1 = ConstructedStyleSheet.prototype
155
127
  proto$1.replace = function replace(contents) {
@@ -164,11 +136,8 @@
164
136
  checkInvocationCorrectness(this)
165
137
  if (typeof contents === "string") {
166
138
  var self_1 = this
167
- var style = $basicStyleSheet.get(self_1).ownerNode
168
- style.textContent = hasBrokenRules
169
- ? fixBrokenRules(rejectImports(contents))
170
- : rejectImports(contents)
171
- $basicStyleSheet.set(self_1, style.sheet)
139
+ $basicStyleElement.get(self_1).textContent = rejectImports(contents)
140
+ $appliedMethods.set(self_1, [])
172
141
  $locations.get(self_1).forEach(function (location) {
173
142
  if (location.isConnected()) {
174
143
  restyleAdopter(self_1, getAdopterByLocation(self_1, location))
@@ -181,7 +150,15 @@
181
150
  enumerable: true,
182
151
  get: function cssRules() {
183
152
  checkInvocationCorrectness(this)
184
- return $basicStyleSheet.get(this).cssRules
153
+ return $basicStyleElement.get(this).sheet.cssRules
154
+ },
155
+ })
156
+ defineProperty(proto$1, "media", {
157
+ configurable: true,
158
+ enumerable: true,
159
+ get: function media() {
160
+ checkInvocationCorrectness(this)
161
+ return $basicStyleElement.get(this).sheet.media
185
162
  },
186
163
  })
187
164
  cssStyleSheetMethods.forEach(function (method) {
@@ -189,22 +166,15 @@
189
166
  var self = this
190
167
  checkInvocationCorrectness(self)
191
168
  var args = arguments
169
+ $appliedMethods.get(self).push({ method: method, args: args })
192
170
  $locations.get(self).forEach(function (location) {
193
171
  if (location.isConnected()) {
194
172
  var sheet = getAdopterByLocation(self, location).sheet
195
173
  sheet[method].apply(sheet, args)
196
174
  }
197
175
  })
198
- if (hasBrokenRules) {
199
- if (method === "insertRule") {
200
- args[0] = fixBrokenRules(args[0])
201
- }
202
- if (method === "addRule") {
203
- args[1] = fixBrokenRules(args[1])
204
- }
205
- }
206
- var basic = $basicStyleSheet.get(self)
207
- return basic[method].apply(basic, args)
176
+ var basicSheet = $basicStyleElement.get(self).sheet
177
+ return basicSheet[method].apply(basicSheet, args)
208
178
  }
209
179
  })
210
180
  defineProperty(ConstructedStyleSheet, Symbol.hasInstance, {