@lwrjs/fs-asset-provider 0.9.0-alpha.26 → 0.9.0-alpha.27

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.
@@ -34,7 +34,7 @@ function hash(filePath) {
34
34
  return (result.mtimeMs * 1e4).toString();
35
35
  }
36
36
  var FsAssetProvider = class {
37
- constructor(pluginConfig, providerConfig) {
37
+ constructor(_pluginConfig, providerConfig) {
38
38
  this.name = "fs-asset-provider";
39
39
  this.cachedAssets = new Map();
40
40
  const {
@@ -42,6 +42,14 @@ var FsAssetProvider = class {
42
42
  runtimeEnvironment: {watchFiles},
43
43
  watcherFactory
44
44
  } = providerConfig;
45
+ const {assets, rootDir, layoutsDir, contentDir, basePath} = providerConfig.config;
46
+ this.basePath = basePath;
47
+ this.resourcePaths = {assets, rootDir, layoutsDir, contentDir};
48
+ this.assetPathMap = assets.reduce((map, asset) => {
49
+ const urlPath = `${basePath}${asset.urlPath}`;
50
+ map.set(urlPath, asset.dir || asset.file);
51
+ return map;
52
+ }, new Map());
45
53
  this.emitter = appEmitter;
46
54
  if (watchFiles && watcherFactory) {
47
55
  this.watcher = watcherFactory.setupWatcher(this.onModuleChange.bind(this));
@@ -53,34 +61,40 @@ var FsAssetProvider = class {
53
61
  throw new Error("We are observing a file we have not yet processed, this shouldn't happen...");
54
62
  }
55
63
  this.cachedAssets.delete(fileChanged);
56
- cachedAsset = await this.getAsset(fileChanged);
64
+ cachedAsset = await this.getAssetFromFSPath(fileChanged);
57
65
  this.emitter.notifyAssetSourceChanged(cachedAsset);
58
66
  }
59
67
  async initialize() {
60
68
  return;
61
69
  }
62
- async getAsset(fullAssetPath) {
63
- fullAssetPath = decodeURI(fullAssetPath);
70
+ async getAsset(assetId) {
71
+ const fullAssetPath = (0, import_shared_utils.normalizeAssetSpecifier)(assetId, this.assetPathMap, this.resourcePaths, this.basePath);
72
+ return this.getAssetFromFSPath(fullAssetPath);
73
+ }
74
+ async getAssetFromFSPath(assetPath) {
75
+ const fullAssetPath = decodeURI(assetPath);
64
76
  if (!this.cachedAssets.has(fullAssetPath)) {
65
- const [assetPath] = fullAssetPath.split("?");
66
- const ext = (0, import_path.extname)(`x.${assetPath}`).toLowerCase().substr(1);
67
- if (ext && import_fs.default.existsSync(assetPath)) {
68
- const ownHash = hash(assetPath);
69
- const mimeType = (0, import_shared_utils.mimeLookup)(assetPath);
77
+ const [assetPath2] = fullAssetPath.split("?");
78
+ const ext = (0, import_path.extname)(`x.${assetPath2}`).toLowerCase().substring(1);
79
+ if (ext && import_fs.default.existsSync(assetPath2)) {
80
+ const ownHash = hash(assetPath2);
81
+ const mimeType = (0, import_shared_utils.mimeLookup)(assetPath2);
70
82
  const assetDef = {
71
- entry: assetPath,
83
+ entry: assetPath2,
72
84
  ext,
73
85
  mime: mimeType,
74
86
  ownHash,
75
87
  content: (passedEncoding) => {
76
88
  const encoding = passedEncoding || (mimeType && mimeType.startsWith("text/") ? "utf-8" : null);
77
- return import_fs.default.readFileSync(assetPath, {encoding});
89
+ return import_fs.default.readFileSync(assetPath2, {encoding});
78
90
  }
79
91
  };
80
92
  this.cachedAssets.set(fullAssetPath, assetDef);
81
93
  if (this.watcher) {
82
- this.watcher.add(assetPath);
94
+ this.watcher.add(assetPath2);
83
95
  }
96
+ } else {
97
+ return void 0;
84
98
  }
85
99
  }
86
100
  return this.cachedAssets.get(fullAssetPath);
@@ -1,12 +1,22 @@
1
- import { ProviderContext, AssetProvider, AssetSource, Watcher, LwrAppEmitter } from '@lwrjs/types';
1
+ import type { ProviderContext, AssetProvider, AssetSource, Watcher, LwrAppEmitter, AssetIdentifier, ResourcePaths } from '@lwrjs/types';
2
2
  export default class FsAssetProvider implements AssetProvider {
3
3
  name: string;
4
4
  emitter: LwrAppEmitter;
5
5
  watcher?: Watcher;
6
6
  cachedAssets: Map<string, AssetSource>;
7
- constructor(pluginConfig: unknown, providerConfig: ProviderContext);
7
+ resourcePaths: ResourcePaths;
8
+ assetPathMap: Map<string, string>;
9
+ basePath: string;
10
+ constructor(_pluginConfig: unknown, providerConfig: ProviderContext);
8
11
  onModuleChange(fileChanged: string): Promise<void>;
9
12
  initialize(): Promise<void>;
10
- getAsset(fullAssetPath: string): Promise<AssetSource | undefined>;
13
+ /**
14
+ * Public API to get an AssetSource object from an asset identifier
15
+ */
16
+ getAsset(assetId: AssetIdentifier): Promise<AssetSource | undefined>;
17
+ /**
18
+ * Get and AssetSource object from a resolved filesystem file path.
19
+ */
20
+ private getAssetFromFSPath;
11
21
  }
12
22
  //# sourceMappingURL=index.d.ts.map
package/build/es/index.js CHANGED
@@ -1,15 +1,23 @@
1
1
  import fs from 'fs';
2
2
  import { extname } from 'path';
3
- import { mimeLookup } from '@lwrjs/shared-utils';
3
+ import { mimeLookup, normalizeAssetSpecifier } from '@lwrjs/shared-utils';
4
4
  function hash(filePath) {
5
5
  const result = fs.statSync(filePath);
6
6
  return (result.mtimeMs * 10000).toString();
7
7
  }
8
8
  export default class FsAssetProvider {
9
- constructor(pluginConfig, providerConfig) {
9
+ constructor(_pluginConfig, providerConfig) {
10
10
  this.name = 'fs-asset-provider';
11
11
  this.cachedAssets = new Map();
12
12
  const { appEmitter, runtimeEnvironment: { watchFiles }, watcherFactory, } = providerConfig;
13
+ const { assets, rootDir, layoutsDir, contentDir, basePath } = providerConfig.config;
14
+ this.basePath = basePath;
15
+ this.resourcePaths = { assets, rootDir, layoutsDir, contentDir };
16
+ this.assetPathMap = assets.reduce((map, asset) => {
17
+ const urlPath = `${basePath}${asset.urlPath}`;
18
+ map.set(urlPath, asset.dir || asset.file);
19
+ return map;
20
+ }, new Map());
13
21
  this.emitter = appEmitter;
14
22
  if (watchFiles && watcherFactory) {
15
23
  this.watcher = watcherFactory.setupWatcher(this.onModuleChange.bind(this));
@@ -22,19 +30,29 @@ export default class FsAssetProvider {
22
30
  }
23
31
  // Update the cache entry
24
32
  this.cachedAssets.delete(fileChanged);
25
- cachedAsset = await this.getAsset(fileChanged);
33
+ cachedAsset = await this.getAssetFromFSPath(fileChanged);
26
34
  this.emitter.notifyAssetSourceChanged(cachedAsset);
27
35
  }
28
36
  // -- Public API --------------------------------------------------------------------
29
37
  async initialize() {
30
38
  return;
31
39
  }
32
- async getAsset(fullAssetPath) {
33
- fullAssetPath = decodeURI(fullAssetPath); // decodeURIComponent decodes too many things (eg: /)
40
+ /**
41
+ * Public API to get an AssetSource object from an asset identifier
42
+ */
43
+ async getAsset(assetId) {
44
+ const fullAssetPath = normalizeAssetSpecifier(assetId, this.assetPathMap, this.resourcePaths, this.basePath);
45
+ return this.getAssetFromFSPath(fullAssetPath);
46
+ }
47
+ /**
48
+ * Get and AssetSource object from a resolved filesystem file path.
49
+ */
50
+ async getAssetFromFSPath(assetPath) {
51
+ const fullAssetPath = decodeURI(assetPath); // decodeURIComponent decodes too many things (eg: /)
34
52
  if (!this.cachedAssets.has(fullAssetPath)) {
35
53
  const [assetPath] = fullAssetPath.split('?'); // remove query params
36
54
  // Clever normalization to avoid corner cases
37
- const ext = extname(`x.${assetPath}`).toLowerCase().substr(1);
55
+ const ext = extname(`x.${assetPath}`).toLowerCase().substring(1);
38
56
  if (ext && fs.existsSync(assetPath)) {
39
57
  const ownHash = hash(assetPath);
40
58
  const mimeType = mimeLookup(assetPath);
@@ -54,6 +72,10 @@ export default class FsAssetProvider {
54
72
  this.watcher.add(assetPath);
55
73
  }
56
74
  }
75
+ else {
76
+ // returned undefined move on to next provider
77
+ return undefined;
78
+ }
57
79
  }
58
80
  return this.cachedAssets.get(fullAssetPath);
59
81
  }
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.9.0-alpha.26",
7
+ "version": "0.9.0-alpha.27",
8
8
  "homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
9
9
  "repository": {
10
10
  "type": "git",
@@ -30,13 +30,13 @@
30
30
  "build/**/*.d.ts"
31
31
  ],
32
32
  "dependencies": {
33
- "@lwrjs/shared-utils": "0.9.0-alpha.26"
33
+ "@lwrjs/shared-utils": "0.9.0-alpha.27"
34
34
  },
35
35
  "devDependencies": {
36
- "@lwrjs/types": "0.9.0-alpha.26"
36
+ "@lwrjs/types": "0.9.0-alpha.27"
37
37
  },
38
38
  "engines": {
39
39
  "node": ">=16.0.0 <20"
40
40
  },
41
- "gitHead": "6da956eb29622d49423b0a6ee63300de1f3e121c"
41
+ "gitHead": "a7fcd8493b8a3286197e829b28b54670e7d4e97c"
42
42
  }