Skip to content

Commit 117cc9a

Browse files
committed
Pushing in types. Pushing in vite compatibility.
1 parent 3976fc6 commit 117cc9a

17 files changed

+885
-38
lines changed

package.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "0.5.3",
44
"main": "dist/index.js",
55
"module": "dist/index.mjs",
6-
"types": "dist/main.d.ts",
6+
"types": "types/index.d.ts",
77
"author": {
88
"name": "Ian Carson"
99
},
@@ -27,16 +27,15 @@
2727
"@types/jest": "^27.0.2",
2828
"jest": "^27.3.1",
2929
"lorem-ipsum": "^2.0.4",
30-
"rollup": "^2.61.1",
31-
"rollup-typescript-plugin-projects": "^0.5.2",
3230
"ts-jest": "^27.0.7",
3331
"typescript": "^4.5.3",
3432
"unique-names-generator": "^4.6.0"
3533
},
3634
"dependencies": {
3735
"@rollup/plugin-typescript": "^8.3.0",
38-
"rollup": "^2.59.0",
39-
"tslib": "^2.3.1"
36+
"rollup": "^2.66.1",
37+
"tslib": "^2.3.1",
38+
"vite": "^2.7.13"
4039
},
4140
"files": [
4241
"dist",

rollup.confg.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import typescript from "rollup-typescript-plugin-projects";
1+
import typescript from "@rollup/plugin-typescript";
22
import fs from "fs";
33

44
let pkg = JSON.parse(fs.readFileSync("package.json"));

src/TypescriptPlugin.ts

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import Watch from "./Watch";
1313
import FileRepositoryCache from "./File/FileRepositoryCache";
1414
import { FileHelpers } from "./File/FileHelper";
1515
import { ProjectOptions } from "./SolutionBuilderConfigProcessor";
16+
import { ResolvedConfig } from "vite";
1617

1718
export interface TypescriptPluginOptions
1819
{
@@ -50,6 +51,12 @@ export default class TypescriptPlugin implements Plugin
5051
private readonly fileRepository: FileRepositoryCache;
5152
private readonly solutionBuilder: SolutionBuilderPlugin;
5253
private readonly watch: Watch;
54+
private _viteConfig: ResolvedConfig | undefined;
55+
56+
public get viteConfig(): ResolvedConfig | undefined
57+
{
58+
return this._viteConfig;
59+
}
5360

5461
//region Rollup Context
5562
private _rollupContext: PluginContext | undefined;
@@ -82,6 +89,12 @@ export default class TypescriptPlugin implements Plugin
8289
} );
8390
}
8491

92+
//Vite plugins
93+
public configResolved( config: ResolvedConfig )
94+
{
95+
this._viteConfig = config;
96+
}
97+
8598
//region Rollup Plugin Methods
8699

87100
/**
@@ -100,19 +113,21 @@ export default class TypescriptPlugin implements Plugin
100113
this.fileRepository.resetConsumed();
101114
this.solutionBuilder.run();
102115

103-
if ( this.pluginOptions.includeUnusedFiles ) {
104-
for ( let contextPath of this.fileRepository.getContextDestinations() ) {
105-
let file = contextPath.file;
106-
let baseDir = file.compilerOptions.rootDir;
116+
if (!this.viteConfig || this.viteConfig.command === 'build') {
117+
if ( this.pluginOptions.includeUnusedFiles ) {
118+
for ( let contextPath of this.fileRepository.getContextDestinations() ) {
119+
let file = contextPath.file;
120+
let baseDir = file.compilerOptions.rootDir;
107121

108-
//TODO Cannot handle outputting by file at the moment.
109-
if ( !baseDir ) throw new Error( `TypeScript config outDir not set. Rollup does not know where to output.` );
122+
//TODO Cannot handle outputting by file at the moment.
123+
if ( !baseDir ) throw new Error( `TypeScript config outDir not set. Rollup does not know where to output.` );
110124

111-
this.rollupContext.emitFile( {
112-
type: 'chunk',
113-
id: relative( baseDir, contextPath.path ),
114-
preserveSignature: "allow-extension"
115-
} );
125+
this.rollupContext.emitFile( {
126+
type: 'chunk',
127+
id: relative( baseDir, contextPath.path ),
128+
preserveSignature: "allow-extension"
129+
} );
130+
}
116131
}
117132
}
118133
}
@@ -178,22 +193,24 @@ export default class TypescriptPlugin implements Plugin
178193
*/
179194
public async buildEnd()
180195
{
181-
for ( let path of this.fileRepository.getConsumedPaths() ) {
182-
let contextPath = this.fileRepository.getDefinitionFromPath( path );
196+
if (!this.viteConfig || this.viteConfig.command === 'build') {
197+
for ( let path of this.fileRepository.getConsumedPaths() ) {
198+
let contextPath = this.fileRepository.getDefinitionFromPath( path );
183199

184-
// Skip if no definition file found.
185-
if ( !contextPath ) return;
200+
// Skip if no definition file found.
201+
if ( !contextPath ) return;
186202

187-
let baseDir = contextPath.file.compilerOptions.outDir;
203+
let baseDir = contextPath.file.compilerOptions.outDir;
188204

189-
//TODO Cannot handle outputting by file at the moment.
190-
if ( !baseDir ) throw new Error( `TypeScript config rootDir not set. Rollup does not know where to output.` );
205+
//TODO Cannot handle outputting by file at the moment.
206+
if ( !baseDir ) throw new Error( `TypeScript config rootDir not set. Rollup does not know where to output.` );
191207

192-
this.rollupContext.emitFile( {
193-
type: 'asset',
194-
fileName: relative( baseDir, contextPath.contextPath ),
195-
source: contextPath.context.text
196-
} );
208+
this.rollupContext.emitFile( {
209+
type: 'asset',
210+
fileName: relative( baseDir, contextPath.contextPath ),
211+
source: contextPath.context.text
212+
} );
213+
}
197214
}
198215
}
199216

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export default function typescript( options: TypescriptPluginOptions = <Typescri
1818
const typescriptPlugin = new TypescriptPlugin(options);
1919
return <Plugin>{
2020
name: typescriptPlugin.name,
21+
configResolved: typescriptPlugin.configResolved.bind(typescriptPlugin),
2122
/**
2223
* We have to wait till buildStart for the context to be created by rollup.
2324
* @param options

tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
"lib": [
55
"es6"
66
],
7-
// "module": "CommonJS",
87
"moduleResolution": "node",
98
"declaration": true,
9+
"declarationDir": "./types",
1010
"sourceMap": true,
1111
"esModuleInterop": true,
1212
"incremental": true,
1313
"strict": true,
1414
"target": "es2020",
15-
"outDir": "./dist",
15+
"outDir": "./temp",
1616
"rootDir": "./src",
1717
"baseUrl": ".",
1818
"paths": {

types/BuildMode.d.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { SemanticDiagnosticsBuilderProgram, SolutionBuilder, SolutionBuilderHostBase, WatchStatusReporter } from "typescript";
2+
/**
3+
* Helps create a SolutionBuilder with or without watch based on the watchMode toggle.
4+
*
5+
* Due to how similar these projects are, We don't need to create different configurations as the watch method on
6+
* SolutionBuilderPlugin will never be called when watch is disabled.
7+
*/
8+
export default class BuildMode {
9+
private readonly generateHostHooks?;
10+
private _host;
11+
private _solutionBuilder;
12+
get host(): SolutionBuilderHostBase<SemanticDiagnosticsBuilderProgram>;
13+
private set host(value);
14+
get solutionBuilder(): SolutionBuilder<SemanticDiagnosticsBuilderProgram>;
15+
private set solutionBuilder(value);
16+
private createWatchMode;
17+
private createExecuteMode;
18+
constructor(watchMode: boolean, generateHostHooks?: (host: SolutionBuilderHostBase<SemanticDiagnosticsBuilderProgram>) => void, watchDiagnosticsBind?: WatchStatusReporter);
19+
}

types/File/ContextsCollection.d.ts

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import { Context, ContextPathData, ContextsElement, ContextType, File } from "./Definitions";
2+
import ContextsMap from "./ContextsMap";
3+
/**
4+
* The ContextCollection handles processing new context and files within a global project scope.
5+
* It can help to index directories and also remove stale contexts as changes are applied.
6+
*
7+
* {@see FileRepositoryCache} for more information on how to use this class.
8+
*/
9+
export default class ContextsCollection implements Iterable<ContextsElement> {
10+
/**
11+
* Used to address looking for directories. Does not help with requesting directories that have no registered files,
12+
* but I don't see that being a problem.
13+
* @private
14+
**/
15+
private directoryIndex;
16+
/**
17+
* Tracks all source files in the collection.
18+
* @private
19+
*/
20+
private readonly _files;
21+
/**
22+
* contextPath represents reverse mappings for direct resources such as Definitions, and Destinations
23+
* @private
24+
*/
25+
private readonly contextPathMap;
26+
/**
27+
* Provides a map for all source path contexts
28+
* @private
29+
*/
30+
private readonly pathMap;
31+
/**
32+
* Returns a list of the current files.
33+
*/
34+
get files(): File[];
35+
/**
36+
* Handles assigning new context's to a file.
37+
* @param path The source path of the original file being processed.
38+
* @param contextPath The context path for the resource being appended
39+
* @param context Data related to the context path such as content
40+
* @param file Source File metadata such as the filetype.
41+
*/
42+
assign(path: string, contextPath: string, context: Context, file?: File): File;
43+
/**
44+
* Helps track directories inside the FileRepository. Currently, we don't support
45+
* recursion through cached directories.
46+
* @param path
47+
* @private
48+
*
49+
* @see registerFile
50+
* @see registerOutput
51+
*/
52+
private registerDirectory;
53+
/**
54+
* Used to clear out files within a project
55+
* @param file
56+
*/
57+
deleteByFile(file: File): void;
58+
/**
59+
* Uses the file path to retrieve a file, and remove all related contexts.
60+
* @param path
61+
* @see deleteByFile
62+
*/
63+
deleteByPath(path: string): void;
64+
/**
65+
* Retrieves all contexts that match the given source path
66+
* @param path
67+
*/
68+
getContexts(path: string): ContextsMap | undefined;
69+
/**
70+
* Retrieves the relatable ContextPathData for a path as long as it's a ContextDestination, ContextDefinition, or ContextSource
71+
*
72+
* ContextDeclaration are not searchable here as their paths are often relative. Use {@see getContextFromPath} instead.
73+
* @param contextPath
74+
*/
75+
getContext<T extends Context>(contextPath: string): ContextPathData<T> | undefined;
76+
/**
77+
* Returns all the contexts of a given type from the contextMap.
78+
*
79+
* ContextDeclarations are not available with this method. Use {@see getContextsOfTypePath} instead.
80+
* @param type
81+
*/
82+
getContextsOfType<T extends Context>(type?: ContextType): ContextPathData<T>[];
83+
/**
84+
* Retrieves the first result of a given Context type.
85+
*
86+
* Not recommended for ContextDeclarations as there will be more than one. Use {@see getContextsOfTypePath} instead.
87+
* @param path
88+
* @param type
89+
*/
90+
getFirstContextsOfTypePath<T extends Context>(path: string, type: ContextType): ContextPathData<T> | undefined;
91+
/**
92+
* Provides a list of all related contexts within a given path.
93+
* @param path
94+
* @param type
95+
*/
96+
getContextsOfTypePath<T extends Context>(path: string, type?: ContextType): ContextPathData<T>[];
97+
/**
98+
* @see getFile
99+
* @param path
100+
*/
101+
hasFile(path: string): boolean;
102+
/**
103+
* Provides the results of a path by first checking if it's a source path, or a context path.
104+
* @param path
105+
*/
106+
getFile<T extends File>(path: string): T;
107+
/**
108+
* Look for a specific context inside the source contexts object.
109+
* @param path
110+
* @param contextPath
111+
*/
112+
getContextFromPath<T extends Context>(path: string, contextPath: string): ContextPathData<T> | undefined;
113+
/**
114+
* Checks if a registered directory exists.
115+
* @param path
116+
*
117+
* @see registerDirectory
118+
*/
119+
hasDirectory(path: string): boolean;
120+
[Symbol.iterator](): Iterator<ContextsElement>;
121+
}

types/File/ContextsMap.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Context, File } from "./Definitions";
2+
/**
3+
* Keeps track of all the contexts assigned to a source path.
4+
*
5+
* Context paths must all be unique per source.
6+
*/
7+
export default class ContextsMap implements Iterable<Context> {
8+
file: File;
9+
readonly contexts: Map<string, Context>;
10+
constructor(file?: File);
11+
[Symbol.iterator](): Iterator<Context>;
12+
}

0 commit comments

Comments
 (0)