@reflex-stack/tsp 0.1.11 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. package/README.md +40 -45
  2. package/package.json +5 -5
  3. package/src/cli.js +55 -58
  4. package/src/commands/init.js +65 -28
  5. package/src/commands/size-report.js +29 -34
  6. package/src/config.js +0 -2
  7. package/src/tests.js +1 -4
  8. package/src/utils.js +5 -3
  9. package/tests/example-package/LICENCE +0 -21
  10. package/tests/example-package/README.md +0 -31
  11. package/tests/example-package/dist/common-dep.d.ts +0 -1
  12. package/tests/example-package/dist/common-dep.js +0 -1
  13. package/tests/example-package/dist/index.d.ts +0 -2
  14. package/tests/example-package/dist/index.js +0 -2
  15. package/tests/example-package/dist/root-dep.d.ts +0 -1
  16. package/tests/example-package/dist/root-dep.js +0 -1
  17. package/tests/example-package/dist/stuff.d.ts +0 -1
  18. package/tests/example-package/dist/stuff.js +0 -5
  19. package/tests/example-package/dist/submodule/index.d.ts +0 -1
  20. package/tests/example-package/dist/submodule/index.js +0 -5
  21. package/tests/example-package/dist/submodule/submodule-dep.d.ts +0 -1
  22. package/tests/example-package/dist/submodule/submodule-dep.js +0 -1
  23. package/tests/example-package/package-lock.json +0 -1061
  24. package/tests/example-package/package.json +0 -47
  25. package/tests/example-package/reports/main-dark.svg +0 -1
  26. package/tests/example-package/reports/main-light.svg +0 -1
  27. package/tests/example-package/reports/submodule-dark.svg +0 -1
  28. package/tests/example-package/reports/submodule-light.svg +0 -1
  29. package/tests/example-package/reports/total-dark.svg +0 -1
  30. package/tests/example-package/reports/total-light.svg +0 -1
  31. package/tests/example-package/tests/test.js +0 -25
  32. package/tests/example-package/tests/tsconfig.json +0 -5
  33. package/tests/example-package/tsconfig.json +0 -36
@@ -98,51 +98,46 @@ export async function sizeReport ( bundles, config ) {
98
98
  return allBundleSizes
99
99
  }
100
100
 
101
- export async function cleanSizeReports ( config ) {
102
- const dir = new Directory( join(config.cwd, config.reports) )
103
- await dir.ensureParents()
104
- await dir.clean()
105
- }
106
-
107
101
  export function generateJSON ( sizeReport, config ) {
108
102
  writeFileSync(
109
- join(config.cwd, config.reports, 'size-report.json'),
103
+ join(config.cwd, 'bundle-sizes.json'),
110
104
  JSON.stringify( sizeReport ),
111
105
  { encoding: 'utf-8' }
112
106
  )
113
107
  }
114
108
 
115
- async function generateTextSVG ( filePath, scheme, text ) {
116
- const svgBitFile = new File( filePath );
117
- const fontSize = 14
118
- const height = fontSize + 1
119
- const width = text.length * fontSize * 0.6 + 1
120
- svgBitFile.content( () => [
121
- `<svg width="${width}" height="${height}" xmlns="http://www.w3.org/2000/svg">`,
122
- `<style> text { fill: ${scheme === "dark" ? "white" : "black" }; font-family: Consolas, Monaco, "Lucida Console", monospace; font-size: ${fontSize}px; }</style>`,
123
- `<text y="${height - 1}">${text}</text>`,
124
- `</svg>`,
125
- ].join(""))
126
- await svgBitFile.ensureParents()
127
- await svgBitFile.save();
109
+ async function replaceBundleSizeInFile( filePath, id, size ) {
110
+ // Open file and replace tags like <bundle-size id="{id}">{size}</bundle-size>
111
+ // If you find any tag with any size in it, with this ID, replace the size with the new from arguments
112
+ // For ex: ("README.md", "main", 500b)
113
+ // Before: <bundle-size id="main">200b</bundle-size>
114
+ // After: <bundle-size id="main">500b</bundle-size>
115
+ // Should do it for all instances in file. Can be done sync if possible.
116
+ const file = new File( filePath )
117
+ await file.load()
118
+ const escapedId = id.replace( /[.*+?^${}()|[\]\\]/g, '\\$&' )
119
+ const regex = new RegExp( `<bundle-size id="${ escapedId }">.*?</bundle-size>`, 'g' )
120
+ file.content( ( content ) => {
121
+ return content.replaceAll( regex, `<bundle-size id="${ id }">${ size }</bundle-size>` )
122
+ })
123
+ await file.save()
128
124
  }
129
125
 
130
- export async function generateSVGs ( sizeReport, config ) {
126
+ export async function replaceBundleSizes ( sizeReport, config ) {
127
+ const files = ["README.md"] // fixme add config
131
128
  let total = 0
132
- for ( const bundle of sizeReport ) {
133
- const size = bundle.sizes[2]
134
- total += size
135
- const sizeContent = naiveHumanFileSize( size )
136
- for ( const scheme of ["light", "dark"] ) {
137
- const filePath = join( config.cwd, config.reports, `${bundle.name}-${scheme}.svg` )
138
- await generateTextSVG( filePath, scheme, sizeContent )
129
+ for (const file of files) {
130
+ // Per bundle size
131
+ for ( const bundle of sizeReport ) {
132
+ const size = bundle.sizes[2]
133
+ total += size
134
+ const sizeContent = naiveHumanFileSize( size )
135
+ await replaceBundleSizeInFile(file, bundle.name, sizeContent)
139
136
  }
140
- }
141
- if ( sizeReport.length > 1 ) {
142
- const sizeContent = naiveHumanFileSize( total )
143
- for ( const scheme of ["light", "dark"] ) {
144
- const filePath = join( config.cwd, config.reports, `total-${scheme}.svg` )
145
- await generateTextSVG( filePath, scheme, sizeContent )
137
+ // Total size
138
+ if ( sizeReport.length > 1 ) {
139
+ const sizeContent = naiveHumanFileSize( total )
140
+ await replaceBundleSizeInFile(file, "total", sizeContent)
146
141
  }
147
142
  }
148
143
  }
package/src/config.js CHANGED
@@ -9,9 +9,7 @@ export let getConfig = (userPackage) => {
9
9
  tests: './tests',
10
10
  "test-files": ['test.js'],
11
11
  tmp: './tmp',
12
- reports: './reports',
13
12
  'generate-json-report': true,
14
- 'generate-svg-report': true,
15
13
  // Override with package config
16
14
  ...((userPackage ?? {})['tsp'] ?? {})
17
15
  }
package/src/tests.js CHANGED
@@ -1,5 +1,4 @@
1
1
 
2
-
3
2
  let total = 0
4
3
  let success = 0
5
4
  let failed = 0
@@ -14,14 +13,12 @@ export function startTest () {
14
13
  }
15
14
  }
16
15
 
17
-
18
-
19
16
  export const describe = ( name, fn ) => {
20
17
  console.log(`${ name }`)
21
18
  fn()
22
19
  }
23
20
 
24
- export const it = ( name, fn ) => {
21
+ export const test = ( name, fn ) => {
25
22
  ++total
26
23
  try {
27
24
  fn()
package/src/utils.js CHANGED
@@ -45,9 +45,11 @@ export function showIntroMessage ( noThrow = false ) {
45
45
  * Very naive implementation.
46
46
  */
47
47
  export function naiveHumanFileSize ( size ) {
48
- if ( size > 1000 ) // is it base 10 or ^2 ?
49
- size = ~~(size / 10) / 100 + 'k'
50
- return size + 'b'
48
+ if ( size >= 1000000 )
49
+ size = ~~( size / 10000 ) / 100 + 'mb'
50
+ else if ( size >= 1000 )
51
+ size = ~~( size / 10 ) / 100 + 'kb'
52
+ return size + 'b'
51
53
  }
52
54
 
53
55
  // Get git remote repo URL if in a git repo
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2022 Alexis Bouhet
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
@@ -1,31 +0,0 @@
1
- # TSP Example
2
-
3
- ## This is a test package for @reflex-stack/tsp, do not use it
4
-
5
- Main bundle is <picture style="display: inline-block"><source media="(prefers-color-scheme: dark)" srcset="./reports/main-dark.svg"><img src="./reports/main-light.svg"></picture>
6
- and optional submodule is only <picture style="display: inline-block"><source media="(prefers-color-scheme: dark)" srcset="./reports/submodule-dark.svg"><img src="./reports/submodule-light.svg"></picture>
7
-
8
- ## Install
9
-
10
- `npm i @reflex-stack/tsp-example`
11
-
12
- ## Usage
13
-
14
- ##### Main module
15
- - `import { ... } from "{{ packageNPMName }}"`
16
-
17
- ##### Sub module
18
- - `import { ... } from "{{ packageNPMName }}/submodule"`
19
-
20
- ## Build commands
21
-
22
- ##### Build
23
- - `npm run build`
24
- ##### Test
25
- - `npm run test`
26
- ##### Publish
27
- - `npm run publish`
28
-
29
- ---
30
- ## TSP
31
- This package has been created with [tsp](https://github.com/reflex-stack/tsp)
@@ -1 +0,0 @@
1
- export declare const commonDep = "common dependency";
@@ -1 +0,0 @@
1
- export const commonDep = "common dependency";
@@ -1,2 +0,0 @@
1
- export * from "./root-dep.js";
2
- export { doStuff } from "./stuff.js";
@@ -1,2 +0,0 @@
1
- export * from "./root-dep.js";
2
- export { doStuff } from "./stuff.js";
@@ -1 +0,0 @@
1
- export declare const rootDep = "root dependency";
@@ -1 +0,0 @@
1
- export const rootDep = "root dependency";
@@ -1 +0,0 @@
1
- export declare function doStuff(): string;
@@ -1,5 +0,0 @@
1
- import { commonDep } from "./common-dep.js";
2
- export function doStuff() {
3
- console.log("DO STUFFF");
4
- return `Do stuff ${commonDep}`;
5
- }
@@ -1 +0,0 @@
1
- export declare function doSubmoduleStuff(): string;
@@ -1,5 +0,0 @@
1
- import { submoduleDep } from "./submodule-dep.js";
2
- import { commonDep } from "../common-dep.js";
3
- export function doSubmoduleStuff() {
4
- return `Do submodule stuff ${submoduleDep} ${commonDep}`;
5
- }
@@ -1 +0,0 @@
1
- export declare const submoduleDep = "submodule dependency";
@@ -1 +0,0 @@
1
- export const submoduleDep = "submodule dependency";