|
1 | 1 | import path from 'path';
|
2 |
| - |
3 | 2 | import PluginError from 'plugin-error';
|
| 3 | +import ObjectStream, { EnteredArgs } from 'o-stream'; |
4 | 4 |
|
5 | 5 | import File = require('vinyl');
|
6 | 6 |
|
7 |
| -// @ts-ignore |
8 |
| -import * as map from 'map-stream'; |
9 |
| - |
10 | 7 | export interface FileData {
|
11 | 8 | path: string;
|
12 | 9 | index: number;
|
@@ -49,13 +46,19 @@ function parseImports(file: ReadonlyArray<string>, dir: string): FileData[] {
|
49 | 46 | }
|
50 | 47 |
|
51 | 48 | 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\)/); |
53 | 50 |
|
54 | 51 | if (!matches) {
|
55 | 52 | return null;
|
56 | 53 | }
|
57 | 54 |
|
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) { |
59 | 62 | throw new PluginError('gulp-ts-alias', 'Multiple imports on the same line are currently not supported!');
|
60 | 63 | }
|
61 | 64 |
|
@@ -145,34 +148,39 @@ const aliasPlugin: AliasPlugin = (pluginOptions: PluginOptions) => {
|
145 | 148 | compilerOptions.baseUrl = './';
|
146 | 149 | }
|
147 | 150 |
|
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; |
152 | 154 |
|
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 | + } |
156 | 158 |
|
157 |
| - const contents: Buffer | NodeJS.ReadableStream | null = file.contents; |
| 159 | + if (file.isNull() || !file.contents) { |
| 160 | + args.output.push(file); |
| 161 | + return; |
| 162 | + } |
158 | 163 |
|
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 | + } |
162 | 167 |
|
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); |
165 | 170 |
|
166 |
| - if (imports.length === 0) { |
167 |
| - return cb(null, file); |
168 |
| - } |
| 171 | + if (imports.length === 0) { |
| 172 | + args.output.push(file); |
169 | 173 |
|
170 |
| - const resolved = resolveImports(lines, imports, compilerOptions); |
| 174 | + return; |
| 175 | + } |
171 | 176 |
|
172 |
| - file.contents = Buffer.from(resolved.join('\n')); |
| 177 | + const resolved = resolveImports(lines, imports, compilerOptions); |
173 | 178 |
|
174 |
| - cb(null, file); |
175 |
| - }); |
| 179 | + file.contents = Buffer.from(resolved.join('\n')); |
| 180 | + |
| 181 | + args.output.push(file); |
| 182 | + } |
| 183 | + }) |
176 | 184 | };
|
177 | 185 |
|
178 | 186 | export default aliasPlugin;
|
|
0 commit comments