@jsenv/core 27.0.0-alpha.73 → 27.0.0-alpha.76
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/dist/babel_helpers/objectWithoutProperties/objectWithoutProperties.js +1 -1
- package/dist/js/html_supervisor_setup.js +4 -1
- package/dist/main.js +151 -151
- package/package.json +5 -5
- package/src/build/build.js +5 -8
- package/src/dev/start_dev_server.js +4 -7
- package/src/execute/execute.js +0 -2
- package/src/main.js +1 -1
- package/src/omega/kitchen.js +19 -8
- package/src/omega/server/file_service.js +7 -8
- package/src/omega/url_graph.js +1 -1
- package/src/plugins/file_urls/jsenv_plugin_file_urls.js +3 -7
- package/src/plugins/html_supervisor/client/html_supervisor_setup.js +4 -1
- package/src/plugins/inject_globals/inject_globals.js +57 -0
- package/src/plugins/inject_globals/jsenv_plugin_inject_globals.js +17 -69
- package/src/plugins/inline/jsenv_plugin_data_urls.js +2 -2
- package/src/plugins/inline/jsenv_plugin_inline.js +1 -1
- package/src/plugins/node_esm_resolution/jsenv_plugin_node_esm_resolution.js +2 -0
- package/src/plugins/plugin_controller.js +2 -2
- package/src/plugins/plugins.js +0 -3
- package/src/plugins/transpilation/as_js_classic/jsenv_plugin_as_js_classic.js +3 -3
- package/src/plugins/transpilation/import_assertions/jsenv_plugin_import_assertions.js +12 -12
- package/src/test/execute_plan.js +0 -2
- package/src/test/execute_test_plan.js +0 -2
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import objectWithoutPropertiesLoose from "
|
|
1
|
+
import objectWithoutPropertiesLoose from "../objectWithoutPropertiesLoose/objectWithoutPropertiesLoose.js";
|
|
2
2
|
export default ((source, excluded) => {
|
|
3
3
|
if (source === null) return {};
|
|
4
4
|
var target = objectWithoutPropertiesLoose(source, excluded);
|
|
@@ -32,7 +32,10 @@ window.__html_supervisor__ = {
|
|
|
32
32
|
lastWindowError = e.error
|
|
33
33
|
}
|
|
34
34
|
const cleanup = () => {
|
|
35
|
-
|
|
35
|
+
// the execution of the script itself can remove script from the page
|
|
36
|
+
if (script.parentNode) {
|
|
37
|
+
script.parentNode.removeChild(script)
|
|
38
|
+
}
|
|
36
39
|
window.removeEventListener("error", windowErrorCallback)
|
|
37
40
|
}
|
|
38
41
|
window.addEventListener("error", windowErrorCallback)
|
package/dist/main.js
CHANGED
|
@@ -969,7 +969,9 @@ const jsenvPluginNodeEsmResolver = ({
|
|
|
969
969
|
fetchUrlContent: urlInfo => {
|
|
970
970
|
if (urlInfo.url.startsWith("file:///@ignore/")) {
|
|
971
971
|
return {
|
|
972
|
-
content: "export default {}"
|
|
972
|
+
content: "export default {}",
|
|
973
|
+
contentType: "text/javascript",
|
|
974
|
+
type: "js_module"
|
|
973
975
|
};
|
|
974
976
|
}
|
|
975
977
|
|
|
@@ -1232,17 +1234,9 @@ const jsenvPluginFileUrls = ({
|
|
|
1232
1234
|
|
|
1233
1235
|
const fileBuffer = readFileSync(urlObject);
|
|
1234
1236
|
const contentType = CONTENT_TYPE.fromUrlExtension(urlInfo.url);
|
|
1235
|
-
|
|
1236
|
-
if (CONTENT_TYPE.isTextual(contentType)) {
|
|
1237
|
-
return {
|
|
1238
|
-
contentType,
|
|
1239
|
-
content: String(fileBuffer)
|
|
1240
|
-
};
|
|
1241
|
-
}
|
|
1242
|
-
|
|
1243
1237
|
return {
|
|
1244
|
-
contentType,
|
|
1245
|
-
|
|
1238
|
+
content: CONTENT_TYPE.isTextual(contentType) ? String(fileBuffer) : fileBuffer,
|
|
1239
|
+
contentType
|
|
1246
1240
|
};
|
|
1247
1241
|
}
|
|
1248
1242
|
}];
|
|
@@ -1791,12 +1785,12 @@ const jsenvPluginDataUrls = () => {
|
|
|
1791
1785
|
} = DATA_URL.parse(urlInfo.url);
|
|
1792
1786
|
urlInfo.data.base64Flag = base64Flag;
|
|
1793
1787
|
return {
|
|
1794
|
-
contentType,
|
|
1795
1788
|
content: contentFromUrlData({
|
|
1796
1789
|
contentType,
|
|
1797
1790
|
base64Flag,
|
|
1798
1791
|
urlData
|
|
1799
|
-
})
|
|
1792
|
+
}),
|
|
1793
|
+
contentType
|
|
1800
1794
|
};
|
|
1801
1795
|
},
|
|
1802
1796
|
formatUrl: (reference, context) => {
|
|
@@ -1819,7 +1813,7 @@ const jsenvPluginDataUrls = () => {
|
|
|
1819
1813
|
}
|
|
1820
1814
|
|
|
1821
1815
|
const specifier = DATA_URL.stringify({
|
|
1822
|
-
contentType: urlInfo.
|
|
1816
|
+
contentType: urlInfo.headers["content-type"],
|
|
1823
1817
|
base64Flag: urlInfo.data.base64Flag,
|
|
1824
1818
|
data: urlInfo.data.base64Flag ? dataToBase64(urlInfo.content) : String(urlInfo.content)
|
|
1825
1819
|
});
|
|
@@ -1918,10 +1912,10 @@ const jsenvPluginInlineUrls = () => {
|
|
|
1918
1912
|
}
|
|
1919
1913
|
|
|
1920
1914
|
return {
|
|
1921
|
-
contentType: urlInfo.contentType,
|
|
1922
1915
|
// we want to fetch the original content otherwise we might re-cook
|
|
1923
1916
|
// content already cooked
|
|
1924
|
-
content: urlInfo.originalContent
|
|
1917
|
+
content: urlInfo.originalContent,
|
|
1918
|
+
contentType: urlInfo.contentType
|
|
1925
1919
|
};
|
|
1926
1920
|
}
|
|
1927
1921
|
};
|
|
@@ -2459,78 +2453,6 @@ const babelPluginMetadataImportMetaScenarios = () => {
|
|
|
2459
2453
|
};
|
|
2460
2454
|
};
|
|
2461
2455
|
|
|
2462
|
-
const jsenvPluginInjectGlobals = (globals = {}) => {
|
|
2463
|
-
if (Object.keys(globals).length === 0) {
|
|
2464
|
-
return [];
|
|
2465
|
-
}
|
|
2466
|
-
|
|
2467
|
-
return {
|
|
2468
|
-
name: "jsenv:inject_globals",
|
|
2469
|
-
appliesDuring: "*",
|
|
2470
|
-
transformUrlContent: {
|
|
2471
|
-
html: injectGlobals,
|
|
2472
|
-
js_classic: injectGlobals,
|
|
2473
|
-
js_module: injectGlobals
|
|
2474
|
-
}
|
|
2475
|
-
};
|
|
2476
|
-
};
|
|
2477
|
-
const injectGlobals = (urlInfo, globals) => {
|
|
2478
|
-
if (urlInfo.type === "html") {
|
|
2479
|
-
return globalInjectorOnHtmlEntryPoint(urlInfo, globals);
|
|
2480
|
-
}
|
|
2481
|
-
|
|
2482
|
-
if (urlInfo.type === "js_classic" || urlInfo.type === "js_module") {
|
|
2483
|
-
return globalsInjectorOnJsEntryPoints(urlInfo, globals);
|
|
2484
|
-
}
|
|
2485
|
-
|
|
2486
|
-
throw new Error(`cannot inject globals into "${urlInfo.type}"`);
|
|
2487
|
-
};
|
|
2488
|
-
|
|
2489
|
-
const globalInjectorOnHtmlEntryPoint = async (urlInfo, globals) => {
|
|
2490
|
-
if (!urlInfo.data.isEntryPoint) {
|
|
2491
|
-
return null;
|
|
2492
|
-
} // ideally we would inject an importmap but browser support is too low
|
|
2493
|
-
// (even worse for worker/service worker)
|
|
2494
|
-
// so for now we inject code into entry points
|
|
2495
|
-
|
|
2496
|
-
|
|
2497
|
-
const htmlAst = parseHtmlString(urlInfo.content, {
|
|
2498
|
-
storeOriginalPositions: false
|
|
2499
|
-
});
|
|
2500
|
-
const clientCode = generateClientCodeForGlobals({
|
|
2501
|
-
globals,
|
|
2502
|
-
isWebWorker: false
|
|
2503
|
-
});
|
|
2504
|
-
injectScriptAsEarlyAsPossible(htmlAst, createHtmlNode({
|
|
2505
|
-
"tagName": "script",
|
|
2506
|
-
"textContent": clientCode,
|
|
2507
|
-
"injected-by": "jsenv:inject_globals"
|
|
2508
|
-
}));
|
|
2509
|
-
return stringifyHtmlAst(htmlAst);
|
|
2510
|
-
};
|
|
2511
|
-
|
|
2512
|
-
const globalsInjectorOnJsEntryPoints = async (urlInfo, globals) => {
|
|
2513
|
-
if (!urlInfo.data.isEntryPoint && !urlInfo.data.isWebWorkerEntryPoint) {
|
|
2514
|
-
return null;
|
|
2515
|
-
}
|
|
2516
|
-
|
|
2517
|
-
const clientCode = generateClientCodeForGlobals({
|
|
2518
|
-
globals,
|
|
2519
|
-
isWebWorker: isWebWorkerUrlInfo(urlInfo)
|
|
2520
|
-
});
|
|
2521
|
-
const magicSource = createMagicSource(urlInfo.content);
|
|
2522
|
-
magicSource.prepend(clientCode);
|
|
2523
|
-
return magicSource.toContentAndSourcemap();
|
|
2524
|
-
};
|
|
2525
|
-
|
|
2526
|
-
const generateClientCodeForGlobals = ({
|
|
2527
|
-
isWebWorker = false,
|
|
2528
|
-
globals
|
|
2529
|
-
}) => {
|
|
2530
|
-
const globalName = isWebWorker ? "self" : "window";
|
|
2531
|
-
return `Object.assign(${globalName}, ${JSON.stringify(globals, null, " ")});`;
|
|
2532
|
-
};
|
|
2533
|
-
|
|
2534
2456
|
const jsenvPluginCssParcel = () => {
|
|
2535
2457
|
return {
|
|
2536
2458
|
name: "jsenv:css_parcel",
|
|
@@ -2643,14 +2565,14 @@ const jsenvPluginAsModules = () => {
|
|
|
2643
2565
|
|
|
2644
2566
|
const jsonText = JSON.stringify(originalUrlInfo.content.trim());
|
|
2645
2567
|
return {
|
|
2646
|
-
originalUrl: originalUrlInfo.originalUrl,
|
|
2647
|
-
originalContent: originalUrlInfo.originalContent,
|
|
2648
|
-
type: "js_module",
|
|
2649
|
-
contentType: "text/javascript",
|
|
2650
2568
|
// here we could `export default ${jsonText}`:
|
|
2651
2569
|
// but js engine are optimized to recognize JSON.parse
|
|
2652
2570
|
// and use a faster parsing strategy
|
|
2653
|
-
content: `export default JSON.parse(${jsonText})
|
|
2571
|
+
content: `export default JSON.parse(${jsonText})`,
|
|
2572
|
+
contentType: "text/javascript",
|
|
2573
|
+
type: "js_module",
|
|
2574
|
+
originalUrl: originalUrlInfo.originalUrl,
|
|
2575
|
+
originalContent: originalUrlInfo.originalContent
|
|
2654
2576
|
};
|
|
2655
2577
|
}
|
|
2656
2578
|
};
|
|
@@ -2676,16 +2598,16 @@ const jsenvPluginAsModules = () => {
|
|
|
2676
2598
|
canUseTemplateString: true
|
|
2677
2599
|
});
|
|
2678
2600
|
return {
|
|
2679
|
-
originalUrl: originalUrlInfo.originalUrl,
|
|
2680
|
-
originalContent: originalUrlInfo.originalContent,
|
|
2681
|
-
type: "js_module",
|
|
2682
|
-
contentType: "text/javascript",
|
|
2683
2601
|
content: `import { InlineContent } from ${JSON.stringify(inlineContentClientFileUrl)}
|
|
2684
2602
|
|
|
2685
2603
|
const inlineContent = new InlineContent(${cssText}, { type: "text/css" })
|
|
2686
2604
|
const stylesheet = new CSSStyleSheet()
|
|
2687
2605
|
stylesheet.replaceSync(inlineContent.text)
|
|
2688
|
-
export default stylesheet
|
|
2606
|
+
export default stylesheet`,
|
|
2607
|
+
contentType: "text/javascript",
|
|
2608
|
+
type: "js_module",
|
|
2609
|
+
originalUrl: originalUrlInfo.originalUrl,
|
|
2610
|
+
originalContent: originalUrlInfo.originalContent
|
|
2689
2611
|
};
|
|
2690
2612
|
}
|
|
2691
2613
|
};
|
|
@@ -2711,14 +2633,14 @@ const jsenvPluginAsModules = () => {
|
|
|
2711
2633
|
canUseTemplateString: true
|
|
2712
2634
|
});
|
|
2713
2635
|
return {
|
|
2714
|
-
originalUrl: originalUrlInfo.originalUrl,
|
|
2715
|
-
originalContent: originalUrlInfo.originalContent,
|
|
2716
|
-
type: "js_module",
|
|
2717
|
-
contentType: "text/javascript",
|
|
2718
2636
|
content: `import { InlineContent } from ${JSON.stringify(inlineContentClientFileUrl)}
|
|
2719
2637
|
|
|
2720
2638
|
const inlineContent = new InlineContent(${textPlain}, { type: "text/plain" })
|
|
2721
|
-
export default inlineContent.text
|
|
2639
|
+
export default inlineContent.text`,
|
|
2640
|
+
contentType: "text/javascript",
|
|
2641
|
+
type: "js_module",
|
|
2642
|
+
originalUrl: originalUrlInfo.originalUrl,
|
|
2643
|
+
originalContent: originalUrlInfo.originalContent
|
|
2722
2644
|
};
|
|
2723
2645
|
}
|
|
2724
2646
|
};
|
|
@@ -3202,11 +3124,11 @@ const jsenvPluginAsJsClassicConversion = ({
|
|
|
3202
3124
|
});
|
|
3203
3125
|
urlInfo.data.jsClassicFormat = jsClassicFormat;
|
|
3204
3126
|
return {
|
|
3127
|
+
content,
|
|
3128
|
+
contentType: "text/javascript",
|
|
3129
|
+
type: "js_classic",
|
|
3205
3130
|
originalUrl: originalUrlInfo.originalUrl,
|
|
3206
3131
|
originalContent: originalUrlInfo.originalContent,
|
|
3207
|
-
type: "js_classic",
|
|
3208
|
-
contentType: "text/javascript",
|
|
3209
|
-
content,
|
|
3210
3132
|
sourcemap
|
|
3211
3133
|
};
|
|
3212
3134
|
}
|
|
@@ -6027,7 +5949,6 @@ const getCorePlugins = ({
|
|
|
6027
5949
|
nodeEsmResolution,
|
|
6028
5950
|
fileSystemMagicResolution,
|
|
6029
5951
|
directoryReferenceAllowed,
|
|
6030
|
-
injectedGlobals,
|
|
6031
5952
|
transpilation = true,
|
|
6032
5953
|
minification = false,
|
|
6033
5954
|
bundling = false,
|
|
@@ -6057,7 +5978,7 @@ const getCorePlugins = ({
|
|
|
6057
5978
|
rootDirectoryUrl,
|
|
6058
5979
|
runtimeCompat,
|
|
6059
5980
|
...nodeEsmResolution
|
|
6060
|
-
}), jsenvPluginUrlResolution(), jsenvPluginUrlVersion(),
|
|
5981
|
+
}), jsenvPluginUrlResolution(), jsenvPluginUrlVersion(), jsenvPluginCommonJsGlobals(), jsenvPluginImportMetaScenarios(), jsenvPluginNodeRuntime({
|
|
6061
5982
|
runtimeCompat
|
|
6062
5983
|
}), jsenvPluginBundling(bundling), jsenvPluginMinification(minification), jsenvPluginImportMetaHot(), ...(clientAutoreload ? [jsenvPluginAutoreload({
|
|
6063
5984
|
rootDirectoryUrl,
|
|
@@ -6368,7 +6289,7 @@ const createUrlInfo = url => {
|
|
|
6368
6289
|
sourcemap: null,
|
|
6369
6290
|
sourcemapReference: null,
|
|
6370
6291
|
timing: {},
|
|
6371
|
-
|
|
6292
|
+
headers: {}
|
|
6372
6293
|
};
|
|
6373
6294
|
};
|
|
6374
6295
|
|
|
@@ -6666,14 +6587,15 @@ const returnValueAssertions = [{
|
|
|
6666
6587
|
if (typeof valueReturned === "object") {
|
|
6667
6588
|
const {
|
|
6668
6589
|
shouldHandle,
|
|
6669
|
-
content
|
|
6590
|
+
content,
|
|
6591
|
+
body
|
|
6670
6592
|
} = valueReturned;
|
|
6671
6593
|
|
|
6672
6594
|
if (shouldHandle === false) {
|
|
6673
6595
|
return undefined;
|
|
6674
6596
|
}
|
|
6675
6597
|
|
|
6676
|
-
if (typeof content !== "string" && !Buffer.isBuffer(content)) {
|
|
6598
|
+
if (typeof content !== "string" && !Buffer.isBuffer(content) && !body) {
|
|
6677
6599
|
throw new Error(`Unexpected "content" returned by plugin: it must be a string or a buffer; got ${content}`);
|
|
6678
6600
|
}
|
|
6679
6601
|
|
|
@@ -7297,17 +7219,35 @@ const createKitchen = ({
|
|
|
7297
7219
|
return;
|
|
7298
7220
|
}
|
|
7299
7221
|
|
|
7300
|
-
|
|
7222
|
+
let {
|
|
7223
|
+
content,
|
|
7224
|
+
contentType,
|
|
7301
7225
|
data,
|
|
7302
7226
|
type,
|
|
7303
7227
|
subtype,
|
|
7304
|
-
contentType = "application/octet-stream",
|
|
7305
7228
|
originalUrl,
|
|
7306
7229
|
originalContent,
|
|
7307
|
-
content,
|
|
7308
7230
|
sourcemap,
|
|
7309
|
-
filename
|
|
7231
|
+
filename,
|
|
7232
|
+
status = 200,
|
|
7233
|
+
headers = {},
|
|
7234
|
+
body
|
|
7310
7235
|
} = fetchUrlContentReturnValue;
|
|
7236
|
+
|
|
7237
|
+
if (status !== 200) {
|
|
7238
|
+
throw new Error(`unexpected status, ${status}`);
|
|
7239
|
+
}
|
|
7240
|
+
|
|
7241
|
+
if (content === undefined) {
|
|
7242
|
+
content = body;
|
|
7243
|
+
}
|
|
7244
|
+
|
|
7245
|
+
if (contentType === undefined) {
|
|
7246
|
+
contentType = headers["content-type"] || "application/octet-stream";
|
|
7247
|
+
}
|
|
7248
|
+
|
|
7249
|
+
urlInfo.contentType = contentType;
|
|
7250
|
+
urlInfo.headers = headers;
|
|
7311
7251
|
urlInfo.type = type || reference.expectedType || inferUrlInfoType({
|
|
7312
7252
|
url: urlInfo.url,
|
|
7313
7253
|
contentType
|
|
@@ -7316,8 +7256,7 @@ const createKitchen = ({
|
|
|
7316
7256
|
url: urlInfo.url,
|
|
7317
7257
|
type: urlInfo.type,
|
|
7318
7258
|
subtype: urlInfo.subtype
|
|
7319
|
-
});
|
|
7320
|
-
urlInfo.contentType = contentType; // during build urls info are reused and load returns originalUrl/originalContent
|
|
7259
|
+
}); // during build urls info are reused and load returns originalUrl/originalContent
|
|
7321
7260
|
|
|
7322
7261
|
urlInfo.originalUrl = originalUrl || urlInfo.originalUrl;
|
|
7323
7262
|
urlInfo.originalContent = originalContent === undefined ? content : originalContent;
|
|
@@ -8042,7 +7981,7 @@ const createFileService = ({
|
|
|
8042
7981
|
status: 304,
|
|
8043
7982
|
headers: {
|
|
8044
7983
|
"cache-control": `private,max-age=0,must-revalidate`,
|
|
8045
|
-
...urlInfo.
|
|
7984
|
+
...urlInfo.headers
|
|
8046
7985
|
}
|
|
8047
7986
|
};
|
|
8048
7987
|
}
|
|
@@ -8057,7 +7996,6 @@ const createFileService = ({
|
|
|
8057
7996
|
urlInfo.type = null;
|
|
8058
7997
|
urlInfo.subtype = null;
|
|
8059
7998
|
urlInfo.timing = {};
|
|
8060
|
-
urlInfo.responseHeaders = {};
|
|
8061
7999
|
}
|
|
8062
8000
|
|
|
8063
8001
|
const {
|
|
@@ -8073,10 +8011,7 @@ const createFileService = ({
|
|
|
8073
8011
|
outDirectoryUrl: scenario === "dev" ? `${rootDirectoryUrl}.jsenv/${runtimeName}@${runtimeVersion}/` : `${rootDirectoryUrl}.jsenv/${scenario}/${runtimeName}@${runtimeVersion}/`
|
|
8074
8012
|
});
|
|
8075
8013
|
let {
|
|
8076
|
-
response
|
|
8077
|
-
contentType,
|
|
8078
|
-
content,
|
|
8079
|
-
contentEtag
|
|
8014
|
+
response
|
|
8080
8015
|
} = urlInfo;
|
|
8081
8016
|
|
|
8082
8017
|
if (response) {
|
|
@@ -8087,13 +8022,13 @@ const createFileService = ({
|
|
|
8087
8022
|
url: reference.url,
|
|
8088
8023
|
status: 200,
|
|
8089
8024
|
headers: {
|
|
8090
|
-
"content-
|
|
8091
|
-
"content-length": Buffer.byteLength(content),
|
|
8025
|
+
"content-length": Buffer.byteLength(urlInfo.content),
|
|
8092
8026
|
"cache-control": `private,max-age=0,must-revalidate`,
|
|
8093
|
-
"eTag": contentEtag,
|
|
8094
|
-
...urlInfo.
|
|
8027
|
+
"eTag": urlInfo.contentEtag,
|
|
8028
|
+
...urlInfo.headers,
|
|
8029
|
+
"content-type": urlInfo.contentType
|
|
8095
8030
|
},
|
|
8096
|
-
body: content,
|
|
8031
|
+
body: urlInfo.content,
|
|
8097
8032
|
timing: urlInfo.timing
|
|
8098
8033
|
};
|
|
8099
8034
|
kitchen.pluginController.callHooks("augmentResponse", {
|
|
@@ -8396,11 +8331,10 @@ const startDevServer = async ({
|
|
|
8396
8331
|
// (because node cluster won't work)
|
|
8397
8332
|
devServerAutoreload = typeof process.send !== "function" && !parentPort && !process.env.VSCODE_INSPECTOR_OPTIONS,
|
|
8398
8333
|
clientFiles = {
|
|
8399
|
-
"
|
|
8400
|
-
"
|
|
8334
|
+
"./src/": true,
|
|
8335
|
+
"./src/**/.*/": false,
|
|
8401
8336
|
// any folder starting with a dot is ignored (includes .git,.jsenv for instance)
|
|
8402
|
-
"
|
|
8403
|
-
"./**/node_modules/": false
|
|
8337
|
+
"./src/**/node_modules/": false
|
|
8404
8338
|
},
|
|
8405
8339
|
cooldownBetweenFileEvents,
|
|
8406
8340
|
clientAutoreload = true,
|
|
@@ -8417,8 +8351,7 @@ const startDevServer = async ({
|
|
|
8417
8351
|
},
|
|
8418
8352
|
plugins = [],
|
|
8419
8353
|
urlAnalysis = {},
|
|
8420
|
-
htmlSupervisor =
|
|
8421
|
-
injectedGlobals,
|
|
8354
|
+
htmlSupervisor = false,
|
|
8422
8355
|
nodeEsmResolution,
|
|
8423
8356
|
fileSystemMagicResolution,
|
|
8424
8357
|
transpilation,
|
|
@@ -8577,7 +8510,6 @@ const startDevServer = async ({
|
|
|
8577
8510
|
runtimeCompat,
|
|
8578
8511
|
urlAnalysis,
|
|
8579
8512
|
htmlSupervisor,
|
|
8580
|
-
injectedGlobals,
|
|
8581
8513
|
nodeEsmResolution,
|
|
8582
8514
|
fileSystemMagicResolution,
|
|
8583
8515
|
transpilation,
|
|
@@ -9135,7 +9067,6 @@ const executePlan = async (plan, {
|
|
|
9135
9067
|
scenario,
|
|
9136
9068
|
sourcemaps,
|
|
9137
9069
|
plugins,
|
|
9138
|
-
injectedGlobals,
|
|
9139
9070
|
nodeEsmResolution,
|
|
9140
9071
|
fileSystemMagicResolution,
|
|
9141
9072
|
transpilation,
|
|
@@ -9220,7 +9151,6 @@ const executePlan = async (plan, {
|
|
|
9220
9151
|
htmlSupervisor: true,
|
|
9221
9152
|
nodeEsmResolution,
|
|
9222
9153
|
fileSystemMagicResolution,
|
|
9223
|
-
injectedGlobals,
|
|
9224
9154
|
transpilation: { ...transpilation,
|
|
9225
9155
|
getCustomBabelPlugins: ({
|
|
9226
9156
|
clientRuntimeCompat
|
|
@@ -9738,7 +9668,6 @@ const executeTestPlan = async ({
|
|
|
9738
9668
|
coverageSkipFull = false,
|
|
9739
9669
|
sourcemaps = "inline",
|
|
9740
9670
|
plugins = [],
|
|
9741
|
-
injectedGlobals,
|
|
9742
9671
|
nodeEsmResolution,
|
|
9743
9672
|
fileSystemMagicResolution,
|
|
9744
9673
|
writeGeneratedFiles = false,
|
|
@@ -9819,7 +9748,6 @@ const executeTestPlan = async ({
|
|
|
9819
9748
|
scenario: "test",
|
|
9820
9749
|
sourcemaps,
|
|
9821
9750
|
plugins,
|
|
9822
|
-
injectedGlobals,
|
|
9823
9751
|
nodeEsmResolution,
|
|
9824
9752
|
fileSystemMagicResolution,
|
|
9825
9753
|
writeGeneratedFiles,
|
|
@@ -11899,7 +11827,6 @@ const build = async ({
|
|
|
11899
11827
|
nodeEsmResolution,
|
|
11900
11828
|
fileSystemMagicResolution,
|
|
11901
11829
|
directoryReferenceAllowed,
|
|
11902
|
-
injectedGlobals,
|
|
11903
11830
|
transpilation = {},
|
|
11904
11831
|
bundling = true,
|
|
11905
11832
|
minification = true,
|
|
@@ -11908,11 +11835,10 @@ const build = async ({
|
|
|
11908
11835
|
// "filename", "search_param"
|
|
11909
11836
|
lineBreakNormalization = process.platform === "win32",
|
|
11910
11837
|
clientFiles = {
|
|
11911
|
-
"
|
|
11912
|
-
"
|
|
11838
|
+
"./src/": true,
|
|
11839
|
+
"./src/**/.*/": false,
|
|
11913
11840
|
// any folder starting with a dot is ignored (includes .git,.jsenv for instance)
|
|
11914
|
-
"./
|
|
11915
|
-
"./**/node_modules/": false
|
|
11841
|
+
"./src/**/node_modules/": false
|
|
11916
11842
|
},
|
|
11917
11843
|
cooldownBetweenFileEvents,
|
|
11918
11844
|
watch = false,
|
|
@@ -12012,7 +11938,6 @@ build ${entryPointKeys.length} entry points`);
|
|
|
12012
11938
|
nodeEsmResolution,
|
|
12013
11939
|
fileSystemMagicResolution,
|
|
12014
11940
|
directoryReferenceAllowed,
|
|
12015
|
-
injectedGlobals,
|
|
12016
11941
|
transpilation: { ...transpilation,
|
|
12017
11942
|
babelHelpersAsImport: !useExplicitJsClassicConversion,
|
|
12018
11943
|
jsModuleAsJsClassic: false
|
|
@@ -12985,10 +12910,10 @@ const applyUrlVersioning = async ({
|
|
|
12985
12910
|
const rawUrlInfo = rawGraph.getUrlInfo(rawUrls[versionedUrlInfo.url]);
|
|
12986
12911
|
const finalUrlInfo = finalGraph.getUrlInfo(versionedUrlInfo.url);
|
|
12987
12912
|
return {
|
|
12988
|
-
|
|
12989
|
-
sourcemap: finalUrlInfo ? finalUrlInfo.sourcemap : undefined,
|
|
12913
|
+
content: versionedUrlInfo.content,
|
|
12990
12914
|
contentType: versionedUrlInfo.contentType,
|
|
12991
|
-
|
|
12915
|
+
originalContent: rawUrlInfo ? rawUrlInfo.originalContent : undefined,
|
|
12916
|
+
sourcemap: finalUrlInfo ? finalUrlInfo.sourcemap : undefined
|
|
12992
12917
|
};
|
|
12993
12918
|
}
|
|
12994
12919
|
|
|
@@ -13307,7 +13232,6 @@ const execute = async ({
|
|
|
13307
13232
|
plugins = [],
|
|
13308
13233
|
nodeEsmResolution,
|
|
13309
13234
|
fileSystemMagicResolution,
|
|
13310
|
-
injectedGlobals,
|
|
13311
13235
|
transpilation,
|
|
13312
13236
|
htmlSupervisor = true,
|
|
13313
13237
|
writeGeneratedFiles = false,
|
|
@@ -13361,7 +13285,6 @@ const execute = async ({
|
|
|
13361
13285
|
scenario,
|
|
13362
13286
|
runtimeCompat,
|
|
13363
13287
|
htmlSupervisor,
|
|
13364
|
-
injectedGlobals,
|
|
13365
13288
|
nodeEsmResolution,
|
|
13366
13289
|
fileSystemMagicResolution,
|
|
13367
13290
|
transpilation
|
|
@@ -13436,4 +13359,81 @@ const execute = async ({
|
|
|
13436
13359
|
}
|
|
13437
13360
|
};
|
|
13438
13361
|
|
|
13439
|
-
|
|
13362
|
+
const injectGlobals = (urlInfo, globals) => {
|
|
13363
|
+
if (urlInfo.type === "html") {
|
|
13364
|
+
return globalInjectorOnHtml(urlInfo, globals);
|
|
13365
|
+
}
|
|
13366
|
+
|
|
13367
|
+
if (urlInfo.type === "js_classic" || urlInfo.type === "js_module") {
|
|
13368
|
+
return globalsInjectorOnJs(urlInfo, globals);
|
|
13369
|
+
}
|
|
13370
|
+
|
|
13371
|
+
throw new Error(`cannot inject globals into "${urlInfo.type}"`);
|
|
13372
|
+
};
|
|
13373
|
+
|
|
13374
|
+
const globalInjectorOnHtml = async (urlInfo, globals) => {
|
|
13375
|
+
// ideally we would inject an importmap but browser support is too low
|
|
13376
|
+
// (even worse for worker/service worker)
|
|
13377
|
+
// so for now we inject code into entry points
|
|
13378
|
+
const htmlAst = parseHtmlString(urlInfo.content, {
|
|
13379
|
+
storeOriginalPositions: false
|
|
13380
|
+
});
|
|
13381
|
+
const clientCode = generateClientCodeForGlobals({
|
|
13382
|
+
globals,
|
|
13383
|
+
isWebWorker: false
|
|
13384
|
+
});
|
|
13385
|
+
injectScriptAsEarlyAsPossible(htmlAst, createHtmlNode({
|
|
13386
|
+
"tagName": "script",
|
|
13387
|
+
"textContent": clientCode,
|
|
13388
|
+
"injected-by": "jsenv:inject_globals"
|
|
13389
|
+
}));
|
|
13390
|
+
return stringifyHtmlAst(htmlAst);
|
|
13391
|
+
};
|
|
13392
|
+
|
|
13393
|
+
const globalsInjectorOnJs = async (urlInfo, globals) => {
|
|
13394
|
+
const clientCode = generateClientCodeForGlobals({
|
|
13395
|
+
globals,
|
|
13396
|
+
isWebWorker: urlInfo.subtype === "worker" || urlInfo.subtype === "service_worker" || urlInfo.subtype === "shared_worker"
|
|
13397
|
+
});
|
|
13398
|
+
const magicSource = createMagicSource(urlInfo.content);
|
|
13399
|
+
magicSource.prepend(clientCode);
|
|
13400
|
+
return magicSource.toContentAndSourcemap();
|
|
13401
|
+
};
|
|
13402
|
+
|
|
13403
|
+
const generateClientCodeForGlobals = ({
|
|
13404
|
+
isWebWorker = false,
|
|
13405
|
+
globals
|
|
13406
|
+
}) => {
|
|
13407
|
+
const globalName = isWebWorker ? "self" : "window";
|
|
13408
|
+
return `Object.assign(${globalName}, ${JSON.stringify(globals, null, " ")});`;
|
|
13409
|
+
};
|
|
13410
|
+
|
|
13411
|
+
const jsenvPluginInjectGlobals = urlAssociations => {
|
|
13412
|
+
return {
|
|
13413
|
+
name: "jsenv:inject_globals",
|
|
13414
|
+
appliesDuring: "*",
|
|
13415
|
+
transformUrlContent: async urlInfo => {
|
|
13416
|
+
const url = Object.keys(urlAssociations).find(url => {
|
|
13417
|
+
return url === urlInfo.url;
|
|
13418
|
+
});
|
|
13419
|
+
|
|
13420
|
+
if (!url) {
|
|
13421
|
+
return null;
|
|
13422
|
+
}
|
|
13423
|
+
|
|
13424
|
+
let globals = urlAssociations[url];
|
|
13425
|
+
|
|
13426
|
+
if (typeof globals === "function") {
|
|
13427
|
+
globals = await globals();
|
|
13428
|
+
}
|
|
13429
|
+
|
|
13430
|
+
if (Object.keys(globals).length === 0) {
|
|
13431
|
+
return null;
|
|
13432
|
+
}
|
|
13433
|
+
|
|
13434
|
+
return injectGlobals(urlInfo, globals);
|
|
13435
|
+
}
|
|
13436
|
+
};
|
|
13437
|
+
};
|
|
13438
|
+
|
|
13439
|
+
export { build, chromium, chromiumIsolatedTab, defaultCoverageConfig, execute, executeTestPlan, firefox, firefoxIsolatedTab, jsenvPluginInjectGlobals, nodeProcess, startBuildServer, startDevServer, webkit, webkitIsolatedTab };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jsenv/core",
|
|
3
|
-
"version": "27.0.0-alpha.
|
|
3
|
+
"version": "27.0.0-alpha.76",
|
|
4
4
|
"description": "Tool to develop, test and build js projects",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
@@ -68,7 +68,7 @@
|
|
|
68
68
|
"@financial-times/polyfill-useragent-normaliser": "2.0.1",
|
|
69
69
|
"@jsenv/abort": "4.1.2",
|
|
70
70
|
"@jsenv/babel-plugins": "1.0.3",
|
|
71
|
-
"@jsenv/filesystem": "4.0.
|
|
71
|
+
"@jsenv/filesystem": "4.0.6",
|
|
72
72
|
"@jsenv/importmap": "1.2.0",
|
|
73
73
|
"@jsenv/integrity": "0.0.1",
|
|
74
74
|
"@jsenv/log": "1.6.3",
|
|
@@ -76,9 +76,9 @@
|
|
|
76
76
|
"@jsenv/node-esm-resolution": "0.0.10",
|
|
77
77
|
"@jsenv/server": "12.6.3",
|
|
78
78
|
"@jsenv/uneval": "1.6.0",
|
|
79
|
-
"@jsenv/utils": "1.8.
|
|
79
|
+
"@jsenv/utils": "1.8.9",
|
|
80
80
|
"@jsenv/url-meta": "7.0.0",
|
|
81
|
-
"@jsenv/urls": "1.2.
|
|
81
|
+
"@jsenv/urls": "1.2.3",
|
|
82
82
|
"construct-style-sheets-polyfill": "3.1.0",
|
|
83
83
|
"cssnano": "5.1.7",
|
|
84
84
|
"cssnano-preset-default": "5.2.7",
|
|
@@ -118,4 +118,4 @@
|
|
|
118
118
|
"redux": "4.1.2",
|
|
119
119
|
"rollup": "2.70.1"
|
|
120
120
|
}
|
|
121
|
-
}
|
|
121
|
+
}
|
package/src/build/build.js
CHANGED
|
@@ -109,7 +109,6 @@ export const build = async ({
|
|
|
109
109
|
nodeEsmResolution,
|
|
110
110
|
fileSystemMagicResolution,
|
|
111
111
|
directoryReferenceAllowed,
|
|
112
|
-
injectedGlobals,
|
|
113
112
|
transpilation = {},
|
|
114
113
|
bundling = true,
|
|
115
114
|
minification = true,
|
|
@@ -118,10 +117,9 @@ export const build = async ({
|
|
|
118
117
|
lineBreakNormalization = process.platform === "win32",
|
|
119
118
|
|
|
120
119
|
clientFiles = {
|
|
121
|
-
"
|
|
122
|
-
"
|
|
123
|
-
"./
|
|
124
|
-
"./**/node_modules/": false,
|
|
120
|
+
"./src/": true,
|
|
121
|
+
"./src/**/.*/": false, // any folder starting with a dot is ignored (includes .git,.jsenv for instance)
|
|
122
|
+
"./src/**/node_modules/": false,
|
|
125
123
|
},
|
|
126
124
|
cooldownBetweenFileEvents,
|
|
127
125
|
watch = false,
|
|
@@ -224,7 +222,6 @@ build ${entryPointKeys.length} entry points`)
|
|
|
224
222
|
nodeEsmResolution,
|
|
225
223
|
fileSystemMagicResolution,
|
|
226
224
|
directoryReferenceAllowed,
|
|
227
|
-
injectedGlobals,
|
|
228
225
|
transpilation: {
|
|
229
226
|
...transpilation,
|
|
230
227
|
babelHelpersAsImport: !useExplicitJsClassicConversion,
|
|
@@ -1187,12 +1184,12 @@ const applyUrlVersioning = async ({
|
|
|
1187
1184
|
)
|
|
1188
1185
|
const finalUrlInfo = finalGraph.getUrlInfo(versionedUrlInfo.url)
|
|
1189
1186
|
return {
|
|
1187
|
+
content: versionedUrlInfo.content,
|
|
1188
|
+
contentType: versionedUrlInfo.contentType,
|
|
1190
1189
|
originalContent: rawUrlInfo
|
|
1191
1190
|
? rawUrlInfo.originalContent
|
|
1192
1191
|
: undefined,
|
|
1193
1192
|
sourcemap: finalUrlInfo ? finalUrlInfo.sourcemap : undefined,
|
|
1194
|
-
contentType: versionedUrlInfo.contentType,
|
|
1195
|
-
content: versionedUrlInfo.content,
|
|
1196
1193
|
}
|
|
1197
1194
|
}
|
|
1198
1195
|
return versionedUrlInfo
|
|
@@ -44,10 +44,9 @@ export const startDevServer = async ({
|
|
|
44
44
|
!parentPort &&
|
|
45
45
|
!process.env.VSCODE_INSPECTOR_OPTIONS,
|
|
46
46
|
clientFiles = {
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
-
"./**/node_modules/": false,
|
|
47
|
+
"./src/": true,
|
|
48
|
+
"./src/**/.*/": false, // any folder starting with a dot is ignored (includes .git,.jsenv for instance)
|
|
49
|
+
"./src/**/node_modules/": false,
|
|
51
50
|
},
|
|
52
51
|
cooldownBetweenFileEvents,
|
|
53
52
|
clientAutoreload = true,
|
|
@@ -65,8 +64,7 @@ export const startDevServer = async ({
|
|
|
65
64
|
},
|
|
66
65
|
plugins = [],
|
|
67
66
|
urlAnalysis = {},
|
|
68
|
-
htmlSupervisor =
|
|
69
|
-
injectedGlobals,
|
|
67
|
+
htmlSupervisor = false,
|
|
70
68
|
nodeEsmResolution,
|
|
71
69
|
fileSystemMagicResolution,
|
|
72
70
|
transpilation,
|
|
@@ -188,7 +186,6 @@ export const startDevServer = async ({
|
|
|
188
186
|
|
|
189
187
|
urlAnalysis,
|
|
190
188
|
htmlSupervisor,
|
|
191
|
-
injectedGlobals,
|
|
192
189
|
nodeEsmResolution,
|
|
193
190
|
fileSystemMagicResolution,
|
|
194
191
|
transpilation,
|
package/src/execute/execute.js
CHANGED
|
@@ -32,7 +32,6 @@ export const execute = async ({
|
|
|
32
32
|
plugins = [],
|
|
33
33
|
nodeEsmResolution,
|
|
34
34
|
fileSystemMagicResolution,
|
|
35
|
-
injectedGlobals,
|
|
36
35
|
transpilation,
|
|
37
36
|
htmlSupervisor = true,
|
|
38
37
|
writeGeneratedFiles = false,
|
|
@@ -87,7 +86,6 @@ export const execute = async ({
|
|
|
87
86
|
runtimeCompat,
|
|
88
87
|
|
|
89
88
|
htmlSupervisor,
|
|
90
|
-
injectedGlobals,
|
|
91
89
|
nodeEsmResolution,
|
|
92
90
|
fileSystemMagicResolution,
|
|
93
91
|
transpilation,
|
package/src/main.js
CHANGED
|
@@ -24,4 +24,4 @@ export { startBuildServer } from "./build/start_build_server.js"
|
|
|
24
24
|
|
|
25
25
|
// advanced
|
|
26
26
|
export { execute } from "./execute/execute.js"
|
|
27
|
-
export {
|
|
27
|
+
export { jsenvPluginInjectGlobals } from "./plugins/inject_globals/jsenv_plugin_inject_globals.js"
|
package/src/omega/kitchen.js
CHANGED
|
@@ -272,24 +272,36 @@ export const createKitchen = ({
|
|
|
272
272
|
)
|
|
273
273
|
return
|
|
274
274
|
}
|
|
275
|
-
|
|
275
|
+
let {
|
|
276
|
+
content,
|
|
277
|
+
contentType,
|
|
276
278
|
data,
|
|
277
279
|
type,
|
|
278
280
|
subtype,
|
|
279
|
-
contentType = "application/octet-stream",
|
|
280
281
|
originalUrl,
|
|
281
282
|
originalContent,
|
|
282
|
-
content,
|
|
283
283
|
sourcemap,
|
|
284
284
|
filename,
|
|
285
|
+
|
|
286
|
+
status = 200,
|
|
287
|
+
headers = {},
|
|
288
|
+
body,
|
|
285
289
|
} = fetchUrlContentReturnValue
|
|
290
|
+
if (status !== 200) {
|
|
291
|
+
throw new Error(`unexpected status, ${status}`)
|
|
292
|
+
}
|
|
293
|
+
if (content === undefined) {
|
|
294
|
+
content = body
|
|
295
|
+
}
|
|
296
|
+
if (contentType === undefined) {
|
|
297
|
+
contentType = headers["content-type"] || "application/octet-stream"
|
|
298
|
+
}
|
|
299
|
+
urlInfo.contentType = contentType
|
|
300
|
+
urlInfo.headers = headers
|
|
286
301
|
urlInfo.type =
|
|
287
302
|
type ||
|
|
288
303
|
reference.expectedType ||
|
|
289
|
-
inferUrlInfoType({
|
|
290
|
-
url: urlInfo.url,
|
|
291
|
-
contentType,
|
|
292
|
-
})
|
|
304
|
+
inferUrlInfoType({ url: urlInfo.url, contentType })
|
|
293
305
|
urlInfo.subtype =
|
|
294
306
|
subtype ||
|
|
295
307
|
reference.expectedSubtype ||
|
|
@@ -298,7 +310,6 @@ export const createKitchen = ({
|
|
|
298
310
|
type: urlInfo.type,
|
|
299
311
|
subtype: urlInfo.subtype,
|
|
300
312
|
})
|
|
301
|
-
urlInfo.contentType = contentType
|
|
302
313
|
// during build urls info are reused and load returns originalUrl/originalContent
|
|
303
314
|
urlInfo.originalUrl = originalUrl || urlInfo.originalUrl
|
|
304
315
|
urlInfo.originalContent =
|
|
@@ -66,7 +66,7 @@ export const createFileService = ({
|
|
|
66
66
|
status: 304,
|
|
67
67
|
headers: {
|
|
68
68
|
"cache-control": `private,max-age=0,must-revalidate`,
|
|
69
|
-
...urlInfo.
|
|
69
|
+
...urlInfo.headers,
|
|
70
70
|
},
|
|
71
71
|
}
|
|
72
72
|
}
|
|
@@ -84,7 +84,6 @@ export const createFileService = ({
|
|
|
84
84
|
urlInfo.type = null
|
|
85
85
|
urlInfo.subtype = null
|
|
86
86
|
urlInfo.timing = {}
|
|
87
|
-
urlInfo.responseHeaders = {}
|
|
88
87
|
}
|
|
89
88
|
const { runtimeName, runtimeVersion } = parseUserAgentHeader(
|
|
90
89
|
request.headers["user-agent"],
|
|
@@ -100,7 +99,7 @@ export const createFileService = ({
|
|
|
100
99
|
? `${rootDirectoryUrl}.jsenv/${runtimeName}@${runtimeVersion}/`
|
|
101
100
|
: `${rootDirectoryUrl}.jsenv/${scenario}/${runtimeName}@${runtimeVersion}/`,
|
|
102
101
|
})
|
|
103
|
-
let { response
|
|
102
|
+
let { response } = urlInfo
|
|
104
103
|
if (response) {
|
|
105
104
|
return response
|
|
106
105
|
}
|
|
@@ -108,13 +107,13 @@ export const createFileService = ({
|
|
|
108
107
|
url: reference.url,
|
|
109
108
|
status: 200,
|
|
110
109
|
headers: {
|
|
111
|
-
"content-
|
|
112
|
-
"content-length": Buffer.byteLength(content),
|
|
110
|
+
"content-length": Buffer.byteLength(urlInfo.content),
|
|
113
111
|
"cache-control": `private,max-age=0,must-revalidate`,
|
|
114
|
-
"eTag": contentEtag,
|
|
115
|
-
...urlInfo.
|
|
112
|
+
"eTag": urlInfo.contentEtag,
|
|
113
|
+
...urlInfo.headers,
|
|
114
|
+
"content-type": urlInfo.contentType,
|
|
116
115
|
},
|
|
117
|
-
body: content,
|
|
116
|
+
body: urlInfo.content,
|
|
118
117
|
timing: urlInfo.timing,
|
|
119
118
|
}
|
|
120
119
|
kitchen.pluginController.callHooks(
|
package/src/omega/url_graph.js
CHANGED
|
@@ -178,15 +178,11 @@ export const jsenvPluginFileUrls = ({
|
|
|
178
178
|
}
|
|
179
179
|
const fileBuffer = readFileSync(urlObject)
|
|
180
180
|
const contentType = CONTENT_TYPE.fromUrlExtension(urlInfo.url)
|
|
181
|
-
if (CONTENT_TYPE.isTextual(contentType)) {
|
|
182
|
-
return {
|
|
183
|
-
contentType,
|
|
184
|
-
content: String(fileBuffer),
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
181
|
return {
|
|
182
|
+
content: CONTENT_TYPE.isTextual(contentType)
|
|
183
|
+
? String(fileBuffer)
|
|
184
|
+
: fileBuffer,
|
|
188
185
|
contentType,
|
|
189
|
-
content: fileBuffer,
|
|
190
186
|
}
|
|
191
187
|
},
|
|
192
188
|
},
|
|
@@ -32,7 +32,10 @@ window.__html_supervisor__ = {
|
|
|
32
32
|
lastWindowError = e.error
|
|
33
33
|
}
|
|
34
34
|
const cleanup = () => {
|
|
35
|
-
|
|
35
|
+
// the execution of the script itself can remove script from the page
|
|
36
|
+
if (script.parentNode) {
|
|
37
|
+
script.parentNode.removeChild(script)
|
|
38
|
+
}
|
|
36
39
|
window.removeEventListener("error", windowErrorCallback)
|
|
37
40
|
}
|
|
38
41
|
window.addEventListener("error", windowErrorCallback)
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import {
|
|
2
|
+
parseHtmlString,
|
|
3
|
+
injectScriptAsEarlyAsPossible,
|
|
4
|
+
createHtmlNode,
|
|
5
|
+
stringifyHtmlAst,
|
|
6
|
+
} from "@jsenv/utils/html_ast/html_ast.js"
|
|
7
|
+
import { createMagicSource } from "@jsenv/utils/sourcemap/magic_source.js"
|
|
8
|
+
|
|
9
|
+
export const injectGlobals = (urlInfo, globals) => {
|
|
10
|
+
if (urlInfo.type === "html") {
|
|
11
|
+
return globalInjectorOnHtml(urlInfo, globals)
|
|
12
|
+
}
|
|
13
|
+
if (urlInfo.type === "js_classic" || urlInfo.type === "js_module") {
|
|
14
|
+
return globalsInjectorOnJs(urlInfo, globals)
|
|
15
|
+
}
|
|
16
|
+
throw new Error(`cannot inject globals into "${urlInfo.type}"`)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const globalInjectorOnHtml = async (urlInfo, globals) => {
|
|
20
|
+
// ideally we would inject an importmap but browser support is too low
|
|
21
|
+
// (even worse for worker/service worker)
|
|
22
|
+
// so for now we inject code into entry points
|
|
23
|
+
const htmlAst = parseHtmlString(urlInfo.content, {
|
|
24
|
+
storeOriginalPositions: false,
|
|
25
|
+
})
|
|
26
|
+
const clientCode = generateClientCodeForGlobals({
|
|
27
|
+
globals,
|
|
28
|
+
isWebWorker: false,
|
|
29
|
+
})
|
|
30
|
+
injectScriptAsEarlyAsPossible(
|
|
31
|
+
htmlAst,
|
|
32
|
+
createHtmlNode({
|
|
33
|
+
"tagName": "script",
|
|
34
|
+
"textContent": clientCode,
|
|
35
|
+
"injected-by": "jsenv:inject_globals",
|
|
36
|
+
}),
|
|
37
|
+
)
|
|
38
|
+
return stringifyHtmlAst(htmlAst)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const globalsInjectorOnJs = async (urlInfo, globals) => {
|
|
42
|
+
const clientCode = generateClientCodeForGlobals({
|
|
43
|
+
globals,
|
|
44
|
+
isWebWorker:
|
|
45
|
+
urlInfo.subtype === "worker" ||
|
|
46
|
+
urlInfo.subtype === "service_worker" ||
|
|
47
|
+
urlInfo.subtype === "shared_worker",
|
|
48
|
+
})
|
|
49
|
+
const magicSource = createMagicSource(urlInfo.content)
|
|
50
|
+
magicSource.prepend(clientCode)
|
|
51
|
+
return magicSource.toContentAndSourcemap()
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const generateClientCodeForGlobals = ({ isWebWorker = false, globals }) => {
|
|
55
|
+
const globalName = isWebWorker ? "self" : "window"
|
|
56
|
+
return `Object.assign(${globalName}, ${JSON.stringify(globals, null, " ")});`
|
|
57
|
+
}
|
|
@@ -1,76 +1,24 @@
|
|
|
1
|
-
import {
|
|
2
|
-
parseHtmlString,
|
|
3
|
-
injectScriptAsEarlyAsPossible,
|
|
4
|
-
createHtmlNode,
|
|
5
|
-
stringifyHtmlAst,
|
|
6
|
-
} from "@jsenv/utils/html_ast/html_ast.js"
|
|
7
|
-
import { createMagicSource } from "@jsenv/utils/sourcemap/magic_source.js"
|
|
8
|
-
import { isWebWorkerUrlInfo } from "@jsenv/core/src/omega/web_workers.js"
|
|
1
|
+
import { injectGlobals } from "./inject_globals.js"
|
|
9
2
|
|
|
10
|
-
export const jsenvPluginInjectGlobals = (
|
|
11
|
-
if (Object.keys(globals).length === 0) {
|
|
12
|
-
return []
|
|
13
|
-
}
|
|
3
|
+
export const jsenvPluginInjectGlobals = (urlAssociations) => {
|
|
14
4
|
return {
|
|
15
5
|
name: "jsenv:inject_globals",
|
|
16
6
|
appliesDuring: "*",
|
|
17
|
-
transformUrlContent: {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
7
|
+
transformUrlContent: async (urlInfo) => {
|
|
8
|
+
const url = Object.keys(urlAssociations).find((url) => {
|
|
9
|
+
return url === urlInfo.url
|
|
10
|
+
})
|
|
11
|
+
if (!url) {
|
|
12
|
+
return null
|
|
13
|
+
}
|
|
14
|
+
let globals = urlAssociations[url]
|
|
15
|
+
if (typeof globals === "function") {
|
|
16
|
+
globals = await globals()
|
|
17
|
+
}
|
|
18
|
+
if (Object.keys(globals).length === 0) {
|
|
19
|
+
return null
|
|
20
|
+
}
|
|
21
|
+
return injectGlobals(urlInfo, globals)
|
|
21
22
|
},
|
|
22
23
|
}
|
|
23
24
|
}
|
|
24
|
-
|
|
25
|
-
export const injectGlobals = (urlInfo, globals) => {
|
|
26
|
-
if (urlInfo.type === "html") {
|
|
27
|
-
return globalInjectorOnHtmlEntryPoint(urlInfo, globals)
|
|
28
|
-
}
|
|
29
|
-
if (urlInfo.type === "js_classic" || urlInfo.type === "js_module") {
|
|
30
|
-
return globalsInjectorOnJsEntryPoints(urlInfo, globals)
|
|
31
|
-
}
|
|
32
|
-
throw new Error(`cannot inject globals into "${urlInfo.type}"`)
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const globalInjectorOnHtmlEntryPoint = async (urlInfo, globals) => {
|
|
36
|
-
if (!urlInfo.data.isEntryPoint) {
|
|
37
|
-
return null
|
|
38
|
-
}
|
|
39
|
-
// ideally we would inject an importmap but browser support is too low
|
|
40
|
-
// (even worse for worker/service worker)
|
|
41
|
-
// so for now we inject code into entry points
|
|
42
|
-
const htmlAst = parseHtmlString(urlInfo.content, {
|
|
43
|
-
storeOriginalPositions: false,
|
|
44
|
-
})
|
|
45
|
-
const clientCode = generateClientCodeForGlobals({
|
|
46
|
-
globals,
|
|
47
|
-
isWebWorker: false,
|
|
48
|
-
})
|
|
49
|
-
injectScriptAsEarlyAsPossible(
|
|
50
|
-
htmlAst,
|
|
51
|
-
createHtmlNode({
|
|
52
|
-
"tagName": "script",
|
|
53
|
-
"textContent": clientCode,
|
|
54
|
-
"injected-by": "jsenv:inject_globals",
|
|
55
|
-
}),
|
|
56
|
-
)
|
|
57
|
-
return stringifyHtmlAst(htmlAst)
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
const globalsInjectorOnJsEntryPoints = async (urlInfo, globals) => {
|
|
61
|
-
if (!urlInfo.data.isEntryPoint && !urlInfo.data.isWebWorkerEntryPoint) {
|
|
62
|
-
return null
|
|
63
|
-
}
|
|
64
|
-
const clientCode = generateClientCodeForGlobals({
|
|
65
|
-
globals,
|
|
66
|
-
isWebWorker: isWebWorkerUrlInfo(urlInfo),
|
|
67
|
-
})
|
|
68
|
-
const magicSource = createMagicSource(urlInfo.content)
|
|
69
|
-
magicSource.prepend(clientCode)
|
|
70
|
-
return magicSource.toContentAndSourcemap()
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
const generateClientCodeForGlobals = ({ isWebWorker = false, globals }) => {
|
|
74
|
-
const globalName = isWebWorker ? "self" : "window"
|
|
75
|
-
return `Object.assign(${globalName}, ${JSON.stringify(globals, null, " ")});`
|
|
76
|
-
}
|
|
@@ -22,8 +22,8 @@ export const jsenvPluginDataUrls = () => {
|
|
|
22
22
|
} = DATA_URL.parse(urlInfo.url)
|
|
23
23
|
urlInfo.data.base64Flag = base64Flag
|
|
24
24
|
return {
|
|
25
|
-
contentType,
|
|
26
25
|
content: contentFromUrlData({ contentType, base64Flag, urlData }),
|
|
26
|
+
contentType,
|
|
27
27
|
}
|
|
28
28
|
},
|
|
29
29
|
formatUrl: (reference, context) => {
|
|
@@ -40,7 +40,7 @@ export const jsenvPluginDataUrls = () => {
|
|
|
40
40
|
return reference.generatedUrl
|
|
41
41
|
}
|
|
42
42
|
const specifier = DATA_URL.stringify({
|
|
43
|
-
contentType: urlInfo.
|
|
43
|
+
contentType: urlInfo.headers["content-type"],
|
|
44
44
|
base64Flag: urlInfo.data.base64Flag,
|
|
45
45
|
data: urlInfo.data.base64Flag
|
|
46
46
|
? dataToBase64(urlInfo.content)
|
|
@@ -26,10 +26,10 @@ const jsenvPluginInlineUrls = () => {
|
|
|
26
26
|
return null
|
|
27
27
|
}
|
|
28
28
|
return {
|
|
29
|
-
contentType: urlInfo.contentType,
|
|
30
29
|
// we want to fetch the original content otherwise we might re-cook
|
|
31
30
|
// content already cooked
|
|
32
31
|
content: urlInfo.originalContent,
|
|
32
|
+
contentType: urlInfo.contentType,
|
|
33
33
|
}
|
|
34
34
|
},
|
|
35
35
|
}
|
|
@@ -266,11 +266,11 @@ const returnValueAssertions = [
|
|
|
266
266
|
return { content: valueReturned }
|
|
267
267
|
}
|
|
268
268
|
if (typeof valueReturned === "object") {
|
|
269
|
-
const { shouldHandle, content } = valueReturned
|
|
269
|
+
const { shouldHandle, content, body } = valueReturned
|
|
270
270
|
if (shouldHandle === false) {
|
|
271
271
|
return undefined
|
|
272
272
|
}
|
|
273
|
-
if (typeof content !== "string" && !Buffer.isBuffer(content)) {
|
|
273
|
+
if (typeof content !== "string" && !Buffer.isBuffer(content) && !body) {
|
|
274
274
|
throw new Error(
|
|
275
275
|
`Unexpected "content" returned by plugin: it must be a string or a buffer; got ${content}`,
|
|
276
276
|
)
|
package/src/plugins/plugins.js
CHANGED
|
@@ -10,7 +10,6 @@ import { jsenvPluginInline } from "./inline/jsenv_plugin_inline.js"
|
|
|
10
10
|
import { jsenvPluginHtmlSupervisor } from "./html_supervisor/jsenv_plugin_html_supervisor.js"
|
|
11
11
|
import { jsenvPluginCommonJsGlobals } from "./commonjs_globals/jsenv_plugin_commonjs_globals.js"
|
|
12
12
|
import { jsenvPluginImportMetaScenarios } from "./import_meta_scenarios/jsenv_plugin_import_meta_scenarios.js"
|
|
13
|
-
import { jsenvPluginInjectGlobals } from "./inject_globals/jsenv_plugin_inject_globals.js"
|
|
14
13
|
import { jsenvPluginTranspilation } from "./transpilation/jsenv_plugin_transpilation.js"
|
|
15
14
|
import { jsenvPluginNodeRuntime } from "./node_runtime/jsenv_plugin_node_runtime.js"
|
|
16
15
|
// build only
|
|
@@ -32,7 +31,6 @@ export const getCorePlugins = ({
|
|
|
32
31
|
nodeEsmResolution,
|
|
33
32
|
fileSystemMagicResolution,
|
|
34
33
|
directoryReferenceAllowed,
|
|
35
|
-
injectedGlobals,
|
|
36
34
|
transpilation = true,
|
|
37
35
|
minification = false,
|
|
38
36
|
bundling = false,
|
|
@@ -67,7 +65,6 @@ export const getCorePlugins = ({
|
|
|
67
65
|
}),
|
|
68
66
|
jsenvPluginUrlResolution(),
|
|
69
67
|
jsenvPluginUrlVersion(),
|
|
70
|
-
jsenvPluginInjectGlobals(injectedGlobals),
|
|
71
68
|
jsenvPluginCommonJsGlobals(),
|
|
72
69
|
jsenvPluginImportMetaScenarios(),
|
|
73
70
|
|
|
@@ -123,11 +123,11 @@ const jsenvPluginAsJsClassicConversion = ({
|
|
|
123
123
|
})
|
|
124
124
|
urlInfo.data.jsClassicFormat = jsClassicFormat
|
|
125
125
|
return {
|
|
126
|
+
content,
|
|
127
|
+
contentType: "text/javascript",
|
|
128
|
+
type: "js_classic",
|
|
126
129
|
originalUrl: originalUrlInfo.originalUrl,
|
|
127
130
|
originalContent: originalUrlInfo.originalContent,
|
|
128
|
-
type: "js_classic",
|
|
129
|
-
contentType: "text/javascript",
|
|
130
|
-
content,
|
|
131
131
|
sourcemap,
|
|
132
132
|
}
|
|
133
133
|
},
|
|
@@ -98,14 +98,14 @@ const jsenvPluginAsModules = () => {
|
|
|
98
98
|
}
|
|
99
99
|
const jsonText = JSON.stringify(originalUrlInfo.content.trim())
|
|
100
100
|
return {
|
|
101
|
-
originalUrl: originalUrlInfo.originalUrl,
|
|
102
|
-
originalContent: originalUrlInfo.originalContent,
|
|
103
|
-
type: "js_module",
|
|
104
|
-
contentType: "text/javascript",
|
|
105
101
|
// here we could `export default ${jsonText}`:
|
|
106
102
|
// but js engine are optimized to recognize JSON.parse
|
|
107
103
|
// and use a faster parsing strategy
|
|
108
104
|
content: `export default JSON.parse(${jsonText})`,
|
|
105
|
+
contentType: "text/javascript",
|
|
106
|
+
type: "js_module",
|
|
107
|
+
originalUrl: originalUrlInfo.originalUrl,
|
|
108
|
+
originalContent: originalUrlInfo.originalContent,
|
|
109
109
|
}
|
|
110
110
|
},
|
|
111
111
|
}
|
|
@@ -130,10 +130,6 @@ const jsenvPluginAsModules = () => {
|
|
|
130
130
|
canUseTemplateString: true,
|
|
131
131
|
})
|
|
132
132
|
return {
|
|
133
|
-
originalUrl: originalUrlInfo.originalUrl,
|
|
134
|
-
originalContent: originalUrlInfo.originalContent,
|
|
135
|
-
type: "js_module",
|
|
136
|
-
contentType: "text/javascript",
|
|
137
133
|
content: `import { InlineContent } from ${JSON.stringify(
|
|
138
134
|
inlineContentClientFileUrl,
|
|
139
135
|
)}
|
|
@@ -142,6 +138,10 @@ const jsenvPluginAsModules = () => {
|
|
|
142
138
|
const stylesheet = new CSSStyleSheet()
|
|
143
139
|
stylesheet.replaceSync(inlineContent.text)
|
|
144
140
|
export default stylesheet`,
|
|
141
|
+
contentType: "text/javascript",
|
|
142
|
+
type: "js_module",
|
|
143
|
+
originalUrl: originalUrlInfo.originalUrl,
|
|
144
|
+
originalContent: originalUrlInfo.originalContent,
|
|
145
145
|
}
|
|
146
146
|
},
|
|
147
147
|
}
|
|
@@ -166,16 +166,16 @@ const jsenvPluginAsModules = () => {
|
|
|
166
166
|
canUseTemplateString: true,
|
|
167
167
|
})
|
|
168
168
|
return {
|
|
169
|
-
originalUrl: originalUrlInfo.originalUrl,
|
|
170
|
-
originalContent: originalUrlInfo.originalContent,
|
|
171
|
-
type: "js_module",
|
|
172
|
-
contentType: "text/javascript",
|
|
173
169
|
content: `import { InlineContent } from ${JSON.stringify(
|
|
174
170
|
inlineContentClientFileUrl,
|
|
175
171
|
)}
|
|
176
172
|
|
|
177
173
|
const inlineContent = new InlineContent(${textPlain}, { type: "text/plain" })
|
|
178
174
|
export default inlineContent.text`,
|
|
175
|
+
contentType: "text/javascript",
|
|
176
|
+
type: "js_module",
|
|
177
|
+
originalUrl: originalUrlInfo.originalUrl,
|
|
178
|
+
originalContent: originalUrlInfo.originalContent,
|
|
179
179
|
}
|
|
180
180
|
},
|
|
181
181
|
}
|
package/src/test/execute_plan.js
CHANGED
|
@@ -57,7 +57,6 @@ export const executePlan = async (
|
|
|
57
57
|
scenario,
|
|
58
58
|
sourcemaps,
|
|
59
59
|
plugins,
|
|
60
|
-
injectedGlobals,
|
|
61
60
|
nodeEsmResolution,
|
|
62
61
|
fileSystemMagicResolution,
|
|
63
62
|
transpilation,
|
|
@@ -144,7 +143,6 @@ export const executePlan = async (
|
|
|
144
143
|
htmlSupervisor: true,
|
|
145
144
|
nodeEsmResolution,
|
|
146
145
|
fileSystemMagicResolution,
|
|
147
|
-
injectedGlobals,
|
|
148
146
|
transpilation: {
|
|
149
147
|
...transpilation,
|
|
150
148
|
getCustomBabelPlugins: ({ clientRuntimeCompat }) => {
|
|
@@ -83,7 +83,6 @@ export const executeTestPlan = async ({
|
|
|
83
83
|
|
|
84
84
|
sourcemaps = "inline",
|
|
85
85
|
plugins = [],
|
|
86
|
-
injectedGlobals,
|
|
87
86
|
nodeEsmResolution,
|
|
88
87
|
fileSystemMagicResolution,
|
|
89
88
|
writeGeneratedFiles = false,
|
|
@@ -168,7 +167,6 @@ export const executeTestPlan = async ({
|
|
|
168
167
|
scenario: "test",
|
|
169
168
|
sourcemaps,
|
|
170
169
|
plugins,
|
|
171
|
-
injectedGlobals,
|
|
172
170
|
nodeEsmResolution,
|
|
173
171
|
fileSystemMagicResolution,
|
|
174
172
|
writeGeneratedFiles,
|