@vizhub/runtime 0.0.1 → 0.0.3

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 (120) hide show
  1. package/dist/assets/setupV3Runtime-BVr5lyKp.js +240 -0
  2. package/dist/computeSrcDoc.d.ts +13 -0
  3. package/dist/computeSrcDoc.d.ts.map +1 -0
  4. package/{src/index.ts → dist/index.d.ts} +1 -0
  5. package/dist/index.d.ts.map +1 -0
  6. package/{src/v2Runtime/bundle/rollup.browser.js → dist/index.js} +9286 -12810
  7. package/dist/useRuntime.d.ts +13 -0
  8. package/dist/useRuntime.d.ts.map +1 -0
  9. package/dist/v2Runtime/bundle/bubleJSXOnly.d.ts +5 -0
  10. package/dist/v2Runtime/bundle/bubleJSXOnly.d.ts.map +1 -0
  11. package/dist/v2Runtime/bundle/packageJson.d.ts +10 -0
  12. package/dist/v2Runtime/bundle/packageJson.d.ts.map +1 -0
  13. package/dist/v2Runtime/computeSrcDocV2.d.ts +3 -0
  14. package/dist/v2Runtime/computeSrcDocV2.d.ts.map +1 -0
  15. package/dist/v2Runtime/computeSrcDocV2.test.d.ts +2 -0
  16. package/dist/v2Runtime/computeSrcDocV2.test.d.ts.map +1 -0
  17. package/dist/v2Runtime/getComputedIndexHtml.d.ts +4 -0
  18. package/dist/v2Runtime/getComputedIndexHtml.d.ts.map +1 -0
  19. package/dist/v2Runtime/getComputedIndexHtml.test.d.ts +2 -0
  20. package/dist/v2Runtime/getComputedIndexHtml.test.d.ts.map +1 -0
  21. package/dist/v2Runtime/getText.d.ts +3 -0
  22. package/dist/v2Runtime/getText.d.ts.map +1 -0
  23. package/dist/v2Runtime/transformFiles.d.ts +3 -0
  24. package/dist/v2Runtime/transformFiles.d.ts.map +1 -0
  25. package/dist/v2Runtime/v3FilesToV2Files.d.ts +3 -0
  26. package/dist/v2Runtime/v3FilesToV2Files.d.ts.map +1 -0
  27. package/dist/v2Runtime/v3FilesToV2Files.test.d.ts +2 -0
  28. package/dist/v2Runtime/v3FilesToV2Files.test.d.ts.map +1 -0
  29. package/dist/v3Runtime/build.d.ts +14 -0
  30. package/dist/v3Runtime/build.d.ts.map +1 -0
  31. package/dist/v3Runtime/build.test.d.ts +2 -0
  32. package/dist/v3Runtime/build.test.d.ts.map +1 -0
  33. package/dist/v3Runtime/cleanRollupErrorMessage.d.ts +5 -0
  34. package/dist/v3Runtime/cleanRollupErrorMessage.d.ts.map +1 -0
  35. package/dist/v3Runtime/computeSrcDocV3.d.ts +7 -0
  36. package/dist/v3Runtime/computeSrcDocV3.d.ts.map +1 -0
  37. package/dist/v3Runtime/extractVizImport.d.ts +7 -0
  38. package/dist/v3Runtime/extractVizImport.d.ts.map +1 -0
  39. package/dist/v3Runtime/extractVizImport.test.d.ts +2 -0
  40. package/dist/v3Runtime/extractVizImport.test.d.ts.map +1 -0
  41. package/dist/v3Runtime/generateRollupErrorMessage.d.ts +5 -0
  42. package/dist/v3Runtime/generateRollupErrorMessage.d.ts.map +1 -0
  43. package/dist/v3Runtime/importFromViz.d.ts +1 -0
  44. package/dist/v3Runtime/importFromViz.d.ts.map +1 -0
  45. package/{src/v3Runtime/index.ts → dist/v3Runtime/index.d.ts} +1 -0
  46. package/dist/v3Runtime/index.d.ts.map +1 -0
  47. package/dist/v3Runtime/parseId.d.ts +7 -0
  48. package/dist/v3Runtime/parseId.d.ts.map +1 -0
  49. package/dist/v3Runtime/setupV3Runtime.d.ts +15 -0
  50. package/dist/v3Runtime/setupV3Runtime.d.ts.map +1 -0
  51. package/dist/v3Runtime/transformDSV/index.d.ts +3 -0
  52. package/dist/v3Runtime/transformDSV/index.d.ts.map +1 -0
  53. package/dist/v3Runtime/transformSvelte.d.ts +6 -0
  54. package/dist/v3Runtime/transformSvelte.d.ts.map +1 -0
  55. package/dist/v3Runtime/types.d.ts +65 -0
  56. package/dist/v3Runtime/types.d.ts.map +1 -0
  57. package/dist/v3Runtime/urlLoad.d.ts +1 -0
  58. package/dist/v3Runtime/urlLoad.d.ts.map +1 -0
  59. package/dist/v3Runtime/virtual.d.ts +1 -0
  60. package/dist/v3Runtime/virtual.d.ts.map +1 -0
  61. package/dist/v3Runtime/vizCache.d.ts +11 -0
  62. package/dist/v3Runtime/vizCache.d.ts.map +1 -0
  63. package/dist/v3Runtime/vizCache.test.d.ts +2 -0
  64. package/dist/v3Runtime/vizCache.test.d.ts.map +1 -0
  65. package/dist/v3Runtime/vizLoad.d.ts +8 -0
  66. package/dist/v3Runtime/vizLoad.d.ts.map +1 -0
  67. package/dist/v3Runtime/vizLoadSvelte.d.ts +1 -0
  68. package/dist/v3Runtime/vizLoadSvelte.d.ts.map +1 -0
  69. package/dist/v3Runtime/vizResolve.d.ts +10 -0
  70. package/dist/v3Runtime/vizResolve.d.ts.map +1 -0
  71. package/dist/v3Runtime/worker.d.ts +2 -0
  72. package/dist/v3Runtime/worker.d.ts.map +1 -0
  73. package/package.json +13 -6
  74. package/src/computeSrcDoc.ts +0 -68
  75. package/src/useRuntime.ts +0 -394
  76. package/src/v2Runtime/bundle/bubleJSXOnly.ts +0 -34
  77. package/src/v2Runtime/bundle/getLibraries.js +0 -31
  78. package/src/v2Runtime/bundle/hypothetical.js +0 -232
  79. package/src/v2Runtime/bundle/index.js +0 -88
  80. package/src/v2Runtime/bundle/packageJson.ts +0 -49
  81. package/src/v2Runtime/bundle.test.js +0 -151
  82. package/src/v2Runtime/computeSrcDocV2.test.ts +0 -163
  83. package/src/v2Runtime/computeSrcDocV2.ts +0 -34
  84. package/src/v2Runtime/getComputedIndexHtml.test.ts +0 -33
  85. package/src/v2Runtime/getComputedIndexHtml.ts +0 -106
  86. package/src/v2Runtime/getText.ts +0 -19
  87. package/src/v2Runtime/magicSandbox.js +0 -291
  88. package/src/v2Runtime/packageJson.js +0 -42
  89. package/src/v2Runtime/transformFiles.test.js +0 -18
  90. package/src/v2Runtime/transformFiles.ts +0 -15
  91. package/src/v2Runtime/v3FilesToV2Files.test.ts +0 -20
  92. package/src/v2Runtime/v3FilesToV2Files.ts +0 -14
  93. package/src/v3Runtime/build.test.ts +0 -474
  94. package/src/v3Runtime/build.ts +0 -270
  95. package/src/v3Runtime/cleanRollupErrorMessage.ts +0 -15
  96. package/src/v3Runtime/computeSrcDocV3.ts +0 -151
  97. package/src/v3Runtime/extractVizImport.test.ts +0 -41
  98. package/src/v3Runtime/extractVizImport.ts +0 -34
  99. package/src/v3Runtime/generateRollupErrorMessage.ts +0 -84
  100. package/src/v3Runtime/importFromViz.ts +0 -36
  101. package/src/v3Runtime/parseId.ts +0 -14
  102. package/src/v3Runtime/setupV3Runtime.ts +0 -478
  103. package/src/v3Runtime/transformDSV/d3-dsv-custom-build/bundle-modified-src.js +0 -121
  104. package/src/v3Runtime/transformDSV/d3-dsv-custom-build/bundle-modified.js +0 -121
  105. package/src/v3Runtime/transformDSV/d3-dsv-custom-build/bundle.js +0 -239
  106. package/src/v3Runtime/transformDSV/d3-dsv-custom-build/index.js +0 -1
  107. package/src/v3Runtime/transformDSV/d3-dsv-custom-build/package-lock.json +0 -475
  108. package/src/v3Runtime/transformDSV/d3-dsv-custom-build/package.json +0 -19
  109. package/src/v3Runtime/transformDSV/d3-dsv-custom-build/rollup.config.js +0 -9
  110. package/src/v3Runtime/transformDSV/index.ts +0 -71
  111. package/src/v3Runtime/transformSvelte.ts +0 -111
  112. package/src/v3Runtime/types.ts +0 -158
  113. package/src/v3Runtime/urlLoad.ts +0 -33
  114. package/src/v3Runtime/virtual.ts +0 -27
  115. package/src/v3Runtime/vizCache.test.ts +0 -126
  116. package/src/v3Runtime/vizCache.ts +0 -60
  117. package/src/v3Runtime/vizLoad.ts +0 -68
  118. package/src/v3Runtime/vizLoadSvelte.ts +0 -46
  119. package/src/v3Runtime/vizResolve.ts +0 -100
  120. package/src/v3Runtime/worker.ts +0 -231
package/src/useRuntime.ts DELETED
@@ -1,394 +0,0 @@
1
- import {
2
- RefObject,
3
- useEffect,
4
- useMemo,
5
- useRef,
6
- useCallback,
7
- } from 'react';
8
- import {
9
- Content,
10
- FileId,
11
- SlugKey,
12
- VizId,
13
- generateFileId,
14
- getRuntimeVersion,
15
- } from 'entities';
16
- import { V3Runtime } from './v3Runtime/setupV3Runtime';
17
-
18
- const debug = false;
19
-
20
- // Sets up either the v2 or v3 runtime environment.
21
- // Meant to support dynamic switching between the two.
22
- export const useRuntime = ({
23
- content,
24
- iframeRef,
25
- srcdocErrorMessage,
26
- setSrcdocErrorMessage,
27
- vizCacheContents,
28
- isVisual,
29
- slugResolutionCache,
30
- submitContentOperation,
31
- }: {
32
- content: Content;
33
- iframeRef: RefObject<HTMLIFrameElement>;
34
- srcdocErrorMessage: string | null;
35
- setSrcdocErrorMessage: (error: string | null) => void;
36
- vizCacheContents: Record<string, Content>;
37
-
38
- // If this is false, there is no iframeRef.current.
39
- isVisual: boolean;
40
- slugResolutionCache: Record<SlugKey, VizId>;
41
- submitContentOperation: (
42
- next: (content: Content) => Content,
43
- ) => void;
44
- }) => {
45
- // This ref is used to skip the first mount.
46
- const initialMount = useRef(true);
47
-
48
- // `runtimeVersion` is used to determine which runtime
49
- // to use. It's either 2 or 3.
50
- const runtimeVersion: number = useMemo(
51
- () => getRuntimeVersion(content),
52
- [content],
53
- );
54
-
55
- const v3RuntimeRef = useRef<V3Runtime | null>(null);
56
-
57
- const getV3Runtime = useCallback(async () => {
58
- if (v3RuntimeRef.current === null) {
59
- // throw new Error('v3Runtime.current is null');
60
- // Poll for this to be defined.
61
- // const interval = setInterval(() => {
62
- // if (debug) {
63
- // console.log('polling for v3Runtime.current...');
64
- // }
65
- // if (v3RuntimeRef.current !== null) {
66
- // clearInterval(interval);
67
- // return v3RuntimeRef.current;
68
- // }
69
- // }, 100);
70
- return new Promise<V3Runtime>((resolve) => {
71
- const interval = setInterval(() => {
72
- if (debug) {
73
- console.log('polling for v3Runtime.current...');
74
- }
75
- if (v3RuntimeRef.current !== null) {
76
- clearInterval(interval);
77
- resolve(v3RuntimeRef.current);
78
- }
79
- }, 100);
80
- });
81
- }
82
- return v3RuntimeRef.current;
83
- }, []);
84
-
85
- const vizCacheContentsRef = useRef(vizCacheContents);
86
-
87
- useEffect(() => {
88
- vizCacheContentsRef.current = vizCacheContents;
89
- }, [vizCacheContents]);
90
-
91
- // Handles cache misses for viz content,
92
- // when a viz imports from another viz.
93
- const getLatestContent = useCallback(
94
- async (vizId: VizId): Promise<Content> => {
95
- // Sanity check, should never happen.
96
- if (!vizCacheContentsRef.current) {
97
- throw new Error(
98
- 'vizCacheContentsRef.current is null',
99
- );
100
- }
101
-
102
- const content = vizCacheContentsRef.current[vizId];
103
-
104
- // If the viz content for this import is already tracked,
105
- // then return it.
106
- if (content) {
107
- return content;
108
- } else {
109
- // TODO make this happen by:
110
- // * Fetching the viz content from the server
111
- // * Using a new API endpoint that returns the viz content
112
- // * Ingesting the snapshot and incorporating it into the vizCacheContents
113
- throw new Error(
114
- `TODO client-side fetching of newly imported vizzes. Current workaround: refresh the page`,
115
- );
116
- }
117
- },
118
- [],
119
- );
120
-
121
- // Handles cache misses for slug resolution,
122
- // when a viz imports from another viz.
123
- const resolveSlugKey = useCallback(
124
- async (slugKey: SlugKey): Promise<VizId> => {
125
- // Sanity check, should never happen.
126
- if (!slugResolutionCache) {
127
- throw new Error('slugResolutionCache is null');
128
- }
129
-
130
- const vizId = slugResolutionCache[slugKey];
131
-
132
- // If the viz ID for this slug is already tracked,
133
- // then return it.
134
- if (vizId) {
135
- return vizId;
136
- } else {
137
- // TODO make this happen by:
138
- // * Resolving the slug from the server
139
- // * Using a new API endpoint that returns the viz id for a slugKey
140
- throw new Error(
141
- `TODO client-side resolution of newly imported vizzes. Current workaround: refresh the page`,
142
- );
143
- }
144
- },
145
- [],
146
- );
147
-
148
- // Set up the v3 runtime.
149
- // TODO QA the following:
150
- // * Adding and removing index.js
151
- // * Adding and removing index.html
152
- // * Switching between versions of the runtime
153
- useEffect(() => {
154
- // If the viz is not visual (README.md only), then
155
- // we don't need to set up the v3 runtime.
156
- if (isVisual === false) {
157
- return;
158
- }
159
- if (runtimeVersion === 3) {
160
- // Load the v3 runtime.
161
- import('./v3Runtime/setupV3Runtime').then(
162
- ({ setupV3Runtime }) => {
163
- const iframe = iframeRef.current;
164
-
165
- // Should never happen. Added to pacify TypeScript.
166
- if (iframe === null) {
167
- throw new Error('iframe is null');
168
- }
169
-
170
- if (!content.id) {
171
- throw new Error('content.id is not defined');
172
- }
173
-
174
- // Writes the content of a file.
175
- const writeFile = (
176
- name: string,
177
- text: string,
178
- ) => {
179
- submitContentOperation((content: Content) => {
180
- // For new files, generate a fileId.
181
- let fileId: FileId = generateFileId();
182
-
183
- // For existing files, get the fileId.
184
- const { files } = content;
185
- if (files !== undefined) {
186
- const fileIds = Object.keys(files);
187
- const foundFileId = fileIds.find(
188
- (fileId) => files[fileId].name === name,
189
- );
190
- if (foundFileId) {
191
- fileId = foundFileId;
192
- }
193
- }
194
-
195
- return {
196
- ...content,
197
- files: {
198
- ...content.files,
199
- [fileId]: {
200
- name,
201
- text,
202
- },
203
- },
204
- // Trigger a re-run.
205
- isInteracting: true,
206
- };
207
- });
208
-
209
- // Clear the `isInteracting` property.
210
- setTimeout(() => {
211
- // This somewhat cryptic logic
212
- // deletes the `isInteracting` property
213
- // from the document.
214
- submitContentOperation(
215
- ({ isInteracting, ...newDocument }) =>
216
- newDocument,
217
- );
218
- }, 0);
219
- };
220
-
221
- v3RuntimeRef.current = setupV3Runtime({
222
- vizId: content.id,
223
- iframe,
224
- setSrcdocErrorMessage,
225
- getLatestContent,
226
- resolveSlugKey,
227
- writeFile,
228
- });
229
- },
230
- );
231
- }
232
- }, [runtimeVersion, isVisual, submitContentOperation]);
233
-
234
- // Send updates of imported vizzes to the V3 runtime.
235
- const previousVizCacheContents = useRef(vizCacheContents);
236
-
237
- // Track the srcdoc error message, but only check it
238
- // when we are attempting to run (do not re-run when
239
- // the error message changes or is cleared).
240
- const srcdocErrorMessageRef = useRef(srcdocErrorMessage);
241
- useEffect(() => {
242
- srcdocErrorMessageRef.current = srcdocErrorMessage;
243
- }, [srcdocErrorMessage]);
244
-
245
- useEffect(() => {
246
- // Don't crash for v2 runtime!
247
- if (runtimeVersion !== 3) {
248
- return;
249
- }
250
-
251
- // Don't run on first render.
252
- if (initialMount.current === true) {
253
- return;
254
- }
255
-
256
- // Don't run if the viz is not visual (README.md only).
257
- if (isVisual === false) {
258
- return;
259
- }
260
-
261
- // console.log(
262
- // 'TODO update the v3 runtime when imported vizzes change',
263
- // );
264
-
265
- // Find the imported vizzes that have changed.
266
- const changedVizIds = Object.keys(
267
- vizCacheContents,
268
- ).filter((vizId) => {
269
- return (
270
- previousVizCacheContents.current[vizId] !==
271
- vizCacheContents[vizId]
272
- );
273
- });
274
- previousVizCacheContents.current = vizCacheContents;
275
-
276
- if (debug) {
277
- console.log(
278
- '[useRuntime] changedVizIds',
279
- changedVizIds,
280
- );
281
- }
282
-
283
- if (changedVizIds.length === 0) {
284
- return;
285
- }
286
-
287
- // // See if any of the vizzes we import from are interacting.
288
- let isInteracting = false;
289
- for (const vizId of changedVizIds) {
290
- if (vizCacheContents[vizId].isInteracting) {
291
- isInteracting = true;
292
- break;
293
- }
294
- }
295
- if (debug) {
296
- console.log('isInteracting', isInteracting);
297
- }
298
-
299
- const update = async () => {
300
- const v3Runtime = await getV3Runtime();
301
- // Sanity check, should never happen.
302
- if (v3Runtime === null) {
303
- throw new Error('v3Runtime is null');
304
- }
305
- if (isInteracting) {
306
- // console.log('Running the code!');
307
- // console.log(
308
- // 'srcdocErrorMessageRef.current',
309
- // srcdocErrorMessageRef.current,
310
- // );
311
-
312
- // If we are recovering from an error,
313
- // clear the error message, and run the code
314
- // totally fresh by re-computing the srcdoc.
315
- if (srcdocErrorMessageRef.current) {
316
- v3Runtime.resetSrcdoc(changedVizIds);
317
- // try {
318
- // const srcdoc = await computeSrcDocV3(content);
319
- // if (iframeRef.current) {
320
- // iframeRef.current.srcdoc = srcdoc;
321
- // }
322
- // } catch (error) {
323
- // console.error(error);
324
- // setSrcdocErrorMessage(error.message);
325
- // }
326
- } else {
327
- // Re-run the code with hot reloading.
328
- v3Runtime.invalidateVizCache(changedVizIds);
329
- }
330
- }
331
- };
332
- update();
333
- }, [vizCacheContents, runtimeVersion]);
334
-
335
- // Compute V2 updates on the main thread.
336
- useEffect(() => {
337
- // We don't need to execute a "run" on first render,
338
- // because SSR handles the initial run by injecting
339
- // the srcdoc into the page server-side.
340
- if (initialMount.current) {
341
- return;
342
- }
343
-
344
- // The following code only runs after the user
345
- // has edited the code and the files have changed.
346
- if (runtimeVersion === 2) {
347
- // // Debounce the updates.
348
- const v2Run = async () => {
349
- // Clear the console before each run.
350
- console.clear();
351
-
352
- // Clear out the old error.
353
- setSrcdocErrorMessage(null);
354
-
355
- // v2RuntimeWorker.current.postMessage({ content });
356
-
357
- // Set process on global scope so computeSrcDoc doesn't break.
358
- // @ts-ignore
359
- globalThis.process = {};
360
-
361
- // Lazy load computeSrcDoc because it's a large chunk.
362
- const { computeSrcDocV2 } = await import(
363
- './v2Runtime/computeSrcDocV2'
364
- );
365
-
366
- // console.log(computeSrcDoc);
367
- try {
368
- const srcdoc = await computeSrcDocV2(content);
369
- if (iframeRef.current) {
370
- iframeRef.current.srcdoc = srcdoc;
371
- }
372
- } catch (error) {
373
- console.error(error);
374
- setSrcdocErrorMessage(error.message);
375
- }
376
- };
377
- if (content.isInteracting) {
378
- v2Run();
379
- }
380
- }
381
- }, [
382
- content.files,
383
- content.isInteracting,
384
- runtimeVersion,
385
- ]);
386
-
387
- // Track the initial mount.
388
- useEffect(() => {
389
- if (initialMount.current) {
390
- initialMount.current = false;
391
- return;
392
- }
393
- }, []);
394
- };
@@ -1,34 +0,0 @@
1
- // Derived from https://github.com/rollup/rollup-plugin-buble/blob/master/src/index.js
2
- // import { transform } from 'buble-jsx-only';
3
-
4
- import { transform } from 'buble';
5
- // import { createFilter } from 'rollup-pluginutils';
6
-
7
- export default function buble(options) {
8
- if (!options) options = {};
9
- // var filter = createFilter(
10
- // options.include,
11
- // options.exclude,
12
- // );
13
-
14
- if (!options.transforms) options.transforms = {};
15
- options.transforms.modules = false;
16
-
17
- return {
18
- name: 'buble',
19
-
20
- transform: function (code, id) {
21
- // if (!filter(id)) return null;
22
-
23
- try {
24
- return transform(code, options);
25
- } catch (e) {
26
- e.plugin = 'buble';
27
- if (!e.loc) e.loc = {};
28
- e.loc.file = id;
29
- e.frame = e.snippet;
30
- throw e;
31
- }
32
- },
33
- };
34
- }
@@ -1,31 +0,0 @@
1
- import vizhubLibraries from 'vizhub-libraries';
2
- import { getConfiguredLibraries } from './packageJson';
3
-
4
- export const getLibraries = (files) => {
5
- const configuredLibraries = getConfiguredLibraries(files);
6
- const userLibrariesNames = configuredLibraries
7
- ? Object.keys(configuredLibraries)
8
- : [];
9
-
10
- const userLibraries = userLibrariesNames.reduce(
11
- (globals, packageName) => {
12
- // in case if user created settings but not provide global, stub global with vizhub known global name
13
- const globalName =
14
- configuredLibraries[packageName].global ||
15
- vizhubLibraries[packageName];
16
-
17
- if (globalName) {
18
- globals[packageName] = globalName;
19
- } else {
20
- console.warn(
21
- `There is no global name for ${packageName}.\n Please add it to "vizhub.${packageName}.global" section in package.json.`,
22
- );
23
- }
24
-
25
- return globals;
26
- },
27
- {},
28
- );
29
-
30
- return { ...vizhubLibraries, ...userLibraries };
31
- };
@@ -1,232 +0,0 @@
1
- // Temporary file (fingers crossed)
2
- // See https://github.com/Permutatrix/rollup-plugin-hypothetical/pull/9
3
- import path from 'path-posix';
4
-
5
- function isAbsolute(p) {
6
- return path.isAbsolute(p) || /^[A-Za-z]:\//.test(p);
7
- }
8
-
9
- function isExternal(p) {
10
- return !/^(\.?\.?|[A-Za-z]:)\//.test(p);
11
- }
12
-
13
- function absolutify(p, cwd) {
14
- if (cwd) {
15
- return path.join(cwd, p);
16
- } else {
17
- return './' + p;
18
- }
19
- }
20
-
21
- function forEachInObjectOrMap(object, map, callback) {
22
- if (object && map) {
23
- throw Error('Both an Object and a Map were supplied!');
24
- }
25
-
26
- if (map) {
27
- map.forEach(callback);
28
- } else if (object) {
29
- for (var key in object) {
30
- callback(object[key], key);
31
- }
32
- }
33
- // if neither was supplied, do nothing.
34
- }
35
-
36
- export default function rollupPluginHypothetical(options) {
37
- options = options || {};
38
- var files0 = options.files;
39
- var files0AsMap = options.filesMap;
40
- var allowFallthrough = options.allowFallthrough || false;
41
- var allowRelativeExternalFallthrough =
42
- options.allowRelativeExternalFallthrough || false;
43
- var allowExternalFallthrough =
44
- options.allowExternalFallthrough;
45
- if (allowExternalFallthrough === undefined) {
46
- allowExternalFallthrough = true;
47
- }
48
- var leaveIdsAlone = options.leaveIdsAlone || false;
49
- var impliedExtensions = options.impliedExtensions;
50
- if (impliedExtensions === undefined) {
51
- impliedExtensions = ['.js', '/'];
52
- } else {
53
- impliedExtensions = Array.prototype.slice.call(
54
- impliedExtensions,
55
- );
56
- }
57
- var cwd = options.cwd;
58
- if (cwd !== false) {
59
- if (cwd === undefined) {
60
- cwd = process.cwd();
61
- }
62
- cwd = unixStylePath(cwd);
63
- }
64
-
65
- var files = new Map();
66
- if (leaveIdsAlone) {
67
- forEachInObjectOrMap(
68
- files0,
69
- files0AsMap,
70
- function (contents, f) {
71
- files.set(f, contents);
72
- },
73
- );
74
- } else {
75
- forEachInObjectOrMap(
76
- files0,
77
- files0AsMap,
78
- function (contents, f) {
79
- var unixStyleF = unixStylePath(f);
80
- var pathIsExternal = isExternal(unixStyleF);
81
- var p = path.normalize(unixStyleF);
82
- if (pathIsExternal && !isExternal(p)) {
83
- throw Error(
84
- 'Supplied external file path "' +
85
- unixStyleF +
86
- '" normalized to "' +
87
- p +
88
- '"!',
89
- );
90
- }
91
- if (!isAbsolute(p) && !pathIsExternal) {
92
- p = absolutify(p, cwd);
93
- }
94
- files.set(p, contents);
95
- },
96
- );
97
- }
98
-
99
- function basicResolve(importee) {
100
- if (files.has(importee)) {
101
- return importee;
102
- } else if (!allowFallthrough) {
103
- throw Error(dneMessage(importee));
104
- }
105
- }
106
-
107
- var resolveId = leaveIdsAlone
108
- ? basicResolve
109
- : function (importee, importer) {
110
- importee = unixStylePath(importee);
111
-
112
- // the entry file is never external.
113
- var importeeIsExternal =
114
- Boolean(importer) && isExternal(importee);
115
-
116
- var importeeIsRelativeToExternal =
117
- importer &&
118
- !importeeIsExternal &&
119
- isExternal(importer) &&
120
- !isAbsolute(importee);
121
-
122
- if (importeeIsExternal) {
123
- var normalizedImportee = path.normalize(importee);
124
- if (!isExternal(normalizedImportee)) {
125
- throw Error(
126
- 'External import "' +
127
- importee +
128
- '" normalized to "' +
129
- normalizedImportee +
130
- '"!',
131
- );
132
- }
133
- importee = normalizedImportee;
134
- } else if (importeeIsRelativeToExternal) {
135
- var joinedImportee = path.join(
136
- path.dirname(importer),
137
- importee,
138
- );
139
- if (!isExternal(joinedImportee)) {
140
- throw Error(
141
- 'Import "' +
142
- importee +
143
- '" relative to external import "' +
144
- importer +
145
- '" results in "' +
146
- joinedImportee +
147
- '"!',
148
- );
149
- }
150
- importee = joinedImportee;
151
- } else {
152
- if (!isAbsolute(importee) && importer) {
153
- importee = path.join(
154
- path.dirname(importer),
155
- importee,
156
- );
157
- } else {
158
- importee = path.normalize(importee);
159
- }
160
- if (!isAbsolute(importee)) {
161
- importee = absolutify(importee, cwd);
162
- }
163
- }
164
-
165
- if (files.has(importee)) {
166
- return importee;
167
- } else if (impliedExtensions) {
168
- for (
169
- var i = 0, len = impliedExtensions.length;
170
- i < len;
171
- ++i
172
- ) {
173
- var extended = importee + impliedExtensions[i];
174
- if (files.has(extended)) {
175
- return extended;
176
- }
177
- }
178
- }
179
- if (
180
- importeeIsExternal &&
181
- !allowExternalFallthrough
182
- ) {
183
- throw Error(dneMessage(importee));
184
- }
185
- if (
186
- importeeIsRelativeToExternal &&
187
- !allowRelativeExternalFallthrough
188
- ) {
189
- throw Error(dneMessage(importee));
190
- }
191
- if (
192
- !importeeIsExternal &&
193
- !importeeIsRelativeToExternal &&
194
- !allowFallthrough
195
- ) {
196
- throw Error(dneMessage(importee));
197
- }
198
- if (importeeIsRelativeToExternal) {
199
- // we have to resolve this case specially because Rollup won't
200
- // treat it as external if we don't.
201
- // we have to trust that the user has informed Rollup that this import
202
- // is supposed to be external... ugh.
203
- return importee;
204
- }
205
- };
206
-
207
- return {
208
- resolveId: resolveId,
209
- load: function (id) {
210
- if (files.has(id)) {
211
- return files.get(id);
212
- } else {
213
- id = resolveId(id);
214
- return id && files.get(id);
215
- }
216
- },
217
- };
218
- }
219
-
220
- function unixStylePath(p) {
221
- return p.split('\\').join('/');
222
- }
223
-
224
- function dneMessage(id) {
225
- return (
226
- 'Import failed: `' +
227
- id +
228
- '` not found. Double check that the file `' +
229
- id +
230
- '` exists.'
231
- );
232
- }