Skip to content
This repository was archived by the owner on Dec 12, 2023. It is now read-only.

Commit a599740

Browse files
committed
Fallback on index.d.ts when main is missing
1 parent a167555 commit a599740

3 files changed

Lines changed: 25 additions & 69 deletions

File tree

src/bundle.spec.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,9 @@ test('bundle', t => {
3333
``,
3434
`// Generated by typings`,
3535
`// Source: index.d.ts`,
36-
`declare module \'~example/index\' {`,
36+
`declare module \'example\' {`,
3737
`export { test } from \'~example~test\'`,
3838
`}`,
39-
'declare module \'example/index\' {',
40-
'export * from \'~example/index\';',
41-
'}',
42-
'declare module \'example\' {',
43-
'export * from \'~example/index\';',
44-
'}',
4539
''
4640
].join('\n'))
4741
})

src/lib/compile.spec.ts

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ test('compile', t => {
217217
)
218218
.then(out => {
219219
t.equal(out.results.main, [
220-
'declare module \'~foobar/file\' {',
220+
'declare module \'foobar\' {',
221221
'function foo (value: string): foo.Bar;',
222222
'',
223223
'module foo {',
@@ -230,14 +230,6 @@ test('compile', t => {
230230
'',
231231
'export = foo;',
232232
'}',
233-
'declare module \'foobar/file\' {',
234-
'import main = require(\'~foobar/file\');',
235-
'export = main;',
236-
'}',
237-
'declare module \'foobar\' {',
238-
'import main = require(\'~foobar/file\');',
239-
'export = main;',
240-
'}',
241233
''
242234
].join('\n'))
243235

@@ -270,17 +262,11 @@ test('compile', t => {
270262
)
271263
.then(out => {
272264
t.equal(out.results.main, [
273-
'declare module \'~test/index\' {',
265+
'declare module \'test\' {',
274266
'const foo: string;',
275267
'',
276268
'export default foo;',
277269
'}',
278-
'declare module \'test/index\' {',
279-
'export { default } from \'~test/index\';',
280-
'}',
281-
'declare module \'test\' {',
282-
'export { default } from \'~test/index\';',
283-
'}',
284270
''
285271
].join('\n'))
286272
})
@@ -334,16 +320,10 @@ test('compile', t => {
334320
'export * from \'~test~foo/index\';',
335321
'}',
336322
'',
337-
'declare module \'~test/index\' {',
323+
'declare module \'test\' {',
338324
'import * as x from \'~test~foo/x\'',
339325
'export * from \'~test~foo\'',
340326
'}',
341-
'declare module \'test/index\' {',
342-
'export * from \'~test/index\';',
343-
'}',
344-
'declare module \'test\' {',
345-
'export * from \'~test/index\';',
346-
'}',
347327
''
348328
].join('\n'))
349329
})
@@ -625,7 +605,7 @@ test('compile', t => {
625605

626606
return compile(main, ['main'], { name: 'main', cwd: __dirname, global: false, meta: false, emitter })
627607
.catch(function (error) {
628-
t.ok(/^Unable to resolve entry "\.d\.ts" file for "main"/.test(error.message))
608+
t.ok(/^Unable to read typings for "main"/.test(error.message))
629609
})
630610
})
631611

@@ -690,19 +670,11 @@ test('compile', t => {
690670
'export = main;',
691671
'}',
692672
'',
693-
'declare module \'~main/index\' {',
673+
'declare module \'main\' {',
694674
'import * as foo from \'~main/override\'',
695675
'',
696676
'export = foo',
697677
'}',
698-
'declare module \'main/index\' {',
699-
'import main = require(\'~main/index\');',
700-
'export = main;',
701-
'}',
702-
'declare module \'main\' {',
703-
'import main = require(\'~main/index\');',
704-
'export = main;',
705-
'}',
706678
''
707679
].join('\n'))
708680
})
@@ -742,11 +714,8 @@ test('compile', t => {
742714
].join('\n'))
743715

744716
t.equal(out.results.browser, [
745-
'declare module \'~main~dep/index\' {',
746-
'export function isDep (): boolean;',
747-
'}',
748717
'declare module \'~main~dep\' {',
749-
'export * from \'~main~dep/index\';',
718+
'export function isDep (): boolean;',
750719
'}',
751720
'',
752721
'declare module \'~main/index\' {',

src/lib/compile.ts

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -177,14 +177,13 @@ function getStringifyOptions (
177177

178178
const referenced: ts.Map<boolean> = {}
179179
const dependencies: ts.Map<StringifyOptions> = {}
180-
const entry = main == null ? main : resolveFrom(tree.src, normalizeToDefinition(main))
180+
const entry = main == null ? undefined : normalizeToDefinition(main)
181181
const prefix = `${parent ? parent.prefix : ''}${DEPENDENCY_SEPARATOR}${options.name}`
182182

183183
return extend(options, {
184184
tree,
185185
entry,
186186
prefix,
187-
isTypings,
188187
overrides,
189188
referenced,
190189
dependencies,
@@ -222,20 +221,9 @@ function compileDependencyPath (
222221
parentModule?: ModuleOptions
223222
): Promise<string> {
224223
const { tree, entry } = options
224+
const dependencyPath = resolveFrom(tree.src, path || entry || 'index.d.ts')
225225

226-
// Fallback to resolving the entry file.
227-
if (path == null) {
228-
if (entry == null) {
229-
return Promise.reject(new TypingsError(
230-
`Unable to resolve entry ".d.ts" file for "${options.name}", ` +
231-
'please make sure the module has a main or typings field'
232-
))
233-
}
234-
235-
return stringifyDependencyPath(resolveFrom(tree.src, entry), options, parentModule)
236-
}
237-
238-
return stringifyDependencyPath(resolveFrom(tree.src, path), options, parentModule)
226+
return stringifyDependencyPath(dependencyPath, path, options, parentModule)
239227
}
240228

241229
/**
@@ -244,7 +232,6 @@ function compileDependencyPath (
244232
interface StringifyOptions extends CompileOptions {
245233
entry: string
246234
prefix: string
247-
isTypings: boolean
248235
overrides: Overrides
249236
referenced: ts.Map<boolean>
250237
dependencies: ts.Map<StringifyOptions>
@@ -321,11 +308,12 @@ interface ModuleOptions {
321308
*/
322309
function stringifyDependencyPath (
323310
rawPath: string,
311+
originalPath: string,
324312
options: StringifyOptions,
325313
moduleOptions: ModuleOptions
326314
): Promise<string> {
327315
const path = getPath(rawPath, options)
328-
const { tree, global, cwd, resolution, name, readFiles, imported, meta, entry, emitter } = options
316+
const { tree, global, cwd, resolution, name, readFiles, imported, meta, emitter } = options
329317
const importedPath = importPath(rawPath, pathFromDefinition(rawPath), options)
330318

331319
// Return `null` to skip the dependency writing, could have the same import twice.
@@ -386,12 +374,12 @@ function stringifyDependencyPath (
386374
return loadByModuleName(path)
387375
}
388376

389-
return stringifyDependencyPath(path, options, childModuleOptions)
377+
return stringifyDependencyPath(path, importedFile, options, childModuleOptions)
390378
})
391379

392380
return Promise.all(imports)
393381
.then<string>(imports => {
394-
const stringified = stringifySourceFile(sourceFile, options, childModuleOptions)
382+
const stringified = stringifySourceFile(sourceFile, originalPath, options, childModuleOptions)
395383

396384
for (const reference of referencedFiles) {
397385
emitter.emit('reference', { name, rawPath, reference, tree, resolution })
@@ -411,7 +399,7 @@ function stringifyDependencyPath (
411399
const relativePath = relativeTo(tree.src, path)
412400

413401
// Provide better errors for the entry path.
414-
if (rawPath === entry) {
402+
if (originalPath == null) {
415403
return Promise.reject(new TypingsError(
416404
`Unable to read typings for "${options.name}". ` +
417405
`${authorPhrase} check the entry paths in "${basename(tree.src)}" are up to date`,
@@ -475,9 +463,14 @@ function importPath (path: string, name: string, options: StringifyOptions) {
475463
/**
476464
* Stringify a dependency file contents.
477465
*/
478-
function stringifySourceFile (sourceFile: ts.SourceFile, options: StringifyOptions, moduleOptions: ModuleOptions) {
479-
const { isExternal, path, rawPath } = moduleOptions
480-
const { tree, name, prefix, parent, isTypings, cwd, global, entry } = options
466+
function stringifySourceFile (
467+
sourceFile: ts.SourceFile,
468+
originalPath: string,
469+
options: StringifyOptions,
470+
moduleOptions: ModuleOptions
471+
) {
472+
const { isExternal, path } = moduleOptions
473+
const { tree, name, prefix, parent, cwd, global } = options
481474
const parentModule = moduleOptions.parent
482475

483476
// Output information for the original type source.
@@ -601,12 +594,12 @@ function stringifySourceFile (sourceFile: ts.SourceFile, options: StringifyOptio
601594
return declareText(name, imports.join(EOL))
602595
}
603596

604-
const isEntry = rawPath === entry
597+
const isEntry = originalPath == null
605598
const moduleText = normalizeEOL(processTree(sourceFile, replacer, read), EOL)
606599
const moduleName = parent && parent.global ? name : prefix
607600

608601
// Direct usage of definition/typings. This is *not* a psuedo-module.
609-
if (isEntry && isTypings && !hasLocalImports) {
602+
if (isEntry && !hasLocalImports) {
610603
return meta + declareText(parent ? moduleName : name, moduleText)
611604
}
612605

0 commit comments

Comments
 (0)