diff --git a/README.md b/README.md index f0ee0ba..4431446 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,18 @@ First, setup your project using `klap init`: npx klap init ``` +**Prefer Typescript Setup ?** `init` using `ts` argument: + +```bash +npx klap init ts +``` + +**Want to use JSX with Typescript?** `init` using `tsx` argument: + +```bash +npx klap init tsx +``` + This will create a minimal `package.json` with `source`, `main`, `module` and `browser` entries and the `build`, `watch` and `start` scripts. ```jsonc diff --git a/cli.js b/cli.js index fe7c401..604ed21 100755 --- a/cli.js +++ b/cli.js @@ -1,24 +1,24 @@ #!/usr/bin/env node -const path = require('path') -const { init, klap, read, log, error, info } = require('./dist') -const { name, version } = require('./package.json') -const command = process.argv[2] +const path = require('path'); +const { init, klap, read, log, error, info } = require('./dist'); +const { name, version } = require('./package.json'); +const command = process.argv[2]; -;(async () => { +(async () => { switch (command) { case 'init': - log(`${name}@${version} - Initializing your package...`) - await init(command) - break + log(`${name}@${version} - Initializing your package...`); + await init(command); + break; case 'build': case 'watch': case 'start': - log(`${name}@${version} - Working on ${command}...`) + log(`${name}@${version} - Working on ${command}...`); const pkg = JSON.parse( await read(path.join(process.cwd(), 'package.json')) - ) - await klap(command, pkg) - break + ); + await klap(command, pkg); + break; case 'help': info(` ${name}@${version} - Usage @@ -27,9 +27,9 @@ ${name}@${version} - Usage klap build - bundle your package, in production mode. klap watch - bundle your package and watch for changes. klap start - start a development server. -`) - break +`); + break; default: - error('No Such Command !!') + error('No Such Command !!'); } -})() +})(); diff --git a/examples/react-component/dist/index.cjs.js b/examples/react-component/dist/index.cjs.js index c077c24..bd4eaa8 100644 --- a/examples/react-component/dist/index.cjs.js +++ b/examples/react-component/dist/index.cjs.js @@ -2,7 +2,7 @@ var t, o = (t = require('react')) && 'object' == typeof t && 'default' in t ? t.default - : t + : t; exports.Button = ({ onClick: t }) => - o.createElement('button', { class: 'button', onClick: t }, 'Hello Button') + o.createElement('button', { class: 'button', onClick: t }, 'Hello Button'); //# sourceMappingURL=index.cjs.js.map diff --git a/examples/react-component/dist/index.esm.js b/examples/react-component/dist/index.esm.js index a2d0496..5e6197d 100644 --- a/examples/react-component/dist/index.esm.js +++ b/examples/react-component/dist/index.esm.js @@ -1,5 +1,5 @@ -import o from 'react' +import o from 'react'; const t = ({ onClick: t }) => - o.createElement('button', { class: 'button', onClick: t }, 'Hello Button') -export { t as Button } + o.createElement('button', { class: 'button', onClick: t }, 'Hello Button'); +export { t as Button }; //# sourceMappingURL=index.esm.js.map diff --git a/examples/react-component/dist/index.js b/examples/react-component/dist/index.js index 842d916..6fa950e 100644 --- a/examples/react-component/dist/index.js +++ b/examples/react-component/dist/index.js @@ -1,18 +1,18 @@ -var e, t -;(e = this), +var e, t; +(e = this), (t = function(e, t) { - ;(t = t && t.hasOwnProperty('default') ? t.default : t), + (t = t && t.hasOwnProperty('default') ? t.default : t), (e.Button = function(e) { return t.createElement( 'button', { class: 'button', onClick: e.onClick }, 'Hello Button' - ) - }) + ); + }); }), 'object' == typeof exports && 'undefined' != typeof module ? t(exports, require('react')) : 'function' == typeof define && define.amd ? define(['exports', 'react'], t) - : t(((e = e || self).reactComponent = {}), e.React) + : t(((e = e || self).reactComponent = {}), e.React); //# sourceMappingURL=index.js.map diff --git a/examples/react-component/package-lock.json b/examples/react-component/package-lock.json index df59380..a3f86b3 100644 --- a/examples/react-component/package-lock.json +++ b/examples/react-component/package-lock.json @@ -1,77 +1,77 @@ { - "name": "react-component", - "version": "0.0.0", - "lockfileVersion": 1, - "requires": true, - "dependencies": { - "js-tokens": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", - "integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==" - }, - "klap": { - "version": "0.3.0", - "resolved": "https://registry.npmjs.org/klap/-/klap-0.3.0.tgz", - "integrity": "sha512-JgTlV2psnLTOobuP7mQi0PZUGN+cViQbK3NkN8t0WURyaASN7zgGITExkm0fIi+AZ9Y/kYn1iKGa/BAwpFoLfw==", - "dev": true - }, - "loose-envify": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/loose-envify/-/loose-envify-1.4.0.tgz", - "integrity": "sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==", - "requires": { - "js-tokens": "^3.0.0 || ^4.0.0" - } - }, - "object-assign": { - "version": "4.1.1", - "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", - "integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=" - }, - "prop-types": { - "version": "15.7.2", - "resolved": "https://registry.npmjs.org/prop-types/-/prop-types-15.7.2.tgz", - "integrity": "sha512-8QQikdH7//R2vurIJSutZ1smHYTcLpRWEOlHnzcWHmBYrOGUysKwSsrC89BCiFj3CbrfJ/nXFdJepOVrY1GCHQ==", - "requires": { - "loose-envify": "^1.4.0", - "object-assign": "^4.1.1", - "react-is": "^16.8.1" - } - }, - "react": { - "version": "16.11.0", - "resolved": "https://registry.npmjs.org/react/-/react-16.11.0.tgz", - "integrity": "sha512-M5Y8yITaLmU0ynd0r1Yvfq98Rmll6q8AxaEe88c8e7LxO8fZ2cNgmFt0aGAS9wzf1Ao32NKXtCl+/tVVtkxq6g==", - "requires": { - "loose-envify": "^1.1.0", - "object-assign": "^4.1.1", - "prop-types": "^15.6.2" - } - }, - "react-dom": { - "version": "16.11.0", - "resolved": "https://registry.npmjs.org/react-dom/-/react-dom-16.11.0.tgz", - "integrity": "sha512-nrRyIUE1e7j8PaXSPtyRKtz+2y9ubW/ghNgqKFHHAHaeP0fpF5uXR+sq8IMRHC+ZUxw7W9NyCDTBtwWxvkb0iA==", - "requires": { - "loose-envify": "^1.1.0", - "object-assign": "^4.1.1", - "prop-types": "^15.6.2", - "scheduler": "^0.17.0" - } - }, - "react-is": { - "version": "16.11.0", - "resolved": "https://registry.npmjs.org/react-is/-/react-is-16.11.0.tgz", - "integrity": "sha512-gbBVYR2p8mnriqAwWx9LbuUrShnAuSCNnuPGyc7GJrMVQtPDAh8iLpv7FRuMPFb56KkaVZIYSz1PrjI9q0QPCw==" - }, - "scheduler": { - "version": "0.17.0", - "resolved": "https://registry.npmjs.org/scheduler/-/scheduler-0.17.0.tgz", - "integrity": "sha512-7rro8Io3tnCPuY4la/NuI5F2yfESpnfZyT6TtkXnSWVkcu0BCDJ+8gk5ozUaFaxpIyNuWAPXrH0yFcSi28fnDA==", - "requires": { - "loose-envify": "^1.1.0", - "object-assign": "^4.1.1" - } - } - } + "name": "react-component", + "version": "0.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "js-tokens": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", + "integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==" + }, + "klap": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/klap/-/klap-0.3.0.tgz", + "integrity": "sha512-JgTlV2psnLTOobuP7mQi0PZUGN+cViQbK3NkN8t0WURyaASN7zgGITExkm0fIi+AZ9Y/kYn1iKGa/BAwpFoLfw==", + "dev": true + }, + "loose-envify": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/loose-envify/-/loose-envify-1.4.0.tgz", + "integrity": "sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==", + "requires": { + "js-tokens": "^3.0.0 || ^4.0.0" + } + }, + "object-assign": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", + "integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=" + }, + "prop-types": { + "version": "15.7.2", + "resolved": "https://registry.npmjs.org/prop-types/-/prop-types-15.7.2.tgz", + "integrity": "sha512-8QQikdH7//R2vurIJSutZ1smHYTcLpRWEOlHnzcWHmBYrOGUysKwSsrC89BCiFj3CbrfJ/nXFdJepOVrY1GCHQ==", + "requires": { + "loose-envify": "^1.4.0", + "object-assign": "^4.1.1", + "react-is": "^16.8.1" + } + }, + "react": { + "version": "16.11.0", + "resolved": "https://registry.npmjs.org/react/-/react-16.11.0.tgz", + "integrity": "sha512-M5Y8yITaLmU0ynd0r1Yvfq98Rmll6q8AxaEe88c8e7LxO8fZ2cNgmFt0aGAS9wzf1Ao32NKXtCl+/tVVtkxq6g==", + "requires": { + "loose-envify": "^1.1.0", + "object-assign": "^4.1.1", + "prop-types": "^15.6.2" + } + }, + "react-dom": { + "version": "16.11.0", + "resolved": "https://registry.npmjs.org/react-dom/-/react-dom-16.11.0.tgz", + "integrity": "sha512-nrRyIUE1e7j8PaXSPtyRKtz+2y9ubW/ghNgqKFHHAHaeP0fpF5uXR+sq8IMRHC+ZUxw7W9NyCDTBtwWxvkb0iA==", + "requires": { + "loose-envify": "^1.1.0", + "object-assign": "^4.1.1", + "prop-types": "^15.6.2", + "scheduler": "^0.17.0" + } + }, + "react-is": { + "version": "16.11.0", + "resolved": "https://registry.npmjs.org/react-is/-/react-is-16.11.0.tgz", + "integrity": "sha512-gbBVYR2p8mnriqAwWx9LbuUrShnAuSCNnuPGyc7GJrMVQtPDAh8iLpv7FRuMPFb56KkaVZIYSz1PrjI9q0QPCw==" + }, + "scheduler": { + "version": "0.17.0", + "resolved": "https://registry.npmjs.org/scheduler/-/scheduler-0.17.0.tgz", + "integrity": "sha512-7rro8Io3tnCPuY4la/NuI5F2yfESpnfZyT6TtkXnSWVkcu0BCDJ+8gk5ozUaFaxpIyNuWAPXrH0yFcSi28fnDA==", + "requires": { + "loose-envify": "^1.1.0", + "object-assign": "^4.1.1" + } + } + } } diff --git a/examples/react-component/package.json b/examples/react-component/package.json index 5b6fd37..7bf3425 100644 --- a/examples/react-component/package.json +++ b/examples/react-component/package.json @@ -1,27 +1,27 @@ { - "name": "react-component", - "version": "0.0.0", - "private": true, - "files": [ - "dist" - ], - "main": "dist/index.cjs.js", - "module": "dist/index.esm.js", - "source": "src/index.js", - "browser": "dist/index.js", - "scripts": { - "build": "klap build", - "start": "klap start", - "watch": "klap watch" - }, - "dependencies": { - "react": "*", - "react-dom": "*" - }, - "devDependencies": { - "klap": "2.1.0" - }, - "klap": { - "example": "public/index.js" - } + "name": "react-component", + "version": "0.0.0", + "private": true, + "files": [ + "dist" + ], + "main": "dist/index.cjs.js", + "module": "dist/index.esm.js", + "source": "src/index.js", + "browser": "dist/index.js", + "scripts": { + "build": "klap build", + "start": "klap start", + "watch": "klap watch" + }, + "dependencies": { + "react": "*", + "react-dom": "*" + }, + "devDependencies": { + "klap": "2.1.0" + }, + "klap": { + "example": "public/index.js" + } } diff --git a/examples/react-component/public/index.js b/examples/react-component/public/index.js index ec054f1..5097319 100644 --- a/examples/react-component/public/index.js +++ b/examples/react-component/public/index.js @@ -1,5 +1,8 @@ -import React from 'react' -import { render } from 'react-dom' -import { Button } from '../src' +import React from 'react'; +import { render } from 'react-dom'; +import { Button } from '../src'; -render( - ) -} + ); +}; -export { Button } +export { Button }; diff --git a/examples/react-sc-typescript/dist/index.cjs.js b/examples/react-sc-typescript/dist/index.cjs.js index c6506d7..67b363a 100644 --- a/examples/react-sc-typescript/dist/index.cjs.js +++ b/examples/react-sc-typescript/dist/index.cjs.js @@ -1,23 +1,23 @@ -var n +var n; function t() { const n = - ((o = ['\n\tbackground-color: red;\n\twidth: 100%;\n']), + ((o = ['\n background-color: red;\n width: 100%;\n']), r || (r = o.slice(0)), (o.raw = r), - o) - var o, r + o); + var o, r; return ( (t = function() { - return n + return n; }), n - ) + ); } const o = ((n = require('styled-components')) && 'object' == typeof n && 'default' in n ? n.default : n -).button(t()) -exports.Button = o +).button(t()); +exports.Button = o; //# sourceMappingURL=index.cjs.js.map diff --git a/examples/react-sc-typescript/dist/index.esm.js b/examples/react-sc-typescript/dist/index.esm.js index 049219b..b3a599c 100644 --- a/examples/react-sc-typescript/dist/index.esm.js +++ b/examples/react-sc-typescript/dist/index.esm.js @@ -1,18 +1,18 @@ -import n from 'styled-components' -function t() { +import n from 'styled-components'; +function o() { const n = - ((o = ['\n\tbackground-color: red;\n\twidth: 100%;\n']), - r || (r = o.slice(0)), - (o.raw = r), - o) - var o, r + ((r = ['\n background-color: red;\n width: 100%;\n']), + t || (t = r.slice(0)), + (r.raw = t), + r); + var r, t; return ( - (t = function() { - return n + (o = function() { + return n; }), n - ) + ); } -const o = n.button(t()) -export { o as Button } +const r = n.button(o()); +export { r as Button }; //# sourceMappingURL=index.esm.js.map diff --git a/examples/react-sc-typescript/dist/index.js b/examples/react-sc-typescript/dist/index.js index e6384e1..b773600 100644 --- a/examples/react-sc-typescript/dist/index.js +++ b/examples/react-sc-typescript/dist/index.js @@ -1,27 +1,27 @@ -var e, n -;(e = this), +var e, n; +(e = this), (n = function(e, n) { function t() { var e, n, o = - ((e = ['\n\tbackground-color: red;\n\twidth: 100%;\n']), + ((e = ['\n background-color: red;\n width: 100%;\n']), n || (n = e.slice(0)), (e.raw = n), - e) + e); return ( (t = function() { - return o + return o; }), o - ) + ); } - var o = (n = n && n.hasOwnProperty('default') ? n.default : n).button(t()) - e.Button = o + var o = (n = n && n.hasOwnProperty('default') ? n.default : n).button(t()); + e.Button = o; }), 'object' == typeof exports && 'undefined' != typeof module ? n(exports, require('styled-components')) : 'function' == typeof define && define.amd ? define(['exports', 'styled-components'], n) - : n(((e = e || self).reactScTypescript = {}), e.styledComponents) + : n(((e = e || self).reactScTypescript = {}), e.styledComponents); //# sourceMappingURL=index.js.map diff --git a/examples/react-sc-typescript/package.json b/examples/react-sc-typescript/package.json index 2216c0c..3d3c07a 100644 --- a/examples/react-sc-typescript/package.json +++ b/examples/react-sc-typescript/package.json @@ -1,30 +1,30 @@ { - "name": "react-sc-typescript", - "version": "0.0.0", - "files": [ - "dist" - ], - "main": "dist/index.cjs.js", - "module": "dist/index.esm.js", - "source": "src/index.tsx", - "browser": "dist/index.js", - "scripts": { - "build": "klap build", - "start": "klap start", - "watch": "klap watch" - }, - "dependencies": { - "react": "*", - "react-dom": "*", - "styled-components": "*" - }, - "devDependencies": { - "klap": "2.1.0" - }, - "klap": { - "globals": { - "styled-components": "styledComponents" - }, - "example": "public/index.tsx" - } + "name": "react-sc-typescript", + "version": "0.0.0", + "files": [ + "dist" + ], + "main": "dist/index.cjs.js", + "module": "dist/index.esm.js", + "source": "src/index.tsx", + "browser": "dist/index.js", + "scripts": { + "build": "klap build", + "start": "klap start", + "watch": "klap watch" + }, + "dependencies": { + "react": "*", + "react-dom": "*", + "styled-components": "*" + }, + "devDependencies": { + "klap": "2.1.0" + }, + "klap": { + "globals": { + "styled-components": "styledComponents" + }, + "example": "public/index.tsx" + } } diff --git a/examples/react-sc-typescript/src/index.tsx b/examples/react-sc-typescript/src/index.tsx index 11bf2e5..f2c66fa 100644 --- a/examples/react-sc-typescript/src/index.tsx +++ b/examples/react-sc-typescript/src/index.tsx @@ -1,9 +1,9 @@ -import React from 'react' -import styled from 'styled-components' +import React from 'react'; +import styled from 'styled-components'; const Button = styled.button` - background-color: red; - width: 100%; -` + background-color: red; + width: 100%; +`; -export { Button } +export { Button }; diff --git a/examples/react-typescript/dist/index.cjs.js b/examples/react-typescript/dist/index.cjs.js index c077c24..bd4eaa8 100644 --- a/examples/react-typescript/dist/index.cjs.js +++ b/examples/react-typescript/dist/index.cjs.js @@ -2,7 +2,7 @@ var t, o = (t = require('react')) && 'object' == typeof t && 'default' in t ? t.default - : t + : t; exports.Button = ({ onClick: t }) => - o.createElement('button', { class: 'button', onClick: t }, 'Hello Button') + o.createElement('button', { class: 'button', onClick: t }, 'Hello Button'); //# sourceMappingURL=index.cjs.js.map diff --git a/examples/react-typescript/dist/index.esm.js b/examples/react-typescript/dist/index.esm.js index a2d0496..5e6197d 100644 --- a/examples/react-typescript/dist/index.esm.js +++ b/examples/react-typescript/dist/index.esm.js @@ -1,5 +1,5 @@ -import o from 'react' +import o from 'react'; const t = ({ onClick: t }) => - o.createElement('button', { class: 'button', onClick: t }, 'Hello Button') -export { t as Button } + o.createElement('button', { class: 'button', onClick: t }, 'Hello Button'); +export { t as Button }; //# sourceMappingURL=index.esm.js.map diff --git a/examples/react-typescript/dist/index.js b/examples/react-typescript/dist/index.js index 03ecaa4..4ad75e8 100644 --- a/examples/react-typescript/dist/index.js +++ b/examples/react-typescript/dist/index.js @@ -1,18 +1,18 @@ -var e, t -;(e = this), +var e, t; +(e = this), (t = function(e, t) { - ;(t = t && t.hasOwnProperty('default') ? t.default : t), + (t = t && t.hasOwnProperty('default') ? t.default : t), (e.Button = function(e) { return t.createElement( 'button', { class: 'button', onClick: e.onClick }, 'Hello Button' - ) - }) + ); + }); }), 'object' == typeof exports && 'undefined' != typeof module ? t(exports, require('react')) : 'function' == typeof define && define.amd ? define(['exports', 'react'], t) - : t(((e = e || self).reactTypescript = {}), e.React) + : t(((e = e || self).reactTypescript = {}), e.React); //# sourceMappingURL=index.js.map diff --git a/examples/react-typescript/package-lock.json b/examples/react-typescript/package-lock.json index 8d652a5..5454fa7 100644 --- a/examples/react-typescript/package-lock.json +++ b/examples/react-typescript/package-lock.json @@ -1,77 +1,77 @@ { - "name": "react-typescript", - "version": "0.0.0", - "lockfileVersion": 1, - "requires": true, - "dependencies": { - "js-tokens": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", - "integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==" - }, - "klap": { - "version": "0.3.0", - "resolved": "https://registry.npmjs.org/klap/-/klap-0.3.0.tgz", - "integrity": "sha512-JgTlV2psnLTOobuP7mQi0PZUGN+cViQbK3NkN8t0WURyaASN7zgGITExkm0fIi+AZ9Y/kYn1iKGa/BAwpFoLfw==", - "dev": true - }, - "loose-envify": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/loose-envify/-/loose-envify-1.4.0.tgz", - "integrity": "sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==", - "requires": { - "js-tokens": "^3.0.0 || ^4.0.0" - } - }, - "object-assign": { - "version": "4.1.1", - "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", - "integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=" - }, - "prop-types": { - "version": "15.7.2", - "resolved": "https://registry.npmjs.org/prop-types/-/prop-types-15.7.2.tgz", - "integrity": "sha512-8QQikdH7//R2vurIJSutZ1smHYTcLpRWEOlHnzcWHmBYrOGUysKwSsrC89BCiFj3CbrfJ/nXFdJepOVrY1GCHQ==", - "requires": { - "loose-envify": "^1.4.0", - "object-assign": "^4.1.1", - "react-is": "^16.8.1" - } - }, - "react": { - "version": "16.11.0", - "resolved": "https://registry.npmjs.org/react/-/react-16.11.0.tgz", - "integrity": "sha512-M5Y8yITaLmU0ynd0r1Yvfq98Rmll6q8AxaEe88c8e7LxO8fZ2cNgmFt0aGAS9wzf1Ao32NKXtCl+/tVVtkxq6g==", - "requires": { - "loose-envify": "^1.1.0", - "object-assign": "^4.1.1", - "prop-types": "^15.6.2" - } - }, - "react-dom": { - "version": "16.11.0", - "resolved": "https://registry.npmjs.org/react-dom/-/react-dom-16.11.0.tgz", - "integrity": "sha512-nrRyIUE1e7j8PaXSPtyRKtz+2y9ubW/ghNgqKFHHAHaeP0fpF5uXR+sq8IMRHC+ZUxw7W9NyCDTBtwWxvkb0iA==", - "requires": { - "loose-envify": "^1.1.0", - "object-assign": "^4.1.1", - "prop-types": "^15.6.2", - "scheduler": "^0.17.0" - } - }, - "react-is": { - "version": "16.11.0", - "resolved": "https://registry.npmjs.org/react-is/-/react-is-16.11.0.tgz", - "integrity": "sha512-gbBVYR2p8mnriqAwWx9LbuUrShnAuSCNnuPGyc7GJrMVQtPDAh8iLpv7FRuMPFb56KkaVZIYSz1PrjI9q0QPCw==" - }, - "scheduler": { - "version": "0.17.0", - "resolved": "https://registry.npmjs.org/scheduler/-/scheduler-0.17.0.tgz", - "integrity": "sha512-7rro8Io3tnCPuY4la/NuI5F2yfESpnfZyT6TtkXnSWVkcu0BCDJ+8gk5ozUaFaxpIyNuWAPXrH0yFcSi28fnDA==", - "requires": { - "loose-envify": "^1.1.0", - "object-assign": "^4.1.1" - } - } - } + "name": "react-typescript", + "version": "0.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "js-tokens": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", + "integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==" + }, + "klap": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/klap/-/klap-0.3.0.tgz", + "integrity": "sha512-JgTlV2psnLTOobuP7mQi0PZUGN+cViQbK3NkN8t0WURyaASN7zgGITExkm0fIi+AZ9Y/kYn1iKGa/BAwpFoLfw==", + "dev": true + }, + "loose-envify": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/loose-envify/-/loose-envify-1.4.0.tgz", + "integrity": "sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==", + "requires": { + "js-tokens": "^3.0.0 || ^4.0.0" + } + }, + "object-assign": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", + "integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=" + }, + "prop-types": { + "version": "15.7.2", + "resolved": "https://registry.npmjs.org/prop-types/-/prop-types-15.7.2.tgz", + "integrity": "sha512-8QQikdH7//R2vurIJSutZ1smHYTcLpRWEOlHnzcWHmBYrOGUysKwSsrC89BCiFj3CbrfJ/nXFdJepOVrY1GCHQ==", + "requires": { + "loose-envify": "^1.4.0", + "object-assign": "^4.1.1", + "react-is": "^16.8.1" + } + }, + "react": { + "version": "16.11.0", + "resolved": "https://registry.npmjs.org/react/-/react-16.11.0.tgz", + "integrity": "sha512-M5Y8yITaLmU0ynd0r1Yvfq98Rmll6q8AxaEe88c8e7LxO8fZ2cNgmFt0aGAS9wzf1Ao32NKXtCl+/tVVtkxq6g==", + "requires": { + "loose-envify": "^1.1.0", + "object-assign": "^4.1.1", + "prop-types": "^15.6.2" + } + }, + "react-dom": { + "version": "16.11.0", + "resolved": "https://registry.npmjs.org/react-dom/-/react-dom-16.11.0.tgz", + "integrity": "sha512-nrRyIUE1e7j8PaXSPtyRKtz+2y9ubW/ghNgqKFHHAHaeP0fpF5uXR+sq8IMRHC+ZUxw7W9NyCDTBtwWxvkb0iA==", + "requires": { + "loose-envify": "^1.1.0", + "object-assign": "^4.1.1", + "prop-types": "^15.6.2", + "scheduler": "^0.17.0" + } + }, + "react-is": { + "version": "16.11.0", + "resolved": "https://registry.npmjs.org/react-is/-/react-is-16.11.0.tgz", + "integrity": "sha512-gbBVYR2p8mnriqAwWx9LbuUrShnAuSCNnuPGyc7GJrMVQtPDAh8iLpv7FRuMPFb56KkaVZIYSz1PrjI9q0QPCw==" + }, + "scheduler": { + "version": "0.17.0", + "resolved": "https://registry.npmjs.org/scheduler/-/scheduler-0.17.0.tgz", + "integrity": "sha512-7rro8Io3tnCPuY4la/NuI5F2yfESpnfZyT6TtkXnSWVkcu0BCDJ+8gk5ozUaFaxpIyNuWAPXrH0yFcSi28fnDA==", + "requires": { + "loose-envify": "^1.1.0", + "object-assign": "^4.1.1" + } + } + } } diff --git a/examples/react-typescript/package.json b/examples/react-typescript/package.json index ae591a6..315ef2a 100644 --- a/examples/react-typescript/package.json +++ b/examples/react-typescript/package.json @@ -1,27 +1,27 @@ { - "name": "react-typescript", - "version": "0.0.0", - "private": true, - "files": [ - "dist" - ], - "main": "dist/index.cjs.js", - "module": "dist/index.esm.js", - "source": "src/index.tsx", - "browser": "dist/index.js", - "scripts": { - "build": "klap build", - "start": "klap start", - "watch": "klap watch" - }, - "dependencies": { - "react": "*", - "react-dom": "*" - }, - "devDependencies": { - "klap": "2.1.0" - }, - "klap": { - "example": "public/index.tsx" - } + "name": "react-typescript", + "version": "0.0.0", + "private": true, + "files": [ + "dist" + ], + "main": "dist/index.cjs.js", + "module": "dist/index.esm.js", + "source": "src/index.tsx", + "browser": "dist/index.js", + "scripts": { + "build": "klap build", + "start": "klap start", + "watch": "klap watch" + }, + "dependencies": { + "react": "*", + "react-dom": "*" + }, + "devDependencies": { + "klap": "2.1.0" + }, + "klap": { + "example": "public/index.tsx" + } } diff --git a/examples/react-typescript/public/index.tsx b/examples/react-typescript/public/index.tsx index ec054f1..5097319 100644 --- a/examples/react-typescript/public/index.tsx +++ b/examples/react-typescript/public/index.tsx @@ -1,5 +1,8 @@ -import React from 'react' -import { render } from 'react-dom' -import { Button } from '../src' +import React from 'react'; +import { render } from 'react-dom'; +import { Button } from '../src'; -render( - ) -} + return ( + + ); +}; -export { Button } +export { Button }; diff --git a/examples/scaffold-typescript/dist/index.cjs.js b/examples/scaffold-typescript/dist/index.cjs.js index d45acc8..098dedc 100644 --- a/examples/scaffold-typescript/dist/index.cjs.js +++ b/examples/scaffold-typescript/dist/index.cjs.js @@ -1,2 +1,2 @@ -exports.sum = (e, o) => e + o +exports.sum = (e, o) => e + o; //# sourceMappingURL=index.cjs.js.map diff --git a/examples/scaffold-typescript/dist/index.esm.js b/examples/scaffold-typescript/dist/index.esm.js index 27d5ce3..0ac5f45 100644 --- a/examples/scaffold-typescript/dist/index.esm.js +++ b/examples/scaffold-typescript/dist/index.esm.js @@ -1,3 +1,3 @@ -const o = (o, t) => o + t -export { o as sum } +const o = (o, t) => o + t; +export { o as sum }; //# sourceMappingURL=index.esm.js.map diff --git a/examples/scaffold-typescript/dist/index.js b/examples/scaffold-typescript/dist/index.js index 34e1855..1f32205 100644 --- a/examples/scaffold-typescript/dist/index.js +++ b/examples/scaffold-typescript/dist/index.js @@ -1,13 +1,13 @@ -var e, n -;(e = this), +var e, n; +(e = this), (n = function(e) { e.sum = function(e, n) { - return e + n - } + return e + n; + }; }), 'object' == typeof exports && 'undefined' != typeof module ? n(exports) : 'function' == typeof define && define.amd ? define(['exports'], n) - : n(((e = e || self).scaffoldTypescript = {})) + : n(((e = e || self).scaffoldTypescript = {})); //# sourceMappingURL=index.js.map diff --git a/examples/scaffold-typescript/package.json b/examples/scaffold-typescript/package.json index 0bde547..a89f447 100644 --- a/examples/scaffold-typescript/package.json +++ b/examples/scaffold-typescript/package.json @@ -1,28 +1,28 @@ { - "name": "scaffold-typescript", - "version": "0.0.0", - "private": true, - "files": [ - "dist" - ], - "main": "dist/index.cjs.js", - "module": "dist/index.esm.js", - "source": "src/index.ts", - "browser": "dist/index.js", - "scripts": { - "build": "klap build", - "start": "klap start", - "watch": "klap watch" - }, - "devDependencies": { - "klap": "2.1.0" - }, - "klap": { - "namedExports": { - "react-dom": [ - "render" - ] - }, - "example": "public/index.js" - } + "name": "scaffold-typescript", + "version": "0.0.0", + "private": true, + "files": [ + "dist" + ], + "main": "dist/index.cjs.js", + "module": "dist/index.esm.js", + "source": "src/index.ts", + "browser": "dist/index.js", + "scripts": { + "build": "klap build", + "start": "klap start", + "watch": "klap watch" + }, + "devDependencies": { + "klap": "2.1.0" + }, + "klap": { + "namedExports": { + "react-dom": [ + "render" + ] + }, + "example": "public/index.js" + } } diff --git a/examples/scaffold-typescript/src/index.ts b/examples/scaffold-typescript/src/index.ts index 059895c..4abbfcc 100644 --- a/examples/scaffold-typescript/src/index.ts +++ b/examples/scaffold-typescript/src/index.ts @@ -1 +1 @@ -export const sum = (a: number, b: number) => a + b +export const sum = (a: number, b: number) => a + b; diff --git a/examples/scaffold/dist/index.cjs.js b/examples/scaffold/dist/index.cjs.js index d45acc8..098dedc 100644 --- a/examples/scaffold/dist/index.cjs.js +++ b/examples/scaffold/dist/index.cjs.js @@ -1,2 +1,2 @@ -exports.sum = (e, o) => e + o +exports.sum = (e, o) => e + o; //# sourceMappingURL=index.cjs.js.map diff --git a/examples/scaffold/dist/index.esm.js b/examples/scaffold/dist/index.esm.js index 27d5ce3..0ac5f45 100644 --- a/examples/scaffold/dist/index.esm.js +++ b/examples/scaffold/dist/index.esm.js @@ -1,3 +1,3 @@ -const o = (o, t) => o + t -export { o as sum } +const o = (o, t) => o + t; +export { o as sum }; //# sourceMappingURL=index.esm.js.map diff --git a/examples/scaffold/dist/index.js b/examples/scaffold/dist/index.js index 291735a..bf23f77 100644 --- a/examples/scaffold/dist/index.js +++ b/examples/scaffold/dist/index.js @@ -1,13 +1,13 @@ -var e, n -;(e = this), +var e, n; +(e = this), (n = function(e) { e.sum = function(e, n) { - return e + n - } + return e + n; + }; }), 'object' == typeof exports && 'undefined' != typeof module ? n(exports) : 'function' == typeof define && define.amd ? define(['exports'], n) - : n(((e = e || self).scaffold = {})) + : n(((e = e || self).scaffold = {})); //# sourceMappingURL=index.js.map diff --git a/examples/scaffold/package.json b/examples/scaffold/package.json index 61b89df..319c49b 100644 --- a/examples/scaffold/package.json +++ b/examples/scaffold/package.json @@ -1,23 +1,23 @@ { - "name": "scaffold", - "version": "0.0.0", - "private": true, - "files": [ - "dist" - ], - "main": "dist/index.cjs.js", - "module": "dist/index.esm.js", - "source": "src/index.js", - "browser": "dist/index.js", - "scripts": { - "build": "klap build", - "start": "klap start", - "watch": "klap watch" - }, - "devDependencies": { - "klap": "2.1.0" - }, - "klap": { - "example": "public/index.js" - } + "name": "scaffold", + "version": "0.0.0", + "private": true, + "files": [ + "dist" + ], + "main": "dist/index.cjs.js", + "module": "dist/index.esm.js", + "source": "src/index.js", + "browser": "dist/index.js", + "scripts": { + "build": "klap build", + "start": "klap start", + "watch": "klap watch" + }, + "devDependencies": { + "klap": "2.1.0" + }, + "klap": { + "example": "public/index.js" + } } diff --git a/examples/scaffold/public/index.js b/examples/scaffold/public/index.js index 5de838f..3361144 100644 --- a/examples/scaffold/public/index.js +++ b/examples/scaffold/public/index.js @@ -1,3 +1,3 @@ -import { sum } from '../src' +import { sum } from '../src'; -console.log('this works => ', sum(2, 3)) +console.log('this works => ', sum(2, 3)); diff --git a/examples/scaffold/src/index.js b/examples/scaffold/src/index.js index 89b8db1..bc5f04d 100644 --- a/examples/scaffold/src/index.js +++ b/examples/scaffold/src/index.js @@ -1 +1 @@ -export const sum = (a, b) => a + b +export const sum = (a, b) => a + b; diff --git a/package.json b/package.json index acf88d9..4b69a81 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ "license": "MIT", "author": "osdevisnot ", "files": [ - "dist" + "dist", + "docs" ], "main": "dist/index.js", "bin": { @@ -39,8 +40,8 @@ "@babel/preset-env": "7.7.7", "@babel/preset-react": "7.7.4", "@babel/preset-typescript": "7.7.7", - "@osdevisnot/prettier": "3.0.1", - "@rollup/plugin-replace": "2.2.1", + "@osdevisnot/prettier": "4.0.0", + "@rollup/plugin-replace": "2.3.0", "@types/node": "12.12.21", "@zeit/ncc": "0.20.5", "babel-plugin-macros": "2.8.0", @@ -55,7 +56,7 @@ "mkdirp": "0.5.1", "patch-package": "6.2.0", "pretty-bytes": "5.3.0", - "rollup": "1.27.13", + "rollup": "1.27.14", "rollup-plugin-babel": "4.3.3", "rollup-plugin-commonjs": "10.1.0", "rollup-plugin-json": "4.0.0", @@ -63,7 +64,7 @@ "rollup-plugin-node-resolve": "5.2.0", "rollup-plugin-sourcemaps": "0.4.2", "rollup-pluginutils": "2.8.2", - "semantic-release": "15.13.31", + "semantic-release": "15.14.0", "servor": "3.0.5", "sort-package-json": "1.31.0", "terser": "4.4.3" diff --git a/src/babel.js b/src/babel.js index 1a28bdd..e90a673 100644 --- a/src/babel.js +++ b/src/babel.js @@ -1,21 +1,21 @@ -import { DEFAULT_EXTENSIONS } from '@babel/core' +import { DEFAULT_EXTENSIONS } from '@babel/core'; // babel presets -import presetEnv from '@babel/preset-env' -import presetTs from '@babel/preset-typescript' -import presetReact from '@babel/preset-react' +import presetEnv from '@babel/preset-env'; +import presetTs from '@babel/preset-typescript'; +import presetReact from '@babel/preset-react'; // babel plugins -import pluginObjectRestSpread from '@babel/plugin-proposal-object-rest-spread' -import pluginAsyncToPromise from 'babel-plugin-transform-async-to-promises' -import pluginClassProperties from '@babel/plugin-proposal-class-properties' -import pluginTransformRegen from '@babel/plugin-transform-regenerator' -import pluginStyledComponents from 'babel-plugin-styled-components' -import pluginMacros from 'babel-plugin-macros' +import pluginObjectRestSpread from '@babel/plugin-proposal-object-rest-spread'; +import pluginAsyncToPromise from 'babel-plugin-transform-async-to-promises'; +import pluginClassProperties from '@babel/plugin-proposal-class-properties'; +import pluginTransformRegen from '@babel/plugin-transform-regenerator'; +import pluginStyledComponents from 'babel-plugin-styled-components'; +import pluginMacros from 'babel-plugin-macros'; export const babelConfig = (command, pkg, options) => { - const extensions = [...DEFAULT_EXTENSIONS, '.ts', '.tsx', '.json'] - const { browserlist, pragma, frag, format } = options + const extensions = [...DEFAULT_EXTENSIONS, '.ts', '.tsx', '.json']; + const { browserlist, pragma, frag, format } = options; const presets = [ [ @@ -29,9 +29,16 @@ export const babelConfig = (command, pkg, options) => { }, ], // Note: when using `React`, presetTs needs `React` as jsxPragma, vs presetReact needs `React.createElement`, but when using `h` as pragma, both presets needs it to be just `h` - [presetTs, { jsxPragma: pragma.split('.').shift(), isTSX: true, allExtensions: true }], + [ + presetTs, + { + jsxPragma: pragma.split('.').shift(), + isTSX: true, + allExtensions: true, + }, + ], [presetReact, { pragma, pragmaFrag: frag }], - ] + ]; const plugins = [ [pluginObjectRestSpread, { loose: true, useBuiltIns: true }], @@ -40,7 +47,7 @@ export const babelConfig = (command, pkg, options) => { [pluginTransformRegen, { async: false }], pluginStyledComponents, pluginMacros, - ] + ]; - return { presets, plugins, extensions } -} + return { presets, plugins, extensions }; +}; diff --git a/src/init-create.js b/src/init-create.js new file mode 100644 index 0000000..8416e91 --- /dev/null +++ b/src/init-create.js @@ -0,0 +1,54 @@ +export const createLicense = author => { + return `MIT License + +Copyright (c) ${new Date().getFullYear()} ${author} + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE.`; +}; + +export const createIndex = pkg => { + return ` + + + + + + ${pkg.name} example + + +
+ + +`; +}; + +export const createTsConfig = () => `{ + "include": ["src"], + "compilerOptions": { + "target": "esnext", + "module": "esnext", + "moduleResolution": "node", + "declaration": true, + "declarationDir": "dist/types", + "emitDeclarationOnly": true, + "jsx": "react", + "lib": ["dom", "esnext"], + "removeComments": true + } +}`; diff --git a/src/init.js b/src/init.js index 78dbb74..123971c 100644 --- a/src/init.js +++ b/src/init.js @@ -1,75 +1,104 @@ -import merge from 'deepmerge' -import sort from 'sort-package-json' -import cli from '../package.json' -import { info, log, warn } from './logger' -import { exists, read, write } from './utils' +import merge from 'deepmerge'; +import sort from 'sort-package-json'; +import cli from '../package.json'; +import { createIndex, createLicense, createTsConfig } from './init-create'; +import { error, info, log } from './logger'; +import { exec, exists, read, write } from './utils'; -const writePackage = async () => { +const unscopedName = name => name.split('/').pop(); + +const gitInfo = () => { + let user = exec('git config github.username'); + if (!user) user = exec('git config user.name'); + let email = exec('git config user.email'); + return { user, email }; +}; + +const writePackage = async (template, { user, email }) => { let pkg = {}, - name = process - .cwd() - .split('/') - .pop() - let source = `src/${name}.js` + name = unscopedName(process.cwd()); + let source = `src/${name}.${template}`; if (await exists('./package.json')) { - pkg = JSON.parse(await read('./package.json')) + pkg = JSON.parse(await read('./package.json')); } - pkg = merge({ name, version: '0.0.0', license: 'MIT' }, pkg) + pkg = merge({ name, version: '0.0.0', license: 'MIT', description: '' }, pkg); + pkg = merge( + { repository: `${user}/${pkg.name}`, author: `${user} <${email}>` }, + pkg + ); pkg = merge(pkg, { main: `dist/${name}.cjs.js`, + unpkg: `dist/${name}.esm.js`, module: `dist/${name}.esm.js`, browser: `dist/${name}.js`, source, files: ['dist'], scripts: { build: 'klap build', - prepublishOnly: 'klap build', + prepublishOnly: 'yarn build', start: 'klap start', watch: 'klap watch', }, + prettier: '@osdevisnot/prettier', devDependencies: { [cli.name]: cli.version, + '@osdevisnot/prettier': cli.devDependencies['@osdevisnot/prettier'], }, - }) - await write('./package.json', JSON.stringify(sort(pkg), null, ' ')) - info('\t- wrote ./package.json') - return pkg -} + }); + if (template !== 'js') { + pkg = merge(pkg, { klap: { example: `public/index.${template}` } }); + if (template === 'ts' || template === 'tsx') { + pkg = merge(pkg, { types: 'dist/types' }); + } + } + await write('./package.json', JSON.stringify(sort(pkg), null, ' ')); + info('\t- wrote ./package.json'); + return pkg; +}; -const writeFiles = async pkg => { - const files = { - [pkg.source]: `export const sum = (a, b) => a + b;`, - 'public/index.js': `import { sum } from '../${pkg.source}';\n\nconsole.log('this works => ', sum(2, 3));`, - 'public/index.html': ` - - - - - - ${pkg.name} example - - -
- - -`, +const writeFiles = async (pkg, template) => { + const defaults = { + LICENSE: createLicense(pkg.author), '.gitignore': ['node_modules', 'dist', 'coverage'].join('\n'), - } + 'public/index.html': createIndex(pkg), + [`public/index.${template}`]: `import { sum } from '../src/${unscopedName( + pkg.name + )}';\n\nconsole.log('this works => ', sum(2, 3));`, + [pkg.source]: `export const sum = (a, b) => a + b;`, + }; + // overrides based on templates + const templates = { + ts: { + [pkg.source]: `export const sum = (a: number, b: number): number => a + b;`, + 'tsconfig.json': createTsConfig(), + }, + }; + const files = Object.assign( + {}, + defaults, + templates[template.substring(0, 2)] + ); for (let [file, content] of Object.entries(files)) { if (!(await exists(file))) { - await write(file, content) - info(`\t- wrote ./${file}`) + await write(file, content); + info(`\t- wrote ./${file}`); } } -} +}; export const init = async () => { - const pkg = await writePackage() - await writeFiles(pkg) - if (!pkg.author) { - log('\npackage author not configured...') - warn('Consider using `yarn init -y` or `npm init -y` command.') + let template = process.argv[3] || 'js'; // js, jsx, ts, tsx + if ( + template == 'js' || + template == 'ts' || + template == 'jsx' || + template == 'tsx' + ) { + } else { + error('Invalid init template. Try one of js, jsx, ts, tsx'); } - log('\nWant to use typescript with klap?') - info('Check https://bit.ly/2tzP98y for more examples.\n') -} + const pkg = await writePackage(template, gitInfo()); + await writeFiles(pkg, template); + log('\nWant to use typescript with klap?'); + info('Initialize your package with `klap init ts`\n'); +}; diff --git a/src/klap.js b/src/klap.js index a6a4652..7c56d2e 100644 --- a/src/klap.js +++ b/src/klap.js @@ -1,118 +1,167 @@ -import del from 'del' -import { dirname } from 'path' -import { rollup, watch } from 'rollup' -import { error, info } from './logger' -import { getOptions } from './options' -import { plugins } from './plugins' +import del from 'del'; +import { dirname } from 'path'; +import { rollup, watch } from 'rollup'; +import { error, info } from './logger'; +import { getOptions } from './options'; +import { plugins } from './plugins'; -const defaultOptions = { esModule: false, strict: false, freeze: false } +const defaultOptions = { esModule: false, strict: false, freeze: false }; const buildConfig = (command, pkg, options) => { - const { dependencies = {}, peerDependencies = {}, main, module, browser } = pkg - const { name, globals, source: input, sourcemap } = options - const external = Object.keys({ ...dependencies, ...peerDependencies }) + const { + dependencies = {}, + peerDependencies = {}, + main, + module, + browser, + } = pkg; + const { name, globals, source: input, sourcemap } = options; + const external = Object.keys({ ...dependencies, ...peerDependencies }); let inputOptions = [ - main && { external, input, plugins: plugins(command, pkg, { ...options, format: 'cjs' }) }, - module && { external, input, plugins: plugins(command, pkg, { ...options, format: 'es' }) }, - browser && { external, input, plugins: plugins(command, pkg, { ...options, format: 'umd' }) } - ].filter(Boolean) + main && { + external, + input, + plugins: plugins(command, pkg, { ...options, format: 'cjs' }), + }, + module && { + external, + input, + plugins: plugins(command, pkg, { ...options, format: 'es' }), + }, + browser && { + external, + input, + plugins: plugins(command, pkg, { ...options, format: 'umd' }), + }, + ].filter(Boolean); let outputOptions = [ main && { ...defaultOptions, file: main, format: 'cjs', sourcemap }, module && { ...defaultOptions, file: module, format: 'es', sourcemap }, - browser && { ...defaultOptions, file: browser, format: 'umd', name, sourcemap, globals } - ].filter(Boolean) + browser && { + ...defaultOptions, + file: browser, + format: 'umd', + name, + sourcemap, + globals, + }, + ].filter(Boolean); - return { inputOptions, outputOptions } -} + return { inputOptions, outputOptions }; +}; const startConfig = (command, pkg, options) => { - const { module, browser } = pkg - const { name, globals, example: input, sourcemap, target } = options - let inputOptions, outputOptions + const { module, browser } = pkg; + const { name, globals, example: input, sourcemap, target } = options; + let inputOptions, outputOptions; if (target === 'es') { - inputOptions = { input, plugins: plugins(command, pkg, { ...options, format: 'es' }) } - outputOptions = { ...defaultOptions, file: module, format: 'es', sourcemap } + inputOptions = { + input, + plugins: plugins(command, pkg, { ...options, format: 'es' }), + }; + outputOptions = { + ...defaultOptions, + file: module, + format: 'es', + sourcemap, + }; } else if (target === 'umd') { - inputOptions = { input, plugins: plugins(command, pkg, { ...options, format: 'umd' }) } - outputOptions = { ...defaultOptions, file: browser, format: 'umd', name, sourcemap, globals } + inputOptions = { + input, + plugins: plugins(command, pkg, { ...options, format: 'umd' }), + }; + outputOptions = { + ...defaultOptions, + file: browser, + format: 'umd', + name, + sourcemap, + globals, + }; } - return { inputOptions, outputOptions } -} + return { inputOptions, outputOptions }; +}; const deleteDirs = async pkg => { - const dirs = {} - ;['main', 'module', 'browser'].map(type => pkg[type] && (dirs[dirname(pkg[type]) + '/*.js'] = true)) - await del(Object.keys(dirs)) -} + const dirs = {}; + ['main', 'module', 'browser'].map( + type => pkg[type] && (dirs[dirname(pkg[type]) + '/*.js'] = true) + ); + await del(Object.keys(dirs)); +}; const writeBundle = async (bundle, outputOptions) => { - await bundle.generate(outputOptions) - await bundle.write(outputOptions) -} + await bundle.generate(outputOptions); + await bundle.write(outputOptions); +}; const build = async (options, index, inputOptions) => { - let err, bundle + let err, bundle; try { - bundle = await rollup(inputOptions) + bundle = await rollup(inputOptions); } catch (e) { - err = e + err = e; } - err ? error(err) : await writeBundle(bundle, options) -} + err ? error(err) : await writeBundle(bundle, options); +}; const processWatcher = event => { switch (event.code) { case 'ERROR': - error(event.error) - break + error(event.error); + break; case 'END': - info(`${new Date().toLocaleTimeString('en-GB')} - Waiting for Changes...`) - break + info( + `${new Date().toLocaleTimeString('en-GB')} - Waiting for Changes...` + ); + break; } -} +}; const klap = async (command, pkg) => { - const options = getOptions(pkg) - await deleteDirs(pkg) - let config, watchOptions, watcher + const options = getOptions(pkg); + await deleteDirs(pkg); + let config, watchOptions, watcher; switch (command) { case 'build': - config = buildConfig(command, pkg, options) - config.outputOptions.map((opts, index) => build(opts, index, config.inputOptions[index])) - break + config = buildConfig(command, pkg, options); + config.outputOptions.map((opts, index) => + build(opts, index, config.inputOptions[index]) + ); + break; case 'watch': - config = buildConfig(command, pkg, options) + config = buildConfig(command, pkg, options); watchOptions = config.outputOptions.map((output, index) => ({ ...config.inputOptions[index], - output - })) - watcher = watch(watchOptions) - watcher.on('event', processWatcher) - break + output, + })); + watcher = watch(watchOptions); + watcher.on('event', processWatcher); + break; case 'start': - config = startConfig(command, pkg, options) + config = startConfig(command, pkg, options); watchOptions = { ...config.inputOptions, - output: config.outputOptions - } - watcher = watch(watchOptions) - watcher.on('event', processWatcher) - break + output: config.outputOptions, + }; + watcher = watch(watchOptions); + watcher.on('event', processWatcher); + break; } -} +}; // Experimental: Export internals to support extending parts of `klap` // Remove parts once we determine what we need in `tslib-cli` -export { babelConfig } from './babel' -export { init } from './init' -export { bold, error, gray, green, info, log, warn } from './logger' -export { getOptions } from './options' -export { servor } from './packages/servor' -export { sizeme } from './packages/sizeme' -export { terser } from './packages/terser' -export { plugins } from './plugins' -export { exists, read, safePackageName, write } from './utils' -export { klap } +export { babelConfig } from './babel'; +export { init } from './init'; +export { bold, error, gray, green, info, log, warn } from './logger'; +export { getOptions } from './options'; +export { servor } from './packages/servor'; +export { sizeme } from './packages/sizeme'; +export { terser } from './packages/terser'; +export { plugins } from './plugins'; +export { exists, read, safePackageName, write } from './utils'; +export { klap }; diff --git a/src/logger.js b/src/logger.js index fb7ecbe..f0c55c8 100644 --- a/src/logger.js +++ b/src/logger.js @@ -1,8 +1,8 @@ -import { bold, green, gray } from 'kleur' +import { bold, gray, green } from 'kleur'; -const error = message => console.error(bold().red(message)) -const warn = message => console.warn(bold().yellow(message)) -const info = message => console.error(green(message)) -const log = message => console.error(gray(message)) +const error = message => console.error(bold().red(message)); +const warn = message => console.warn(bold().yellow(message)); +const info = message => console.error(green(message)); +const log = message => console.error(gray(message)); -export { error, warn, info, log, gray, green, bold } +export { error, warn, info, log, gray, green, bold }; diff --git a/src/options.js b/src/options.js index 6113014..98379f7 100644 --- a/src/options.js +++ b/src/options.js @@ -1,8 +1,8 @@ -import getopts from 'getopts' -import { safePackageName } from './utils' +import getopts from 'getopts'; +import { safePackageName } from './utils'; const getOptions = pkg => { - const { klap = {}, source = 'src/index.js' } = pkg + const { klap = {}, source = 'src/index.js' } = pkg; const { name = pkg.name, port = 1234, @@ -16,7 +16,7 @@ const getOptions = pkg => { fallback = 'public/index.html', example = 'public/index.js', browserlist = '>1%, not ie 11, not op_mini all', - } = klap + } = klap; const opts = getopts(process.argv.slice(2), { boolean: ['sourcemap', 'minify'], string: ['pragma', 'frag'], @@ -42,8 +42,8 @@ const getOptions = pkg => { example, browserlist, }, - }) - return { ...opts, globals, namedExports } -} + }); + return { ...opts, globals, namedExports }; +}; -export { getOptions } +export { getOptions }; diff --git a/src/packages/servor.js b/src/packages/servor.js index 2dc1f5d..8b93472 100644 --- a/src/packages/servor.js +++ b/src/packages/servor.js @@ -1,20 +1,20 @@ /** * A rollup plugin to start `servor` as build step */ -import _servor from 'servor' -import { warn } from '../logger' +import _servor from 'servor'; +import { warn } from '../logger'; -let singleton = false +let singleton = false; export const servor = options => { return { name: 'servor', generateBundle() { if (!singleton) { - _servor({ silent: true, browse: false, ...options }) - warn(`Servor Listening on http://localhost:${options.port || 8080}`) - singleton = true + _servor({ silent: true, browse: false, ...options }); + warn(`Servor Listening on http://localhost:${options.port || 8080}`); + singleton = true; } }, - } -} + }; +}; diff --git a/src/packages/sizeme.js b/src/packages/sizeme.js index 53f339f..93b4df9 100644 --- a/src/packages/sizeme.js +++ b/src/packages/sizeme.js @@ -4,16 +4,16 @@ * - `rollup-plugin-filesize` does an internal terser pass for all the files * - This is little expensive, specially when we use `terser` to generate final output anyway. */ -import gzip from 'gzip-size' -import prettyBytes from 'pretty-bytes' -import { info } from '../logger' +import gzip from 'gzip-size'; +import prettyBytes from 'pretty-bytes'; +import { info } from '../logger'; export const sizeme = () => { const showSize = bundle => { - const { code, fileName } = bundle - const size = prettyBytes(gzip.sync(code)) - info(`\t${size}\t${fileName}`) - } + const { code, fileName } = bundle; + const size = prettyBytes(gzip.sync(code)); + info(`\t${size}\t${fileName}`); + }; return { name: 'sizeme', @@ -22,8 +22,8 @@ export const sizeme = () => { Object.keys(bundle) .map(file => bundle[file]) .filter(bundle => !bundle.isAsset) - .forEach(bundle => showSize(bundle)) + .forEach(bundle => showSize(bundle)); } }, - } -} + }; +}; diff --git a/src/packages/terser.js b/src/packages/terser.js index 5cdb5f8..d255ec6 100644 --- a/src/packages/terser.js +++ b/src/packages/terser.js @@ -5,32 +5,34 @@ * - `jest-worker` had issues with bundling approach we want to follow for `klap`. */ -import { codeFrameColumns } from '@babel/code-frame' -import { createFilter } from 'rollup-pluginutils' -import { minify } from 'terser' -import { warn, error } from '../logger' +import { codeFrameColumns } from '@babel/code-frame'; +import { createFilter } from 'rollup-pluginutils'; +import { minify } from 'terser'; +import { warn, error } from '../logger'; const transform = (code, options) => { - const result = minify(code, options) + const result = minify(code, options); if (result.error) { - throw result.error + throw result.error; } if (result.warnings) { - result.warnings.forEach(warning => warn(warning)) + result.warnings.forEach(warning => warn(warning)); } - return result -} + return result; +}; export const terser = (options = {}) => { - const filter = createFilter(options.include, options.exclude, { resolve: false }) + const filter = createFilter(options.include, options.exclude, { + resolve: false, + }); return { name: 'terser', renderChunk(code, chunk) { - if (!filter(chunk.fileName)) return null + if (!filter(chunk.fileName)) return null; - let result + let result; try { result = transform(code, { sourceMap: options.sourcemap, @@ -38,16 +40,16 @@ export const terser = (options = {}) => { toplevel: true, mangle: { properties: { regex: '^_' } }, compress: { passes: 10, pure_getters: true }, - }) + }); } catch (err) { - const { message, line, col: column } = err - error(codeFrameColumns(code, { start: { line, column } }, { message })) - throw err + const { message, line, col: column } = err; + error(codeFrameColumns(code, { start: { line, column } }, { message })); + throw err; } return { code: result.code, map: result.map, - } + }; }, - } -} + }; +}; diff --git a/src/plugins.js b/src/plugins.js index 1404525..7b4727a 100644 --- a/src/plugins.js +++ b/src/plugins.js @@ -1,35 +1,44 @@ -import json from 'rollup-plugin-json' -import nodeGlobals from 'rollup-plugin-node-globals' -import nodeResolve from 'rollup-plugin-node-resolve' -import commonjs from 'rollup-plugin-commonjs' -import babel from 'rollup-plugin-babel' -import replace from '@rollup/plugin-replace' -import sourcemaps from 'rollup-plugin-sourcemaps' +import json from 'rollup-plugin-json'; +import nodeGlobals from 'rollup-plugin-node-globals'; +import nodeResolve from 'rollup-plugin-node-resolve'; +import commonjs from 'rollup-plugin-commonjs'; +import babel from 'rollup-plugin-babel'; +import replace from '@rollup/plugin-replace'; +import sourcemaps from 'rollup-plugin-sourcemaps'; -import { terser } from './packages/terser' -import { sizeme } from './packages/sizeme' -import { servor } from './packages/servor' +import { terser } from './packages/terser'; +import { sizeme } from './packages/sizeme'; +import { servor } from './packages/servor'; -import { babelConfig } from './babel' +import { babelConfig } from './babel'; const plugins = (command, pkg, options) => { - const { extensions, presets, plugins } = babelConfig(command, pkg, options) - const { sourcemap, minify, fallback, port, namedExports } = options + const { extensions, presets, plugins } = babelConfig(command, pkg, options); + const { sourcemap, minify, fallback, port, namedExports } = options; - const babelDefaults = { babelrc: false, configFile: false, compact: false } + const babelDefaults = { babelrc: false, configFile: false, compact: false }; return [ sourcemap && sourcemaps(), json(), nodeGlobals(), - nodeResolve({ mainFields: ['module', 'jsnext:main', 'browser', 'main'], extensions }), + nodeResolve({ + mainFields: ['module', 'jsnext:main', 'browser', 'main'], + extensions, + }), commonjs({ extensions, include: /\/node_modules\//, namedExports }), - babel({ ...babelDefaults, exclude: 'node_modules/**', extensions, presets, plugins }), + babel({ + ...babelDefaults, + exclude: 'node_modules/**', + extensions, + presets, + plugins, + }), replace({ 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV) }), command !== 'start' && minify && terser({ sourcemap, warnings: false }), command !== 'start' && sizeme(), command === 'start' && servor({ fallback, port }), - ].filter(Boolean) -} + ].filter(Boolean); +}; -export { plugins } +export { plugins }; diff --git a/src/utils.js b/src/utils.js index a712680..14f30b6 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,19 +1,24 @@ -import mkdir from 'mkdirp' -import { dirname } from 'path' -import { promisify } from 'util' -import { exists as _exists, readFile as _readFile, writeFile as _writeFile } from 'fs' +import { execSync } from 'child_process'; +import { + exists as _exists, + readFile as _readFile, + writeFile as _writeFile, +} from 'fs'; +import mkdir from 'mkdirp'; +import { dirname } from 'path'; +import { promisify } from 'util'; -const readFile = promisify(_readFile) -const writeFile = promisify(_writeFile) -const exists = promisify(_exists) +const readFile = promisify(_readFile); +const writeFile = promisify(_writeFile); +const exists = promisify(_exists); -const read = async p => await readFile(p, 'utf-8') +const read = async p => await readFile(p, 'utf-8'); const write = async (p, d) => { - const dest = dirname(p) - if (!(await exists(dest))) mkdir.sync(dest) - await writeFile(p, d + '\n', 'utf-8') -} + const dest = dirname(p); + if (!(await exists(dest))) mkdir.sync(dest); + await writeFile(p, d + '\n', 'utf-8'); +}; const snakeToCamel = str => str.replace(/([-_][a-z])/g, group => @@ -21,8 +26,14 @@ const snakeToCamel = str => .toUpperCase() .replace('-', '') .replace('_', '') - ) + ); -const safePackageName = str => snakeToCamel(str.replace('@', '').replace('/', '.')) +const safePackageName = str => + snakeToCamel(str.replace('@', '').replace('/', '.')); -export { exists, read, write, safePackageName } +const trim = str => str.replace(/^\s|\s$/, ''); + +const exec = cmd => + trim(execSync(cmd, { stdio: ['ignore', 'pipe', 'ignore'] }).toString()); + +export { exists, read, write, safePackageName, exec }; diff --git a/yarn.lock b/yarn.lock index 22e4a59..fe72670 100644 --- a/yarn.lock +++ b/yarn.lock @@ -881,17 +881,17 @@ dependencies: "@types/node" ">= 8" -"@osdevisnot/prettier@3.0.1": - version "3.0.1" - resolved "https://registry.yarnpkg.com/@osdevisnot/prettier/-/prettier-3.0.1.tgz#0234c44e610dc1b5bec5d5e6d5598912a8623123" - integrity sha512-n225PgKkbrnXhb/1kCiY11jngGOopzSn6bAyllkTXOCkdtlJrFBTCKHDIE3g+iqeL3Qac+And5Eiw++kTCmwmA== +"@osdevisnot/prettier@4.0.0": + version "4.0.0" + resolved "https://registry.yarnpkg.com/@osdevisnot/prettier/-/prettier-4.0.0.tgz#fc2c7b9e888535d55ad8ed116fcdc7a7d54580fd" + integrity sha512-PoPemdwYPWCZbXu/+nDOLXhr68r2idbpWUQ4JoAEKk1LuCwaUGnOEM06CYGye9eZGk1ovj+oQftMBuPGIcWZMg== dependencies: prettier "*" -"@rollup/plugin-replace@2.2.1": - version "2.2.1" - resolved "https://registry.yarnpkg.com/@rollup/plugin-replace/-/plugin-replace-2.2.1.tgz#94af20cb3d70cccdcec991d1f97dd373936ec544" - integrity sha512-dgq5ijT8fK18KTb1inenZ61ivTayV7pvbz2+ivT+VN20BOgJVM1fqoBETqGHKgFVm/J9BhR82mQyAtxfpPv1lQ== +"@rollup/plugin-replace@2.3.0": + version "2.3.0" + resolved "https://registry.yarnpkg.com/@rollup/plugin-replace/-/plugin-replace-2.3.0.tgz#86d88746383e40dd81cffb5216449cc51a734eb9" + integrity sha512-rzWAMqXAHC1w3eKpK6LxRqiF4f3qVFaa1sGii6Bp3rluKcwHNOpPt+hWRCmAH6SDEPtbPiLFf0pfNQyHs6Btlg== dependencies: magic-string "^0.25.2" rollup-pluginutils "^2.6.0" @@ -5737,10 +5737,10 @@ rollup-pluginutils@2.8.2, rollup-pluginutils@^2.0.1, rollup-pluginutils@^2.3.1, dependencies: estree-walker "^0.6.1" -rollup@1.27.13: - version "1.27.13" - resolved "https://registry.yarnpkg.com/rollup/-/rollup-1.27.13.tgz#d6d3500512daacbf8de54d2800de62d893085b90" - integrity sha512-hDi7M07MpmNSDE8YVwGVFA8L7n8jTLJ4lG65nMAijAyqBe//rtu4JdxjUBE7JqXfdpqxqDTbCDys9WcqdpsQvw== +rollup@1.27.14: + version "1.27.14" + resolved "https://registry.yarnpkg.com/rollup/-/rollup-1.27.14.tgz#940718d5eec1a6887e399aa0089944bae5c4f377" + integrity sha512-DuDjEyn8Y79ALYXMt+nH/EI58L5pEw5HU9K38xXdRnxQhvzUTI/nxAawhkAHUQeudANQ//8iyrhVRHJBuR6DSQ== dependencies: "@types/estree" "*" "@types/node" "*" @@ -5794,10 +5794,10 @@ safe-regex@^1.1.0: resolved "https://registry.yarnpkg.com/safer-buffer/-/safer-buffer-2.1.2.tgz#44fa161b0187b9549dd84bb91802f9bd8385cd6a" integrity sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg== -semantic-release@15.13.31: - version "15.13.31" - resolved "https://registry.yarnpkg.com/semantic-release/-/semantic-release-15.13.31.tgz#75560e61562b5b7aa3e66256fd3454683c6508a6" - integrity sha512-mrtYkH4p0FvXIRFCsr2r5il/A+Uj7oeeq+dgyojAbr4Tzywv9AlCYHeE3A8U3eE4bMJPiBV4YnQRsk1QS8yDDw== +semantic-release@15.14.0: + version "15.14.0" + resolved "https://registry.yarnpkg.com/semantic-release/-/semantic-release-15.14.0.tgz#6ee79b7b3598332378190412880049709fa23376" + integrity sha512-Cn43W35AOLY0RMcDbtwhJODJmWg6YCs1+R5jRQsTmmkEGzkV4B2F/QXkjVZpl4UbH91r93GGH0xhoq9kh7I5PA== dependencies: "@semantic-release/commit-analyzer" "^6.1.0" "@semantic-release/error" "^2.2.0"