Skip to content

Commit

Permalink
feat: enable support for typescript references
Browse files Browse the repository at this point in the history
- add script to build typescript project references
- reorg project layout for better typescript refs
- switch to tsconfig.json
- add copy-resources script
  • Loading branch information
raymondfeng committed Mar 8, 2019
1 parent d4eb70c commit 9866136
Show file tree
Hide file tree
Showing 149 changed files with 1,118 additions and 581 deletions.
6 changes: 0 additions & 6 deletions benchmark/index.ts

This file was deleted.

3 changes: 2 additions & 1 deletion benchmark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
"performance",
"benchmark"
],
"main": "index.js",
"types": "./dist/index.d.ts",
"engines": {
"node": ">=8.9"
},
"scripts": {
"build": "lb-tsc es2017 --outDir dist",
"build:resources": "lb-copy-resources",
"clean": "lb-clean dist",
"pretest": "npm run clean && npm run build",
"test": "lb-mocha \"dist/__tests__\"",
Expand Down
8 changes: 0 additions & 8 deletions benchmark/tsconfig.build.json

This file was deleted.

28 changes: 28 additions & 0 deletions benchmark/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"$schema": "http://json.schemastore.org/tsconfig",
"extends": "../packages/build/config/tsconfig.common.json",
"compilerOptions": {
"rootDir": "src",
"composite": true,
"target": "es2017",
"outDir": "dist"
},
"references": [
{
"path": "../packages/testlab/tsconfig.json"
},
{
"path": "../examples/todo/tsconfig.json"
},
{
"path": "../packages/openapi-spec-builder/tsconfig.json"
},
{
"path": "../packages/rest/tsconfig.json"
}
],
"include": [
"src/**/*",
"src/**/*.json"
]
}
122 changes: 122 additions & 0 deletions bin/update-project-refs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#!/usr/bin/env node
// Copyright IBM Corp. 2017,2018. All Rights Reserved.
// Node module: loopback-next
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

/**
* This is an internal script to update TypeScript project references based on
* lerna's local package dependencies.
*
* See https://www.typescriptlang.org/docs/handbook/project-references.html
*/
'use strict';

const path = require('path');
const fs = require('fs');
const util = require('util');
const debug = require('debug')('loopback:build');
const buildUtils = require('../packages/build/bin/utils');

const Project = require('@lerna/project');
const PackageGraph = require('@lerna/package-graph');

const TSCONFIG = 'tsconfig.json';

async function updateReferences(options) {
options = options || {};
const dryRun = options.dryRun;
const project = new Project(process.cwd());
const packages = await project.getPackages();

const rootRefs = [];
const graph = new PackageGraph(packages);

for (const p of graph.values()) {
debug('Package %s', p.pkg.name);
const pkgLocation = p.pkg.location;
const tsconfigFile = path.join(pkgLocation, TSCONFIG);
// Skip non-typescript packages
if (!fs.existsSync(tsconfigFile)) {
debug('Skipping non-TS package: %s', p.pkg.name);
continue;
}
rootRefs.push({
path: path.join(path.relative(project.rootPath, pkgLocation), TSCONFIG),
});
const tsconfig = require(tsconfigFile);
const refs = [];
for (const d of p.localDependencies.keys()) {
const depPkg = graph.get(d);
// Skip non-typescript packages
if (!fs.existsSync(path.join(depPkg.pkg.location, TSCONFIG))) {
debug('Skipping non-TS dependency: %s', depPkg.pkg.name);
continue;
}
const relativePath = path.relative(pkgLocation, depPkg.pkg.location);
refs.push({path: path.join(relativePath, TSCONFIG)});
}
tsconfig.compilerOptions = tsconfig.compilerOptions || {};
// composite must be true for project refs
tsconfig.compilerOptions.composite = true;
// outDir & target have to be set in tsconfig instead of CLI for tsc -b
tsconfig.compilerOptions.target =
tsconfig.compilerOptions.target || buildUtils.getCompilationTarget();
tsconfig.compilerOptions.outDir =
tsconfig.compilerOptions.outDir ||
buildUtils.getDistribution(tsconfig.compilerOptions.target);
if (!tsconfig.include) {
// To include ts/json files
tsconfig.include = ['src/**/*', 'src/**/*.json'];
}
tsconfig.references = refs;

// Convert to JSON
const tsconfigJson = JSON.stringify(tsconfig, null, 2);

if (!dryRun) {
// Using `-f` to overwrite tsconfig.json
fs.writeFileSync(tsconfigFile, tsconfigJson + '\n', {encoding: 'utf-8'});
debug('%s has been updated.', tsconfigFile);
} else {
// Otherwise write to console
debug(tsconfigJson);
console.log('%s', p.pkg.name);
refs.forEach(r => console.log(' %s', r.path));
}
}

const rootTsconfigFile = path.join(project.rootPath, 'tsconfig.json');
const rootTsconfig = require(rootTsconfigFile);
rootTsconfig.compilerOptions = rootTsconfig.compilerOptions || {};
rootTsconfig.compilerOptions.composite = true;
rootTsconfig.references = rootRefs;

// Reset files/include/exclude. The root should use project references now.
rootTsconfig.files = [];
delete rootTsconfig.include;
delete rootTsconfig.exclude;

// Convert to JSON
const rootTsconfigJson = JSON.stringify(rootTsconfig, null, 2);
if (!dryRun) {
// Using `-f` to overwrite tsconfig.json
fs.writeFileSync(rootTsconfigFile, rootTsconfigJson + '\n', {
encoding: 'utf-8',
});
debug('%s has been updated.', rootTsconfigFile);
console.log('TypeScript project references have been updated.');
} else {
debug(rootTsconfigJson);
console.log('\n%s', path.relative(project.rootPath, rootTsconfigFile));
rootRefs.forEach(r => console.log(' %s', r.path));
console.log(
'\nThis is a dry-run. Please use -f option to update tsconfig files.',
);
}
}

if (require.main === module) {
const dryRun = process.argv[2] !== '-f';
updateReferences({dryRun});
}
3 changes: 0 additions & 3 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
"engines": {
"node": ">=8.9"
},
"files": [
"**/*"
],
"keywords": [
"LoopBack",
"docs"
Expand Down
19 changes: 8 additions & 11 deletions docs/site/Testing-your-application.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ helper method; for example:
{% include code-caption.html content="src/__tests__/helpers/database.helpers.ts" %}

```ts
import {ProductRepository, CategoryRepository} from '../../src/repositories';
import {ProductRepository, CategoryRepository} from '../../repositories';
import {testdb} from '../fixtures/datasources/testdb.datasource';

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

```ts
import {Person} from '../../../src/models';
import {Person} from '../../../models';
import {givenPersonData} from '../../helpers/database.helpers';
import {expect} from '@loopback/testlab';

Expand Down Expand Up @@ -576,7 +576,7 @@ import {
givenEmptyDatabase,
givenCategory,
} from '../../helpers/database.helpers';
import {CategoryRepository} from '../../../src/repositories';
import {CategoryRepository} from '../../../repositories';
import {expect} from '@loopback/testlab';
import {testdb} from '../../fixtures/datasources/testdb.datasource';

Expand Down Expand Up @@ -609,8 +609,8 @@ ingredient.
```ts
import {expect} from '@loopback/testlab';
import {givenEmptyDatabase, givenProduct} from '../../helpers/database.helpers';
import {ProductController} from '../../../src/controllers';
import {ProductRepository} from '../../../src/repositories';
import {ProductController} from '../../../controllers';
import {ProductRepository} from '../../../repositories';
import {testdb} from '../../fixtures/datasources/testdb.datasource';

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

```ts
import {
GeoService,
GeoServiceProvider,
} from '../../src/services/geo.service.ts';
import {GeoDataSource} from '../../src/datasources/geo.datasource.ts';
import {GeoService, GeoServiceProvider} from '../../services/geo.service.ts';
import {GeoDataSource} from '../../datasources/geo.datasource.ts';

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

```ts
import {merge} from 'lodash';
import * as GEO_CODER_CONFIG from '../src/datasources/geo.datasource.json';
import * as GEO_CODER_CONFIG from '../datasources/geo.datasource.json';

function givenGeoService() {
const config = merge({}, GEO_CODER_CONFIG, {
Expand Down
8 changes: 0 additions & 8 deletions examples/express-composition/tsconfig.build.json

This file was deleted.

6 changes: 0 additions & 6 deletions examples/hello-world/index.d.ts

This file was deleted.

8 changes: 0 additions & 8 deletions examples/hello-world/index.ts

This file was deleted.

4 changes: 3 additions & 1 deletion examples/hello-world/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
"node": ">=8.9"
},
"author": "IBM Corp.",
"types": "./dist/index.d.ts",
"scripts": {
"acceptance": "lb-mocha \"dist/__tests__/acceptance/**/*.js\"",
"build:apidocs": "lb-apidocs",
"build": "lb-tsc es2017 --outDir dist",
"build:watch": "lb-tsc es2017 --outDir dist --watch",
"build:resources": "lb-copy-resources",
"build:watch": "lb-tsc --watch",
"clean": "lb-clean *example-hello-world*.tgz dist package api-docs",
"verify": "npm pack && tar xf *example-hello-world*.tgz && tree package && npm run clean",
"lint": "npm run prettier:check && npm run tslint",
Expand Down
8 changes: 0 additions & 8 deletions examples/hello-world/tsconfig.build.json

This file was deleted.

25 changes: 25 additions & 0 deletions examples/hello-world/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"$schema": "http://json.schemastore.org/tsconfig",
"extends": "@loopback/build/config/tsconfig.common.json",
"compilerOptions": {
"rootDir": "src",
"composite": true,
"target": "es2017",
"outDir": "dist"
},
"references": [
{
"path": "../../packages/testlab/tsconfig.json"
},
{
"path": "../../packages/core/tsconfig.json"
},
{
"path": "../../packages/rest/tsconfig.json"
}
],
"include": [
"src/**/*",
"src/**/*.json"
]
}
6 changes: 0 additions & 6 deletions examples/log-extension/index.d.ts

This file was deleted.

6 changes: 0 additions & 6 deletions examples/log-extension/index.ts

This file was deleted.

4 changes: 3 additions & 1 deletion examples/log-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
"node": ">=8.9"
},
"author": "IBM Corp.",
"types": "./dist/index.d.ts",
"scripts": {
"build:apidocs": "lb-apidocs",
"build": "lb-tsc es2017 --outDir dist",
"build:watch": "lb-tsc es2017 --outDir dist --watch",
"build:resources": "lb-copy-resources",
"build:watch": "lb-tsc --watch",
"clean": "lb-clean *example-log-extension-*.tgz dist package api-docs",
"lint": "npm run prettier:check && npm run tslint",
"lint:fix": "npm run tslint:fix && npm run prettier:fix",
Expand Down
8 changes: 0 additions & 8 deletions examples/log-extension/tsconfig.build.json

This file was deleted.

31 changes: 31 additions & 0 deletions examples/log-extension/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"$schema": "http://json.schemastore.org/tsconfig",
"extends": "@loopback/build/config/tsconfig.common.json",
"compilerOptions": {
"rootDir": "src",
"composite": true,
"target": "es2017",
"outDir": "dist"
},
"references": [
{
"path": "../../packages/testlab/tsconfig.json"
},
{
"path": "../../packages/context/tsconfig.json"
},
{
"path": "../../packages/core/tsconfig.json"
},
{
"path": "../../packages/openapi-v3/tsconfig.json"
},
{
"path": "../../packages/rest/tsconfig.json"
}
],
"include": [
"src/**/*",
"src/**/*.json"
]
}
Loading

0 comments on commit 9866136

Please sign in to comment.