From a9a0ac8b0017c16fce6015eb18caaff81de65bed Mon Sep 17 00:00:00 2001 From: Bryan Forbes Date: Fri, 13 May 2016 17:32:45 -0500 Subject: [PATCH 1/2] Emit declaration files into the correct location based on WebPack's context --- src/index.ts | 13 ++++++++++--- src/instance.ts | 1 + 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index cef4ff5..2e05e18 100644 --- a/src/index.ts +++ b/src/index.ts @@ -121,10 +121,17 @@ async function compiler(webpack: IWebPack, text: string): Promise { } if (result.declaration) { - webpack.emitFile( - path.relative(process.cwd(), result.declaration.sourceName), - result.declaration.text + const dtsPath: string = loaderUtils.interpolateName( + { + resourcePath: result.declaration.sourceName, + options: webpack.options + }, + '[path][name].[ext]', + { + context: webpack.options.context + } ); + webpack.emitFile(dtsPath, result.declaration.text); } resultText = result.text; diff --git a/src/instance.ts b/src/instance.ts index 187d4cf..03e7835 100644 --- a/src/instance.ts +++ b/src/instance.ts @@ -60,6 +60,7 @@ export interface IWebPack { clearDependencies: () => void; emitFile: (fileName: string, text: string) => void; options: { + context: string; atl?: { plugins: LoaderPluginDef[] } From 069f1b0063d857523ad67cb208cf79ef5820d146 Mon Sep 17 00:00:00 2001 From: Bryan Forbes Date: Fri, 13 May 2016 18:52:15 -0500 Subject: [PATCH 2/2] Add unit test for contextual declaration files --- src/test/declaration.ts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/test/declaration.ts b/src/test/declaration.ts index dd51f5d..a9626b1 100644 --- a/src/test/declaration.ts +++ b/src/test/declaration.ts @@ -26,4 +26,30 @@ describe('main test', function() { // elided import expect(assets).to.include('src/test/fixtures/declaration/iface.d.ts'); }); + + it('should emit declaration files in context', async function() { + this.timeout(10000); + + let config = { + context: fixturePath(['declaration']), + entry: { + 'basic': 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('basic.d.ts'); + expect(assets).to.include('basic.js'); + + // TODO: Code should be changed to output elided import into the correct location + expect(assets).to.include('src/test/fixtures/declaration/iface.d.ts'); + }); });