@cubing/dev-config 0.7.1 → 0.7.2

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.
@@ -7,6 +7,7 @@ import "../../chunks/chunk-OZDRWEHO.js";
7
7
  import {
8
8
  build
9
9
  } from "esbuild";
10
+ import { Path } from "path-class";
10
11
  var plugin = {
11
12
  name: "mark-bare-imports-as-external",
12
13
  setup(build2) {
@@ -74,6 +75,8 @@ async function checkAllowedImports(groups, options) {
74
75
  entryPoints,
75
76
  plugins: [plugin],
76
77
  ...options?.overrideEsbuildOptions,
78
+ // Bogus `outdir` to avoid an error.
79
+ outdir: (await Path.makeTempDir()).path,
77
80
  write: false,
78
81
  metafile: true
79
82
  });
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/lib/check-allowed-imports/checkAllowedImports.ts"],
4
- "sourcesContent": ["import {\n type BuildOptions,\n build,\n type ImportKind,\n type Metafile,\n type Plugin,\n} from \"esbuild\";\nimport { es2022Lib } from \"../../esbuild/es2022\";\n\n/**\n * Note:\n * - A file may be matched by any parent path scope key.\n * - Files in a given scope key are allowed to import any other within the same scope.\n */\nexport type AllowedImports = {\n [scope: string]: { static?: string[]; dynamic?: string[] };\n};\n\nconst plugin = {\n name: \"mark-bare-imports-as-external\",\n setup(build) {\n const filter = /^[^./]|^\\.[^./]|^\\.\\.[^/]/; // Must not start with \"/\" or \"./\" or \"../\"\n build.onResolve({ filter }, (args) => ({\n path: args.path,\n external: true,\n }));\n },\n} satisfies Plugin;\n\nexport async function checkAllowedImports(\n groups: {\n [description: string]: {\n entryPoints: string[];\n allowedImports: AllowedImports;\n };\n },\n options?: { overrideEsbuildOptions: BuildOptions },\n): Promise<void> {\n let failure = false;\n\n console.log(\"+ means a new file\");\n console.log(\"- means a valid import for that file\");\n\n for (const [description, { entryPoints, allowedImports }] of Object.entries(\n groups,\n )) {\n console.log(`# ${description}`);\n // From https://github.com/evanw/esbuild/issues/619#issuecomment-1504100390\n\n const { metafile } = await build({\n ...es2022Lib(),\n entryPoints,\n plugins: [plugin],\n ...options?.overrideEsbuildOptions,\n write: false,\n metafile: true,\n });\n\n // Starts with the path and then keeps chopping off from the right.\n function* pathPrefixes(path: string) {\n const pathParts = path.split(\"/\");\n for (let n = pathParts.length; n > 0; n--) {\n yield pathParts.slice(0, n).join(\"/\");\n }\n }\n\n function matchingPathPrefix(matchPrefixes: string[], path: string) {\n for (const pathPrefix of pathPrefixes(path)) {\n if (matchPrefixes.includes(pathPrefix)) {\n return pathPrefix;\n }\n }\n return false;\n }\n\n const importKindMap: Partial<Record<ImportKind, \"static\" | \"dynamic\">> = {\n \"import-statement\": \"static\",\n \"dynamic-import\": \"dynamic\",\n } as const;\n\n function checkImport(\n sourcePath: string,\n importInfo: {\n path: string;\n kind: ImportKind;\n external?: boolean;\n original?: string;\n },\n allowedImports: AllowedImports,\n ) {\n const importKind = importKindMap[importInfo.kind];\n if (!importKind) {\n throw new Error(\"Unexpected import kind!\");\n }\n for (const sourcePathPrefix of pathPrefixes(sourcePath)) {\n const matchingSourcePathPrefix = matchingPathPrefix(\n Object.keys(allowedImports),\n sourcePathPrefix,\n );\n if (matchingSourcePathPrefix) {\n const allowedImportsForKind =\n allowedImports[matchingSourcePathPrefix][importKind];\n if (\n typeof allowedImportsForKind !== \"undefined\" &&\n !Array.isArray(allowedImportsForKind)\n ) {\n throw new Error(\n `Expected a string list for ${importKind} imports under the scope \"${matchingSourcePathPrefix}\"`,\n );\n }\n if (\n matchingPathPrefix(\n [\n matchingSourcePathPrefix, // allow importing from any source group to itself.\n ...(allowedImportsForKind ?? []),\n ],\n importInfo.path,\n )\n ) {\n process.stdout.write(\"-\");\n return;\n }\n }\n }\n failure = true;\n console.error(`\\n\u274C File has disallowed ${importKind} import:`);\n console.error(`From file: ${sourcePath}`);\n console.error(`Importing: ${importInfo.path}`);\n }\n\n async function checkImports(\n metafile: Metafile,\n allowedImports: AllowedImports,\n ) {\n for (const [filePath, importInfoList] of Object.entries(\n metafile.inputs,\n )) {\n process.stdout.write(\"+\");\n for (const importInfo of importInfoList.imports) {\n checkImport(filePath, importInfo, allowedImports);\n }\n }\n console.log();\n }\n\n await checkImports(metafile, allowedImports);\n }\n\n if (failure) {\n throw new Error(\"Failure\");\n }\n}\n"],
5
- "mappings": ";;;;;;AAAA;AAAA,EAEE;AAAA,OAIK;AAYP,IAAM,SAAS;AAAA,EACb,MAAM;AAAA,EACN,MAAMA,QAAO;AACX,UAAM,SAAS;AACf,IAAAA,OAAM,UAAU,EAAE,OAAO,GAAG,CAAC,UAAU;AAAA,MACrC,MAAM,KAAK;AAAA,MACX,UAAU;AAAA,IACZ,EAAE;AAAA,EACJ;AACF;AAEA,eAAsB,oBACpB,QAMA,SACe;AACf,MAAI,UAAU;AAEd,UAAQ,IAAI,oBAAoB;AAChC,UAAQ,IAAI,sCAAsC;AAElD,aAAW,CAAC,aAAa,EAAE,aAAa,eAAe,CAAC,KAAK,OAAO;AAAA,IAClE;AAAA,EACF,GAAG;AAqBD,QAASC,sBAAT,SAA4B,eAAyB,MAAc;AACjE,iBAAW,cAAc,aAAa,IAAI,GAAG;AAC3C,YAAI,cAAc,SAAS,UAAU,GAAG;AACtC,iBAAO;AAAA,QACT;AAAA,MACF;AACA,aAAO;AAAA,IACT,GAOSC,eAAT,SACE,YACA,YAMAC,iBACA;AACA,YAAM,aAAa,cAAc,WAAW,IAAI;AAChD,UAAI,CAAC,YAAY;AACf,cAAM,IAAI,MAAM,yBAAyB;AAAA,MAC3C;AACA,iBAAW,oBAAoB,aAAa,UAAU,GAAG;AACvD,cAAM,2BAA2BF;AAAA,UAC/B,OAAO,KAAKE,eAAc;AAAA,UAC1B;AAAA,QACF;AACA,YAAI,0BAA0B;AAC5B,gBAAM,wBACJA,gBAAe,wBAAwB,EAAE,UAAU;AACrD,cACE,OAAO,0BAA0B,eACjC,CAAC,MAAM,QAAQ,qBAAqB,GACpC;AACA,kBAAM,IAAI;AAAA,c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AS;AACX,UAAM,IAAI,MAAM,SAAS;AAAA,EAC3B;AACF;",
4
+ "sourcesContent": ["import {\n type BuildOptions,\n build,\n type ImportKind,\n type Metafile,\n type Plugin,\n} from \"esbuild\";\nimport { Path } from \"path-class\";\nimport { es2022Lib } from \"../../esbuild/es2022\";\n\n/**\n * Note:\n * - A file may be matched by any parent path scope key.\n * - Files in a given scope key are allowed to import any other within the same scope.\n */\nexport type AllowedImports = {\n [scope: string]: { static?: string[]; dynamic?: string[] };\n};\n\nconst plugin = {\n name: \"mark-bare-imports-as-external\",\n setup(build) {\n const filter = /^[^./]|^\\.[^./]|^\\.\\.[^/]/; // Must not start with \"/\" or \"./\" or \"../\"\n build.onResolve({ filter }, (args) => ({\n path: args.path,\n external: true,\n }));\n },\n} satisfies Plugin;\n\nexport async function checkAllowedImports(\n groups: {\n [description: string]: {\n entryPoints: string[];\n allowedImports: AllowedImports;\n };\n },\n options?: { overrideEsbuildOptions: BuildOptions },\n): Promise<void> {\n let failure = false;\n\n console.log(\"+ means a new file\");\n console.log(\"- means a valid import for that file\");\n\n for (const [description, { entryPoints, allowedImports }] of Object.entries(\n groups,\n )) {\n console.log(`# ${description}`);\n // From https://github.com/evanw/esbuild/issues/619#issuecomment-1504100390\n\n const { metafile } = await build({\n ...es2022Lib(),\n entryPoints,\n plugins: [plugin],\n ...options?.overrideEsbuildOptions,\n // Bogus `outdir` to avoid an error.\n outdir: (await Path.makeTempDir()).path,\n write: false,\n metafile: true,\n });\n\n // Starts with the path and then keeps chopping off from the right.\n function* pathPrefixes(path: string) {\n const pathParts = path.split(\"/\");\n for (let n = pathParts.length; n > 0; n--) {\n yield pathParts.slice(0, n).join(\"/\");\n }\n }\n\n function matchingPathPrefix(matchPrefixes: string[], path: string) {\n for (const pathPrefix of pathPrefixes(path)) {\n if (matchPrefixes.includes(pathPrefix)) {\n return pathPrefix;\n }\n }\n return false;\n }\n\n const importKindMap: Partial<Record<ImportKind, \"static\" | \"dynamic\">> = {\n \"import-statement\": \"static\",\n \"dynamic-import\": \"dynamic\",\n } as const;\n\n function checkImport(\n sourcePath: string,\n importInfo: {\n path: string;\n kind: ImportKind;\n external?: boolean;\n original?: string;\n },\n allowedImports: AllowedImports,\n ) {\n const importKind = importKindMap[importInfo.kind];\n if (!importKind) {\n throw new Error(\"Unexpected import kind!\");\n }\n for (const sourcePathPrefix of pathPrefixes(sourcePath)) {\n const matchingSourcePathPrefix = matchingPathPrefix(\n Object.keys(allowedImports),\n sourcePathPrefix,\n );\n if (matchingSourcePathPrefix) {\n const allowedImportsForKind =\n allowedImports[matchingSourcePathPrefix][importKind];\n if (\n typeof allowedImportsForKind !== \"undefined\" &&\n !Array.isArray(allowedImportsForKind)\n ) {\n throw new Error(\n `Expected a string list for ${importKind} imports under the scope \"${matchingSourcePathPrefix}\"`,\n );\n }\n if (\n matchingPathPrefix(\n [\n matchingSourcePathPrefix, // allow importing from any source group to itself.\n ...(allowedImportsForKind ?? []),\n ],\n importInfo.path,\n )\n ) {\n process.stdout.write(\"-\");\n return;\n }\n }\n }\n failure = true;\n console.error(`\\n\u274C File has disallowed ${importKind} import:`);\n console.error(`From file: ${sourcePath}`);\n console.error(`Importing: ${importInfo.path}`);\n }\n\n async function checkImports(\n metafile: Metafile,\n allowedImports: AllowedImports,\n ) {\n for (const [filePath, importInfoList] of Object.entries(\n metafile.inputs,\n )) {\n process.stdout.write(\"+\");\n for (const importInfo of importInfoList.imports) {\n checkImport(filePath, importInfo, allowedImports);\n }\n }\n console.log();\n }\n\n await checkImports(metafile, allowedImports);\n }\n\n if (failure) {\n throw new Error(\"Failure\");\n }\n}\n"],
5
+ "mappings": ";;;;;;AAAA;AAAA,EAEE;AAAA,OAIK;AACP,SAAS,YAAY;AAYrB,IAAM,SAAS;AAAA,EACb,MAAM;AAAA,EACN,MAAMA,QAAO;AACX,UAAM,SAAS;AACf,IAAAA,OAAM,UAAU,EAAE,OAAO,GAAG,CAAC,UAAU;AAAA,MACrC,MAAM,KAAK;AAAA,MACX,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BAcA,cAAAC;AApCT,YAAQ,IAAI,KAAK,WAAW,EAAE;AAG9B,UAAM,EAAE,SAAS,IAAI,MAAM,MAAM;AAAA,MAC/B,GAAG,UAAU;AAAA,MACb;AAAA,MACA,SAAS,CAAC,MAAM;AAAA,MAChB,GAAG,SAAS;AAAA;AAAA,MAEZ,SAAS,MAAM,KAAK,YAAY,GAAG;AAAA,MACnC,OAAO;AAAA,MACP,UAAU;AAAA,IACZ,CAAC;AAGD,cAAU,aAAa,MAAc;AACnC,YAAM,YAAY,KAAK,MAAM,GAAG;AAChC,eAAS,IAAI,UAAU,QAAQ,IAAI,GAAG,KAAK;AACzC,cAAM,UAAU,MAAM,GAAG,CAAC,EAAE,KAAK,GAAG;AAAA,MACtC;AAAA,IACF;AAWA,UAAM,gBAAmE;AAAA,MACvE,oBAAoB;AAAA,MACpB,kBAAkB;AAAA,IACpB;AAoDA,mBAAe,aACbE,WACAD,iBACA;AACA,iBAAW,CAAC,UAAU,cAAc,KAAK,OAAO;AAAA,QAC9CC,UAAS;AAAA,MACX,GAAG;AACD,gBAAQ,OAAO,MAAM,GAAG;AACxB,mBAAW,cAAc,eAAe,SAAS;AAC/C,UAAAF,aAAY,UAAU,YAAYC,eAAc;AAAA,QAClD;AAAA,MACF;AACA,cAAQ,IAAI;AAAA,IACd;AAEA,UAAM,aAAa,UAAU,cAAc;AAAA,EAC7C;AAEA,MAAI,SAAS;AACX,UAAM,IAAI,MAAM,SAAS;AAAA,EAC3B;AACF;",
6
6
  "names": ["build", "matchingPathPrefix", "checkImport", "allowedImports", "metafile"]
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cubing/dev-config",
3
- "version": "0.7.1",
3
+ "version": "0.7.2",
4
4
  "description": "Common dev configs for projects.",
5
5
  "author": {
6
6
  "name": "Lucas Garron",