Skip to content

Commit

Permalink
Dynamic Import, Require, and Better Testing (#60)
Browse files Browse the repository at this point in the history
Dynamic Import, Require, and Better Testing
  • Loading branch information
dhkatz authored Aug 23, 2019
2 parents dcd43bd + e6d7ae4 commit 0eb4359
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 143 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ This file uses change log convention from [Keep a CHANGELOG].

## [Unreleased]

## [1.0.0] - 2019-08-23
### Added
- Added support for `require()` and `import()` call imports.
- Added a lot more tests to raise the coverage of the plugin.

## [0.2.1] - 2019-08-22
### Changed
- Updated testing and CI
Expand Down Expand Up @@ -45,7 +50,8 @@ This file uses change log convention from [Keep a CHANGELOG].
[Keep a CHANGELOG]: http://keepachangelog.com
[Semantic Versioning]: http://semver.org/

[unreleased]: https://github.com/dhkatz/gulp-ts-alias/compare/0.2.1...HEAD
[unreleased]: https://github.com/dhkatz/gulp-ts-alias/compare/1.0.0...HEAD
[1.0.0]: https://github.com/dhkatz/gulp-ts-alias/compare/0.2.1...1.0.0
[0.2.1]: https://github.com/dhkatz/gulp-ts-alias/compare/0.2.0...0.2.1
[0.2.0]: https://github.com/dhkatz/gulp-ts-alias/compare/0.1.5...0.2.0
[0.1.5]: https://github.com/dhkatz/gulp-ts-alias/compare/0.1.4...0.1.5
Expand Down
85 changes: 7 additions & 78 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gulp-ts-alias",
"version": "0.2.1",
"version": "1.0.0",
"description": "Use Gulp to resolve Typescript path aliases during compilation.",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand Down Expand Up @@ -41,13 +41,12 @@
"registry": "https://registry.npmjs.org"
},
"devDependencies": {
"@types/event-stream": "^3.3.34",
"@types/jest": "^24.0.0",
"@types/vinyl": "^2.0.2",
"coveralls": "^3.0.6",
"event-stream": "^3.3.4",
"jest": "^24.9.0",
"merge2": "^1.2.3",
"o-stream": "^0.2.2",
"ts-jest": "^24.0.2",
"tslint": "^5.12.1",
"tslint-eslint-rules": "^5.4.0",
Expand Down
60 changes: 34 additions & 26 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import path from 'path';

import PluginError from 'plugin-error';
import ObjectStream, { EnteredArgs } from 'o-stream';

import File = require('vinyl');

// @ts-ignore
import * as map from 'map-stream';

export interface FileData {
path: string;
index: number;
Expand Down Expand Up @@ -49,13 +46,19 @@ function parseImports(file: ReadonlyArray<string>, dir: string): FileData[] {
}

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

if (!matches) {
return null;
}

if (matches.length > 3) {
const multiple = [/from (["'])(.*?)\1/g, /import\((["'])(.*?)\1\)/g, /require\((["'])(.*?)\1\)/g].some((exp) => {
const results = line.match(exp);

return results && results.length > 1;
})

if (multiple) {
throw new PluginError('gulp-ts-alias', 'Multiple imports on the same line are currently not supported!');
}

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

return map((file: File, cb: (error: any, file?: any) => void) => {
if (file.isNull() || !file.contents) {
return cb(null, file);
}
return ObjectStream.transform({
onEntered: (args: EnteredArgs<File, File>) => {
const file = args.object;

if (file.isStream()) {
return cb(new PluginError('gulp-ts-alias', 'Streaming is not supported.'));
}
if (file.isStream()) {
throw new PluginError('gulp-ts-alias', 'Streaming is not supported.');
}

const contents: Buffer | NodeJS.ReadableStream | null = file.contents;
if (file.isNull() || !file.contents) {
args.output.push(file);
return;
}

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

const lines = contents.toString().split('\n');
const imports = parseImports(lines, file.path);
const lines = file.contents.toString().split('\n');
const imports = parseImports(lines, file.path);

if (imports.length === 0) {
return cb(null, file);
}
if (imports.length === 0) {
args.output.push(file);

const resolved = resolveImports(lines, imports, compilerOptions);
return;
}

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

cb(null, file);
});
file.contents = Buffer.from(resolved.join('\n'));

args.output.push(file);
}
})
};

export default aliasPlugin;
Expand Down
Loading

0 comments on commit 0eb4359

Please sign in to comment.