Skip to content
This repository has been archived by the owner on Dec 1, 2019. It is now read-only.

Commit

Permalink
feat(*): impl declaration option basic support
Browse files Browse the repository at this point in the history
  • Loading branch information
s-panferov committed Mar 29, 2016
1 parent 12c708c commit 0169df5
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ function isSourceMapEmit(fileName, outputFileName, sourceFileName) {
&& (outputFileName.substr(-7) === '.js.map' || outputFileName.substr(-8) === '.jsx.map');
}

function isDeclarationEmit(fileName, outputFileName, sourceFileName) {
return sourceFileName === fileName
&& (outputFileName.substr(-5) === '.d.ts');
}

export function findResultFor(output: host.IEmitOutput, fileName: string) {
let text;
let sourceMap;
let declaration: host.IOutputFile;
fileName = withoutExt(path.normalize(fileName));

for (let i = 0; i < output.outputFiles.length; i++) {
Expand All @@ -33,10 +39,14 @@ export function findResultFor(output: host.IEmitOutput, fileName: string) {
if (isSourceMapEmit(fileName, outputFileName, sourceFileName)) {
sourceMap = o.text;
}
if (isDeclarationEmit(fileName, outputFileName, sourceFileName)) {
declaration = o;
}
}
return {
text: text,
sourceMap: sourceMap
sourceMap: sourceMap,
declaration
};
}

Expand Down
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ async function compiler(webpack: IWebPack, text: string): Promise<void> {
throw new Error('No output found for ' + fileName);
}

if (result.declaration) {
webpack.emitFile(
path.relative(process.cwd(), result.declaration.sourceName),
result.declaration.text
);
}

resultText = result.text;

let sourceFileName = fileName.replace(process.cwd() + '/', '');
Expand Down
16 changes: 16 additions & 0 deletions src/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export interface IWebPack {
resolve: () => void;
addDependency: (dep: string) => void;
clearDependencies: () => void;
emitFile: (fileName: string, text: string) => void;
options: {
atl?: {
plugins: LoaderPluginDef[]
Expand Down Expand Up @@ -358,6 +359,21 @@ function setupAfterCompile(compiler, instanceName, forkChecker = false) {
}
});

if (instance.options.declaration) {
phantomImports.forEach(imp => {
let output = instance.tsState.services.getEmitOutput(imp);
let declarationFile = output.outputFiles.filter(filePath =>
!!filePath.name.match(/\.d.ts$/))[0];
if (declarationFile) {
let assetPath = path.relative(process.cwd(), declarationFile.name);
compilation.assets[assetPath] = {
source: () => declarationFile.text,
size: () => declarationFile.text.length
};
}
});
}

instance.compiledFiles = {};
compilation.fileDependencies.push.apply(compilation.fileDependencies, phantomImports);
compilation.fileDependencies = _.uniq(compilation.fileDependencies);
Expand Down
29 changes: 29 additions & 0 deletions src/test/declaration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {
cleanAndCompile, expect, readOutputFile,
fixturePath, readFixture, expectSource, createConfig
} from './utils';

describe('main test', function() {

it('should emit declaration files', async function() {
// babel need some time to init
this.timeout(10000);

let config = {
entry: fixturePath(['declaration', 'basic.ts'])
};

let loaderQuery = {
declaration: true
};

let stats = await cleanAndCompile(createConfig(config, { loaderQuery }));
expect(stats.compilation.errors.length).eq(0);
let assets = Object.keys(stats.compilation.assets);

expect(assets).to.include('src/test/fixtures/declaration/basic.d.ts');

// elided import
expect(assets).to.include('src/test/fixtures/declaration/iface.d.ts');
});
});
3 changes: 3 additions & 0 deletions src/test/fixtures/declaration/basic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Iface } from './iface';

export function foo(iface: Iface): Iface { return iface; }
4 changes: 4 additions & 0 deletions src/test/fixtures/declaration/iface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

export interface Iface {
name: string;
}
5 changes: 5 additions & 0 deletions src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,17 @@
"./resolver.ts",
"./test/babel.ts",
"./test/checker.ts",
"./test/declaration.ts",
"./test/fixtures/babel/babel.ts",
"./test/fixtures/basic/basic.ts",
"./test/fixtures/checker/to-check.ts",
"./test/fixtures/declaration/basic.ts",
"./test/fixtures/declaration/iface.ts",
"./test/fixtures/errors/with-type-errors.ts",
"./test/fixtures/salsa/index.ts",
"./test/index.ts",
"./test/output/src/test/fixtures/declaration/basic.d.ts",
"./test/output/src/test/fixtures/declaration/iface.d.ts",
"./test/salsa.ts",
"./test/utils.ts",
"./tsconfig-utils.ts"
Expand Down

0 comments on commit 0169df5

Please sign in to comment.