-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split into two packages, improve Babel plugin
- Loading branch information
1 parent
7cdfe1c
commit 9cbb437
Showing
38 changed files
with
8,525 additions
and
179 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 |
---|---|---|
|
@@ -23,22 +23,14 @@ jobs: | |
- name: Lint files | ||
run: yarn lint | ||
|
||
- name: Check Flow | ||
run: yarn flow | ||
|
||
- name: Run tests with coverage | ||
run: yarn run coverage | ||
|
||
- name: Run build | ||
run: yarn run build | ||
|
||
- name: Coveralls | ||
uses: coverallsapp/github-action@master | ||
with: | ||
path-to-lcov: ./packages/superoverload/coverage/lcov.info | ||
github-token: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
- name: Check in Bundlephobia | ||
uses: carlesnunez/[email protected] | ||
|
||
env: | ||
CI: true |
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,60 +1,20 @@ | ||
{ | ||
"name": "superoverload", | ||
"version": "1.4.0", | ||
"description": "Function overload for JavaScript", | ||
"main": "build/index.js", | ||
"scripts": { | ||
"test": "yarn run build && jest", | ||
"benchmark": "yarn run build && node ./benchmark", | ||
"build": "rollup -c && prettier build/index.js --write", | ||
"format": "prettier --write **/*.{md,json,js} && eslint --fix .", | ||
"lint": "eslint ./**/*.js", | ||
"flow": "flow .", | ||
"pre-commit": "yarn run lint && yarn run flow", | ||
"coverage": "jest --coverage", | ||
"release": "np" | ||
}, | ||
"keywords": [ | ||
"function", | ||
"overload", | ||
"javascript" | ||
], | ||
"author": "Aleksey Verkholantsev <[email protected]> (https://github.com/verkholantsev)", | ||
"contributors": [ | ||
"escaton (https://github.com/escaton)" | ||
], | ||
"name": "superoverload-repo", | ||
"version": "1.0.0", | ||
"description": "Superoverload Repo", | ||
"main": "index.js", | ||
"repository": "[email protected]:verkholantsev/superoverload.git", | ||
"author": "Aleksei Verkholantcev <[email protected]>", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/verkholantsev/superoverload.git" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.9.0", | ||
"@babel/plugin-external-helpers": "^7.8.3", | ||
"@babel/preset-env": "^7.9.0", | ||
"@babel/preset-flow": "^7.9.0", | ||
"@babel/register": "^7.9.0", | ||
"babel-eslint": "^10.1.0", | ||
"benchmark": "^2.1.4", | ||
"captain-git-hook": "^2.0.0", | ||
"coveralls": "^3.0.11", | ||
"eslint": "^6.8.0", | ||
"eslint-config-prettier": "^6.10.0", | ||
"eslint-plugin-flowtype": "^4.6.0", | ||
"eslint-plugin-prettier": "^3.1.2", | ||
"flow-bin": "^0.121.0", | ||
"jest": "^25.1.0", | ||
"lodash": "^4.17.15", | ||
"np": "^6.2.0", | ||
"prettier": "^1.19.1", | ||
"rollup": "^2.1.0", | ||
"rollup-plugin-babel": "^4.4.0", | ||
"rollup-plugin-commonjs": "^10.1.0", | ||
"rollup-plugin-node-resolve": "^5.2.0", | ||
"rollup-plugin-terser": "^5.3.0" | ||
"private": true, | ||
"scripts": { | ||
"coverage": "yarn workspaces run coverage", | ||
"format": "prettier --write \"packages/**/*.{js,json,md}\"", | ||
"lint": "yarn workspaces run lint", | ||
"pre-commit": "yarn run lint", | ||
"test": "yarn run lint && yarn workspaces run test" | ||
}, | ||
"dependencies": {}, | ||
"engines": { | ||
"node": ">=10" | ||
} | ||
"workspaces": [ | ||
"packages/*" | ||
] | ||
} |
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 @@ | ||
fixtures/ |
Binary file not shown.
Binary file added
BIN
+12 KB
...l-plugin-compress-template-literals/fixtures/compress-template-literals/.options.json.swp
Binary file not shown.
18 changes: 18 additions & 0 deletions
18
...mplate-literals/fixtures/compress-template-literals/compresses-template-literals/input.js
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,18 @@ | ||
compress(` | ||
var a = 1; | ||
var b = ${x}; | ||
console.log(a + b); | ||
`); | ||
|
||
compress(` | ||
if ( | ||
true | ||
) | ||
{ | ||
console.log('true'); | ||
} | ||
else | ||
{ | ||
console.log('nope'); | ||
} | ||
`) |
2 changes: 2 additions & 0 deletions
2
...plate-literals/fixtures/compress-template-literals/compresses-template-literals/output.js
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,2 @@ | ||
`var a=1;var b=${x};console.log(a+b);`; | ||
`if(true){console.log('true');}else{console.log('nope');}`; |
3 changes: 3 additions & 0 deletions
3
.../babel-plugin-compress-template-literals/fixtures/compress-template-literals/options.json
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,3 @@ | ||
{ | ||
"plugins": ["compress-template-literals"] | ||
} |
1 change: 1 addition & 0 deletions
1
...-template-literals/fixtures/compress-template-literals/removes-compress-function/input.js
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 @@ | ||
function compress() {} |
Empty file.
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,78 @@ | ||
function isWhitespace(char) { | ||
return /\s/.test(char); | ||
} | ||
|
||
function isNonWhitespace(char) { | ||
return /[^\s]/.test(char); | ||
} | ||
|
||
function isStringBoundary(char) { | ||
return char === "'" || char === '"' || char === '`'; | ||
} | ||
|
||
function compressString(input) { | ||
const output = []; | ||
let i = 0; | ||
let isBetweenWords = false; | ||
let isInString = false; | ||
while (i < input.length) { | ||
const char = input[i]; | ||
const previousChar = output[output.length - 1]; | ||
|
||
if (isStringBoundary(char) && previousChar !== '\\') { | ||
isInString = !isInString; | ||
output.push(char); | ||
} else if (isInString) { | ||
output.push(char); | ||
} else if (isNonWhitespace(char)) { | ||
if ( | ||
isBetweenWords && | ||
output.length > 0 && | ||
/\w/.test(previousChar) && | ||
/\w/.test(char) | ||
) { | ||
output.push(' '); | ||
} | ||
output.push(char); | ||
isBetweenWords = false; | ||
} else if (isWhitespace(char)) { | ||
isBetweenWords = true; | ||
} | ||
|
||
i++; | ||
} | ||
return output.join(''); | ||
} | ||
|
||
module.exports = function (babel) { | ||
const {types: t} = babel; | ||
|
||
return { | ||
name: 'ast-transform', // not required | ||
visitor: { | ||
FunctionDeclaration(path) { | ||
if (path.node.id.name === 'compress') { | ||
path.remove(); | ||
} | ||
}, | ||
CallExpression(path) { | ||
if (path.node.callee.name === 'compress') { | ||
path.node.arguments[0].quasis.forEach(templateElement => { | ||
templateElement.value.raw = compressString( | ||
templateElement.value.raw, | ||
); | ||
templateElement.value.cooked = compressString( | ||
templateElement.value.cooked, | ||
); | ||
}); | ||
path.replaceWith( | ||
t.templateLiteral( | ||
path.node.arguments[0].quasis, | ||
path.node.arguments[0].expressions, | ||
), | ||
); | ||
} | ||
}, | ||
}, | ||
}; | ||
}; |
22 changes: 22 additions & 0 deletions
22
packages/babel-plugin-compress-template-literals/package.json
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,22 @@ | ||
{ | ||
"name": "babel-plugin-compress-template-literals", | ||
"version": "1.0.0", | ||
"description": "Babel plugin to compress template literals which contain JS code", | ||
"main": "index.js", | ||
"author": "Aleksei Verkholantcev <[email protected]>", | ||
"license": "MIT", | ||
"private": true, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"@babel/helper-plugin-test-runner": "^7.8.3", | ||
"eslint": "^6.8.0", | ||
"eslint-plugin-prettier": "^3.1.2", | ||
"jest": "^25.1.0", | ||
"prettier": "^2.0.1" | ||
}, | ||
"scripts": { | ||
"test": "jest test.js", | ||
"lint": "eslint .", | ||
"coverage": "echo 'Not implemented'" | ||
} | ||
} |
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,5 @@ | ||
'use strict'; | ||
|
||
const runner = require('@babel/helper-plugin-test-runner').default; | ||
|
||
runner(__dirname); |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
{ | ||
"name": "superoverload", | ||
"version": "1.4.0", | ||
"description": "Function overload for JavaScript", | ||
"main": "build/index.js", | ||
"scripts": { | ||
"test": "yarn run build && jest", | ||
"benchmark": "yarn run build && node ./benchmark", | ||
"build": "rollup -c && prettier build/index.js --write", | ||
"format": "prettier --write **/*.{md,json,js} && eslint --fix .", | ||
"lint": "eslint ./**/*.js && flow .", | ||
"flow": "flow .", | ||
"pre-commit": "yarn run lint && yarn run flow", | ||
"coverage": "jest --coverage", | ||
"release": "np" | ||
}, | ||
"keywords": [ | ||
"function", | ||
"overload", | ||
"javascript" | ||
], | ||
"author": "Aleksey Verkholantsev <[email protected]> (https://github.com/verkholantsev)", | ||
"contributors": [ | ||
"escaton (https://github.com/escaton)" | ||
], | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/verkholantsev/superoverload.git" | ||
}, | ||
"devDependencies": { | ||
"babel-plugin-compress-template-literals": "^1.0.0", | ||
"@babel/core": "^7.9.0", | ||
"@babel/plugin-external-helpers": "^7.8.3", | ||
"@babel/preset-env": "^7.9.0", | ||
"@babel/preset-flow": "^7.9.0", | ||
"@babel/register": "^7.9.0", | ||
"babel-eslint": "^10.1.0", | ||
"benchmark": "^2.1.4", | ||
"captain-git-hook": "^2.0.0", | ||
"coveralls": "^3.0.11", | ||
"eslint": "^6.8.0", | ||
"eslint-config-prettier": "^6.10.0", | ||
"eslint-plugin-flowtype": "^4.6.0", | ||
"eslint-plugin-prettier": "^3.1.2", | ||
"flow-bin": "^0.121.0", | ||
"jest": "^25.1.0", | ||
"lodash": "^4.17.15", | ||
"np": "^6.2.0", | ||
"prettier": "^1.19.1", | ||
"rollup": "^2.1.0", | ||
"rollup-plugin-babel": "^4.4.0", | ||
"rollup-plugin-commonjs": "^10.1.0", | ||
"rollup-plugin-node-resolve": "^5.2.0", | ||
"rollup-plugin-terser": "^5.3.0" | ||
}, | ||
"dependencies": {}, | ||
"engines": { | ||
"node": ">=10" | ||
} | ||
} |
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.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.