@ohos-ports/lwrjs-static 0.24.0-beta.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.
Files changed (29) hide show
  1. package/LICENSE +10 -0
  2. package/build/cjs/index.cjs +22 -0
  3. package/build/cjs/providers/static-asset-provider.cjs +103 -0
  4. package/build/cjs/providers/static-bundle-provider.cjs +246 -0
  5. package/build/cjs/providers/static-module-provider.cjs +138 -0
  6. package/build/cjs/providers/static-resource-provider.cjs +73 -0
  7. package/build/cjs/site-metadata.cjs +215 -0
  8. package/build/cjs/tools/dedupe-bundles.cjs +108 -0
  9. package/build/cjs/transformers/mrt-static-uri-transformer.cjs +62 -0
  10. package/build/cjs/utils/decision-tree.cjs +209 -0
  11. package/build/es/index.d.ts +2 -0
  12. package/build/es/index.js +2 -0
  13. package/build/es/providers/static-asset-provider.d.ts +16 -0
  14. package/build/es/providers/static-asset-provider.js +90 -0
  15. package/build/es/providers/static-bundle-provider.d.ts +59 -0
  16. package/build/es/providers/static-bundle-provider.js +257 -0
  17. package/build/es/providers/static-module-provider.d.ts +15 -0
  18. package/build/es/providers/static-module-provider.js +118 -0
  19. package/build/es/providers/static-resource-provider.d.ts +10 -0
  20. package/build/es/providers/static-resource-provider.js +53 -0
  21. package/build/es/site-metadata.d.ts +80 -0
  22. package/build/es/site-metadata.js +243 -0
  23. package/build/es/tools/dedupe-bundles.d.ts +3 -0
  24. package/build/es/tools/dedupe-bundles.js +89 -0
  25. package/build/es/transformers/mrt-static-uri-transformer.d.ts +3 -0
  26. package/build/es/transformers/mrt-static-uri-transformer.js +40 -0
  27. package/build/es/utils/decision-tree.d.ts +35 -0
  28. package/build/es/utils/decision-tree.js +287 -0
  29. package/package.json +77 -0
@@ -0,0 +1,215 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getProtoOf = Object.getPrototypeOf;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
7
+ var __markAsModule = (target) => __defProp(target, "__esModule", {value: true});
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, {get: all[name], enumerable: true});
11
+ };
12
+ var __exportStar = (target, module2, desc) => {
13
+ if (module2 && typeof module2 === "object" || typeof module2 === "function") {
14
+ for (let key of __getOwnPropNames(module2))
15
+ if (!__hasOwnProp.call(target, key) && key !== "default")
16
+ __defProp(target, key, {get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable});
17
+ }
18
+ return target;
19
+ };
20
+ var __toModule = (module2) => {
21
+ return __exportStar(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? {get: () => module2.default, enumerable: true} : {value: module2, enumerable: true})), module2);
22
+ };
23
+
24
+ // packages/@lwrjs/static/src/site-metadata.ts
25
+ __markAsModule(exports);
26
+ __export(exports, {
27
+ SITE_LOCALE_PREFIX: () => SITE_LOCALE_PREFIX,
28
+ SITE_SSR_PREFIX: () => SITE_SSR_PREFIX,
29
+ SITE_VERSION_PREFIX: () => SITE_VERSION_PREFIX,
30
+ SiteMetadataImpl: () => SiteMetadataImpl,
31
+ getSiteBundleId: () => getSiteBundleId,
32
+ getSiteResourceId: () => getSiteResourceId,
33
+ parseSiteId: () => parseSiteId,
34
+ resolveStaticBundleVersion: () => resolveStaticBundleVersion
35
+ });
36
+ var import_path = __toModule(require("path"));
37
+ var import_fs_extra = __toModule(require("fs-extra"));
38
+ var import_diagnostics = __toModule(require("@lwrjs/diagnostics"));
39
+ var import_decision_tree = __toModule(require("./utils/decision-tree.cjs"));
40
+ var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
41
+ var SITE_METADATA_PATH = ".metadata";
42
+ var STATIC_BUNDLE_METADATA_PATH = import_path.default.join(SITE_METADATA_PATH, "/bundle-metadata.json");
43
+ var DEBUG_STATIC_BUNDLE_METADATA_PATH = import_path.default.join(SITE_METADATA_PATH, "/bundle-metadata-debug.json");
44
+ var STATIC_RESOURCE_METADATA_PATH = import_path.default.join(SITE_METADATA_PATH, "/resource-metadata.json");
45
+ var DEBUG_STATIC_RESOURCE_METADATA_PATH = import_path.default.join(SITE_METADATA_PATH, "/resource-metadata-debug.json");
46
+ var STATIC_ASSET_METADATA_PATH = import_path.default.join(SITE_METADATA_PATH, "/asset-metadata.json");
47
+ var SITE_VERSION_PREFIX = `|${import_shared_utils.VERSION_SIGIL}/`;
48
+ var SITE_LOCALE_PREFIX = `|${import_shared_utils.LOCALE_SIGIL}/`;
49
+ var SITE_SSR_PREFIX = `|${import_shared_utils.SSR_SIGIL}`;
50
+ var SiteMetadataImpl = class {
51
+ constructor(options) {
52
+ this.options = options;
53
+ this.siteBundles = this.readStaticBundleMetadata(options.rootDir, STATIC_BUNDLE_METADATA_PATH);
54
+ this.debugSiteBundles = this.readStaticBundleMetadata(options.rootDir, DEBUG_STATIC_BUNDLE_METADATA_PATH);
55
+ this.siteResources = this.readStaticResourceMetadata(options.rootDir, STATIC_RESOURCE_METADATA_PATH);
56
+ this.debugSiteResources = this.readStaticResourceMetadata(options.rootDir, DEBUG_STATIC_RESOURCE_METADATA_PATH);
57
+ this.siteAssets = this.readStaticAssetsMetadata(options.rootDir, STATIC_ASSET_METADATA_PATH);
58
+ }
59
+ getSiteRootDir() {
60
+ return this.options.rootDir;
61
+ }
62
+ getSiteBundles() {
63
+ return this.siteBundles;
64
+ }
65
+ getDebugSiteBundles() {
66
+ return this.debugSiteBundles;
67
+ }
68
+ getSiteResources() {
69
+ return this.siteResources;
70
+ }
71
+ getDebugSiteResources() {
72
+ return this.debugSiteResources;
73
+ }
74
+ getSiteAssets() {
75
+ return this.siteAssets;
76
+ }
77
+ getSiteBundlesDecisionTree() {
78
+ if (!this.bundleDecisionTree) {
79
+ this.bundleDecisionTree = new import_decision_tree.default();
80
+ const localeFallbacks = (0, import_decision_tree.createFallbackMap)(this.options.i18n);
81
+ for (const [key, bundle] of Object.entries(this.siteBundles.bundles)) {
82
+ this.bundleDecisionTree.insert(key, bundle, false, localeFallbacks);
83
+ }
84
+ for (const [key, bundle] of Object.entries(this.debugSiteBundles.bundles)) {
85
+ this.bundleDecisionTree.insert(key, bundle, true, localeFallbacks);
86
+ }
87
+ }
88
+ return this.bundleDecisionTree;
89
+ }
90
+ getSiteResourcesDecisionTree() {
91
+ if (!this.resourceDecisionTree) {
92
+ this.resourceDecisionTree = new import_decision_tree.default();
93
+ for (const [key, resource] of Object.entries(this.siteResources.resources)) {
94
+ this.resourceDecisionTree.insert(key, resource, false);
95
+ }
96
+ for (const [key, resource] of Object.entries(this.debugSiteResources.resources)) {
97
+ this.resourceDecisionTree.insert(key, resource, true);
98
+ }
99
+ }
100
+ return this.resourceDecisionTree;
101
+ }
102
+ async persistSiteMetadata() {
103
+ const siteMetadataPath = import_path.default.join(this.options.rootDir, SITE_METADATA_PATH);
104
+ if (siteMetadataPath.indexOf("__skip_directory_creation__") !== -1)
105
+ return;
106
+ try {
107
+ if (!import_fs_extra.default.existsSync(siteMetadataPath)) {
108
+ await import_fs_extra.default.mkdir(siteMetadataPath, {recursive: true});
109
+ }
110
+ const bundleMetadataPath = import_path.default.join(this.options.rootDir, STATIC_BUNDLE_METADATA_PATH);
111
+ await import_fs_extra.default.writeJSON(bundleMetadataPath, this.siteBundles, {spaces: 2});
112
+ const debugBundleMetadataPath = import_path.default.join(this.options.rootDir, DEBUG_STATIC_BUNDLE_METADATA_PATH);
113
+ await import_fs_extra.default.writeJSON(debugBundleMetadataPath, this.debugSiteBundles, {spaces: 2});
114
+ const resourceMetadataPath = import_path.default.join(this.options.rootDir, STATIC_RESOURCE_METADATA_PATH);
115
+ await import_fs_extra.default.writeJSON(resourceMetadataPath, this.siteResources, {spaces: 2});
116
+ const debugResourceMetadataPath = import_path.default.join(this.options.rootDir, DEBUG_STATIC_RESOURCE_METADATA_PATH);
117
+ await import_fs_extra.default.writeJSON(debugResourceMetadataPath, this.debugSiteResources, {spaces: 2});
118
+ const assetMetadataPath = import_path.default.join(this.options.rootDir, STATIC_ASSET_METADATA_PATH);
119
+ return import_fs_extra.default.writeJSON(assetMetadataPath, this.siteAssets, {spaces: 2});
120
+ } catch (err) {
121
+ import_diagnostics.logger.error(`[SiteMetadata] Failed to save site metadata ${siteMetadataPath}`);
122
+ import_diagnostics.logger.error(err);
123
+ }
124
+ }
125
+ readStaticBundleMetadata(staticRoot, metadataPath) {
126
+ let bundleMetadataPath;
127
+ let siteBundles = {bundles: {}};
128
+ try {
129
+ bundleMetadataPath = import_path.default.join(staticRoot, metadataPath);
130
+ const savedMetadata = import_fs_extra.default.readJSONSync(bundleMetadataPath);
131
+ siteBundles = savedMetadata;
132
+ } catch (error) {
133
+ if (error.code === "ENOENT") {
134
+ import_diagnostics.logger.debug({
135
+ label: `SiteMetadata`,
136
+ message: `Failed to load Static Bundle Metadata: ${bundleMetadataPath}`
137
+ });
138
+ } else {
139
+ throw error;
140
+ }
141
+ }
142
+ return siteBundles;
143
+ }
144
+ readStaticResourceMetadata(staticRoot, metadataPath) {
145
+ let resourceMetadataPath;
146
+ let siteResources = {resources: {}};
147
+ try {
148
+ resourceMetadataPath = import_path.default.join(staticRoot, metadataPath);
149
+ const savedMetadata = import_fs_extra.default.readJSONSync(resourceMetadataPath);
150
+ siteResources = savedMetadata;
151
+ } catch (error) {
152
+ if (error.code === "ENOENT") {
153
+ import_diagnostics.logger.debug({
154
+ label: `SiteMetadata`,
155
+ message: `Failed to load Static Resource Metadata: ${resourceMetadataPath}`
156
+ });
157
+ } else {
158
+ throw error;
159
+ }
160
+ }
161
+ return siteResources;
162
+ }
163
+ readStaticAssetsMetadata(staticRoot, metadataPath) {
164
+ let assetMetadataPath;
165
+ let siteAssets = {
166
+ assets: {}
167
+ };
168
+ try {
169
+ assetMetadataPath = import_path.default.join(staticRoot, metadataPath);
170
+ siteAssets = import_fs_extra.default.readJSONSync(assetMetadataPath);
171
+ } catch (error) {
172
+ if (error.code === "ENOENT") {
173
+ import_diagnostics.logger.debug({
174
+ label: `SiteMetadata`,
175
+ message: `Failed to load Static Resource Metadata: ${assetMetadataPath}`
176
+ });
177
+ } else {
178
+ throw error;
179
+ }
180
+ }
181
+ return siteAssets;
182
+ }
183
+ };
184
+ function resolveStaticBundleVersion(metadataVersion, requestedVersion) {
185
+ return metadataVersion || requestedVersion || import_shared_utils.VERSION_NOT_PROVIDED;
186
+ }
187
+ function parseSiteId(input) {
188
+ const parts = input.split("|");
189
+ const specifier = parts[0];
190
+ const variants = {};
191
+ for (let i = 1; i < parts.length; i++) {
192
+ const [sigil, value] = parts[i].split("/");
193
+ if (sigil && value) {
194
+ variants[sigil] = value;
195
+ } else if (sigil) {
196
+ variants[sigil] = "true";
197
+ }
198
+ }
199
+ return {
200
+ specifier,
201
+ variants
202
+ };
203
+ }
204
+ function getSiteBundleId({specifier, namespace, name = "", version}, locale, ssr, i18n) {
205
+ if (!specifier) {
206
+ specifier = namespace ? `${namespace}/${name}` : name;
207
+ }
208
+ const versionedSpecifier = version && version !== import_shared_utils.VERSION_NOT_PROVIDED ? `${specifier}${SITE_VERSION_PREFIX}${(0, import_shared_utils.normalizeVersionToUri)(version)}` : specifier;
209
+ const ssrSpecifier = ssr ? `${versionedSpecifier}${SITE_SSR_PREFIX}` : versionedSpecifier;
210
+ return i18n?.defaultLocale === locale ? ssrSpecifier : `${ssrSpecifier}${SITE_LOCALE_PREFIX}${locale}`;
211
+ }
212
+ function getSiteResourceId({specifier, version}) {
213
+ const versionedSpecifier = version && version !== import_shared_utils.VERSION_NOT_PROVIDED ? `${specifier}${SITE_VERSION_PREFIX}${(0, import_shared_utils.normalizeVersionToUri)(version)}` : specifier;
214
+ return versionedSpecifier;
215
+ }
@@ -0,0 +1,108 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getProtoOf = Object.getPrototypeOf;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
7
+ var __markAsModule = (target) => __defProp(target, "__esModule", {value: true});
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, {get: all[name], enumerable: true});
11
+ };
12
+ var __exportStar = (target, module2, desc) => {
13
+ if (module2 && typeof module2 === "object" || typeof module2 === "function") {
14
+ for (let key of __getOwnPropNames(module2))
15
+ if (!__hasOwnProp.call(target, key) && key !== "default")
16
+ __defProp(target, key, {get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable});
17
+ }
18
+ return target;
19
+ };
20
+ var __toModule = (module2) => {
21
+ return __exportStar(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? {get: () => module2.default, enumerable: true} : {value: module2, enumerable: true})), module2);
22
+ };
23
+
24
+ // packages/@lwrjs/static/src/tools/dedupe-bundles.ts
25
+ __markAsModule(exports);
26
+ __export(exports, {
27
+ dedupeBundles: () => dedupeBundles
28
+ });
29
+ var import_fs_extra = __toModule(require("fs-extra"));
30
+ var import_path = __toModule(require("path"));
31
+ var import_diagnostics = __toModule(require("@lwrjs/diagnostics"));
32
+ var import_site_metadata = __toModule(require("../site-metadata.cjs"));
33
+ var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
34
+ async function dedupeBundles(rootDir, i18n) {
35
+ const siteMetadata = new import_site_metadata.SiteMetadataImpl({
36
+ rootDir,
37
+ i18n
38
+ });
39
+ const siteBundles = siteMetadata.getSiteBundles();
40
+ const decisionTree = siteMetadata.getSiteBundlesDecisionTree();
41
+ import_diagnostics.logger.info({
42
+ label: `dedupeBundles`,
43
+ message: `Deduplicating ${Object.keys(siteBundles.bundles).length} bundles`
44
+ });
45
+ for (const [siteIdStr, metadata] of Object.entries(siteBundles.bundles)) {
46
+ const siteId = (0, import_site_metadata.parseSiteId)(siteIdStr);
47
+ const localeId = siteId.variants[import_shared_utils.LOCALE_SIGIL];
48
+ if (!localeId || localeId === i18n.defaultLocale) {
49
+ continue;
50
+ }
51
+ const currentPath = (0, import_shared_utils.joinUrlPath)(rootDir, metadata.path);
52
+ const currentSrc = import_fs_extra.default.readFileSync(currentPath);
53
+ import_diagnostics.logger.debug({
54
+ label: `dedupeBundles`,
55
+ message: `${siteIdStr} -> ${(0, import_shared_utils.hashContent)(currentSrc)}`
56
+ });
57
+ const locale = i18n.locales.find((l) => l.id === localeId);
58
+ const fallBackLocale = locale?.fallback ?? i18n.defaultLocale;
59
+ const fallbackMetadata = decisionTree.find(siteIdStr, false, false, fallBackLocale);
60
+ if (fallbackMetadata) {
61
+ const fallbackSrc = import_fs_extra.default.readFileSync((0, import_shared_utils.joinUrlPath)(rootDir, fallbackMetadata.path));
62
+ import_diagnostics.logger.debug({
63
+ label: `dedupeBundles`,
64
+ message: `fallback ${siteIdStr},${fallBackLocale} -> ${(0, import_shared_utils.hashContent)(fallbackSrc)}`
65
+ });
66
+ if (currentSrc.equals(fallbackSrc)) {
67
+ import_diagnostics.logger.debug({
68
+ label: `dedupeBundles`,
69
+ message: `Remove duplicate variant ${siteIdStr}`
70
+ });
71
+ delete siteBundles.bundles[siteIdStr];
72
+ if (metadata.path != fallbackMetadata.path) {
73
+ import_fs_extra.default.removeSync(currentPath);
74
+ }
75
+ }
76
+ }
77
+ }
78
+ import_diagnostics.logger.info({
79
+ label: `dedupeBundles`,
80
+ message: `Deduplicated down to ${Object.keys(siteBundles.bundles).length} bundles`
81
+ });
82
+ await siteMetadata.persistSiteMetadata();
83
+ deleteEmptyFolders(rootDir);
84
+ }
85
+ function deleteEmptyFolders(directory) {
86
+ if (!import_fs_extra.default.existsSync(directory)) {
87
+ import_diagnostics.logger.warn({label: `dedupeBundles`, message: `Directory does not exist: ${directory}`});
88
+ return;
89
+ }
90
+ const files = import_fs_extra.default.readdirSync(directory);
91
+ if (files.length === 0) {
92
+ import_fs_extra.default.rmdirSync(directory);
93
+ import_diagnostics.logger.debug({label: `dedupeBundles`, message: `Deleted empty folder: ${directory}`});
94
+ return;
95
+ }
96
+ files.forEach((file) => {
97
+ const filePath = import_path.default.join(directory, file);
98
+ const isDirectory = import_fs_extra.default.statSync(filePath).isDirectory();
99
+ if (isDirectory) {
100
+ deleteEmptyFolders(filePath);
101
+ }
102
+ });
103
+ const updatedFiles = import_fs_extra.default.readdirSync(directory);
104
+ if (updatedFiles.length === 0) {
105
+ import_fs_extra.default.rmdirSync(directory);
106
+ import_diagnostics.logger.debug({label: `dedupeBundles`, message: `Deleted empty folder: ${directory}`});
107
+ }
108
+ }
@@ -0,0 +1,62 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getProtoOf = Object.getPrototypeOf;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
7
+ var __markAsModule = (target) => __defProp(target, "__esModule", {value: true});
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, {get: all[name], enumerable: true});
11
+ };
12
+ var __exportStar = (target, module2, desc) => {
13
+ if (module2 && typeof module2 === "object" || typeof module2 === "function") {
14
+ for (let key of __getOwnPropNames(module2))
15
+ if (!__hasOwnProp.call(target, key) && key !== "default")
16
+ __defProp(target, key, {get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable});
17
+ }
18
+ return target;
19
+ };
20
+ var __toModule = (module2) => {
21
+ return __exportStar(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? {get: () => module2.default, enumerable: true} : {value: module2, enumerable: true})), module2);
22
+ };
23
+
24
+ // packages/@lwrjs/static/src/transformers/mrt-static-uri-transformer.ts
25
+ __markAsModule(exports);
26
+ __export(exports, {
27
+ default: () => mrtStaticUriTransformer
28
+ });
29
+ var import_diagnostics = __toModule(require("@lwrjs/diagnostics"));
30
+ var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
31
+ function mrtStaticUriTransformer() {
32
+ return {
33
+ name: "mrt-static-uri-transformer",
34
+ async transformUri(uriDef, _def, runtimeEnvironment) {
35
+ if (uriDef.artifactType === "asset" && runtimeEnvironment?.featureFlags?.ASSETS_ON_LAMBDA) {
36
+ import_diagnostics.logger.debug({
37
+ label: `${this.name}`,
38
+ message: `uri not transformed ${uriDef.entry} -> ${uriDef.uri}`
39
+ });
40
+ return {
41
+ ...uriDef,
42
+ external: false
43
+ };
44
+ }
45
+ let basePathlessUri = uriDef.uri;
46
+ if (runtimeEnvironment?.basePath && uriDef.uri.startsWith(runtimeEnvironment?.basePath)) {
47
+ basePathlessUri = uriDef.uri.replace(runtimeEnvironment?.basePath, "");
48
+ }
49
+ const uri = (0, import_shared_utils.getMrtArtifactUrl)(runtimeEnvironment?.basePath || "", basePathlessUri);
50
+ import_diagnostics.logger.debug({
51
+ label: `${this.name}`,
52
+ message: `normalized ${uriDef.artifactType} url ${uriDef.entry} -> ${uri}`
53
+ });
54
+ return {
55
+ ...uriDef,
56
+ uri,
57
+ immutable: true,
58
+ external: true
59
+ };
60
+ }
61
+ };
62
+ }
@@ -0,0 +1,209 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getProtoOf = Object.getPrototypeOf;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
7
+ var __markAsModule = (target) => __defProp(target, "__esModule", {value: true});
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, {get: all[name], enumerable: true});
11
+ };
12
+ var __exportStar = (target, module2, desc) => {
13
+ if (module2 && typeof module2 === "object" || typeof module2 === "function") {
14
+ for (let key of __getOwnPropNames(module2))
15
+ if (!__hasOwnProp.call(target, key) && key !== "default")
16
+ __defProp(target, key, {get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable});
17
+ }
18
+ return target;
19
+ };
20
+ var __toModule = (module2) => {
21
+ return __exportStar(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? {get: () => module2.default, enumerable: true} : {value: module2, enumerable: true})), module2);
22
+ };
23
+
24
+ // packages/@lwrjs/static/src/utils/decision-tree.ts
25
+ __markAsModule(exports);
26
+ __export(exports, {
27
+ createFallbackMap: () => createFallbackMap,
28
+ default: () => decision_tree_default
29
+ });
30
+ var import_diagnostics = __toModule(require("@lwrjs/diagnostics"));
31
+ var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
32
+ var import_site_metadata = __toModule(require("../site-metadata.cjs"));
33
+ var CHOICE_WILDCARD = "*";
34
+ var CHOICE_EMPTY = "";
35
+ var CHOICE_TRUE = "true";
36
+ var CHOICE_PROD = "prod";
37
+ var CHOICE_DEBUG = "debug";
38
+ var DecisionTreeImpl = class {
39
+ constructor() {
40
+ this.root = new TreeNode();
41
+ }
42
+ insert(siteArtifactId, artifact, debug, localeFallbacks) {
43
+ const decisionPath = this.createPossibleArtifactChoices({
44
+ id: siteArtifactId,
45
+ localeFallbacks,
46
+ debug
47
+ });
48
+ const choices = decisionPath[0];
49
+ const isLeaf = decisionPath.length == 1;
50
+ for (const [index, key] of choices.entries()) {
51
+ const rank = [index];
52
+ if (!this.root.getChild(key, false)) {
53
+ this.root.addChild(key, new TreeNode(key, ""));
54
+ }
55
+ const nextNode = this.root.getChild(key, false);
56
+ if (isLeaf) {
57
+ nextNode.setArtifact(artifact, rank);
58
+ } else {
59
+ this.deepInsert(1, decisionPath, key, nextNode, artifact, rank);
60
+ }
61
+ }
62
+ }
63
+ deepInsert(level, decisionPath, currentPath, currentNode, artifact, rank) {
64
+ if (level >= decisionPath.length)
65
+ return;
66
+ const choices = decisionPath[level];
67
+ const isLeaf = level === decisionPath.length - 1;
68
+ for (const [index, key] of choices.entries()) {
69
+ const nextRank = [...rank, index];
70
+ if (!currentNode.getChild(key, false)) {
71
+ currentNode.addChild(key, new TreeNode(key, currentPath));
72
+ }
73
+ const nextNode = currentNode.getChild(key, false);
74
+ if (isLeaf) {
75
+ nextNode.setArtifact(artifact, nextRank);
76
+ } else {
77
+ this.deepInsert(level + 1, decisionPath, `${currentPath}/${key}`, nextNode, artifact, nextRank);
78
+ }
79
+ }
80
+ }
81
+ find(siteArtifactId, debug, ssr, localeId) {
82
+ const parsedArtifactId = (0, import_site_metadata.parseSiteId)(siteArtifactId);
83
+ const decisionPath = this.createArtifactChoices({
84
+ specifier: parsedArtifactId.specifier,
85
+ version: parsedArtifactId.variants[import_shared_utils.VERSION_SIGIL],
86
+ ssr: ssr ?? import_shared_utils.SSR_SIGIL in parsedArtifactId.variants,
87
+ localeId: localeId ?? parsedArtifactId.variants[import_shared_utils.LOCALE_SIGIL],
88
+ debug
89
+ });
90
+ let currentNode = this.root;
91
+ for (const key of decisionPath) {
92
+ const lastPath = currentNode.getPath();
93
+ currentNode = currentNode.getChild(key);
94
+ if (!currentNode) {
95
+ import_diagnostics.logger.debug(`Module ${key} not found at ${lastPath}`);
96
+ return void 0;
97
+ }
98
+ }
99
+ if (!currentNode.artifact) {
100
+ import_diagnostics.logger.debug(`Artifact not found at ${currentNode.getPath()}`);
101
+ }
102
+ return currentNode.artifact;
103
+ }
104
+ createArtifactChoices({specifier, version, localeId, debug, ssr}) {
105
+ const envChoice = debug ? CHOICE_DEBUG : CHOICE_PROD;
106
+ const versionChoice = getVersionChoice(version);
107
+ const uriVersion = (0, import_shared_utils.normalizeVersionToUri)(versionChoice);
108
+ const ssrChoice = ssr ? CHOICE_TRUE : CHOICE_EMPTY;
109
+ return this.getOrderedChoices(specifier, envChoice, ssrChoice, uriVersion, localeId);
110
+ }
111
+ getOrderedChoices(specifier, envChoice, ssrChoice, uriVersion, localeId) {
112
+ return [specifier, envChoice, ssrChoice, uriVersion, localeId || CHOICE_WILDCARD];
113
+ }
114
+ createPossibleArtifactChoices({
115
+ id,
116
+ localeFallbacks,
117
+ debug
118
+ }) {
119
+ const match = (0, import_site_metadata.parseSiteId)(id);
120
+ const specifier = match.specifier;
121
+ if (!specifier) {
122
+ throw new Error(`Unable to parse${debug ? " debug" : ""} static bundle specifier: ${id}`);
123
+ }
124
+ const versionChoice = getVersionChoice(match.variants[import_shared_utils.VERSION_SIGIL]);
125
+ const versions = versionChoice === CHOICE_EMPTY ? [...new Set([CHOICE_EMPTY, CHOICE_WILDCARD])] : [...new Set([versionChoice, CHOICE_EMPTY])];
126
+ const envChoice = debug ? [CHOICE_DEBUG] : [CHOICE_PROD];
127
+ const ssrChoice = match.variants[import_shared_utils.SSR_SIGIL] || CHOICE_EMPTY;
128
+ const ssr = (0, import_shared_utils.getFeatureFlags)().SSR_COMPILER_ENABLED ? [ssrChoice] : match.variants[import_shared_utils.SSR_SIGIL] ? [CHOICE_TRUE, CHOICE_EMPTY] : [CHOICE_EMPTY, CHOICE_TRUE];
129
+ const localeChoice = match.variants[import_shared_utils.LOCALE_SIGIL];
130
+ const localeId = localeFallbacks?.[localeChoice] ?? [CHOICE_WILDCARD];
131
+ return this.getOrderedChoices([specifier], envChoice, ssr, versions, localeId);
132
+ }
133
+ };
134
+ var decision_tree_default = DecisionTreeImpl;
135
+ var TreeNode = class {
136
+ constructor(value = "", parentPath = "") {
137
+ this.children = new Map();
138
+ this.artifact = void 0;
139
+ this.decisionValue = value;
140
+ this.parentPath = parentPath;
141
+ }
142
+ addChild(value, node) {
143
+ this.children.set(value, node);
144
+ }
145
+ setArtifact(artifact, rank) {
146
+ if (this.artifact && isLowerOrEqualRank(rank, this.rank)) {
147
+ import_diagnostics.logger.debug({
148
+ label: "DecisionTree",
149
+ message: `Ignored Artifact ${this.getPath()} ${this.rank} <= ${rank}`
150
+ });
151
+ return;
152
+ }
153
+ import_diagnostics.logger.debug({
154
+ label: "DecisionTree",
155
+ message: `Added artifact at ${this.getPath()}`
156
+ });
157
+ this.rank = rank;
158
+ this.artifact = artifact;
159
+ }
160
+ getChild(key, allowWildcard = true) {
161
+ return allowWildcard ? this.children.get(key) || this.children.get(CHOICE_WILDCARD) : this.children.get(key);
162
+ }
163
+ getPath() {
164
+ return this.parentPath ? this.parentPath + "|" + this.decisionValue : this.decisionValue;
165
+ }
166
+ };
167
+ function isLowerOrEqualRank(contender, existing) {
168
+ if (!existing) {
169
+ return true;
170
+ }
171
+ if (existing.length !== contender.length) {
172
+ throw new Error(`Paths must be of the same length ${existing} not found at ${contender}`);
173
+ }
174
+ for (let i = 0; i < existing.length; i++) {
175
+ if (contender[i] > existing[i]) {
176
+ return true;
177
+ } else if (contender[i] < existing[i]) {
178
+ return false;
179
+ }
180
+ }
181
+ return true;
182
+ }
183
+ function getVersionChoice(version) {
184
+ if (!version || version === import_shared_utils.VERSION_NOT_PROVIDED) {
185
+ return CHOICE_EMPTY;
186
+ }
187
+ return (0, import_shared_utils.normalizeVersionToUri)(version);
188
+ }
189
+ function createFallbackMap(config) {
190
+ const map = {};
191
+ function findFallbacks(localeId, visited = new Set()) {
192
+ if (visited.has(localeId) || localeId === config.defaultLocale) {
193
+ return [];
194
+ }
195
+ visited.add(localeId);
196
+ const locale = config.locales.find((l) => l.id === localeId);
197
+ if (!locale || !locale.fallback) {
198
+ return [localeId];
199
+ }
200
+ return [localeId, ...findFallbacks(locale.fallback, visited)];
201
+ }
202
+ config.locales.forEach((locale) => {
203
+ if (locale.id !== config.defaultLocale) {
204
+ map[locale.id] = [...new Set([...findFallbacks(locale.id)])];
205
+ }
206
+ });
207
+ map[CHOICE_WILDCARD] = [CHOICE_WILDCARD];
208
+ return map;
209
+ }
@@ -0,0 +1,2 @@
1
+ export * from './tools/dedupe-bundles.js';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,2 @@
1
+ export * from './tools/dedupe-bundles.js';
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,16 @@
1
+ import type { AssetIdentifier, AssetProvider, AssetSource, ProviderContext, ResourcePaths } from '@lwrjs/types';
2
+ export default class StaticAssetProvider implements AssetProvider {
3
+ name: string;
4
+ siteAssets: import("@lwrjs/types").SiteAssets;
5
+ resourcePaths: ResourcePaths;
6
+ siteRootDir: string;
7
+ basePath: string;
8
+ assetsOnLambda: boolean;
9
+ constructor(_config: {}, context: ProviderContext);
10
+ getAsset(assetIdentifier: AssetIdentifier): Promise<AssetSource | undefined>;
11
+ /**
12
+ * Replaces and aliased resource paths (i.e. $assetDir with a qualified specifier)
13
+ */
14
+ private normalizeSpecifier;
15
+ }
16
+ //# sourceMappingURL=static-asset-provider.d.ts.map