Skip to content

Commit fc28a04

Browse files
committed
New changes:
1) Following standard practice, declaration files from packages are now copied to paths like typings/package_name/package_name.d.ts, fixes Urigo/meteor-angular2.0-socially#32 2) Watch only for declaration files only from the typings folder
1 parent 1ad38c4 commit fc28a04

File tree

3 files changed

+77
-26
lines changed

3 files changed

+77
-26
lines changed

compilers/basic_compiler.js

Lines changed: 65 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,12 @@ TsBasicCompiler = class TsBasicCompiler {
7979
};
8080
}
8181

82+
// Reads "files" property of the config.
83+
// Filter out everything except declaration files
84+
// in the "typings" folder.
8285
parsedConfig.typings = [];
8386
if (tsconfig.files) {
84-
parsedConfig.typings = this._parseTypings(tsconfig.files);
87+
parsedConfig.typings = this.filterTypings(tsconfig.files);
8588
}
8689

8790
return parsedConfig;
@@ -106,14 +109,11 @@ TsBasicCompiler = class TsBasicCompiler {
106109
return result.options;
107110
}
108111

109-
// Parses "files" property of the config
110-
// sifting out everything except declaration files
111-
// in the typings folder.
112-
_parseTypings(files) {
113-
check(files, Array);
112+
filterTypings(filePaths) {
113+
check(filePaths, Array);
114114

115-
return files.filter(file => {
116-
return this._typingsRegEx.test(file);
115+
return filePaths.filter(filePath => {
116+
return this._typingsRegEx.test(filePath);
117117
});
118118
}
119119

@@ -137,45 +137,87 @@ TsBasicCompiler = class TsBasicCompiler {
137137
let cfgFile = _.first(files.filter(file => this.isConfigFile(file)));
138138
if (cfgFile) {
139139
let cfgHash = cfgFile.getSourceHash();
140+
// If config has changed,
141+
// create and apply new one.
140142
if (cfgHash !== this._cfgHash) {
141143
this._tsconfig = this._createConfig(cfgFile.getContentsAsString());
142144
this._cfgHash = cfgHash;
143145
}
144146
}
145147
}
146148

149+
// Gets standardized declaration file path, i.e.,
150+
// path that contains package name inside.
151+
// if package "foo" has a declaration file typings/foo.d.ts,
152+
// then standardized path will be typings/foo/foo.d.ts.
153+
_getStandardTypingsFilePath(file) {
154+
let filePath = file.getPathInPackage();
155+
let dirPath = ts.getDirectoryPath(ts.normalizePath(filePath));
156+
let pkgName = file.getPackageName();
157+
if (pkgName.indexOf(':') != -1) {
158+
pkgName = pkgName.split(':')[1];
159+
}
160+
let pkgTest = new RegExp(`.*\/${pkgName}(\/.+|$)`);
161+
if (pkgTest.test(dirPath) === false) {
162+
let pkgDirPath = ts.combinePaths(dirPath, pkgName);
163+
let fileName = ts.getBaseFileName(filePath);
164+
filePath = ts.combinePaths(pkgDirPath, fileName);
165+
}
166+
167+
return filePath;
168+
}
169+
170+
// Copies declaration files from packages to apps.
171+
// Allows only files from the "typings" folder in packages
172+
// and copy them to the "typings" folder in apps as well.
147173
processTypings(files) {
148174
let dtFiles = files.filter(file => {
149-
return this.isDeclarationFile(file) &&
150-
this.isPackageFile(file) &&
151-
!this._typingsMap.has(path);
175+
// Check if it's a package declaration file.
176+
let isPkgTypings = this.isDeclarationFile(file) &&
177+
this.isPackageFile(file);
178+
179+
if (isPkgTypings) {
180+
let path = file.getPathInPackage();
181+
// Check if the file is in the "typings" folder.
182+
if (!this._typingsRegEx.test(path)) {
183+
console.log('Typings path ${path} doesn\'t start with "typings"');
184+
return false;
185+
}
186+
187+
// Check if it's not been processed.
188+
return !this._typingsMap.has(path);
189+
}
190+
191+
return false;
152192
});
193+
153194
let missingFiles = [];
154195
for (let file of dtFiles) {
155-
let path = file.getPathInPackage();
156-
// Resolve typings file relatively the current app folder.
157-
if (!fs.existsSync(path)) {
158-
missingFiles.push(file);
196+
let filePath = this._getStandardTypingsFilePath(file);
197+
198+
// Resolve typings file relatively to the current app folder.
199+
if (!fs.existsSync(filePath)) {
200+
missingFiles.push({ filePath, file });
159201
}
160202
}
203+
161204
if (missingFiles.length) {
162-
missingFiles.forEach(file => {
163-
this._createTypings(file);
164-
this._typingsMap.set(file.getPathInPackage());
205+
missingFiles.forEach(({filePath, file}) => {
206+
this._createTypings(filePath, file);
207+
this._typingsMap.set(filePath);
165208
});
166209

167210
// Report about newly installed typings.
168211
console.log(chalk.green('***** New typings have been added *****'));
169-
missingFiles.forEach(file => {
170-
console.log(chalk.green(file.getPathInPackage()));
212+
missingFiles.forEach(({filePath, file}) => {
213+
console.log(chalk.green(filePath));
171214
});
172215
console.log(chalk.green(
173216
'Add typings in tsconfig.json or by references in files.'));
174217
}
175218
}
176219

177-
_createTypings(file) {
178-
let filePath = file.getPathInPackage();
220+
_createTypings(filePath, file) {
179221
let dirPath = ts.getDirectoryPath(filePath);
180222
if (!fs.existsSync(dirPath)) {
181223
mkdirp.sync(Plugin.convertToOSPath(dirPath));
@@ -218,8 +260,7 @@ TsBasicCompiler = class TsBasicCompiler {
218260
}
219261

220262
isDeclarationFile(file) {
221-
return TypeScript.isDeclarationFile(
222-
file.getBasename());
263+
return TypeScript.isDeclarationFile(file.getBasename());
223264
}
224265

225266
isConfigFile(file) {

compilers/ts_caching_compiler.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,19 @@ TsCachingCompiler = class TsCachingCompiler extends MultiFileCachingCompiler {
7777
referencedPaths = result.referencedPaths;
7878
}
7979

80+
// Watch declaration files only from "typings" folder.
81+
referencedPaths = this.filterTypings(referencedPaths);
82+
83+
// Check and take only the files that exist.
84+
// Otherwise, caching compiler would throw an exception
85+
// on non-existing ones.
8086
referencedPaths = referencedPaths.filter(refPath => {
8187
let realPath = path.resolve(refPath);
82-
return fs.existsSync(realPath);
88+
if (!fs.existsSync(realPath)) {
89+
console.log(`Declaration file ${realPath} doesn't exist`);
90+
return false;
91+
}
92+
return true;
8393
});
8494

8595
let compileResult = {

package.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package.describe({
22
name: 'barbatus:ts-compilers',
3-
version: '0.2.6',
3+
version: '0.2.7',
44
summary: 'TypeScript Compilers for Meteor',
55
git: 'https://github.com/barbatus/ts-compilers',
66
documentation: 'README.md'

0 commit comments

Comments
 (0)