@dpuse/dpuse-development 0.3.812 → 0.3.813

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.
@@ -0,0 +1,12 @@
1
+ import { Plugin } from 'vite';
2
+ /** What a build ships: the installed packages whose code, styles or assets are in its output, as 'name@version', and the
3
+ * packages its output still imports by name, which whoever installs it also receives. */
4
+ export interface ShippedPackagesRecord {
5
+ packages: string[];
6
+ external: string[];
7
+ }
8
+ export declare const SHIPPED_PACKAGES_FILE_NAME = "shipped-packages.json";
9
+ /** Records which installed packages a build ships, read from the bundler's own list of what it put in each output file.
10
+ * Modules loaded at run time by URL are not part of the build, so are not recorded; each reports its own. A bundled
11
+ * package that publishes its own record, as DPUse packages do, adds the packages bundled inside it. */
12
+ export declare function recordShippedPackages(): Plugin;
@@ -0,0 +1,83 @@
1
+ import { existsSync as e, mkdirSync as t, readFileSync as n, writeFileSync as r } from "node:fs";
2
+ import i from "node:path";
3
+ //#region src/vite.ts
4
+ var a = "shipped-packages.json", o = "bundle-analysis-reports", s = /* @__PURE__ */ new Map();
5
+ function c() {
6
+ let e;
7
+ return {
8
+ name: "dpuse-record-shipped-packages",
9
+ apply: "build",
10
+ configResolved(t) {
11
+ e = t;
12
+ },
13
+ generateBundle(t, n) {
14
+ if (e === void 0) return;
15
+ let r = s.get(e.root) ?? {
16
+ packages: /* @__PURE__ */ new Set(),
17
+ external: /* @__PURE__ */ new Set()
18
+ };
19
+ s.set(e.root, r);
20
+ let i = new Set(Object.keys(n));
21
+ for (let t of Object.values(n)) t.type === "chunk" ? l(r, t, i) : u(r, t, e.root);
22
+ },
23
+ closeBundle() {
24
+ if (e === void 0) return;
25
+ let t = s.get(e.root);
26
+ t !== void 0 && p(t, [i.resolve(e.root, o), i.resolve(e.root, e.build.outDir)]);
27
+ }
28
+ };
29
+ }
30
+ function l(e, t, n) {
31
+ let r = [...Object.keys(t.modules), ...t.moduleIds ?? []];
32
+ for (let t of r) d(e, t);
33
+ let i = [...t.imports, ...t.dynamicImports].filter((e) => !n.has(e));
34
+ for (let t of i) {
35
+ let n = h(t);
36
+ n !== void 0 && e.external.add(n);
37
+ }
38
+ }
39
+ function u(e, t, n) {
40
+ let r = t.originalFileNames ?? [];
41
+ for (let t of r) d(e, i.resolve(n, t));
42
+ }
43
+ function d(t, r) {
44
+ let o = f(r);
45
+ if (o === void 0) return;
46
+ let s = JSON.parse(n(i.join(o, "package.json"), "utf-8"));
47
+ if (s.name === void 0 || s.version === void 0) return;
48
+ let c = `${s.name}@${s.version}`;
49
+ if (t.packages.has(c)) return;
50
+ t.packages.add(c);
51
+ let l = i.join(o, "dist", a);
52
+ if (!e(l)) return;
53
+ let { packages: u = [], external: d = [] } = JSON.parse(n(l, "utf-8"));
54
+ for (let e of u) t.packages.add(e);
55
+ for (let e of d) t.external.add(e);
56
+ }
57
+ function f(t) {
58
+ let r = t.replace(/^\0/, "").split("?", 1)[0] ?? "", a = r.lastIndexOf(`${i.sep}node_modules${i.sep}`);
59
+ if (a === -1) return;
60
+ let o = i.dirname(r);
61
+ for (; o.length > a + 12 + 1;) {
62
+ let t = i.join(o, "package.json");
63
+ if (e(t) && JSON.parse(n(t, "utf-8")).name !== void 0) return o;
64
+ o = i.dirname(o);
65
+ }
66
+ }
67
+ function p(e, n) {
68
+ let o = {
69
+ packages: [...e.packages].toSorted(m),
70
+ external: [...e.external].toSorted(m)
71
+ }, s = `${JSON.stringify(o, void 0, 4)}\n`;
72
+ for (let e of n) t(e, { recursive: !0 }), r(i.join(e, a), s, "utf-8");
73
+ }
74
+ function m(e, t) {
75
+ return e.localeCompare(t, "en");
76
+ }
77
+ function h(e) {
78
+ if (e.startsWith(".") || e.startsWith("/") || e.startsWith("node:") || /^[a-z]+:\/\//i.test(e)) return;
79
+ let t = e.split("/", 2);
80
+ return e.startsWith("@") ? t.join("/") : t[0];
81
+ }
82
+ //#endregion
83
+ export { a as SHIPPED_PACKAGES_FILE_NAME, c as recordShippedPackages };
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vite.es.js","names":["path","SHIPPED_PACKAGES_FILE_NAME","REPORT_DIRECTORY","collectionsByRoot","recordShippedPackages","config","resolvedConfig","collection","outputFileNames","bundle","output","collectChunk","collectAsset","writeRecord","moduleIds","chunk","moduleId","importedNames","imported","packageName","extractPackageName","originalFileNames","asset","originalFileName","root","addPackage","packageDirectory","findPackageDirectory","packageJSON","readFileSync","packageKey","nestedRecordPath","existsSync","nestedPackages","nestedExternal","nestedKey","nestedName","filePath","nodeModulesIndex","directory","packageJSONPath","record","compareNames","content","directories","mkdirSync","writeFileSync","a","b","specifier","parts"],"sources":["../src/vite.ts"],"sourcesContent":["// ── External Dependencies & Registrations\nimport path from 'node:path';\nimport { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';\nimport type { Plugin, ResolvedConfig } from 'vite';\n\n// ── Types ────────────────────────────────────────────────────────────────────────────────────────────────────────────\n\n/** What a build ships: the installed packages whose code, styles or assets are in its output, as 'name@version', and the\n * packages its output still imports by name, which whoever installs it also receives. */\nexport interface ShippedPackagesRecord {\n packages: string[];\n external: string[];\n}\n\ninterface ShippedPackagesCollection {\n packages: Set<string>;\n external: Set<string>;\n}\n\ninterface OutputChunk {\n type: 'chunk';\n modules: Record<string, unknown>;\n moduleIds?: string[];\n imports: string[];\n dynamicImports: string[];\n}\n\ninterface OutputAsset {\n type: 'asset';\n originalFileNames?: string[];\n}\n\n// ── Constants ────────────────────────────────────────────────────────────────────────────────────────────────────────\n\nexport const SHIPPED_PACKAGES_FILE_NAME = 'shipped-packages.json';\nconst REPORT_DIRECTORY = 'bundle-analysis-reports';\n\n// ── State ────────────────────────────────────────────────────────────────────────────────────────────────────────────\n\n// A build can run more than once in a process, such as for a web worker, so what each finds is merged per project.\nconst collectionsByRoot = new Map<string, ShippedPackagesCollection>();\n\n// ── Actions ──────────────────────────────────────────────────────────────────────────────────────────────────────────\n\n/** Records which installed packages a build ships, read from the bundler's own list of what it put in each output file.\n * Modules loaded at run time by URL are not part of the build, so are not recorded; each reports its own. A bundled\n * package that publishes its own record, as DPUse packages do, adds the packages bundled inside it. */\nexport function recordShippedPackages(): Plugin {\n let config: ResolvedConfig | undefined;\n\n return {\n name: 'dpuse-record-shipped-packages',\n apply: 'build',\n configResolved(resolvedConfig) {\n config = resolvedConfig;\n },\n generateBundle(_options, bundle) {\n if (config === undefined) return;\n const collection = collectionsByRoot.get(config.root) ?? { packages: new Set<string>(), external: new Set<string>() };\n collectionsByRoot.set(config.root, collection);\n const outputFileNames = new Set(Object.keys(bundle));\n for (const output of Object.values(bundle) as (OutputChunk | OutputAsset)[]) {\n if (output.type === 'chunk') collectChunk(collection, output, outputFileNames);\n else collectAsset(collection, output, config.root);\n }\n },\n closeBundle() {\n if (config === undefined) return;\n const collection = collectionsByRoot.get(config.root);\n if (collection !== undefined) writeRecord(collection, [path.resolve(config.root, REPORT_DIRECTORY), path.resolve(config.root, config.build.outDir)]);\n }\n };\n}\n\n// ── Helpers ──────────────────────────────────────────────────────────────────────────────────────────────────────────\n\nfunction collectChunk(collection: ShippedPackagesCollection, chunk: OutputChunk, outputFileNames: Set<string>): void {\n const moduleIds = [...Object.keys(chunk.modules), ...(chunk.moduleIds ?? [])];\n for (const moduleId of moduleIds) addPackage(collection, moduleId);\n\n // An import of something that is not one of this build's own files is left for the package's user to install.\n const importedNames = [...chunk.imports, ...chunk.dynamicImports].filter((imported) => !outputFileNames.has(imported));\n for (const imported of importedNames) {\n const packageName = extractPackageName(imported);\n if (packageName !== undefined) collection.external.add(packageName);\n }\n}\n\nfunction collectAsset(collection: ShippedPackagesCollection, asset: OutputAsset, root: string): void {\n const originalFileNames = asset.originalFileNames ?? [];\n for (const originalFileName of originalFileNames) addPackage(collection, path.resolve(root, originalFileName));\n}\n\n/* eslint-disable security/detect-non-literal-fs-filename -- Paths come from the build's own module list and config, not user input. */\n\n// Adds the package a module or asset belongs to, and anything bundled inside that package according to its own record.\nfunction addPackage(collection: ShippedPackagesCollection, moduleId: string): void {\n const packageDirectory = findPackageDirectory(moduleId);\n if (packageDirectory === undefined) return;\n const packageJSON = JSON.parse(readFileSync(path.join(packageDirectory, 'package.json'), 'utf-8')) as { name?: string; version?: string };\n if (packageJSON.name === undefined || packageJSON.version === undefined) return;\n const packageKey = `${packageJSON.name}@${packageJSON.version}`;\n if (collection.packages.has(packageKey)) return;\n collection.packages.add(packageKey);\n\n const nestedRecordPath = path.join(packageDirectory, 'dist', SHIPPED_PACKAGES_FILE_NAME);\n if (!existsSync(nestedRecordPath)) return;\n const { packages: nestedPackages = [], external: nestedExternal = [] } = JSON.parse(readFileSync(nestedRecordPath, 'utf-8')) as Partial<ShippedPackagesRecord>;\n for (const nestedKey of nestedPackages) collection.packages.add(nestedKey);\n for (const nestedName of nestedExternal) collection.external.add(nestedName);\n}\n\n// The folder of the installed package holding a file: the nearest folder with a named package.json, inside the last\n// 'node_modules' on the path. Files outside 'node_modules', such as the project's own source, belong to no package.\nfunction findPackageDirectory(moduleId: string): string | undefined {\n const filePath = moduleId.replace(/^\\0/, '').split('?', 1)[0] ?? '';\n const nodeModulesIndex = filePath.lastIndexOf(`${path.sep}node_modules${path.sep}`);\n if (nodeModulesIndex === -1) return undefined;\n let directory = path.dirname(filePath);\n while (directory.length > nodeModulesIndex + 'node_modules'.length + 1) {\n const packageJSONPath = path.join(directory, 'package.json');\n if (existsSync(packageJSONPath) && (JSON.parse(readFileSync(packageJSONPath, 'utf-8')) as { name?: string }).name !== undefined) return directory;\n directory = path.dirname(directory);\n }\n return undefined;\n}\n\n// Written beside the other bundle reports, where 'npm run document' reads it, and into the output, so it is published with\n// the package and a build that bundles this package can add what is inside it.\nfunction writeRecord(collection: ShippedPackagesCollection, directories: string[]): void {\n const record: ShippedPackagesRecord = { packages: [...collection.packages].toSorted(compareNames), external: [...collection.external].toSorted(compareNames) };\n const content = `${JSON.stringify(record, undefined, 4)}\\n`;\n for (const directory of directories) {\n mkdirSync(directory, { recursive: true });\n writeFileSync(path.join(directory, SHIPPED_PACKAGES_FILE_NAME), content, 'utf-8');\n }\n}\n\n/* eslint-enable security/detect-non-literal-fs-filename -- End of the build plugin's file access. */\n\n// The package a bare import names, such as 'vue' for 'vue/dist/x.js' or '@scope/pkg' for '@scope/pkg/sub'. Built-in\n// modules, relative paths and URLs name no package.\nfunction compareNames(a: string, b: string): number {\n return a.localeCompare(b, 'en');\n}\n\nfunction extractPackageName(specifier: string): string | undefined {\n if (specifier.startsWith('.') || specifier.startsWith('/') || specifier.startsWith('node:') || /^[a-z]+:\\/\\//i.test(specifier)) return undefined;\n const parts = specifier.split('/', 2);\n return specifier.startsWith('@') ? parts.join('/') : parts[0];\n}\n"],"mappings":";;;AAkCA,IAAaC,IAA6B,yBACpCC,IAAmB,2BAKnBC,oBAAoB,IAAI,IAAuC;AAOrE,SAAgBC,IAAgC;CAC5C,IAAIC;CAEJ,OAAO;EACH,MAAM;EACN,OAAO;EACP,eAAe,GAAgB;GAC3B,IAASC;EACb;EACA,eAAe,GAAU,GAAQ;GAC7B,IAAID,MAAW,KAAA,GAAW;GAC1B,IAAME,IAAaJ,EAAkB,IAAIE,EAAO,IAAI,KAAK;IAAE,0BAAU,IAAI,IAAY;IAAG,0BAAU,IAAI,IAAY;GAAE;GACpH,EAAkB,IAAIA,EAAO,MAAME,CAAU;GAC7C,IAAMC,IAAkB,IAAI,IAAI,OAAO,KAAKC,CAAM,CAAC;GACnD,KAAK,IAAMC,KAAU,OAAO,OAAOD,CAAM,GACrC,AAAIC,EAAO,SAAS,UAASC,EAAaJ,GAAYG,GAAQF,CAAe,IACxEI,EAAaL,GAAYG,GAAQL,EAAO,IAAI;EAEzD;EACA,cAAc;GACV,IAAIA,MAAW,KAAA,GAAW;GAC1B,IAAME,IAAaJ,EAAkB,IAAIE,EAAO,IAAI;GACpD,AAAIE,MAAe,KAAA,KAAWM,EAAYN,GAAY,CAACP,EAAK,QAAQK,EAAO,MAAMH,CAAgB,GAAGF,EAAK,QAAQK,EAAO,MAAMA,EAAO,MAA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,GAAG,KAAKA,EAAU,WAAW,OAAO,KAAK,gBAAgB,KAAKA,CAAS,GAAG;CAChI,IAAMC,IAAQD,EAAU,MAAM,KAAK,CAAC;CACpC,OAAOA,EAAU,WAAW,GAAG,IAAIC,EAAM,KAAK,GAAG,IAAIA,EAAM;AAC/D"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dpuse/dpuse-development",
3
- "version": "0.3.812",
3
+ "version": "0.3.813",
4
4
  "private": false,
5
5
  "description": "Actions for managing DPUse projects.",
6
6
  "license": "MIT",
@@ -22,7 +22,11 @@
22
22
  "import": "./dist/dpuse-development.es.js",
23
23
  "types": "./dist/types/src/index.d.ts"
24
24
  },
25
- "./prettierrc": "./.prettierrc.json"
25
+ "./prettierrc": "./.prettierrc.json",
26
+ "./vite": {
27
+ "import": "./dist/vite.es.js",
28
+ "types": "./dist/types/src/vite.d.ts"
29
+ }
26
30
  },
27
31
  "files": [
28
32
  ".editorconfig",
@@ -4,6 +4,9 @@ import dts from 'vite-plugin-dts';
4
4
  import Sonda from 'sonda/vite';
5
5
  import { fileURLToPath, URL } from 'node:url';
6
6
 
7
+ // ── DPUse Framework
8
+ import { recordShippedPackages } from '@dpuse/dpuse-development/vite';
9
+
7
10
  // ── Data
8
11
  import config from './config.json' with { type: 'json' };
9
12
 
@@ -24,7 +27,8 @@ export default defineConfig({
24
27
  },
25
28
  // Tests and config files sit in the tsconfig so they get type-checked, but their declarations must not reach the
26
29
  // published package. 'entryRoot' keeps the types under 'dist/types/src', where package.json points.
27
- plugins: [dts({ entryRoot: '.', exclude: ['tests/**', '*.config.*'], outDirs: 'dist/types' })],
30
+ // 'recordShippedPackages' writes the record of what the build ships, which 'npm run document' lists licences from.
31
+ plugins: [dts({ entryRoot: '.', exclude: ['tests/**', '*.config.*'], outDirs: 'dist/types' }), recordShippedPackages()],
28
32
  resolve: {
29
33
  alias: {
30
34
  '~': fileURLToPath(new URL('./', import.meta.url)),
@@ -4,6 +4,9 @@ import dts from 'vite-plugin-dts';
4
4
  import Sonda from 'sonda/vite';
5
5
  import { fileURLToPath, URL } from 'node:url';
6
6
 
7
+ // ── DPUse Framework
8
+ import { recordShippedPackages } from '@dpuse/dpuse-development/vite';
9
+
7
10
  // ── Data
8
11
  import config from './config.json' with { type: 'json' };
9
12
 
@@ -26,7 +29,8 @@ export default defineConfig({
26
29
  },
27
30
  // Tests and config files sit in the tsconfig so they get type-checked, but their declarations must not reach the
28
31
  // published package. 'entryRoot' keeps the types under 'dist/types/src', where package.json points.
29
- plugins: [dts({ entryRoot: '.', exclude: ['tests/**', '*.config.*'], outDirs: 'dist/types' })],
32
+ // 'recordShippedPackages' writes the record of what the build ships, which 'npm run document' lists licences from.
33
+ plugins: [dts({ entryRoot: '.', exclude: ['tests/**', '*.config.*'], outDirs: 'dist/types' }), recordShippedPackages()],
30
34
  resolve: {
31
35
  alias: {
32
36
  '~': fileURLToPath(new URL('./', import.meta.url)),
@@ -4,6 +4,9 @@ import dts from 'vite-plugin-dts';
4
4
  import Sonda from 'sonda/vite';
5
5
  import { fileURLToPath, URL } from 'node:url';
6
6
 
7
+ // ── DPUse Framework
8
+ import { recordShippedPackages } from '@dpuse/dpuse-development/vite';
9
+
7
10
  // ── Data
8
11
  import config from './config.json' with { type: 'json' };
9
12
 
@@ -25,7 +28,8 @@ export default defineConfig({
25
28
  },
26
29
  // Tests and config files sit in the tsconfig so they get type-checked, but their declarations must not reach the
27
30
  // published package. 'entryRoot' keeps the types under 'dist/types/src', where package.json points.
28
- plugins: [dts({ entryRoot: '.', exclude: ['tests/**', '*.config.*'], outDirs: 'dist/types' })],
31
+ // 'recordShippedPackages' writes the record of what the build ships, which 'npm run document' lists licences from.
32
+ plugins: [dts({ entryRoot: '.', exclude: ['tests/**', '*.config.*'], outDirs: 'dist/types' }), recordShippedPackages()],
29
33
  resolve: {
30
34
  alias: {
31
35
  '~': fileURLToPath(new URL('./', import.meta.url)),