Skip to content

Commit 7a402a8

Browse files
Merge pull request #33 from bbc/ensure-snippets-are-compiled-independently
Use a separate compiler instance per snippet
2 parents 3686a26 + 4506393 commit 7a402a8

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## Unreleased
88

9+
## [2.5.3]
10+
### Changed
11+
- Use a separate `ts-node` compiler per-snippet to ensure that compilation of snippets is independent
12+
913
## [2.5.2]
1014
### Removed
1115
- Obsolete Travis CI build badge from README

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"typescript",
1919
"verify"
2020
],
21-
"version": "2.5.2",
21+
"version": "2.5.3",
2222
"main": "dist/index.js",
2323
"@types": "dist/index.d.ts",
2424
"bin": {

src/SnippetCompiler.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export type SnippetCompilationResult = {
2424
};
2525

2626
export class SnippetCompiler {
27-
private readonly compiler: TSNode.Service;
27+
private readonly compilerConfig: TSNode.CreateOptions;
2828

2929
constructor(
3030
private readonly workingDirectory: string,
@@ -35,11 +35,10 @@ export class SnippetCompiler {
3535
packageDefinition.packageRoot,
3636
project
3737
);
38-
const tsConfig = {
38+
this.compilerConfig = {
3939
...(configOptions.config as TSNode.CreateOptions),
4040
transpileOnly: false,
4141
};
42-
this.compiler = TSNode.create(tsConfig);
4342
}
4443

4544
private static loadTypeScriptConfig(
@@ -124,7 +123,8 @@ export class SnippetCompiler {
124123
const id = process.hrtime.bigint().toString();
125124
const codeFile = path.join(this.workingDirectory, `block-${id}.${type}`);
126125
await fsExtra.writeFile(codeFile, code);
127-
this.compiler.compile(code, codeFile);
126+
const compiler = TSNode.create(this.compilerConfig);
127+
compiler.compile(code, codeFile);
128128
}
129129

130130
private removeTemporaryFilePaths(

test/TypeScriptDocsVerifierSpec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,31 @@ export const bob = () => (<div></div>);
388388
}
389389
);
390390

391+
verify.it("compiles snippets independently", async () => {
392+
const snippet1 = `interface Foo { bar: 123 }`;
393+
const snippet2 = `interface Foo { bar: () => void }`;
394+
const typeScriptMarkdown = wrapSnippet(snippet1) + wrapSnippet(snippet2);
395+
await createProject({
396+
markdownFiles: [{ name: "README.md", contents: typeScriptMarkdown }],
397+
});
398+
return await TypeScriptDocsVerifier.compileSnippets().should.eventually.eql(
399+
[
400+
{
401+
file: "README.md",
402+
index: 1,
403+
snippet: snippet1,
404+
linesWithErrors: [],
405+
},
406+
{
407+
file: "README.md",
408+
index: 2,
409+
snippet: snippet2,
410+
linesWithErrors: [],
411+
},
412+
]
413+
);
414+
});
415+
391416
verify.it(
392417
"compiles snippets containing modules",
393418
genSnippet,

0 commit comments

Comments
 (0)