Skip to content

Commit 9e674d2

Browse files
committed
Add support for lsd:module true in package.json
1 parent be759e9 commit 9e674d2

3 files changed

Lines changed: 67 additions & 10 deletions

File tree

README.md

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,12 @@ yarn add -D componentsjs-generator
2727

2828
_If you are already using Components.js, you already have this._
2929

30-
Add the following entries to `package.json`:
30+
Add the following entry to `package.json`:
3131

3232
```text
3333
{
3434
...
35-
"lsd:module": "https://linkedsoftwaredependencies.org/bundles/npm/my-package",
36-
"lsd:components": "components/components.jsonld",
37-
"lsd:contexts": {
38-
"https://linkedsoftwaredependencies.org/bundles/npm/my-package/^1.0.0/components/context.jsonld": "components/context.jsonld"
39-
},
40-
"lsd:importPaths": {
41-
"https://linkedsoftwaredependencies.org/bundles/npm/my-package/^1.0.0/components/": "components/",
42-
"https://linkedsoftwaredependencies.org/bundles/npm/my-package/^1.0.0/config/": "config/"
43-
},
35+
"lsd:module": true,
4436
...
4537
}
4638
```

lib/parse/PackageMetadataLoader.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as Path from 'path';
2+
import { ModuleStateBuilder } from 'componentsjs/lib/loading/ModuleStateBuilder';
23
import type { ResolutionContext } from '../resolution/ResolutionContext';
34

45
/**
@@ -26,6 +27,15 @@ export class PackageMetadataLoader {
2627
throw new Error(`Invalid package: Syntax error in ${packageJsonPath}: ${(<Error> error).message}`);
2728
}
2829

30+
// Preprocess to expand things like `"lsd:module": true`
31+
if (await ModuleStateBuilder.preprocessPackageJson(packageRootDirectory, packageJson)) {
32+
// Add default imports paths, which will not be autogenerated, as they do not exist yet
33+
const baseUrl = Object.keys(packageJson['lsd:contexts'])[0].replace('components/context.jsonld', '');
34+
const basePath = packageJson['lsd:basePath'] || '';
35+
packageJson['lsd:importPaths'][`${baseUrl}components/`] = `${basePath}components/`;
36+
packageJson['lsd:importPaths'][`${baseUrl}config/`] = `${basePath}config/`;
37+
}
38+
2939
// Extract required fields from package.json
3040
const name = packageJson.name;
3141
const version = packageJson.version;

test/parse/PackageMetadataLoader.test.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,61 @@ describe('PackageMetadataLoader', () => {
119119
});
120120
});
121121

122+
it('should return with all required entries with lsd:module true', async() => {
123+
resolutionContext.contentsOverrides = {
124+
[Path.normalize('/package.json')]: `{
125+
"name": "@solid/community-server",
126+
"version": "1.2.3",
127+
"lsd:module": true,
128+
"types": "./index.d.ts"
129+
}`,
130+
};
131+
expect(await loader.load('/')).toEqual({
132+
componentsPath: Path.normalize('/components/components.jsonld'),
133+
contexts: {
134+
'https://linkedsoftwaredependencies.org/bundles/npm/@solid/community-server/^1.0.0/components/context.jsonld':
135+
'components/context.jsonld',
136+
},
137+
importPaths: {
138+
'https://linkedsoftwaredependencies.org/bundles/npm/@solid/community-server/^1.0.0/components/':
139+
'components/',
140+
'https://linkedsoftwaredependencies.org/bundles/npm/@solid/community-server/^1.0.0/config/': 'config/',
141+
},
142+
moduleIri: 'https://linkedsoftwaredependencies.org/bundles/npm/@solid/community-server',
143+
name: '@solid/community-server',
144+
version: '1.2.3',
145+
typesPath: Path.normalize('/index'),
146+
});
147+
});
148+
149+
it('should return with all required entries with lsd:module true and lsd:basePath', async() => {
150+
resolutionContext.contentsOverrides = {
151+
[Path.normalize('/package.json')]: `{
152+
"name": "@solid/community-server",
153+
"version": "1.2.3",
154+
"lsd:module": true,
155+
"lsd:basePath": "dist/",
156+
"types": "./index.d.ts"
157+
}`,
158+
};
159+
expect(await loader.load('/')).toEqual({
160+
componentsPath: Path.normalize('/dist/components/components.jsonld'),
161+
contexts: {
162+
'https://linkedsoftwaredependencies.org/bundles/npm/@solid/community-server/^1.0.0/components/context.jsonld':
163+
'dist/components/context.jsonld',
164+
},
165+
importPaths: {
166+
'https://linkedsoftwaredependencies.org/bundles/npm/@solid/community-server/^1.0.0/components/':
167+
'dist/components/',
168+
'https://linkedsoftwaredependencies.org/bundles/npm/@solid/community-server/^1.0.0/config/': 'dist/config/',
169+
},
170+
moduleIri: 'https://linkedsoftwaredependencies.org/bundles/npm/@solid/community-server',
171+
name: '@solid/community-server',
172+
version: '1.2.3',
173+
typesPath: Path.normalize('/index'),
174+
});
175+
});
176+
122177
it('should error on invalid JSON', async() => {
123178
resolutionContext.contentsOverrides = {
124179
[Path.normalize('/package.json')]: `{`,

0 commit comments

Comments
 (0)