Skip to content

Commit 06df8fc

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 ed3d104 commit 06df8fc

File tree

150 files changed

+1180
-590
lines changed

Some content is hidden

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

150 files changed

+1180
-590
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ docs/site/readmes
1818
/sandbox/*
1919
!/sandbox/README.md
2020
!/sandbox/example/
21+
22+
# TypeScrip build cache
23+
.tsbuildinfo

benchmark/index.ts

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

benchmark/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
"performance",
99
"benchmark"
1010
],
11-
"main": "index.js",
11+
"types": "./dist/index.d.ts",
1212
"engines": {
1313
"node": ">=8.9"
1414
},
1515
"scripts": {
1616
"build": "lb-tsc es2017 --outDir dist",
17+
"build:resources": "lb-copy-resources",
1718
"clean": "lb-clean dist",
1819
"pretest": "npm run clean && npm run build",
1920
"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[2] !== '-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() {
@@ -485,7 +485,7 @@ valid data to create a new model instance.
485485
{% include code-caption.html content="src/__tests__/unit/models/person.model.unit.ts" %}
486486

487487
```ts
488-
import {Person} from '../../../src/models';
488+
import {Person} from '../../../models';
489489
import {givenPersonData} from '../../helpers/database.helpers';
490490
import {expect} from '@loopback/testlab';
491491

@@ -576,7 +576,7 @@ import {
576576
givenEmptyDatabase,
577577
givenCategory,
578578
} from '../../helpers/database.helpers';
579-
import {CategoryRepository} from '../../../src/repositories';
579+
import {CategoryRepository} from '../../../repositories';
580580
import {expect} from '@loopback/testlab';
581581
import {testdb} from '../../fixtures/datasources/testdb.datasource';
582582

@@ -609,8 +609,8 @@ ingredient.
609609
```ts
610610
import {expect} from '@loopback/testlab';
611611
import {givenEmptyDatabase, givenProduct} from '../../helpers/database.helpers';
612-
import {ProductController} from '../../../src/controllers';
613-
import {ProductRepository} from '../../../src/repositories';
612+
import {ProductController} from '../../../controllers';
613+
import {ProductRepository} from '../../../repositories';
614614
import {testdb} from '../../fixtures/datasources/testdb.datasource';
615615

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

659659
```ts
660-
import {
661-
GeoService,
662-
GeoServiceProvider,
663-
} from '../../src/services/geo.service.ts';
664-
import {GeoDataSource} from '../../src/datasources/geo.datasource.ts';
660+
import {GeoService, GeoServiceProvider} from '../../services/geo.service.ts';
661+
import {GeoDataSource} from '../../datasources/geo.datasource.ts';
665662

666663
describe('GeoService', () => {
667664
let service: GeoService;
@@ -681,7 +678,7 @@ instance:
681678

682679
```ts
683680
import {merge} from 'lodash';
684-
import * as GEO_CODER_CONFIG from '../src/datasources/geo.datasource.json';
681+
import * as GEO_CODER_CONFIG from '../datasources/geo.datasource.json';
685682

686683
function givenGeoService() {
687684
const config = merge({}, GEO_CODER_CONFIG, {

examples/express-composition/tsconfig.build.json

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

examples/hello-world/index.d.ts

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

examples/hello-world/index.ts

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
@@ -7,11 +7,13 @@
77
"node": ">=8.9"
88
},
99
"author": "IBM Corp.",
10+
"types": "./dist/index.d.ts",
1011
"scripts": {
1112
"acceptance": "lb-mocha \"dist/__tests__/acceptance/**/*.js\"",
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-hello-world*.tgz dist package api-docs",
1618
"verify": "npm pack && tar xf *example-hello-world*.tgz && tree package && npm run clean",
1719
"lint": "npm run prettier:check && npm run tslint",

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/index.d.ts

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

examples/log-extension/index.ts

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

examples/log-extension/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
"node": ">=8.9"
88
},
99
"author": "IBM Corp.",
10+
"types": "./dist/index.d.ts",
1011
"scripts": {
1112
"build:apidocs": "lb-apidocs",
1213
"build": "lb-tsc es2017 --outDir dist",
13-
"build:watch": "lb-tsc es2017 --outDir dist --watch",
14+
"build:resources": "lb-copy-resources",
15+
"build:watch": "lb-tsc --watch",
1416
"clean": "lb-clean *example-log-extension-*.tgz dist package api-docs",
1517
"lint": "npm run prettier:check && npm run tslint",
1618
"lint:fix": "npm run tslint: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)