@jsenv/core 40.12.7 → 40.12.8

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.
@@ -0,0 +1,119 @@
1
+ import { bundleJsModules } from "@jsenv/plugin-bundling";
2
+ import { injectQueryParams, injectQueryParamsIntoSpecifier } from "@jsenv/urls";
3
+
4
+ const PACKAGE_BUNDLE_QUERY_PARAM = "package_bundle";
5
+ const PACKAGE_NO_BUNDLE_QUERY_PARAM = "package_no_bundle";
6
+ const DYNAMIC_IMPORT_QUERY_PARAM = "dynamic_import";
7
+
8
+ export const jsenvPluginWorkspaceBundle = ({ packageDirectory }) => {
9
+ return {
10
+ name: "jsenv:workspace_bundle",
11
+ appliesDuring: "dev",
12
+ redirectReference: (reference) => {
13
+ if (!reference.url.startsWith("file:")) {
14
+ return null;
15
+ }
16
+ if (reference.searchParams.has(PACKAGE_BUNDLE_QUERY_PARAM)) {
17
+ return null;
18
+ }
19
+ if (reference.searchParams.has(PACKAGE_NO_BUNDLE_QUERY_PARAM)) {
20
+ return null;
21
+ }
22
+ if (
23
+ reference.ownerUrlInfo.searchParams.has(PACKAGE_NO_BUNDLE_QUERY_PARAM)
24
+ ) {
25
+ // we're cooking the bundle, without this check we would have infinite recursion to try to bundle
26
+ // we want to propagate the ?package_no_bundle
27
+ const noBundleUrl = injectQueryParams(reference.url, {
28
+ v: undefined,
29
+ [PACKAGE_NO_BUNDLE_QUERY_PARAM]: "",
30
+ });
31
+ // console.log(
32
+ // `redirecting ${reference.url} to ${noBundleUrl} to cook the bundle`,
33
+ // );
34
+ return noBundleUrl;
35
+ }
36
+ const packageDirectoryUrl = packageDirectory.find(reference.url);
37
+ if (!packageDirectoryUrl) {
38
+ return null;
39
+ }
40
+ if (packageDirectoryUrl === packageDirectory.url) {
41
+ // root package, we don't want to bundle
42
+ return null;
43
+ }
44
+ // we make sure we target the bundle version of the package
45
+ // otherwise we might execute some parts of the package code multiple times.
46
+ // so we need to redirect the potential reference to non entry point to the package main entry point
47
+ const packageJSON = packageDirectory.read(packageDirectoryUrl);
48
+ const rootReference = reference.ownerUrlInfo.dependencies.inject({
49
+ type: "js_import",
50
+ specifier: `${packageJSON.name}?${PACKAGE_BUNDLE_QUERY_PARAM}`,
51
+ });
52
+ // console.log(
53
+ // `redirecting ${reference.url} to ${rootReference.url} to target the package bundle version of the package`,
54
+ // );
55
+ const packageMainUrl = rootReference.url;
56
+ return packageMainUrl;
57
+ },
58
+ fetchUrlContent: async (urlInfo) => {
59
+ if (!urlInfo.searchParams.has(PACKAGE_BUNDLE_QUERY_PARAM)) {
60
+ return null;
61
+ }
62
+ const noBundleSpecifier = injectQueryParamsIntoSpecifier(
63
+ urlInfo.firstReference.specifier,
64
+ {
65
+ [PACKAGE_BUNDLE_QUERY_PARAM]: undefined,
66
+ [PACKAGE_NO_BUNDLE_QUERY_PARAM]: "",
67
+ },
68
+ );
69
+ const noBundleUrlInfo = urlInfo.redirect({
70
+ specifier: noBundleSpecifier,
71
+ });
72
+ if (!noBundleUrlInfo) {
73
+ return null;
74
+ }
75
+ await noBundleUrlInfo.cook();
76
+ await noBundleUrlInfo.cookDependencies({
77
+ // we ignore dynamic import to cook lazyly (as browser request the server)
78
+ // these dynamic imports must inherit "?package_bundle"
79
+ // This is done inside rollup for convenience
80
+ ignoreDynamicImport: true,
81
+ });
82
+ const bundleUrlInfos = await bundleJsModules([noBundleUrlInfo], {
83
+ chunks: false,
84
+ buildDirectoryUrl: new URL("./", import.meta.url),
85
+ preserveDynamicImports: true,
86
+ augmentDynamicImportUrlSearchParams: () => {
87
+ return {
88
+ [DYNAMIC_IMPORT_QUERY_PARAM]: "",
89
+ [PACKAGE_BUNDLE_QUERY_PARAM]: "",
90
+ };
91
+ },
92
+ });
93
+ const bundledUrlInfo = bundleUrlInfos[noBundleUrlInfo.url];
94
+ if (urlInfo.context.dev) {
95
+ for (const sourceUrl of bundledUrlInfo.sourceUrls) {
96
+ urlInfo.dependencies.inject({
97
+ isImplicit: true,
98
+ type: "js_url",
99
+ specifier: sourceUrl,
100
+ });
101
+ }
102
+ }
103
+ return {
104
+ content: bundledUrlInfo.content,
105
+ contentType: "text/javascript",
106
+ type: "js_module",
107
+ originalUrl: urlInfo.originalUrl,
108
+ originalContent: bundledUrlInfo.originalContent,
109
+ sourcemap: bundledUrlInfo.sourcemap,
110
+ data: bundledUrlInfo.data,
111
+ };
112
+ },
113
+ // transformReferenceSearchParams: () => {
114
+ // return {
115
+ // [PACKAGE_BUNDLE_QUERY_PARAM]: undefined,
116
+ // };
117
+ // },
118
+ };
119
+ };