-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: generate correct declarations, diagnostics, and .tsbuildinfo wh…
…en a Rollup cache is being used. Fixes #140
- Loading branch information
Showing
4 changed files
with
161 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import {InputOptions} from "rollup"; | ||
|
||
interface EqualResult { | ||
equal: true; | ||
} | ||
|
||
interface NotEqualResult { | ||
equal: false; | ||
path: string[]; | ||
} | ||
|
||
type EqualityResult = EqualResult | NotEqualResult; | ||
|
||
const ignoredKeys = new Set(["cache"]); | ||
|
||
/** | ||
* Treat the options as equal if their properties are somewhat equal, not taking their cache into account | ||
*/ | ||
export function inputOptionsAreEqual(a: InputOptions, b: InputOptions): boolean { | ||
return inputValuesAreEqual(a, b).equal; | ||
} | ||
|
||
function inputValuesAreEqual(a: unknown, b: unknown, path: string[] = []): EqualityResult { | ||
if (a === b || (a == null && b == null)) return {equal: true}; | ||
else if (typeof a !== typeof b) return {equal: false, path}; | ||
else if (Array.isArray(a)) { | ||
if (!Array.isArray(b)) return {equal: false, path}; | ||
else if (a.length !== b.length) return {equal: false, path}; | ||
else if (a.some((element, index) => !inputValuesAreEqual(element, b[index], [...path, String(index)]).equal)) { | ||
return {equal: false, path}; | ||
} else { | ||
return {equal: true}; | ||
} | ||
} else if (typeof a === "object" && a != null) { | ||
if (typeof b !== "object" || b == null) return {equal: false, path}; | ||
|
||
const aKeys = Object.keys(a).filter(key => !ignoredKeys.has(key)); | ||
const bKeys = Object.keys(b).filter(key => !ignoredKeys.has(key)); | ||
|
||
if (aKeys.length !== bKeys.length) return {equal: false, path}; | ||
else if (aKeys.some(key => !inputValuesAreEqual(a[key as keyof typeof a], b[key as keyof typeof b], [...path, key]).equal)) { | ||
return {equal: false, path}; | ||
} else { | ||
return {equal: true}; | ||
} | ||
} else { | ||
return {equal: true}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import test from "ava"; | ||
import {withTypeScript} from "./util/ts-macro"; | ||
import {formatCode} from "./util/format-code"; | ||
import {generateRollupBundle} from "./setup/setup-rollup"; | ||
|
||
test.serial("Declaration bundling works properly when Rollup is using a cache. #1", withTypeScript, async (t, {typescript}) => { | ||
const bundle = await generateRollupBundle( | ||
[ | ||
{ | ||
entry: true, | ||
fileName: "index.ts", | ||
text: `\ | ||
export default () => 42 | ||
` | ||
} | ||
], | ||
{typescript, runCachedBuild: true, debug: false} | ||
); | ||
const { | ||
declarations: [file] | ||
} = bundle; | ||
|
||
t.deepEqual( | ||
formatCode(file.code), | ||
formatCode(`\ | ||
declare const _default: () => number; | ||
export { _default as default }; | ||
`) | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters