-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add a better package minifier
- Loading branch information
Showing
11 changed files
with
125 additions
and
111 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
[ ] Add a <PartialLoader/> to fetch the model on demand | ||
[ ] Add class and style attributes | ||
[x] Add class and style attributes - implemeted via {...attributes} | ||
[x] store the model inside the component | ||
[x] Rename the row snippet to something else -> slot | ||
[ ] add disabled | ||
[ ] move the css style logic out of the component row({ item, style, index }) | ||
[ ] considering, items={data} itemCount={data.length} itemSize={50}; model is data, items is the view should be transformed into model={data} modelCount={data.length} itemSize={50}, {@render item(...)} | ||
[x] implement minimization for the npm package | ||
[x] implement minimization for the npm package |
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 |
---|---|---|
@@ -1,102 +1,106 @@ | ||
"use strict"; | ||
'use strict'; | ||
|
||
|
||
var newLineCharCode = "\n".charCodeAt(0); | ||
var newLineCharCode = '\n'.charCodeAt(0); | ||
// todo: type the `ts` import (maybe with a local type that defines the expected compiler API structure) | ||
/** Creates a minifier that should be stored and then used to minify one or more files. */ | ||
export function createMinifier(ts) { | ||
var scanner = ts.createScanner(ts.ScriptTarget.Latest, | ||
/* skipTrivia */ false, ts.LanguageVariant.Standard); | ||
return { | ||
minify: minify, | ||
}; | ||
function minify(fileText, options) { | ||
var _a; | ||
var keepJsDocs = (_a = options === null || options === void 0 ? void 0 : options.keepJsDocs) !== null && _a !== void 0 ? _a : false; | ||
var result = ""; | ||
var lastWrittenToken; | ||
var lastHadSeparatingNewLine = false; | ||
scanner.setText(fileText); | ||
while (scanner.scan() !== ts.SyntaxKind.EndOfFileToken) { | ||
var currentToken = scanner.getToken(); | ||
switch (currentToken) { | ||
case ts.SyntaxKind.NewLineTrivia: | ||
lastHadSeparatingNewLine = true; | ||
break; | ||
case ts.SyntaxKind.WhitespaceTrivia: | ||
break; | ||
case ts.SyntaxKind.SingleLineCommentTrivia: | ||
if (isTripleSlashDirective()) { | ||
writeSingleLineComment(); | ||
lastHadSeparatingNewLine = false; | ||
} | ||
break; | ||
case ts.SyntaxKind.MultiLineCommentTrivia: | ||
if (keepJsDocs && isJsDoc()) { | ||
writeJsDoc(); | ||
lastHadSeparatingNewLine = false; | ||
} | ||
break; | ||
default: | ||
// use a newline where ASI is probable | ||
if (currentToken === ts.SyntaxKind.Identifier && | ||
lastHadSeparatingNewLine && | ||
lastWrittenToken !== ts.SyntaxKind.SemicolonToken && | ||
lastWrittenToken !== ts.SyntaxKind.CloseBraceToken && | ||
lastWrittenToken !== ts.SyntaxKind.OpenBraceToken && | ||
lastWrittenToken !== ts.SyntaxKind.OpenParenToken && | ||
lastWrittenToken !== ts.SyntaxKind.CommaToken && | ||
lastWrittenToken !== ts.SyntaxKind.ColonToken) { | ||
result += "\n"; | ||
} | ||
writeText(scanner.getTokenText()); | ||
lastHadSeparatingNewLine = false; | ||
} | ||
} | ||
return result; | ||
function isTripleSlashDirective() { | ||
var tokenText = scanner.getTokenText(); | ||
// todo: better check | ||
return tokenText.startsWith("///") && tokenText.includes("<"); | ||
} | ||
function writeSingleLineComment() { | ||
writeText(scanner.getTokenText()); | ||
// write out the next newline as-is (ex. write \n or \r\n) | ||
var nextToken = scanner.scan(); | ||
if (nextToken === ts.SyntaxKind.NewLineTrivia) { | ||
writeText(scanner.getTokenText()); | ||
} | ||
else if (nextToken !== ts.SyntaxKind.EndOfFileToken) { | ||
throw new Error("Unexpected scenario where the token after a comment was a ".concat(nextToken, ".")); | ||
} | ||
} | ||
function isJsDoc() { | ||
var tokenText = scanner.getTokenText(); | ||
return tokenText.startsWith("/**"); | ||
} | ||
function writeJsDoc() { | ||
writeText(scanner.getTokenText().replace(/^\s+\*/mg, " *")); | ||
} | ||
function writeText(text) { | ||
var token = scanner.getToken(); | ||
// ensure two tokens that would merge into a single token are separated by a space | ||
if (lastWrittenToken != null && | ||
isAlphaNumericToken(token) && | ||
isAlphaNumericToken(lastWrittenToken) && | ||
!wasLastWrittenNewLine()) { | ||
result += " "; | ||
} | ||
result += text; | ||
lastWrittenToken = token; | ||
} | ||
function wasLastWrittenNewLine() { | ||
return result.charCodeAt(result.length - 1) === newLineCharCode; | ||
} | ||
} | ||
function isAlphaNumericToken(token) { | ||
if (token >= ts.SyntaxKind.FirstKeyword && token <= ts.SyntaxKind.LastKeyword) { | ||
return true; | ||
} | ||
return token === ts.SyntaxKind.Identifier; | ||
} | ||
var scanner = ts.createScanner(ts.ScriptTarget.Latest, /* skipTrivia */ false, ts.LanguageVariant.Standard); | ||
return { | ||
minify: minify | ||
}; | ||
function minify(fileText, options) { | ||
var _a; | ||
var keepJsDocs = | ||
(_a = options === null || options === void 0 ? void 0 : options.keepJsDocs) !== null && _a !== void 0 | ||
? _a | ||
: false; | ||
var result = ''; | ||
var lastWrittenToken; | ||
var lastHadSeparatingNewLine = false; | ||
scanner.setText(fileText); | ||
while (scanner.scan() !== ts.SyntaxKind.EndOfFileToken) { | ||
var currentToken = scanner.getToken(); | ||
switch (currentToken) { | ||
case ts.SyntaxKind.NewLineTrivia: | ||
lastHadSeparatingNewLine = true; | ||
break; | ||
case ts.SyntaxKind.WhitespaceTrivia: | ||
break; | ||
case ts.SyntaxKind.SingleLineCommentTrivia: | ||
if (isTripleSlashDirective()) { | ||
writeSingleLineComment(); | ||
lastHadSeparatingNewLine = false; | ||
} | ||
break; | ||
case ts.SyntaxKind.MultiLineCommentTrivia: | ||
if (keepJsDocs && isJsDoc()) { | ||
writeJsDoc(); | ||
lastHadSeparatingNewLine = false; | ||
} | ||
break; | ||
default: | ||
// use a newline where ASI is probable | ||
if ( | ||
currentToken === ts.SyntaxKind.Identifier && | ||
lastHadSeparatingNewLine && | ||
lastWrittenToken !== ts.SyntaxKind.SemicolonToken && | ||
lastWrittenToken !== ts.SyntaxKind.CloseBraceToken && | ||
lastWrittenToken !== ts.SyntaxKind.OpenBraceToken && | ||
lastWrittenToken !== ts.SyntaxKind.OpenParenToken && | ||
lastWrittenToken !== ts.SyntaxKind.CommaToken && | ||
lastWrittenToken !== ts.SyntaxKind.ColonToken | ||
) { | ||
result += '\n'; | ||
} | ||
writeText(scanner.getTokenText()); | ||
lastHadSeparatingNewLine = false; | ||
} | ||
} | ||
return result; | ||
function isTripleSlashDirective() { | ||
var tokenText = scanner.getTokenText(); | ||
// todo: better check | ||
return tokenText.startsWith('///') && tokenText.includes('<'); | ||
} | ||
function writeSingleLineComment() { | ||
writeText(scanner.getTokenText()); | ||
// write out the next newline as-is (ex. write \n or \r\n) | ||
var nextToken = scanner.scan(); | ||
if (nextToken === ts.SyntaxKind.NewLineTrivia) { | ||
writeText(scanner.getTokenText()); | ||
} else if (nextToken !== ts.SyntaxKind.EndOfFileToken) { | ||
throw new Error('Unexpected scenario where the token after a comment was a '.concat(nextToken, '.')); | ||
} | ||
} | ||
function isJsDoc() { | ||
var tokenText = scanner.getTokenText(); | ||
return tokenText.startsWith('/**'); | ||
} | ||
function writeJsDoc() { | ||
writeText(scanner.getTokenText().replace(/^\s+\*/gm, ' *')); | ||
} | ||
function writeText(text) { | ||
var token = scanner.getToken(); | ||
// ensure two tokens that would merge into a single token are separated by a space | ||
if ( | ||
lastWrittenToken && | ||
isAlphaNumericToken(token) && | ||
isAlphaNumericToken(lastWrittenToken) && | ||
!wasLastWrittenNewLine() | ||
) { | ||
result += ' '; | ||
} | ||
result += text; | ||
lastWrittenToken = token; | ||
} | ||
function wasLastWrittenNewLine() { | ||
return result.charCodeAt(result.length - 1) === newLineCharCode; | ||
} | ||
} | ||
function isAlphaNumericToken(token) { | ||
if (token >= ts.SyntaxKind.FirstKeyword && token <= ts.SyntaxKind.LastKeyword) { | ||
return true; | ||
} | ||
return token === ts.SyntaxKind.Identifier; | ||
} | ||
} |
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
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
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