screw-up 0.2.0 → 0.4.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.
package/README.md CHANGED
@@ -10,7 +10,7 @@ Simply package metadata inserter for Vite plugins.
10
10
 
11
11
  ## What is this?
12
12
 
13
- This is a Vite plugin that automatically inserts banner comments containing package metadata (name, version, description, author, license, etc.) into your bundled JavaScript/CSS files.
13
+ This is a Vite plugin that automatically inserts banner comments containing package metadata (name, version, description, author, license, etc.) into your bundled files.
14
14
 
15
15
  This will automatically read metadata from your `package.json`:
16
16
 
@@ -65,7 +65,7 @@ Add the plugin to your `vite.config.ts`:
65
65
 
66
66
  ```typescript
67
67
  import { defineConfig } from 'vite';
68
- import { screwUp } from 'screw-up';
68
+ import screwUp from 'screw-up';
69
69
 
70
70
  export default defineConfig({
71
71
  plugins: [
@@ -81,13 +81,16 @@ export default defineConfig({
81
81
  });
82
82
  ```
83
83
 
84
+ When no `outputKeys` are specified, the plugin uses these default keys with exact sequence:
85
+ `name`, `version`, `description`, `author`, `license` and `repository.url`.
86
+
84
87
  ### Custom Output Keys
85
88
 
86
89
  You can specify which metadata fields to include and in what order:
87
90
 
88
91
  ```typescript
89
92
  import { defineConfig } from 'vite';
90
- import { screwUp } from 'screw-up';
93
+ import screwUp from 'screw-up';
91
94
 
92
95
  export default defineConfig({
93
96
  plugins: [
@@ -115,14 +118,6 @@ This will generate a banner with only the specified fields:
115
118
  */
116
119
  ```
117
120
 
118
- #### Default Output Keys
119
-
120
- When no `outputKeys` are specified, the plugin uses these default keys:
121
-
122
- ```typescript
123
- ['name', 'version', 'description', 'author', 'license', 'repository.url']
124
- ```
125
-
126
121
  ### Working with Nested Objects
127
122
 
128
123
  The plugin automatically flattens nested objects using dot notation:
package/dist/index.cjs CHANGED
@@ -1,8 +1,7 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const fs = require("fs");
4
2
  const promises = require("fs/promises");
5
3
  const path = require("path");
4
+ const fs = require("fs");
6
5
  const flattenObject = (obj, prefix = "", map) => {
7
6
  for (const [key, value] of Object.entries(obj)) {
8
7
  if (!value)
@@ -94,7 +93,11 @@ const generateBanner = (metadata, outputKeys) => {
94
93
  */` : "";
95
94
  };
96
95
  const screwUp = (options = {}) => {
97
- const { outputKeys = ["name", "version", "description", "author", "license", "repository.url"] } = options;
96
+ const {
97
+ outputKeys = ["name", "version", "description", "author", "license", "repository.url"],
98
+ assetFilters = ["\\.d\\.ts$"]
99
+ } = options;
100
+ const assetFiltersRegex = assetFilters.map((filter) => new RegExp(filter));
98
101
  let banner;
99
102
  return {
100
103
  name: "screw-up",
@@ -108,9 +111,32 @@ const screwUp = (options = {}) => {
108
111
  const chunk = bundle[fileName];
109
112
  if (chunk.type === "chunk") {
110
113
  chunk.code = banner + "\n" + chunk.code;
114
+ } else if (chunk.type === "asset" && assetFiltersRegex.some((filter) => filter.test(fileName))) {
115
+ if (typeof chunk.source === "string") {
116
+ chunk.source = banner + "\n\n" + chunk.source;
117
+ }
111
118
  }
112
119
  }
120
+ },
121
+ async writeBundle(options2) {
122
+ if (!options2.dir) return;
123
+ try {
124
+ const files = await promises.readdir(options2.dir, { recursive: true });
125
+ for (const file of files) {
126
+ const filePath = path.join(options2.dir, file);
127
+ if (assetFiltersRegex.some((filter) => filter.test(file))) {
128
+ try {
129
+ const content = await promises.readFile(filePath, "utf-8");
130
+ if (!content.includes(banner)) {
131
+ await promises.writeFile(filePath, banner + "\n\n" + content);
132
+ }
133
+ } catch (error) {
134
+ }
135
+ }
136
+ }
137
+ } catch (error) {
138
+ }
113
139
  }
114
140
  };
115
141
  };
116
- exports.screwUp = screwUp;
142
+ module.exports = screwUp;
package/dist/index.d.ts CHANGED
@@ -1,16 +1,25 @@
1
1
  import { Plugin } from 'vite';
2
2
 
3
+ /**
4
+ * screw-up options
5
+ */
3
6
  export interface ScrewUpOptions {
4
7
  /**
5
8
  * Array of keys to output in banner in the specified order
6
9
  * @default ['name', 'version', 'description', 'author', 'license', 'repository.url']
7
10
  */
8
11
  outputKeys?: string[];
12
+ /**
13
+ * Array of asset file regex to add banner to
14
+ * @default ['\.d\.ts$']
15
+ */
16
+ assetFilters?: string[];
9
17
  }
10
18
  /**
11
19
  * Vite plugin that adds banner to the bundled code
12
20
  * @param options - Plugin options
13
21
  * @returns Vite plugin
14
22
  */
15
- export declare const screwUp: (options?: ScrewUpOptions) => Plugin;
23
+ declare const screwUp: (options?: ScrewUpOptions) => Plugin;
24
+ export default screwUp;
16
25
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAGnC,MAAM,WAAW,cAAc;IAC7B;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,EAAE,CAAC;CACvB;AAED;;;;GAIG;AACH,eAAO,MAAM,OAAO,GAAI,UAAS,cAAmB,KAAG,MAqBtD,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAKnC;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,EAAE,CAAC;IACtB;;;OAGG;IACH,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;CACzB;AAED;;;;GAIG;AACH,QAAA,MAAM,OAAO,GAAI,UAAS,cAAmB,KAAG,MA2D/C,CAAC;AAEF,eAAe,OAAO,CAAC"}
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
- import { existsSync } from "fs";
2
- import { readFile } from "fs/promises";
1
+ import { readFile, readdir, writeFile } from "fs/promises";
3
2
  import { join, dirname } from "path";
3
+ import { existsSync } from "fs";
4
4
  const flattenObject = (obj, prefix = "", map) => {
5
5
  for (const [key, value] of Object.entries(obj)) {
6
6
  if (!value)
@@ -92,7 +92,11 @@ const generateBanner = (metadata, outputKeys) => {
92
92
  */` : "";
93
93
  };
94
94
  const screwUp = (options = {}) => {
95
- const { outputKeys = ["name", "version", "description", "author", "license", "repository.url"] } = options;
95
+ const {
96
+ outputKeys = ["name", "version", "description", "author", "license", "repository.url"],
97
+ assetFilters = ["\\.d\\.ts$"]
98
+ } = options;
99
+ const assetFiltersRegex = assetFilters.map((filter) => new RegExp(filter));
96
100
  let banner;
97
101
  return {
98
102
  name: "screw-up",
@@ -106,11 +110,34 @@ const screwUp = (options = {}) => {
106
110
  const chunk = bundle[fileName];
107
111
  if (chunk.type === "chunk") {
108
112
  chunk.code = banner + "\n" + chunk.code;
113
+ } else if (chunk.type === "asset" && assetFiltersRegex.some((filter) => filter.test(fileName))) {
114
+ if (typeof chunk.source === "string") {
115
+ chunk.source = banner + "\n\n" + chunk.source;
116
+ }
109
117
  }
110
118
  }
119
+ },
120
+ async writeBundle(options2) {
121
+ if (!options2.dir) return;
122
+ try {
123
+ const files = await readdir(options2.dir, { recursive: true });
124
+ for (const file of files) {
125
+ const filePath = join(options2.dir, file);
126
+ if (assetFiltersRegex.some((filter) => filter.test(file))) {
127
+ try {
128
+ const content = await readFile(filePath, "utf-8");
129
+ if (!content.includes(banner)) {
130
+ await writeFile(filePath, banner + "\n\n" + content);
131
+ }
132
+ } catch (error) {
133
+ }
134
+ }
135
+ }
136
+ } catch (error) {
137
+ }
111
138
  }
112
139
  };
113
140
  };
114
141
  export {
115
- screwUp
142
+ screwUp as default
116
143
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "screw-up",
3
- "version": "0.2.0",
3
+ "version": "0.4.1",
4
4
  "description": "Simply package metadata inserter on Vite plugin",
5
5
  "keywords": [
6
6
  "vite",
@@ -35,12 +35,11 @@
35
35
  "test": "rv --npm . && tsc --noEmit && vitest run"
36
36
  },
37
37
  "dependencies": {
38
- "glob": "^11.0.3"
38
+ "vite": "^5.0.0"
39
39
  },
40
40
  "devDependencies": {
41
41
  "@types/node": "^20.0.0",
42
42
  "typescript": "^5.0.0",
43
- "vite": "^5.0.0",
44
43
  "vite-plugin-dts": "^3.0.0",
45
44
  "vitest": "^1.0.0"
46
45
  }