@jsenv/core 27.0.0-alpha.72 → 27.0.0-alpha.75

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.
@@ -1,4 +1,4 @@
1
- import objectWithoutPropertiesLoose from "./oose/objectWithoutPropertiesLoose.js";
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
- document.body.removeChild(script)
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
@@ -2459,78 +2459,6 @@ const babelPluginMetadataImportMetaScenarios = () => {
2459
2459
  };
2460
2460
  };
2461
2461
 
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
2462
  const jsenvPluginCssParcel = () => {
2535
2463
  return {
2536
2464
  name: "jsenv:css_parcel",
@@ -2725,10 +2653,9 @@ export default inlineContent.text`
2725
2653
  return [asJsonModule, asCssModule, asTextModule];
2726
2654
  };
2727
2655
 
2728
- const require$4 = createRequire(import.meta.url);
2729
-
2730
- const babelPluginPackagePath = require$4.resolve("@jsenv/babel-plugins");
2656
+ const requireFromJsenv = createRequire(import.meta.url);
2731
2657
 
2658
+ const babelPluginPackagePath = requireFromJsenv.resolve("@jsenv/babel-plugins");
2732
2659
  const babelPluginPackageUrl = pathToFileURL(babelPluginPackagePath);
2733
2660
  const requireBabelPlugin = createRequire(babelPluginPackageUrl);
2734
2661
 
@@ -3112,9 +3039,6 @@ const jsenvPluginAsJsClassicWorkers = ({
3112
3039
  * But ideally babel should not generate this in the first place
3113
3040
  * and prefer to unique identifier based solely on the specifier basename for instance
3114
3041
  */
3115
-
3116
- const require$3 = createRequire(import.meta.url);
3117
-
3118
3042
  const jsenvPluginAsJsClassic = ({
3119
3043
  systemJsInjection
3120
3044
  }) => {
@@ -3254,11 +3178,11 @@ const convertJsModuleToJsClassic = async ({
3254
3178
  } = await applyBabelPlugins({
3255
3179
  babelPlugins: [...(jsClassicFormat === "system" ? [// propposal-dynamic-import required with systemjs for babel8:
3256
3180
  // https://github.com/babel/babel/issues/10746
3257
- require$3("@babel/plugin-proposal-dynamic-import"), [requireBabelPlugin("babel-plugin-transform-async-to-promises"), {
3181
+ requireFromJsenv("@babel/plugin-proposal-dynamic-import"), [requireBabelPlugin("babel-plugin-transform-async-to-promises"), {
3258
3182
  topLevelAwait: "return"
3259
- }], require$3("@babel/plugin-transform-modules-systemjs")] : [[requireBabelPlugin("babel-plugin-transform-async-to-promises"), {
3183
+ }], requireFromJsenv("@babel/plugin-transform-modules-systemjs")] : [[requireBabelPlugin("babel-plugin-transform-async-to-promises"), {
3260
3184
  topLevelAwait: "simple"
3261
- }], babelPluginTransformImportMetaUrl, require$3("@babel/plugin-transform-modules-umd")])],
3185
+ }], babelPluginTransformImportMetaUrl, requireFromJsenv("@babel/plugin-transform-modules-umd")])],
3262
3186
  urlInfo
3263
3187
  });
3264
3188
  let sourcemap = urlInfo.sourcemap;
@@ -5133,9 +5057,6 @@ const jsenvPluginBundling = bundling => {
5133
5057
  };
5134
5058
  };
5135
5059
 
5136
- const require$2 = createRequire(import.meta.url); // https://github.com/kangax/html-minifier#options-quick-reference
5137
-
5138
-
5139
5060
  const minifyHtml = ({
5140
5061
  htmlUrlInfo,
5141
5062
  options
@@ -5144,11 +5065,9 @@ const minifyHtml = ({
5144
5065
  collapseWhitespace = true,
5145
5066
  removeComments = true
5146
5067
  } = options;
5147
-
5148
5068
  const {
5149
5069
  minify
5150
- } = require$2("html-minifier");
5151
-
5070
+ } = requireFromJsenv("html-minifier");
5152
5071
  const htmlMinified = minify(htmlUrlInfo.content, {
5153
5072
  collapseWhitespace,
5154
5073
  removeComments
@@ -6036,7 +5955,6 @@ const getCorePlugins = ({
6036
5955
  nodeEsmResolution,
6037
5956
  fileSystemMagicResolution,
6038
5957
  directoryReferenceAllowed,
6039
- injectedGlobals,
6040
5958
  transpilation = true,
6041
5959
  minification = false,
6042
5960
  bundling = false,
@@ -6066,7 +5984,7 @@ const getCorePlugins = ({
6066
5984
  rootDirectoryUrl,
6067
5985
  runtimeCompat,
6068
5986
  ...nodeEsmResolution
6069
- }), jsenvPluginUrlResolution(), jsenvPluginUrlVersion(), jsenvPluginInjectGlobals(injectedGlobals), jsenvPluginCommonJsGlobals(), jsenvPluginImportMetaScenarios(), jsenvPluginNodeRuntime({
5987
+ }), jsenvPluginUrlResolution(), jsenvPluginUrlVersion(), jsenvPluginCommonJsGlobals(), jsenvPluginImportMetaScenarios(), jsenvPluginNodeRuntime({
6070
5988
  runtimeCompat
6071
5989
  }), jsenvPluginBundling(bundling), jsenvPluginMinification(minification), jsenvPluginImportMetaHot(), ...(clientAutoreload ? [jsenvPluginAutoreload({
6072
5990
  rootDirectoryUrl,
@@ -7965,8 +7883,6 @@ const determineFileUrlForOutDirectory = ({
7965
7883
  // }
7966
7884
  // }
7967
7885
 
7968
- const require$1 = createRequire(import.meta.url);
7969
-
7970
7886
  const parseUserAgentHeader = memoizeByFirstArgument(userAgent => {
7971
7887
  if (userAgent.includes("node-fetch/")) {
7972
7888
  // it's not really node and conceptually we can't assume the node version
@@ -7977,8 +7893,7 @@ const parseUserAgentHeader = memoizeByFirstArgument(userAgent => {
7977
7893
  };
7978
7894
  }
7979
7895
 
7980
- const UA = require$1("@financial-times/polyfill-useragent-normaliser");
7981
-
7896
+ const UA = requireFromJsenv("@financial-times/polyfill-useragent-normaliser");
7982
7897
  const {
7983
7898
  ua
7984
7899
  } = new UA(userAgent);
@@ -8408,11 +8323,10 @@ const startDevServer = async ({
8408
8323
  // (because node cluster won't work)
8409
8324
  devServerAutoreload = typeof process.send !== "function" && !parentPort && !process.env.VSCODE_INSPECTOR_OPTIONS,
8410
8325
  clientFiles = {
8411
- "./**": true,
8412
- "./**/.*/": false,
8326
+ "./src/": true,
8327
+ "./src/**/.*/": false,
8413
8328
  // any folder starting with a dot is ignored (includes .git,.jsenv for instance)
8414
- "./**/dist/": false,
8415
- "./**/node_modules/": false
8329
+ "./src/**/node_modules/": false
8416
8330
  },
8417
8331
  cooldownBetweenFileEvents,
8418
8332
  clientAutoreload = true,
@@ -8429,8 +8343,7 @@ const startDevServer = async ({
8429
8343
  },
8430
8344
  plugins = [],
8431
8345
  urlAnalysis = {},
8432
- htmlSupervisor = true,
8433
- injectedGlobals,
8346
+ htmlSupervisor = false,
8434
8347
  nodeEsmResolution,
8435
8348
  fileSystemMagicResolution,
8436
8349
  transpilation,
@@ -8589,7 +8502,6 @@ const startDevServer = async ({
8589
8502
  runtimeCompat,
8590
8503
  urlAnalysis,
8591
8504
  htmlSupervisor,
8592
- injectedGlobals,
8593
8505
  nodeEsmResolution,
8594
8506
  fileSystemMagicResolution,
8595
8507
  transpilation,
@@ -9147,7 +9059,6 @@ const executePlan = async (plan, {
9147
9059
  scenario,
9148
9060
  sourcemaps,
9149
9061
  plugins,
9150
- injectedGlobals,
9151
9062
  nodeEsmResolution,
9152
9063
  fileSystemMagicResolution,
9153
9064
  transpilation,
@@ -9232,7 +9143,6 @@ const executePlan = async (plan, {
9232
9143
  htmlSupervisor: true,
9233
9144
  nodeEsmResolution,
9234
9145
  fileSystemMagicResolution,
9235
- injectedGlobals,
9236
9146
  transpilation: { ...transpilation,
9237
9147
  getCustomBabelPlugins: ({
9238
9148
  clientRuntimeCompat
@@ -9750,7 +9660,6 @@ const executeTestPlan = async ({
9750
9660
  coverageSkipFull = false,
9751
9661
  sourcemaps = "inline",
9752
9662
  plugins = [],
9753
- injectedGlobals,
9754
9663
  nodeEsmResolution,
9755
9664
  fileSystemMagicResolution,
9756
9665
  writeGeneratedFiles = false,
@@ -9831,7 +9740,6 @@ const executeTestPlan = async ({
9831
9740
  scenario: "test",
9832
9741
  sourcemaps,
9833
9742
  plugins,
9834
- injectedGlobals,
9835
9743
  nodeEsmResolution,
9836
9744
  fileSystemMagicResolution,
9837
9745
  writeGeneratedFiles,
@@ -10693,15 +10601,11 @@ const getDebugInfo = processOptions => {
10693
10601
  // return typeof process.env.VSCODE_PID === "string"
10694
10602
  // }
10695
10603
 
10696
- const require = createRequire(import.meta.url); // see also https://github.com/sindresorhus/execa/issues/96
10697
-
10698
-
10699
10604
  const killProcessTree = async (processId, {
10700
10605
  signal,
10701
10606
  timeout = 2000
10702
10607
  }) => {
10703
- const pidtree = require("pidtree");
10704
-
10608
+ const pidtree = requireFromJsenv("pidtree");
10705
10609
  let descendantProcessIds;
10706
10610
 
10707
10611
  try {
@@ -11915,7 +11819,6 @@ const build = async ({
11915
11819
  nodeEsmResolution,
11916
11820
  fileSystemMagicResolution,
11917
11821
  directoryReferenceAllowed,
11918
- injectedGlobals,
11919
11822
  transpilation = {},
11920
11823
  bundling = true,
11921
11824
  minification = true,
@@ -11924,11 +11827,10 @@ const build = async ({
11924
11827
  // "filename", "search_param"
11925
11828
  lineBreakNormalization = process.platform === "win32",
11926
11829
  clientFiles = {
11927
- "./**": true,
11928
- "./**/.*/": false,
11830
+ "./src/": true,
11831
+ "./src/**/.*/": false,
11929
11832
  // any folder starting with a dot is ignored (includes .git,.jsenv for instance)
11930
- "./dist/": false,
11931
- "./**/node_modules/": false
11833
+ "./src/**/node_modules/": false
11932
11834
  },
11933
11835
  cooldownBetweenFileEvents,
11934
11836
  watch = false,
@@ -12028,7 +11930,6 @@ build ${entryPointKeys.length} entry points`);
12028
11930
  nodeEsmResolution,
12029
11931
  fileSystemMagicResolution,
12030
11932
  directoryReferenceAllowed,
12031
- injectedGlobals,
12032
11933
  transpilation: { ...transpilation,
12033
11934
  babelHelpersAsImport: !useExplicitJsClassicConversion,
12034
11935
  jsModuleAsJsClassic: false
@@ -13323,7 +13224,6 @@ const execute = async ({
13323
13224
  plugins = [],
13324
13225
  nodeEsmResolution,
13325
13226
  fileSystemMagicResolution,
13326
- injectedGlobals,
13327
13227
  transpilation,
13328
13228
  htmlSupervisor = true,
13329
13229
  writeGeneratedFiles = false,
@@ -13377,7 +13277,6 @@ const execute = async ({
13377
13277
  scenario,
13378
13278
  runtimeCompat,
13379
13279
  htmlSupervisor,
13380
- injectedGlobals,
13381
13280
  nodeEsmResolution,
13382
13281
  fileSystemMagicResolution,
13383
13282
  transpilation
@@ -13452,4 +13351,81 @@ const execute = async ({
13452
13351
  }
13453
13352
  };
13454
13353
 
13455
- export { build, chromium, chromiumIsolatedTab, defaultCoverageConfig, execute, executeTestPlan, firefox, firefoxIsolatedTab, injectGlobals, nodeProcess, startBuildServer, startDevServer, webkit, webkitIsolatedTab };
13354
+ const injectGlobals = (urlInfo, globals) => {
13355
+ if (urlInfo.type === "html") {
13356
+ return globalInjectorOnHtml(urlInfo, globals);
13357
+ }
13358
+
13359
+ if (urlInfo.type === "js_classic" || urlInfo.type === "js_module") {
13360
+ return globalsInjectorOnJs(urlInfo, globals);
13361
+ }
13362
+
13363
+ throw new Error(`cannot inject globals into "${urlInfo.type}"`);
13364
+ };
13365
+
13366
+ const globalInjectorOnHtml = async (urlInfo, globals) => {
13367
+ // ideally we would inject an importmap but browser support is too low
13368
+ // (even worse for worker/service worker)
13369
+ // so for now we inject code into entry points
13370
+ const htmlAst = parseHtmlString(urlInfo.content, {
13371
+ storeOriginalPositions: false
13372
+ });
13373
+ const clientCode = generateClientCodeForGlobals({
13374
+ globals,
13375
+ isWebWorker: false
13376
+ });
13377
+ injectScriptAsEarlyAsPossible(htmlAst, createHtmlNode({
13378
+ "tagName": "script",
13379
+ "textContent": clientCode,
13380
+ "injected-by": "jsenv:inject_globals"
13381
+ }));
13382
+ return stringifyHtmlAst(htmlAst);
13383
+ };
13384
+
13385
+ const globalsInjectorOnJs = async (urlInfo, globals) => {
13386
+ const clientCode = generateClientCodeForGlobals({
13387
+ globals,
13388
+ isWebWorker: urlInfo.subtype === "worker" || urlInfo.subtype === "service_worker" || urlInfo.subtype === "shared_worker"
13389
+ });
13390
+ const magicSource = createMagicSource(urlInfo.content);
13391
+ magicSource.prepend(clientCode);
13392
+ return magicSource.toContentAndSourcemap();
13393
+ };
13394
+
13395
+ const generateClientCodeForGlobals = ({
13396
+ isWebWorker = false,
13397
+ globals
13398
+ }) => {
13399
+ const globalName = isWebWorker ? "self" : "window";
13400
+ return `Object.assign(${globalName}, ${JSON.stringify(globals, null, " ")});`;
13401
+ };
13402
+
13403
+ const jsenvPluginInjectGlobals = urlAssociations => {
13404
+ return {
13405
+ name: "jsenv:inject_globals",
13406
+ appliesDuring: "*",
13407
+ transformUrlContent: async urlInfo => {
13408
+ const url = Object.keys(urlAssociations).find(url => {
13409
+ return url === urlInfo.url;
13410
+ });
13411
+
13412
+ if (!url) {
13413
+ return null;
13414
+ }
13415
+
13416
+ let globals = urlAssociations[url];
13417
+
13418
+ if (typeof globals === "function") {
13419
+ globals = await globals();
13420
+ }
13421
+
13422
+ if (Object.keys(globals).length === 0) {
13423
+ return null;
13424
+ }
13425
+
13426
+ return injectGlobals(urlInfo, globals);
13427
+ }
13428
+ };
13429
+ };
13430
+
13431
+ 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.72",
3
+ "version": "27.0.0-alpha.75",
4
4
  "description": "Tool to develop, test and build js projects",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -24,17 +24,16 @@
24
24
  "exports": {
25
25
  ".": {
26
26
  "import": {
27
- "development": "./main.js",
27
+ "development": "./src/main.js",
28
28
  "default": "./dist/main.js"
29
29
  }
30
30
  },
31
31
  "./*": "./*"
32
32
  },
33
- "main": "main.js",
33
+ "main": "./src/main.js",
34
34
  "files": [
35
35
  "/dist/",
36
- "/src/",
37
- "/main.js"
36
+ "/src/"
38
37
  ],
39
38
  "workspaces": [
40
39
  "./packages/*",
@@ -69,7 +68,7 @@
69
68
  "@financial-times/polyfill-useragent-normaliser": "2.0.1",
70
69
  "@jsenv/abort": "4.1.2",
71
70
  "@jsenv/babel-plugins": "1.0.3",
72
- "@jsenv/filesystem": "4.0.3",
71
+ "@jsenv/filesystem": "4.0.5",
73
72
  "@jsenv/importmap": "1.2.0",
74
73
  "@jsenv/integrity": "0.0.1",
75
74
  "@jsenv/log": "1.6.3",
@@ -77,9 +76,9 @@
77
76
  "@jsenv/node-esm-resolution": "0.0.10",
78
77
  "@jsenv/server": "12.6.3",
79
78
  "@jsenv/uneval": "1.6.0",
80
- "@jsenv/utils": "1.8.6",
79
+ "@jsenv/utils": "1.8.8",
81
80
  "@jsenv/url-meta": "7.0.0",
82
- "@jsenv/urls": "1.2.0",
81
+ "@jsenv/urls": "1.2.2",
83
82
  "construct-style-sheets-polyfill": "3.1.0",
84
83
  "cssnano": "5.1.7",
85
84
  "cssnano-preset-default": "5.2.7",
@@ -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
- "./**": true,
122
- "./**/.*/": false, // any folder starting with a dot is ignored (includes .git,.jsenv for instance)
123
- "./dist/": false,
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,
@@ -44,10 +44,9 @@ export const startDevServer = async ({
44
44
  !parentPort &&
45
45
  !process.env.VSCODE_INSPECTOR_OPTIONS,
46
46
  clientFiles = {
47
- "./**": true,
48
- "./**/.*/": false, // any folder starting with a dot is ignored (includes .git,.jsenv for instance)
49
- "./**/dist/": false,
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 = true,
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,
@@ -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,
@@ -1,13 +1,11 @@
1
- import { createRequire } from "node:module"
2
-
3
- const require = createRequire(import.meta.url)
1
+ import { requireFromJsenv } from "@jsenv/core/src/require_from_jsenv.js"
4
2
 
5
3
  // see also https://github.com/sindresorhus/execa/issues/96
6
4
  export const killProcessTree = async (
7
5
  processId,
8
6
  { signal, timeout = 2000 },
9
7
  ) => {
10
- const pidtree = require("pidtree")
8
+ const pidtree = requireFromJsenv("pidtree")
11
9
 
12
10
  let descendantProcessIds
13
11
  try {
package/src/main.js ADDED
@@ -0,0 +1,27 @@
1
+ // dev
2
+ export { startDevServer } from "./dev/start_dev_server.js"
3
+ // test
4
+ export {
5
+ executeTestPlan,
6
+ defaultCoverageConfig,
7
+ } from "./test/execute_test_plan.js"
8
+ export {
9
+ chromium,
10
+ chromiumIsolatedTab,
11
+ } from "./execute/runtimes/browsers/chromium.js"
12
+ export {
13
+ firefox,
14
+ firefoxIsolatedTab,
15
+ } from "./execute/runtimes/browsers/firefox.js"
16
+ export {
17
+ webkit,
18
+ webkitIsolatedTab,
19
+ } from "./execute/runtimes/browsers/webkit.js"
20
+ export { nodeProcess } from "./execute/runtimes/node/node_process.js"
21
+ // build
22
+ export { build } from "./build/build.js"
23
+ export { startBuildServer } from "./build/start_build_server.js"
24
+
25
+ // advanced
26
+ export { execute } from "./execute/execute.js"
27
+ export { jsenvPluginInjectGlobals } from "./plugins/inject_globals/jsenv_plugin_inject_globals.js"
@@ -1,8 +1,5 @@
1
- import { createRequire } from "node:module"
2
-
3
1
  import { memoizeByFirstArgument } from "@jsenv/utils/memoize/memoize_by_first_argument.js"
4
-
5
- const require = createRequire(import.meta.url)
2
+ import { requireFromJsenv } from "@jsenv/core/src/require_from_jsenv.js"
6
3
 
7
4
  export const parseUserAgentHeader = memoizeByFirstArgument((userAgent) => {
8
5
  if (userAgent.includes("node-fetch/")) {
@@ -13,7 +10,7 @@ export const parseUserAgentHeader = memoizeByFirstArgument((userAgent) => {
13
10
  runtimeVersion: process.version.slice(1),
14
11
  }
15
12
  }
16
- const UA = require("@financial-times/polyfill-useragent-normaliser")
13
+ const UA = requireFromJsenv("@financial-times/polyfill-useragent-normaliser")
17
14
  const { ua } = new UA(userAgent)
18
15
  const { family, major, minor, patch } = ua
19
16
  return {
@@ -32,7 +32,10 @@ window.__html_supervisor__ = {
32
32
  lastWindowError = e.error
33
33
  }
34
34
  const cleanup = () => {
35
- document.body.removeChild(script)
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 = (globals = {}) => {
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
- html: injectGlobals,
19
- js_classic: injectGlobals,
20
- js_module: injectGlobals,
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
- }
@@ -1,12 +1,10 @@
1
- import { createRequire } from "node:module"
2
-
3
- const require = createRequire(import.meta.url)
1
+ import { requireFromJsenv } from "@jsenv/core/src/require_from_jsenv.js"
4
2
 
5
3
  // https://github.com/kangax/html-minifier#options-quick-reference
6
4
  export const minifyHtml = ({ htmlUrlInfo, options } = {}) => {
7
5
  const { collapseWhitespace = true, removeComments = true } = options
8
6
 
9
- const { minify } = require("html-minifier")
7
+ const { minify } = requireFromJsenv("html-minifier")
10
8
  const htmlMinified = minify(htmlUrlInfo.content, {
11
9
  collapseWhitespace,
12
10
  removeComments,
@@ -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
 
@@ -11,10 +11,10 @@
11
11
  * and prefer to unique identifier based solely on the specifier basename for instance
12
12
  */
13
13
 
14
- import { createRequire } from "node:module"
15
14
  import { urlToFilename, injectQueryParams } from "@jsenv/urls"
16
15
  import { readFileSync } from "@jsenv/filesystem"
17
16
 
17
+ import { requireFromJsenv } from "@jsenv/core/src/require_from_jsenv.js"
18
18
  import { applyBabelPlugins } from "@jsenv/utils/js_ast/apply_babel_plugins.js"
19
19
  import { createMagicSource } from "@jsenv/utils/sourcemap/magic_source.js"
20
20
  import { composeTwoSourcemaps } from "@jsenv/utils/sourcemap/sourcemap_composition_v3.js"
@@ -24,8 +24,6 @@ import { babelPluginTransformImportMetaUrl } from "./helpers/babel_plugin_transf
24
24
  import { jsenvPluginAsJsClassicHtml } from "./jsenv_plugin_as_js_classic_html.js"
25
25
  import { jsenvPluginAsJsClassicWorkers } from "./jsenv_plugin_as_js_classic_workers.js"
26
26
 
27
- const require = createRequire(import.meta.url)
28
-
29
27
  export const jsenvPluginAsJsClassic = ({ systemJsInjection }) => {
30
28
  const systemJsClientFileUrl = new URL(
31
29
  "./client/s.js?js_classic",
@@ -171,14 +169,14 @@ const convertJsModuleToJsClassic = async ({
171
169
  ? [
172
170
  // propposal-dynamic-import required with systemjs for babel8:
173
171
  // https://github.com/babel/babel/issues/10746
174
- require("@babel/plugin-proposal-dynamic-import"),
172
+ requireFromJsenv("@babel/plugin-proposal-dynamic-import"),
175
173
  [
176
174
  requireBabelPlugin("babel-plugin-transform-async-to-promises"),
177
175
  {
178
176
  topLevelAwait: "return",
179
177
  },
180
178
  ],
181
- require("@babel/plugin-transform-modules-systemjs"),
179
+ requireFromJsenv("@babel/plugin-transform-modules-systemjs"),
182
180
  ]
183
181
  : [
184
182
  [
@@ -188,7 +186,7 @@ const convertJsModuleToJsClassic = async ({
188
186
  },
189
187
  ],
190
188
  babelPluginTransformImportMetaUrl,
191
- require("@babel/plugin-transform-modules-umd"),
189
+ requireFromJsenv("@babel/plugin-transform-modules-umd"),
192
190
  ]),
193
191
  ],
194
192
  urlInfo,
@@ -1,8 +1,8 @@
1
1
  import { createRequire } from "node:module"
2
2
  import { pathToFileURL } from "node:url"
3
+ import { requireFromJsenv } from "@jsenv/core/src/require_from_jsenv.js"
3
4
 
4
- const require = createRequire(import.meta.url)
5
- const babelPluginPackagePath = require.resolve("@jsenv/babel-plugins")
5
+ const babelPluginPackagePath = requireFromJsenv.resolve("@jsenv/babel-plugins")
6
6
  const babelPluginPackageUrl = pathToFileURL(babelPluginPackagePath)
7
7
 
8
8
  export const requireBabelPlugin = createRequire(babelPluginPackageUrl)
@@ -0,0 +1,3 @@
1
+ import { createRequire } from "node:module"
2
+
3
+ export const requireFromJsenv = createRequire(import.meta.url)
@@ -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,
package/main.js DELETED
@@ -1,27 +0,0 @@
1
- // dev
2
- export { startDevServer } from "./src/dev/start_dev_server.js"
3
- // test
4
- export {
5
- executeTestPlan,
6
- defaultCoverageConfig,
7
- } from "./src/test/execute_test_plan.js"
8
- export {
9
- chromium,
10
- chromiumIsolatedTab,
11
- } from "./src/execute/runtimes/browsers/chromium.js"
12
- export {
13
- firefox,
14
- firefoxIsolatedTab,
15
- } from "./src/execute/runtimes/browsers/firefox.js"
16
- export {
17
- webkit,
18
- webkitIsolatedTab,
19
- } from "./src/execute/runtimes/browsers/webkit.js"
20
- export { nodeProcess } from "./src/execute/runtimes/node/node_process.js"
21
- // build
22
- export { build } from "./src/build/build.js"
23
- export { startBuildServer } from "./src/build/start_build_server.js"
24
-
25
- // advanced
26
- export { execute } from "./src/execute/execute.js"
27
- export { injectGlobals } from "./src/plugins/inject_globals/jsenv_plugin_inject_globals.js"