Skip to content

Commit 49b9a82

Browse files
committed
feat(build): add an option to copy non ts files to outDir
Copies non-.ts files (json, images, text, etc.) to the outDir (dist) so file paths can remain relative. Does so for the `include` directories listed in `tsconfig.json`.
1 parent 5dc4724 commit 49b9a82

File tree

5 files changed

+37
-7
lines changed

5 files changed

+37
-7
lines changed

packages/build/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Please remember to replace `your-module-name` with the name of your module.
5151

5252
Now you run the scripts, such as:
5353

54-
- `npm run build` - Compile TypeScript files
54+
- `npm run build` - Compile TypeScript files and copy resources (non `.ts` files) to outDir
5555
- `npm test` - Run all mocha tests
5656
- `npm run lint` - Run `tslint` and `prettier` on source files
5757

@@ -89,6 +89,11 @@ Now you run the scripts, such as:
8989
For more information, see
9090
<https://www.typescriptlang.org/docs/handbook/compiler-options.html>.
9191

92+
- The following un-official compiler options are available:
93+
|Option|Description|
94+
|--|--|
95+
|`--ignore-resources`|Do not copy any resources to outDir|
96+
9297
- lb-tslint
9398

9499
By default, `lb-tslint` searches your project's root directory for

packages/build/bin/compile-package.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ function run(argv, options) {
2121
const utils = require('./utils');
2222
const path = require('path');
2323
const fs = require('fs');
24+
const glob = require('glob');
25+
const fse = require('fs-extra');
2426

2527
const packageDir = utils.getPackageDir();
2628

@@ -29,9 +31,19 @@ function run(argv, options) {
2931
const isTargetSet = utils.isOptionSet(compilerOpts, '--target');
3032
const isOutDirSet = utils.isOptionSet(compilerOpts, '--outDir');
3133
const isProjectSet = utils.isOptionSet(compilerOpts, '-p', '--project');
34+
const isIgnoreResourcesSet = utils.isOptionSet(
35+
compilerOpts,
36+
'--ignore-resources'
37+
);
3238

3339
var target;
3440

41+
// --ignore-resources is not a TS Compiler option so we remove it from the
42+
// list of compiler options to avoid compiler errors.
43+
if (isIgnoreResourcesSet) {
44+
compilerOpts.splice(compilerOpts.indexOf('--ignore-resources'), 1);
45+
}
46+
3547
if (!isTargetSet) {
3648
// Find the last non-option argument as the `target`
3749
// For example `-p tsconfig.json es2017` or `es2017 -p tsconfig.json`
@@ -110,6 +122,22 @@ function run(argv, options) {
110122

111123
if (outDir) {
112124
args.push('--outDir', outDir);
125+
126+
// Since outDir is set, ts files are compiled into that directory.
127+
// If ignore-resources flag is not passed, copy resources (non-ts files)
128+
// to the same outDir as well.
129+
if (rootDir && tsConfigFile && !isIgnoreResourcesSet) {
130+
const tsConfig = require(tsConfigFile);
131+
const dirs = tsConfig.include
132+
? tsConfig.include.join('|')
133+
: ['src', 'test'].join('|');
134+
135+
const pattern = `@(${dirs})/**/!(*.ts)`;
136+
const files = glob.sync(pattern, {root: packageDir, nodir: true});
137+
for (const file of files) {
138+
fse.copySync(path.join(packageDir, file), path.join(outDir, file));
139+
}
140+
}
113141
}
114142

115143
if (target) {

packages/build/config/tsconfig.build.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
"src",
88
"test"
99
],
10-
"exclude": ["node_modules/**", "packages/*/node_modules/**", "examples/*/node_modules/**", "**/*.d.ts"]
10+
"exclude": ["node_modules/**", "**/*.d.ts"]
1111
}

packages/build/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"cross-spawn": "^6.0.5",
1919
"debug": "^3.1.0",
2020
"fs-extra": "^5.0.0",
21+
"glob": "^7.1.2",
2122
"mocha": "^5.1.1",
2223
"nyc": "^11.7.1",
2324
"prettier": "^1.12.1",

packages/testlab/test/integration/test-sandbox.integration.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@ describe('TestSandbox integration tests', () => {
1111
let sandbox: TestSandbox;
1212
let path: string;
1313
const COPY_FILE = 'copy-me.txt';
14-
const COPY_FILE_PATH = resolve(
15-
__dirname,
16-
'../../../test/fixtures',
17-
COPY_FILE,
18-
);
14+
const COPY_FILE_PATH = resolve(__dirname, '../fixtures', COPY_FILE);
1915

2016
beforeEach(createSandbox);
2117
beforeEach(givenPath);

0 commit comments

Comments
 (0)