Skip to content

Commit 0eb4359

Browse files
authored
Dynamic Import, Require, and Better Testing (#60)
Dynamic Import, Require, and Better Testing
2 parents dcd43bd + e6d7ae4 commit 0eb4359

File tree

5 files changed

+161
-143
lines changed

5 files changed

+161
-143
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ This file uses change log convention from [Keep a CHANGELOG].
55

66
## [Unreleased]
77

8+
## [1.0.0] - 2019-08-23
9+
### Added
10+
- Added support for `require()` and `import()` call imports.
11+
- Added a lot more tests to raise the coverage of the plugin.
12+
813
## [0.2.1] - 2019-08-22
914
### Changed
1015
- Updated testing and CI
@@ -45,7 +50,8 @@ This file uses change log convention from [Keep a CHANGELOG].
4550
[Keep a CHANGELOG]: http://keepachangelog.com
4651
[Semantic Versioning]: http://semver.org/
4752

48-
[unreleased]: https://github.com/dhkatz/gulp-ts-alias/compare/0.2.1...HEAD
53+
[unreleased]: https://github.com/dhkatz/gulp-ts-alias/compare/1.0.0...HEAD
54+
[1.0.0]: https://github.com/dhkatz/gulp-ts-alias/compare/0.2.1...1.0.0
4955
[0.2.1]: https://github.com/dhkatz/gulp-ts-alias/compare/0.2.0...0.2.1
5056
[0.2.0]: https://github.com/dhkatz/gulp-ts-alias/compare/0.1.5...0.2.0
5157
[0.1.5]: https://github.com/dhkatz/gulp-ts-alias/compare/0.1.4...0.1.5

package-lock.json

Lines changed: 7 additions & 78 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gulp-ts-alias",
3-
"version": "0.2.1",
3+
"version": "1.0.0",
44
"description": "Use Gulp to resolve Typescript path aliases during compilation.",
55
"main": "./lib/index.js",
66
"types": "./lib/index.d.ts",
@@ -41,13 +41,12 @@
4141
"registry": "https://registry.npmjs.org"
4242
},
4343
"devDependencies": {
44-
"@types/event-stream": "^3.3.34",
4544
"@types/jest": "^24.0.0",
4645
"@types/vinyl": "^2.0.2",
4746
"coveralls": "^3.0.6",
48-
"event-stream": "^3.3.4",
4947
"jest": "^24.9.0",
5048
"merge2": "^1.2.3",
49+
"o-stream": "^0.2.2",
5150
"ts-jest": "^24.0.2",
5251
"tslint": "^5.12.1",
5352
"tslint-eslint-rules": "^5.4.0",

src/index.ts

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import path from 'path';
2-
32
import PluginError from 'plugin-error';
3+
import ObjectStream, { EnteredArgs } from 'o-stream';
44

55
import File = require('vinyl');
66

7-
// @ts-ignore
8-
import * as map from 'map-stream';
9-
107
export interface FileData {
118
path: string;
129
index: number;
@@ -49,13 +46,19 @@ function parseImports(file: ReadonlyArray<string>, dir: string): FileData[] {
4946
}
5047

5148
function findImport(line: string): string | null {
52-
const matches = line.match(/from (["'])(.*?)\1/);
49+
const matches = line.match(/from (["'])(.*?)\1/) || line.match(/import\((["'])(.*?)\1\)/) || line.match(/require\((["'])(.*?)\1\)/);
5350

5451
if (!matches) {
5552
return null;
5653
}
5754

58-
if (matches.length > 3) {
55+
const multiple = [/from (["'])(.*?)\1/g, /import\((["'])(.*?)\1\)/g, /require\((["'])(.*?)\1\)/g].some((exp) => {
56+
const results = line.match(exp);
57+
58+
return results && results.length > 1;
59+
})
60+
61+
if (multiple) {
5962
throw new PluginError('gulp-ts-alias', 'Multiple imports on the same line are currently not supported!');
6063
}
6164

@@ -145,34 +148,39 @@ const aliasPlugin: AliasPlugin = (pluginOptions: PluginOptions) => {
145148
compilerOptions.baseUrl = './';
146149
}
147150

148-
return map((file: File, cb: (error: any, file?: any) => void) => {
149-
if (file.isNull() || !file.contents) {
150-
return cb(null, file);
151-
}
151+
return ObjectStream.transform({
152+
onEntered: (args: EnteredArgs<File, File>) => {
153+
const file = args.object;
152154

153-
if (file.isStream()) {
154-
return cb(new PluginError('gulp-ts-alias', 'Streaming is not supported.'));
155-
}
155+
if (file.isStream()) {
156+
throw new PluginError('gulp-ts-alias', 'Streaming is not supported.');
157+
}
156158

157-
const contents: Buffer | NodeJS.ReadableStream | null = file.contents;
159+
if (file.isNull() || !file.contents) {
160+
args.output.push(file);
161+
return;
162+
}
158163

159-
if (contents === null) {
160-
return cb(null, file);
161-
}
164+
if (!file.path) {
165+
throw new PluginError('gulp-ts-alias', 'Received file with no path. Files must have path to be resolved.');
166+
}
162167

163-
const lines = contents.toString().split('\n');
164-
const imports = parseImports(lines, file.path);
168+
const lines = file.contents.toString().split('\n');
169+
const imports = parseImports(lines, file.path);
165170

166-
if (imports.length === 0) {
167-
return cb(null, file);
168-
}
171+
if (imports.length === 0) {
172+
args.output.push(file);
169173

170-
const resolved = resolveImports(lines, imports, compilerOptions);
174+
return;
175+
}
171176

172-
file.contents = Buffer.from(resolved.join('\n'));
177+
const resolved = resolveImports(lines, imports, compilerOptions);
173178

174-
cb(null, file);
175-
});
179+
file.contents = Buffer.from(resolved.join('\n'));
180+
181+
args.output.push(file);
182+
}
183+
})
176184
};
177185

178186
export default aliasPlugin;

0 commit comments

Comments
 (0)