diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..f4e9c15 --- /dev/null +++ b/flake.lock @@ -0,0 +1,27 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1727802920, + "narHash": "sha256-HP89HZOT0ReIbI7IJZJQoJgxvB2Tn28V6XS3MNKnfLs=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "27e30d177e57d912d614c88c622dcfdb2e6e6515", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..3247922 --- /dev/null +++ b/flake.nix @@ -0,0 +1,25 @@ +{ + description = "A shell environment for building and testing build-tools"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + }; + + outputs = { nixpkgs, ... }: + let + forAllSystems = function: + nixpkgs.lib.genAttrs nixpkgs.lib.systems.flakeExposed + (system: function nixpkgs.legacyPackages.${system}); + in + { + formatter = forAllSystems (pkgs: pkgs.alejandra); + devShells = forAllSystems (pkgs: { + default = pkgs.mkShell { + packages = with pkgs; [ + pnpm + nodejs_22 + ]; + }; + }); + }; +} diff --git a/src/BuildService.ts b/src/BuildService.ts index 5aebbb7..c7e360b 100644 --- a/src/BuildService.ts +++ b/src/BuildService.ts @@ -286,7 +286,7 @@ export const BuildServiceLive: RTE.ReaderTaskEither< RTE.map(() => file), ), ), - RT.chainFirstW(({ left, right }) => + RT.tap(({ left, right }) => pipe( RT.Do, RT.tap(() => @@ -309,38 +309,56 @@ export const BuildServiceLive: RTE.ReaderTaskEither< ), // Copy `srcDir` files to dist for source maps RTE.tap(() => - Files.copyDirectory( - config.srcDir, - path.join(config.outDir, config.srcDir), - { recursive: true }, - ), - ), - RTE.tapReaderTask(() => - Log.info(color.magenta('PCK') + color.whiteBright(' Copied source files')), + config.emitDeclarationMaps + ? pipe( + Files.copyDirectory( + config.srcDir, + path.join( + config.basePath, + config.outDir, + Src.NEW_SOURCE_DIRECTORY, + config.srcDir, + ), + { recursive: true }, + ), + RTE.tapReaderTask(() => + Log.info( + color.magenta('PCK') + color.whiteBright(' Copied source files'), + ), + ), + ) + : RTE.of(void 0), ), - // Copy non-`srcDir` entrypoints to dist + // Copy non-`srcDir` entrypoints to dist/source-files for source maps RTE.tap(() => - pipe( - entrypoints, - RA.wither(RTE.ApplicativePar)( - ( - entrypoint, - ): RTE.ReaderTaskEither< - Files.FileService, - Files.FileServiceError, - O.Option - > => - rootDirRegex.test(entrypoint) - ? RTE.of(O.none) - : pipe( - Files.copyFile( - entrypoint, - path.join(config.basePath, config.outDir, entrypoint), - ), - RTE.as(O.of(void 0)), - ), - ), - ), + config.emitDeclarationMaps + ? pipe( + entrypoints, + RA.wither(RTE.ApplicativePar)( + ( + entrypoint, + ): RTE.ReaderTaskEither< + Files.FileService, + Files.FileServiceError, + O.Option + > => + rootDirRegex.test(entrypoint) + ? RTE.of(O.none) + : pipe( + Files.copyFile( + entrypoint, + path.join( + config.basePath, + config.outDir, + Src.NEW_SOURCE_DIRECTORY, + entrypoint, + ), + ), + RTE.as(O.of(void 0)), + ), + ), + ) + : RTE.of(void 0), ), RTE.tap(() => config.emitTypes diff --git a/src/ConfigService.ts b/src/ConfigService.ts index 6e18e2e..65b56b2 100644 --- a/src/ConfigService.ts +++ b/src/ConfigService.ts @@ -86,6 +86,15 @@ export type ConfigParameters = { */ readonly srcDir?: string + /** + * Whether to copy the source-directory to "source-files," along with declaration maps. + * This allows consumers of the library to control-click in VsCode or go-to-source and + * be taken to the raw source of the library. + * + * @default true + */ + readonly emitDeclarationMaps?: boolean + /** * A list of package.json keys to omit from the copied file in dist * @@ -147,11 +156,13 @@ export class ConfigService { dtsConfig = 'tsconfig.json', dtsCompilerOverrides = {}, bin = null, + emitDeclarationMaps = true, }: ConfigParameters) { this[ConfigServiceSymbol] = { buildType, srcDir, omittedPackageKeys, + emitDeclarationMaps, dtsConfig, copyFiles, basePath, diff --git a/src/SourceService.ts b/src/SourceService.ts index 736c83a..a5f33ae 100644 --- a/src/SourceService.ts +++ b/src/SourceService.ts @@ -29,12 +29,14 @@ export class SourceService { } } -function flattenDirByAtLeast1Regex(srcDir: string) { +function rewriteSourceFolderDropGrandparentDir(srcDir: string) { return new RegExp(`(.*)../${srcDir}(.*)`, 'gm') } const rootDirRegex = /^\.\.\/(?!\.\.\/)(.*).(m|c)?ts$/ +export const NEW_SOURCE_DIRECTORY = 'source-files' + const rewriteSource: (srcDir: string, file: string) => (source: string) => string = (srcDir, file) => source => { const fileIsInRootDir = rootDirRegex.test(source) @@ -42,13 +44,16 @@ const rewriteSource: (srcDir: string, file: string) => (source: string) => strin // re-point it to the same directory (dist) if (fileIsInRootDir) { const basename = path.basename(source) - const adjusted = basename.startsWith('.') ? basename : './' + basename + const inNewSourceDir = path.join(NEW_SOURCE_DIRECTORY, basename) + const adjusted = inNewSourceDir.startsWith('.') + ? inNewSourceDir + : './' + inNewSourceDir return adjusted // For other files, re-point the src dir to "." } else { const flattenedBy1 = `${source}`.replace( - flattenDirByAtLeast1Regex(srcDir), - `$1${srcDir}$2`, + rewriteSourceFolderDropGrandparentDir(srcDir), + `$1${NEW_SOURCE_DIRECTORY}/${srcDir}$2`, ) const directory = path.dirname(file) const adjusted = path.posix.relative( diff --git a/src/TypesService.ts b/src/TypesService.ts index b1693e8..0f1457b 100644 --- a/src/TypesService.ts +++ b/src/TypesService.ts @@ -351,7 +351,7 @@ function sharedConfig( emitDeclarationOnly: true, sourceMap: true, declaration: true, - declarationMap: true, + declarationMap: config.emitDeclarationMaps, noEmit: false, rootDir: config.basePath, outDir: path.join(config.basePath, config.outDir),