File size: 13,532 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 |
import {
existsSync,
lstatSync,
mkdirSync,
readFileSync,
readdirSync,
writeFileSync
} from 'node:fs'
import path from 'node:path'
import ts from 'typescript'
// This is a modified version of the functionality provided by https://github.com/CarabusX/gulp-preserve-typescript-whitespace
// It has been modified and simplified to work with ts-node instead of gulp.
const preferredTags = {
NEW_LINE_TAG: ['N', 'n'],
SPACES_TAG: ['S', 's'],
SPACES_BEFORE_COLON_TAG: ['C', 'c'],
SAME_LINE_ELSE_TAG: ['E', 'e']
}
type PreferredTags = typeof preferredTags
interface PreserveTypescriptWhitespaceOptions {
preserveNewLines: boolean
preserveMultipleSpaces: boolean
preserveSpacesBeforeColons: boolean
collapseSpacesBeforeRemovedColons: boolean
preserveSameLineElse: boolean
showDebugOutput: boolean
}
interface Block {
code: string
stringOrComment: string
}
interface Metadata extends Record<keyof PreferredTags, string> {
options: Partial<PreserveTypescriptWhitespaceOptions>
}
class ParsedFileMetadata {
public static FILE_METADATA_TAG: string
public startIndex: number
public endIndex: number
constructor(public metadata: Metadata) {}
public serialize() {
return `; /*${ParsedFileMetadata.FILE_METADATA_TAG}${JSON.stringify(
this.metadata
)}${ParsedFileMetadata.FILE_METADATA_TAG}*/\n`
}
static deserialize(fileContents: string) {
const startTagRegex = `;? ?\\/\\*${ParsedFileMetadata.FILE_METADATA_TAG}`
const endTagRegex = `${ParsedFileMetadata.FILE_METADATA_TAG}\\*\\/\\r?\\n?`
const metadataMatch = fileContents.match(
new RegExp(`${startTagRegex}([\\s\\S]*?)${endTagRegex}`)
)
if (metadataMatch === null) {
console.error(`ERROR: Metadata tag not found in '${fileContents}' file.`)
return
}
const startTagIndex = metadataMatch.index
const metadataWithTags = metadataMatch[0]
const serializedMetadata = metadataMatch[1]
const metadata = JSON.parse(serializedMetadata)
const metadataObj = new ParsedFileMetadata(metadata)
metadataObj.startIndex = startTagIndex
metadataObj.endIndex = startTagIndex + metadataWithTags.length
return metadataObj
}
public removeFrom(fileContents: string) {
return (
fileContents.slice(0, this.startIndex) + fileContents.slice(this.endIndex)
)
}
}
const restoreWhitespace = (contents: string) => {
const metadataObj = ParsedFileMetadata.deserialize(contents)
const { metadata } = metadataObj
contents = metadataObj.removeFrom(contents)
const {
options,
NEW_LINE_TAG,
SPACES_TAG,
SPACES_BEFORE_COLON_TAG,
SAME_LINE_ELSE_TAG
} = metadata
if (options.preserveNewLines) {
contents = contents.replace(
new RegExp(`\\/\\*${NEW_LINE_TAG}\\*\\/`, 'g'),
''
)
}
if (options.preserveSpacesBeforeColons) {
contents = contents.replace(
new RegExp(` ?\\/\\*${SPACES_BEFORE_COLON_TAG}([0-9]+)\\*\\/:`, 'g'),
(match, group1: string) => {
const spacesCount = Number(group1)
return `${' '.repeat(spacesCount)}:`
}
)
if (options.collapseSpacesBeforeRemovedColons) {
contents = contents.replace(
new RegExp(
' ?\\/\\*' +
SPACES_BEFORE_COLON_TAG +
'([0-9]+)\\*\\/(?=[,;\\)\\} \\t\\r\\n])',
'g'
),
''
)
contents = contents.replace(
new RegExp(` ?\\/\\*${SPACES_BEFORE_COLON_TAG}([0-9]+)\\*\\/`, 'g'),
' '
)
} else {
contents = contents.replace(
new RegExp(` ?\\/\\*${SPACES_BEFORE_COLON_TAG}([0-9]+)\\*\\/`, 'g'),
(match, group1: string) => {
const spacesCount = Number(group1)
return ' '.repeat(spacesCount)
}
)
}
}
if (options.preserveMultipleSpaces) {
contents = contents.replace(
new RegExp(`\\/\\*${SPACES_TAG}([0-9]+)\\*\\/`, 'g'),
(match, group1: string) => {
const spacesCount = Number(group1)
return ' '.repeat(spacesCount - 2)
}
)
}
if (options.preserveSameLineElse) {
return contents.replace(
new RegExp(
`\\} \\/\\*${SAME_LINE_ELSE_TAG}([0-9]+)\\*\\/\\r?\\n[ \\t]*else`,
'g'
),
(match, group1: string) => {
const spacesCount = Number(group1)
return `}${' '.repeat(spacesCount)}else`
}
)
}
return contents
}
const TAG_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
const createTagForOrdinal = (ordinal: number) => {
let tag = ''
do {
const tagChar = TAG_CHARS[ordinal % TAG_CHARS.length]
tag = tagChar + tag
ordinal = Math.floor(ordinal / TAG_CHARS.length)
} while (ordinal > 0)
return tag
}
const isSimpleTagPresent = (fileContents: string, tag: string) => {
const index = fileContents.search(new RegExp(`\\/\\*${tag}\\*\\/`))
return index !== -1
}
const isTagWithCountPresent = (fileContents: string, tag: string) => {
const index = fileContents.search(new RegExp(`\\/\\*${tag}([0-9]+)\\*\\/`))
return index !== -1
}
const PLUGIN_NAME = 'gulp-preserve-typescript-whitespace'
class UnusedTagsFinder {
public checkedSimpleTags: Set<string>
public checkedTagsWithCount: Set<string>
constructor(
public fileContents: string,
public options: PreserveTypescriptWhitespaceOptions
) {
this.checkedSimpleTags = new Set<string>()
this.checkedTagsWithCount = new Set<string>()
}
public findUnusedTag(preferredTags: string[], isTagWithCount: boolean) {
const checkedTagsSet = isTagWithCount
? this.checkedTagsWithCount
: this.checkedSimpleTags
const isTagPresentFunc = isTagWithCount
? isTagWithCountPresent
: isSimpleTagPresent
for (const tag of preferredTags) {
if (!checkedTagsSet.has(tag)) {
checkedTagsSet.add(tag)
if (!isTagPresentFunc(this.fileContents, tag)) {
return tag
} else if (this.options.showDebugOutput) {
console.debug(`[${PLUGIN_NAME}] Tag already present:`, tag)
}
}
}
for (let i = 0; ; i++) {
const tag = createTagForOrdinal(i)
if (!checkedTagsSet.has(tag)) {
checkedTagsSet.add(tag)
if (!isTagPresentFunc(this.fileContents, tag)) {
return tag
} else if (this.options.showDebugOutput) {
console.debug(`[${PLUGIN_NAME}] Tag already present:`, tag)
}
}
}
}
}
const options = {
preserveNewLines: true,
preserveMultipleSpaces: true,
preserveSpacesBeforeColons: true,
collapseSpacesBeforeRemovedColons: true,
preserveSameLineElse: true,
showDebugOutput: false
}
const rebuildCodeFromBlocks = (blocks: Block[]) => {
return blocks.map(block => block.code + block.stringOrComment).join('')
}
const stringOrCommentEnd = {
"'": /(?<!(?:^|[^\\])(?:\\\\)*\\)'/,
'"': /(?<!(?:^|[^\\])(?:\\\\)*\\)"/,
'`': /(?<!(?:^|[^\\])(?:\\\\)*\\)`/,
'//': /(?=\r?\n)/,
'/*': /\*\//
}
const parseStringAndComments = (
codeToParse: string,
skipEmptyCodeBlocks = true
) => {
const blocks: Block[] = []
while (codeToParse.length > 0) {
let codeBlock: string
let commentBlock: string
const commentStartMatch = codeToParse.match(/['"`]|\/\/|\/\*/)
if (commentStartMatch === null) {
codeBlock = codeToParse
commentBlock = ''
codeToParse = ''
} else {
const commentStartIndex = commentStartMatch.index
codeBlock = codeToParse.slice(0, commentStartIndex)
const commentStartChars = commentStartMatch[0]
const commentContentsIndex = commentStartIndex + commentStartChars.length
const commentEndRegex = stringOrCommentEnd[commentStartChars]
const commentEndMatch = codeToParse
.slice(commentContentsIndex)
.match(commentEndRegex)
if (commentEndMatch === null) {
commentBlock = codeToParse.slice(commentStartIndex)
codeToParse = ''
} else {
const commentEndIndexRelative = commentEndMatch.index
const commentEndChars = commentEndMatch[0]
const nextCodeStartIndex =
commentContentsIndex +
commentEndIndexRelative +
commentEndChars.length
commentBlock = codeToParse.slice(commentStartIndex, nextCodeStartIndex)
codeToParse = codeToParse.slice(nextCodeStartIndex)
}
}
if (skipEmptyCodeBlocks && codeBlock.length === 0 && blocks.length >= 1) {
blocks[blocks.length - 1].stringOrComment += commentBlock
} else {
blocks.push({ code: codeBlock, stringOrComment: commentBlock })
}
}
return blocks
}
const saveWhitespace = (file: string) => {
const unusedTagsFinder = new UnusedTagsFinder(file, options)
const NEW_LINE_TAG = unusedTagsFinder.findUnusedTag(
preferredTags.NEW_LINE_TAG,
false
)
const SPACES_TAG = unusedTagsFinder.findUnusedTag(
preferredTags.SPACES_TAG,
true
)
const SPACES_BEFORE_COLON_TAG = unusedTagsFinder.findUnusedTag(
preferredTags.SPACES_BEFORE_COLON_TAG,
true
)
const SAME_LINE_ELSE_TAG = unusedTagsFinder.findUnusedTag(
preferredTags.SAME_LINE_ELSE_TAG,
true
)
const NEW_LINE_REPLACEMENT = `/*${NEW_LINE_TAG}*/$1`
const blocks = parseStringAndComments(file)
let isFileStart = true
for (const block of blocks) {
block.code = block.code.replace(/\}( *)else/g, (match, group1: string) => {
return `} /*${SAME_LINE_ELSE_TAG}${group1.length}*/else`
})
const regex = isFileStart ? /(?<=[^ \n])( +):/g : /(?<=^|[^ \n])( +):/g
block.code = block.code.replace(regex, (match, group1: string) => {
return ` /*${SPACES_BEFORE_COLON_TAG}${group1.length}*/:`
})
block.code = block.code.replace(
isFileStart ? /(?<=[^ \n])( +)(?![ :])/g : /(?<=^|[^ \n])( +)(?![ :])/g,
(match, group1: string) => ` /*${SPACES_TAG}${group1.length}*/ `
)
block.code = block.code.replace(
isFileStart ? /(?<=(?:^|\n)[ \t]*)(\r?\n)/g : /(?<=\n[ \t]*)(\r?\n)/g,
NEW_LINE_REPLACEMENT
)
isFileStart = false
}
file = rebuildCodeFromBlocks(blocks)
const metadataObj = new ParsedFileMetadata({
options,
NEW_LINE_TAG,
SPACES_TAG,
SPACES_BEFORE_COLON_TAG,
SAME_LINE_ELSE_TAG
})
return metadataObj.serialize() + file
}
export const EXAMPLES_DIRECTORY = '../docs/examples'
export const tsExtensionRegex = /\.tsx?$/
export const hasTSXExtension = (fileName: string) => /\.tsx$/.test(fileName)
export const getTSConfig = (filePath: string) => {
const tsconfigPath = lstatSync(filePath).isDirectory()
? path.join(filePath, 'tsconfig.json')
: filePath
const { config } = ts.readConfigFile(tsconfigPath, ts.sys.readFile) as {
config: Pick<ts.TranspileOptions, 'compilerOptions'>
}
return config
}
/**
* Compiles a single TypeScript file to JavaScript, preserving whitespaces.
* This function takes a path to a TypeScript file (.ts or .tsx),
* compiles it into JavaScript (.js or .jsx), and maintains whitespace
* formatting during the process. It applies TypeScript compilation with custom
* configurations and handles the preservation and restoration of whitespaces.
*
* @param filePath - The file path of the TypeScript file to compile.
* @param tsconfigPath - The file path of the tsconfig file.
*/
const compileTSFile = (filePath: string, tsconfigPath?: string) => {
const fileContents = readFileSync(filePath, 'utf8')
const tsFileName = path.basename(filePath)
const isTSX = hasTSXExtension(tsFileName)
const outputFileExtension = isTSX ? '.jsx' : '.js'
const jsFileName = tsFileName.replace(tsExtensionRegex, outputFileExtension)
const jsx = isTSX ? ts.JsxEmit.Preserve : ts.JsxEmit.None
const savedWhitespaceContents = saveWhitespace(fileContents)
const inputFileDirectory = path.dirname(filePath)
const config = getTSConfig(tsconfigPath ?? inputFileDirectory)
const result = ts.transpileModule(savedWhitespaceContents, {
compilerOptions: {
...config.compilerOptions,
jsx
}
})
const { outDir } = config.compilerOptions
const tsconfigDirectory = path.dirname(tsconfigPath)
const outputFolder = path.join(
tsconfigDirectory,
outDir,
...inputFileDirectory
.split(path.sep)
.slice(tsconfigDirectory.split(path.sep).length)
)
const restoredWhitespaceContents = restoreWhitespace(result.outputText)
const outputFilePath = path.join(outputFolder, jsFileName)
if (!existsSync(outputFolder)) {
mkdirSync(outputFolder)
}
writeFileSync(outputFilePath, restoredWhitespaceContents, 'utf8')
}
/**
* Recursively compiles TypeScript files to JavaScript, preserving whitespaces.
* This function processes all TypeScript files (`.ts` and `.tsx`) in the given
* directory and its subdirectories. It maintains the original whitespace formatting
* during the compilation process to ensure the output closely mirrors the source format.
*
* @param directory - The directory path where TypeScript files are located.
* @param tsconfigPath - The file path of the tsconfig file.
*/
const compileTSWithWhitespace = (directory: string, tsconfigPath: string) => {
readdirSync(directory, { withFileTypes: true }).forEach(entry => {
const filePath = path.join(directory, entry.name)
if (entry.isDirectory()) {
compileTSWithWhitespace(filePath, tsconfigPath)
} else if (tsExtensionRegex.test(entry.name)) {
compileTSFile(filePath, tsconfigPath)
}
})
}
compileTSWithWhitespace(
EXAMPLES_DIRECTORY,
path.join(EXAMPLES_DIRECTORY, 'tsconfig.json')
)
|