Skip to content

Commit 9bb6296

Browse files
committed
feat: enable support for typescript references
- add script to build typescript project references - reorg project layout for better typescript refs - switch to tsconfig.json - add copy-resources script
1 parent 96b1871 commit 9bb6296

File tree

102 files changed

+1185
-292
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+1185
-292
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@ packages/tsdocs/fixtures/monorepo/docs
2929
# ESLint cache
3030
.eslintcache
3131

32+
# TypeScrip build cache
33+
.tsbuildinfo

.vscode/tasks.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,16 @@
8282
},
8383
{
8484
"label": "Build project",
85-
"group": "build",
85+
"group": {
86+
"kind": "build",
87+
"isDefault": true
88+
},
8689
"type": "shell",
8790
"command": "npm",
8891
"args": [
8992
"run",
9093
"-s",
91-
"build"
94+
"build:dev"
9295
],
9396
"problemMatcher": "$tsc"
9497
},

benchmark/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
},
1616
"scripts": {
1717
"build": "lb-tsc es2017 --outDir dist",
18+
"build:resources": "lb-copy-resources",
1819
"clean": "lb-clean dist",
1920
"pretest": "npm run clean && npm run build",
2021
"test": "lb-mocha \"dist/__tests__/**/*.js\"",

benchmark/tsconfig.build.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

benchmark/tsconfig.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"$schema": "http://json.schemastore.org/tsconfig",
3+
"extends": "../packages/build/config/tsconfig.common.json",
4+
"compilerOptions": {
5+
"target": "es2017",
6+
"outDir": "dist",
7+
"rootDir": "src",
8+
"composite": true,
9+
"incremental": true,
10+
"tsBuildInfoFile": ".tsbuildinfo"
11+
},
12+
"references": [
13+
{
14+
"path": "../packages/testlab/tsconfig.json"
15+
},
16+
{
17+
"path": "../examples/todo/tsconfig.json"
18+
},
19+
{
20+
"path": "../packages/openapi-spec-builder/tsconfig.json"
21+
},
22+
{
23+
"path": "../packages/rest/tsconfig.json"
24+
}
25+
],
26+
"include": [
27+
"src/**/*",
28+
"src/**/*.json"
29+
]
30+
}

bin/update-project-refs.js

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#!/usr/bin/env node
2+
// Copyright IBM Corp. 2017,2018. All Rights Reserved.
3+
// Node module: loopback-next
4+
// This file is licensed under the MIT License.
5+
// License text available at https://opensource.org/licenses/MIT
6+
7+
/**
8+
* This is an internal script to update TypeScript project references based on
9+
* lerna's local package dependencies.
10+
*
11+
* See https://www.typescriptlang.org/docs/handbook/project-references.html
12+
*/
13+
'use strict';
14+
15+
const path = require('path');
16+
const fs = require('fs');
17+
const util = require('util');
18+
const debug = require('debug')('loopback:build');
19+
const buildUtils = require('../packages/build/bin/utils');
20+
21+
const Project = require('@lerna/project');
22+
const PackageGraph = require('@lerna/package-graph');
23+
24+
const TSCONFIG = 'tsconfig.json';
25+
26+
async function updateReferences(options) {
27+
options = options || {};
28+
const dryRun = options.dryRun;
29+
const project = new Project(process.cwd());
30+
const packages = await project.getPackages();
31+
32+
const rootRefs = [];
33+
const graph = new PackageGraph(packages);
34+
35+
for (const p of graph.values()) {
36+
debug('Package %s', p.pkg.name);
37+
const pkgLocation = p.pkg.location;
38+
const tsconfigFile = path.join(pkgLocation, TSCONFIG);
39+
// Skip non-typescript packages
40+
if (!fs.existsSync(tsconfigFile)) {
41+
debug('Skipping non-TS package: %s', p.pkg.name);
42+
continue;
43+
}
44+
rootRefs.push({
45+
path: path.join(path.relative(project.rootPath, pkgLocation), TSCONFIG),
46+
});
47+
const tsconfig = require(tsconfigFile);
48+
const refs = [];
49+
for (const d of p.localDependencies.keys()) {
50+
const depPkg = graph.get(d);
51+
// Skip non-typescript packages
52+
if (!fs.existsSync(path.join(depPkg.pkg.location, TSCONFIG))) {
53+
debug('Skipping non-TS dependency: %s', depPkg.pkg.name);
54+
continue;
55+
}
56+
const relativePath = path.relative(pkgLocation, depPkg.pkg.location);
57+
refs.push({path: path.join(relativePath, TSCONFIG)});
58+
}
59+
tsconfig.compilerOptions = Object.assign(
60+
{
61+
// outDir & target have to be set in tsconfig instead of CLI for tsc -b
62+
target: buildUtils.getCompilationTarget(),
63+
outDir: buildUtils.getDistribution(tsconfig.compilerOptions.target),
64+
},
65+
tsconfig.compilerOptions,
66+
{
67+
// composite must be true for project refs
68+
composite: true,
69+
},
70+
);
71+
if (options.incremental) {
72+
// See https://devblogs.microsoft.com/typescript/announcing-typescript-3-4/#faster-subsequent-builds-with-the---incremental-flag
73+
tsconfig.compilerOptions = Object.assign(tsconfig.compilerOptions, {
74+
incremental: true,
75+
tsBuildInfoFile: '.tsbuildinfo',
76+
});
77+
}
78+
79+
if (!tsconfig.include) {
80+
// To include ts/json files
81+
tsconfig.include = ['src/**/*', 'src/**/*.json'];
82+
}
83+
tsconfig.references = refs;
84+
85+
// Convert to JSON
86+
const tsconfigJson = JSON.stringify(tsconfig, null, 2);
87+
88+
if (!dryRun) {
89+
// Using `-f` to overwrite tsconfig.json
90+
fs.writeFileSync(tsconfigFile, tsconfigJson + '\n', {encoding: 'utf-8'});
91+
debug('%s has been updated.', tsconfigFile);
92+
} else {
93+
// Otherwise write to console
94+
debug(tsconfigJson);
95+
console.log('%s', p.pkg.name);
96+
refs.forEach(r => console.log(' %s', r.path));
97+
}
98+
}
99+
100+
const rootTsconfigFile = path.join(project.rootPath, 'tsconfig.json');
101+
const rootTsconfig = require(rootTsconfigFile);
102+
rootTsconfig.compilerOptions = rootTsconfig.compilerOptions || {};
103+
rootTsconfig.compilerOptions.composite = true;
104+
rootTsconfig.references = rootRefs;
105+
106+
// Reset files/include/exclude. The root should use project references now.
107+
rootTsconfig.files = [];
108+
delete rootTsconfig.include;
109+
delete rootTsconfig.exclude;
110+
111+
// Convert to JSON
112+
const rootTsconfigJson = JSON.stringify(rootTsconfig, null, 2);
113+
if (!dryRun) {
114+
// Using `-f` to overwrite tsconfig.json
115+
fs.writeFileSync(rootTsconfigFile, rootTsconfigJson + '\n', {
116+
encoding: 'utf-8',
117+
});
118+
debug('%s has been updated.', rootTsconfigFile);
119+
console.log('TypeScript project references have been updated.');
120+
} else {
121+
debug(rootTsconfigJson);
122+
console.log('\n%s', path.relative(project.rootPath, rootTsconfigFile));
123+
rootRefs.forEach(r => console.log(' %s', r.path));
124+
console.log(
125+
'\nThis is a dry-run. Please use -f option to update tsconfig files.',
126+
);
127+
}
128+
}
129+
130+
if (require.main === module) {
131+
const dryRun = !process.argv.includes('-f');
132+
const incremental = process.argv.includes('--incremental');
133+
updateReferences({dryRun, incremental});
134+
}

docs/package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
"engines": {
88
"node": ">=8.9"
99
},
10-
"files": [
11-
"**/*"
12-
],
1310
"keywords": [
1411
"LoopBack",
1512
"docs"

docs/site/Testing-your-application.md

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ helper method; for example:
129129
{% include code-caption.html content="src/__tests__/helpers/database.helpers.ts" %}
130130

131131
```ts
132-
import {ProductRepository, CategoryRepository} from '../../src/repositories';
132+
import {ProductRepository, CategoryRepository} from '../../repositories';
133133
import {testdb} from '../fixtures/datasources/testdb.datasource';
134134

135135
export async function givenEmptyDatabase() {
@@ -547,7 +547,7 @@ valid data to create a new model instance.
547547
{% include code-caption.html content="src/__tests__/unit/models/person.model.unit.ts" %}
548548

549549
```ts
550-
import {Person} from '../../../src/models';
550+
import {Person} from '../../../models';
551551
import {givenPersonData} from '../../helpers/database.helpers';
552552
import {expect} from '@loopback/testlab';
553553

@@ -638,7 +638,7 @@ import {
638638
givenEmptyDatabase,
639639
givenCategory,
640640
} from '../../helpers/database.helpers';
641-
import {CategoryRepository} from '../../../src/repositories';
641+
import {CategoryRepository} from '../../../repositories';
642642
import {expect} from '@loopback/testlab';
643643
import {testdb} from '../../fixtures/datasources/testdb.datasource';
644644

@@ -671,8 +671,8 @@ ingredient.
671671
```ts
672672
import {expect} from '@loopback/testlab';
673673
import {givenEmptyDatabase, givenProduct} from '../../helpers/database.helpers';
674-
import {ProductController} from '../../../src/controllers';
675-
import {ProductRepository} from '../../../src/repositories';
674+
import {ProductController} from '../../../controllers';
675+
import {ProductRepository} from '../../../repositories';
676676
import {testdb} from '../../fixtures/datasources/testdb.datasource';
677677

678678
describe('ProductController (integration)', () => {
@@ -719,11 +719,8 @@ of the service proxy by invoking the provider. This helper should be typically
719719
invoked once before the integration test suite begins.
720720

721721
```ts
722-
import {
723-
GeoService,
724-
GeoServiceProvider,
725-
} from '../../src/services/geo.service.ts';
726-
import {GeoDataSource} from '../../src/datasources/geo.datasource.ts';
722+
import {GeoService, GeoServiceProvider} from '../../services/geo.service.ts';
723+
import {GeoDataSource} from '../../datasources/geo.datasource.ts';
727724

728725
describe('GeoService', () => {
729726
let service: GeoService;
@@ -743,7 +740,7 @@ instance:
743740

744741
```ts
745742
import {merge} from 'lodash';
746-
import * as GEO_CODER_CONFIG from '../src/datasources/geo.datasource.json';
743+
import * as GEO_CODER_CONFIG from '../datasources/geo.datasource.json';
747744

748745
function givenGeoService() {
749746
const config = merge({}, GEO_CODER_CONFIG, {

examples/context/tsconfig.build.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

examples/context/tsconfig.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"$schema": "http://json.schemastore.org/tsconfig",
3+
"extends": "@loopback/build/config/tsconfig.common.json",
4+
"compilerOptions": {
5+
"target": "es2017",
6+
"outDir": "dist",
7+
"rootDir": "src",
8+
"composite": true
9+
},
10+
"include": [
11+
"src"
12+
],
13+
"references": [
14+
{
15+
"path": "../../packages/testlab/tsconfig.json"
16+
},
17+
{
18+
"path": "../../packages/context/tsconfig.json"
19+
}
20+
]
21+
}

examples/express-composition/tsconfig.build.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

examples/hello-world/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
"node": ">=8.9"
99
},
1010
"author": "IBM Corp.",
11+
"types": "./dist/index.d.ts",
1112
"scripts": {
1213
"acceptance": "lb-mocha \"dist/__tests__/acceptance/**/*.js\"",
1314
"build:apidocs": "lb-apidocs",
1415
"build": "lb-tsc es2017 --outDir dist",
15-
"build:watch": "lb-tsc es2017 --outDir dist --watch",
16+
"build:resources": "lb-copy-resources",
17+
"build:watch": "lb-tsc --watch",
1618
"clean": "lb-clean *example-hello-world*.tgz dist package api-docs",
1719
"verify": "npm pack && tar xf *example-hello-world*.tgz && tree package && npm run clean",
1820
"lint": "npm run prettier:check && npm run eslint",

examples/hello-world/tsconfig.build.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

examples/hello-world/tsconfig.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"$schema": "http://json.schemastore.org/tsconfig",
3+
"extends": "@loopback/build/config/tsconfig.common.json",
4+
"compilerOptions": {
5+
"target": "es2017",
6+
"outDir": "dist",
7+
"rootDir": "src",
8+
"composite": true,
9+
"incremental": true,
10+
"tsBuildInfoFile": ".tsbuildinfo"
11+
},
12+
"references": [
13+
{
14+
"path": "../../packages/testlab/tsconfig.json"
15+
},
16+
{
17+
"path": "../../packages/core/tsconfig.json"
18+
},
19+
{
20+
"path": "../../packages/rest/tsconfig.json"
21+
}
22+
],
23+
"include": [
24+
"src/**/*",
25+
"src/**/*.json"
26+
]
27+
}

examples/log-extension/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
"node": ">=8.9"
99
},
1010
"author": "IBM Corp.",
11+
"types": "./dist/index.d.ts",
1112
"scripts": {
1213
"build:apidocs": "lb-apidocs",
1314
"build": "lb-tsc es2017 --outDir dist",
14-
"build:watch": "lb-tsc es2017 --outDir dist --watch",
15+
"build:resources": "lb-copy-resources",
16+
"build:watch": "lb-tsc --watch",
1517
"clean": "lb-clean *example-log-extension-*.tgz dist package api-docs",
1618
"lint": "npm run prettier:check && npm run eslint",
1719
"lint:fix": "npm run eslint:fix && npm run prettier:fix",

examples/log-extension/tsconfig.build.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)