Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TypeScript server logs for exceptions (#38)
- Loading branch information
Showing
9 changed files
with
160 additions
and
113 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { extractICSS, IICSSExports } from 'icss-utils'; | ||
import * as postcss from 'postcss'; | ||
import * as postcssIcssSelectors from 'postcss-icss-selectors'; | ||
import * as ts_module from 'typescript/lib/tsserverlibrary'; | ||
import * as less from 'less'; | ||
import * as sass from 'sass'; | ||
import * as reserved from 'reserved-words'; | ||
import { transformClasses } from './classTransforms'; | ||
import { Options } from '../options'; | ||
import { Logger } from './Logger'; | ||
|
||
const NOT_CAMELCASE_REGEXP = /[\-_]/; | ||
const processor = postcss(postcssIcssSelectors({ mode: 'local' })); | ||
|
||
const classNameToProperty = (className: string) => `'${className}': string;`; | ||
const classNameToNamedExport = (className: string) => | ||
`export const ${className}: string;`; | ||
|
||
const flattenClassNames = ( | ||
previousValue: string[] = [], | ||
currentValue: string[], | ||
) => previousValue.concat(currentValue); | ||
|
||
export const enum FileTypes { | ||
css = 'css', | ||
less = 'less', | ||
scss = 'scss', | ||
} | ||
|
||
export const getFileType = (fileName: string) => { | ||
if (fileName.endsWith('.css')) return FileTypes.css; | ||
if (fileName.endsWith('.less')) return FileTypes.less; | ||
return FileTypes.scss; | ||
}; | ||
|
||
const getFilePath = (fileName: string) => | ||
fileName.substring(0, fileName.lastIndexOf('/')); | ||
|
||
export class DtsSnapshotCreator { | ||
constructor(private readonly logger: Logger) {} | ||
|
||
getClasses(css: string, fileName: string) { | ||
try { | ||
const fileType = getFileType(fileName); | ||
let transformedCss = ''; | ||
|
||
if (fileType === FileTypes.less) { | ||
less.render(css, { asyncImport: true } as any, (err, output) => { | ||
transformedCss = output.css.toString(); | ||
}); | ||
} else if (fileType === FileTypes.scss) { | ||
const filePath = getFilePath(fileName); | ||
transformedCss = sass | ||
.renderSync({ | ||
data: css, | ||
includePaths: [filePath], | ||
}) | ||
.css.toString(); | ||
} else { | ||
transformedCss = css; | ||
} | ||
|
||
const processedCss = processor.process(transformedCss); | ||
|
||
return extractICSS(processedCss.root).icssExports; | ||
} catch (e) { | ||
this.logger.error(e); | ||
return {}; | ||
} | ||
} | ||
|
||
createExports(classes: IICSSExports, options: Options) { | ||
const isCamelCase = (className: string) => | ||
!NOT_CAMELCASE_REGEXP.test(className); | ||
const isReservedWord = (className: string) => !reserved.check(className); | ||
|
||
const processedClasses = Object.keys(classes) | ||
.map(transformClasses(options.camelCase)) | ||
.reduce(flattenClassNames, []); | ||
const camelCasedKeys = processedClasses | ||
.filter(isCamelCase) | ||
.filter(isReservedWord) | ||
.map(classNameToNamedExport); | ||
|
||
const defaultExport = `\ | ||
declare const classes: { | ||
${processedClasses.map(classNameToProperty).join('\n ')} | ||
}; | ||
export default classes; | ||
`; | ||
|
||
if (camelCasedKeys.length) { | ||
return defaultExport + camelCasedKeys.join('\n') + '\n'; | ||
} | ||
return defaultExport; | ||
} | ||
|
||
getDtsSnapshot( | ||
ts: typeof ts_module, | ||
fileName: string, | ||
scriptSnapshot: ts.IScriptSnapshot, | ||
options: Options, | ||
) { | ||
const css = scriptSnapshot.getText(0, scriptSnapshot.getLength()); | ||
const classes = this.getClasses(css, fileName); | ||
const dts = this.createExports(classes, options); | ||
return ts.ScriptSnapshot.fromString(dts); | ||
} | ||
} |
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,19 @@ | ||
import { pluginName } from './config'; | ||
|
||
export interface Logger { | ||
log(msg: string): void; | ||
error(e: Error): void; | ||
} | ||
|
||
export class LanguageServiceLogger implements Logger { | ||
constructor(private readonly info: ts.server.PluginCreateInfo) {} | ||
|
||
public log(msg: string) { | ||
this.info.project.projectService.logger.info(`[${pluginName}] ${msg}`); | ||
} | ||
|
||
public error(e: Error) { | ||
this.log(`Failed ${e.toString()}`); | ||
this.log(`Stack trace: ${e.stack}`); | ||
} | ||
} |
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
File renamed without changes.
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 @@ | ||
export const pluginName = 'typescript-plugin-css-modules'; |
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 was deleted.
Oops, something went wrong.
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
Is this TODO still relevant?