@vizhub/runtime 0.0.1
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.
- package/README.md +173 -0
- package/package.json +37 -0
- package/src/computeSrcDoc.ts +68 -0
- package/src/index.ts +7 -0
- package/src/useRuntime.ts +394 -0
- package/src/v2Runtime/bundle/bubleJSXOnly.ts +34 -0
- package/src/v2Runtime/bundle/getLibraries.js +31 -0
- package/src/v2Runtime/bundle/hypothetical.js +232 -0
- package/src/v2Runtime/bundle/index.js +88 -0
- package/src/v2Runtime/bundle/packageJson.ts +49 -0
- package/src/v2Runtime/bundle/rollup.browser.js +28414 -0
- package/src/v2Runtime/bundle.test.js +151 -0
- package/src/v2Runtime/computeSrcDocV2.test.ts +163 -0
- package/src/v2Runtime/computeSrcDocV2.ts +34 -0
- package/src/v2Runtime/getComputedIndexHtml.test.ts +33 -0
- package/src/v2Runtime/getComputedIndexHtml.ts +106 -0
- package/src/v2Runtime/getText.ts +19 -0
- package/src/v2Runtime/magicSandbox.js +291 -0
- package/src/v2Runtime/packageJson.js +42 -0
- package/src/v2Runtime/transformFiles.test.js +18 -0
- package/src/v2Runtime/transformFiles.ts +15 -0
- package/src/v2Runtime/v3FilesToV2Files.test.ts +20 -0
- package/src/v2Runtime/v3FilesToV2Files.ts +14 -0
- package/src/v3Runtime/build.test.ts +474 -0
- package/src/v3Runtime/build.ts +270 -0
- package/src/v3Runtime/cleanRollupErrorMessage.ts +15 -0
- package/src/v3Runtime/computeSrcDocV3.ts +151 -0
- package/src/v3Runtime/extractVizImport.test.ts +41 -0
- package/src/v3Runtime/extractVizImport.ts +34 -0
- package/src/v3Runtime/generateRollupErrorMessage.ts +84 -0
- package/src/v3Runtime/importFromViz.ts +36 -0
- package/src/v3Runtime/index.ts +1 -0
- package/src/v3Runtime/parseId.ts +14 -0
- package/src/v3Runtime/setupV3Runtime.ts +478 -0
- package/src/v3Runtime/transformDSV/d3-dsv-custom-build/bundle-modified-src.js +121 -0
- package/src/v3Runtime/transformDSV/d3-dsv-custom-build/bundle-modified.js +121 -0
- package/src/v3Runtime/transformDSV/d3-dsv-custom-build/bundle.js +239 -0
- package/src/v3Runtime/transformDSV/d3-dsv-custom-build/index.js +1 -0
- package/src/v3Runtime/transformDSV/d3-dsv-custom-build/package-lock.json +475 -0
- package/src/v3Runtime/transformDSV/d3-dsv-custom-build/package.json +19 -0
- package/src/v3Runtime/transformDSV/d3-dsv-custom-build/rollup.config.js +9 -0
- package/src/v3Runtime/transformDSV/index.ts +71 -0
- package/src/v3Runtime/transformSvelte.ts +111 -0
- package/src/v3Runtime/types.ts +158 -0
- package/src/v3Runtime/urlLoad.ts +33 -0
- package/src/v3Runtime/virtual.ts +27 -0
- package/src/v3Runtime/vizCache.test.ts +126 -0
- package/src/v3Runtime/vizCache.ts +60 -0
- package/src/v3Runtime/vizLoad.ts +68 -0
- package/src/v3Runtime/vizLoadSvelte.ts +46 -0
- package/src/v3Runtime/vizResolve.ts +100 -0
- package/src/v3Runtime/worker.ts +231 -0
@@ -0,0 +1,34 @@
|
|
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
|
+
}
|
@@ -0,0 +1,31 @@
|
|
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
|
+
};
|
@@ -0,0 +1,232 @@
|
|
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
|
+
}
|
@@ -0,0 +1,88 @@
|
|
1
|
+
import { rollup } from './rollup.browser';
|
2
|
+
import bubleJSXOnly from './bubleJSXOnly';
|
3
|
+
import hypothetical from './hypothetical';
|
4
|
+
import { getLibraries } from './getLibraries';
|
5
|
+
|
6
|
+
// if HTML parser encounter </script> it stops parsing current script
|
7
|
+
// in order to avoid that, </script> should be split into parts.
|
8
|
+
const escapeClosingScriptTag = (code) =>
|
9
|
+
code.split('</script>').join('" + "<" + "/script>" + "');
|
10
|
+
|
11
|
+
const transformFilesToObject = (files) =>
|
12
|
+
files
|
13
|
+
.filter(
|
14
|
+
(file) => file.name.endsWith('.js'),
|
15
|
+
// TODO: add support for other file types
|
16
|
+
// file.name.endsWith('.svelte') ||
|
17
|
+
// file.name.endsWith('.jsx') ||
|
18
|
+
// file.name.endsWith('.ts') ||
|
19
|
+
// file.name.endsWith('.tsx') ||
|
20
|
+
// file.name.endsWith('.vue')
|
21
|
+
)
|
22
|
+
.reduce((accumulator, file) => {
|
23
|
+
accumulator['./' + file.name] = file.text;
|
24
|
+
return accumulator;
|
25
|
+
}, {});
|
26
|
+
|
27
|
+
export const bundle = async (files) => {
|
28
|
+
const libraries = getLibraries(files);
|
29
|
+
|
30
|
+
const outputOptions = {
|
31
|
+
format: 'iife',
|
32
|
+
name: 'bundle',
|
33
|
+
sourcemap: 'inline',
|
34
|
+
globals: libraries,
|
35
|
+
};
|
36
|
+
|
37
|
+
const inputOptions = {
|
38
|
+
input: './index.js',
|
39
|
+
plugins: [
|
40
|
+
hypothetical({
|
41
|
+
files: transformFilesToObject(files),
|
42
|
+
cwd: false,
|
43
|
+
}),
|
44
|
+
bubleJSXOnly({
|
45
|
+
target: {
|
46
|
+
chrome: 71,
|
47
|
+
},
|
48
|
+
}),
|
49
|
+
],
|
50
|
+
external: Object.keys(libraries),
|
51
|
+
// Suppress Rollup warnings
|
52
|
+
onwarn: () => {},
|
53
|
+
};
|
54
|
+
const rollupBundle = await rollup(inputOptions);
|
55
|
+
const { output } =
|
56
|
+
await rollupBundle.generate(outputOptions);
|
57
|
+
|
58
|
+
// Monkey patch magic-string internals
|
59
|
+
// to support characters outside of the Latin1 range, e.g. Cyrillic.
|
60
|
+
//
|
61
|
+
// Related reading:
|
62
|
+
// - https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/btoa#Unicode_strings
|
63
|
+
// - https://github.com/Rich-Harris/magic-string/blob/3466b0230dddc95eb378ed3e0d199e36fbd1f572/src/SourceMap.js#L3
|
64
|
+
//
|
65
|
+
if (output.length !== 1) {
|
66
|
+
throw new Error(
|
67
|
+
'Expected Rollup output length to be 1. This Error is a VizHub bug if it happens.',
|
68
|
+
);
|
69
|
+
}
|
70
|
+
const { code, map } = output[0];
|
71
|
+
|
72
|
+
const escapedCode = escapeClosingScriptTag(code);
|
73
|
+
|
74
|
+
const toString = map.toString.bind(map);
|
75
|
+
map.toString = () =>
|
76
|
+
unescape(encodeURIComponent(toString()));
|
77
|
+
|
78
|
+
// Inspired by https://github.com/rollup/rollup/issues/121
|
79
|
+
const codeWithSourceMap =
|
80
|
+
escapedCode + '\n//# sourceMappingURL=' + map.toUrl();
|
81
|
+
|
82
|
+
return [
|
83
|
+
{
|
84
|
+
name: 'bundle.js',
|
85
|
+
text: codeWithSourceMap,
|
86
|
+
},
|
87
|
+
];
|
88
|
+
};
|
@@ -0,0 +1,49 @@
|
|
1
|
+
import { FilesV2 } from 'entities';
|
2
|
+
|
3
|
+
const EMPTY_PKG_JSON = {
|
4
|
+
dependencies: {},
|
5
|
+
vizhub: {},
|
6
|
+
license: 'MIT',
|
7
|
+
};
|
8
|
+
|
9
|
+
export const packageJSON = (files: FilesV2) => {
|
10
|
+
const packageJsonFile = files.find(
|
11
|
+
(file) => file.name === 'package.json',
|
12
|
+
);
|
13
|
+
if (!packageJsonFile) {
|
14
|
+
return EMPTY_PKG_JSON;
|
15
|
+
}
|
16
|
+
const packageJsonText = packageJsonFile.text;
|
17
|
+
|
18
|
+
try {
|
19
|
+
const pkg = packageJsonText
|
20
|
+
? JSON.parse(packageJsonText)
|
21
|
+
: EMPTY_PKG_JSON;
|
22
|
+
return pkg;
|
23
|
+
} catch (error) {
|
24
|
+
console.log(error);
|
25
|
+
return EMPTY_PKG_JSON;
|
26
|
+
}
|
27
|
+
};
|
28
|
+
|
29
|
+
export const dependencies = (files: FilesV2) =>
|
30
|
+
packageJSON(files).dependencies || {};
|
31
|
+
|
32
|
+
export const getConfiguredLibraries = (files: FilesV2) => {
|
33
|
+
const vizhubConfig = packageJSON(files).vizhub;
|
34
|
+
return vizhubConfig ? vizhubConfig.libraries : {};
|
35
|
+
};
|
36
|
+
|
37
|
+
export const dependencySource = (
|
38
|
+
{ name, version },
|
39
|
+
libraries,
|
40
|
+
) => {
|
41
|
+
const path = libraries[name]
|
42
|
+
? libraries[name].path || ''
|
43
|
+
: '';
|
44
|
+
// unpkg uses file from unpkg or main field when no file specifid in url
|
45
|
+
return `https://unpkg.com/${name}@${version}${path}`;
|
46
|
+
};
|
47
|
+
|
48
|
+
export const getLicense = (files) =>
|
49
|
+
packageJSON(files).license || EMPTY_PKG_JSON.license;
|