Skip to content

Commit 1607bb7

Browse files
authored
Merge pull request #18 from fp-tx/ja/source-files
Make source maps configurable, source files have their own folder
2 parents 31428e2 + 19402ff commit 1607bb7

File tree

6 files changed

+122
-36
lines changed

6 files changed

+122
-36
lines changed

flake.lock

+27
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
description = "A shell environment for building and testing build-tools";
3+
4+
inputs = {
5+
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
6+
};
7+
8+
outputs = { nixpkgs, ... }:
9+
let
10+
forAllSystems = function:
11+
nixpkgs.lib.genAttrs nixpkgs.lib.systems.flakeExposed
12+
(system: function nixpkgs.legacyPackages.${system});
13+
in
14+
{
15+
formatter = forAllSystems (pkgs: pkgs.alejandra);
16+
devShells = forAllSystems (pkgs: {
17+
default = pkgs.mkShell {
18+
packages = with pkgs; [
19+
pnpm
20+
nodejs_22
21+
];
22+
};
23+
});
24+
};
25+
}

src/BuildService.ts

+49-31
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ export const BuildServiceLive: RTE.ReaderTaskEither<
286286
RTE.map(() => file),
287287
),
288288
),
289-
RT.chainFirstW(({ left, right }) =>
289+
RT.tap(({ left, right }) =>
290290
pipe(
291291
RT.Do,
292292
RT.tap(() =>
@@ -309,38 +309,56 @@ export const BuildServiceLive: RTE.ReaderTaskEither<
309309
),
310310
// Copy `srcDir` files to dist for source maps
311311
RTE.tap(() =>
312-
Files.copyDirectory(
313-
config.srcDir,
314-
path.join(config.outDir, config.srcDir),
315-
{ recursive: true },
316-
),
317-
),
318-
RTE.tapReaderTask(() =>
319-
Log.info(color.magenta('PCK') + color.whiteBright(' Copied source files')),
312+
config.emitDeclarationMaps
313+
? pipe(
314+
Files.copyDirectory(
315+
config.srcDir,
316+
path.join(
317+
config.basePath,
318+
config.outDir,
319+
Src.NEW_SOURCE_DIRECTORY,
320+
config.srcDir,
321+
),
322+
{ recursive: true },
323+
),
324+
RTE.tapReaderTask(() =>
325+
Log.info(
326+
color.magenta('PCK') + color.whiteBright(' Copied source files'),
327+
),
328+
),
329+
)
330+
: RTE.of(void 0),
320331
),
321-
// Copy non-`srcDir` entrypoints to dist
332+
// Copy non-`srcDir` entrypoints to dist/source-files for source maps
322333
RTE.tap(() =>
323-
pipe(
324-
entrypoints,
325-
RA.wither(RTE.ApplicativePar)(
326-
(
327-
entrypoint,
328-
): RTE.ReaderTaskEither<
329-
Files.FileService,
330-
Files.FileServiceError,
331-
O.Option<void>
332-
> =>
333-
rootDirRegex.test(entrypoint)
334-
? RTE.of(O.none)
335-
: pipe(
336-
Files.copyFile(
337-
entrypoint,
338-
path.join(config.basePath, config.outDir, entrypoint),
339-
),
340-
RTE.as(O.of(void 0)),
341-
),
342-
),
343-
),
334+
config.emitDeclarationMaps
335+
? pipe(
336+
entrypoints,
337+
RA.wither(RTE.ApplicativePar)(
338+
(
339+
entrypoint,
340+
): RTE.ReaderTaskEither<
341+
Files.FileService,
342+
Files.FileServiceError,
343+
O.Option<void>
344+
> =>
345+
rootDirRegex.test(entrypoint)
346+
? RTE.of(O.none)
347+
: pipe(
348+
Files.copyFile(
349+
entrypoint,
350+
path.join(
351+
config.basePath,
352+
config.outDir,
353+
Src.NEW_SOURCE_DIRECTORY,
354+
entrypoint,
355+
),
356+
),
357+
RTE.as(O.of(void 0)),
358+
),
359+
),
360+
)
361+
: RTE.of(void 0),
344362
),
345363
RTE.tap(() =>
346364
config.emitTypes

src/ConfigService.ts

+11
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@ export type ConfigParameters = {
8686
*/
8787
readonly srcDir?: string
8888

89+
/**
90+
* Whether to copy the source-directory to "source-files," along with declaration maps.
91+
* This allows consumers of the library to control-click in VsCode or go-to-source and
92+
* be taken to the raw source of the library.
93+
*
94+
* @default true
95+
*/
96+
readonly emitDeclarationMaps?: boolean
97+
8998
/**
9099
* A list of package.json keys to omit from the copied file in dist
91100
*
@@ -147,11 +156,13 @@ export class ConfigService {
147156
dtsConfig = 'tsconfig.json',
148157
dtsCompilerOverrides = {},
149158
bin = null,
159+
emitDeclarationMaps = true,
150160
}: ConfigParameters) {
151161
this[ConfigServiceSymbol] = {
152162
buildType,
153163
srcDir,
154164
omittedPackageKeys,
165+
emitDeclarationMaps,
155166
dtsConfig,
156167
copyFiles,
157168
basePath,

src/SourceService.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,31 @@ export class SourceService {
2929
}
3030
}
3131

32-
function flattenDirByAtLeast1Regex(srcDir: string) {
32+
function rewriteSourceFolderDropGrandparentDir(srcDir: string) {
3333
return new RegExp(`(.*)../${srcDir}(.*)`, 'gm')
3434
}
3535

3636
const rootDirRegex = /^\.\.\/(?!\.\.\/)(.*).(m|c)?ts$/
3737

38+
export const NEW_SOURCE_DIRECTORY = 'source-files'
39+
3840
const rewriteSource: (srcDir: string, file: string) => (source: string) => string =
3941
(srcDir, file) => source => {
4042
const fileIsInRootDir = rootDirRegex.test(source)
4143
// If file is from the root directory,
4244
// re-point it to the same directory (dist)
4345
if (fileIsInRootDir) {
4446
const basename = path.basename(source)
45-
const adjusted = basename.startsWith('.') ? basename : './' + basename
47+
const inNewSourceDir = path.join(NEW_SOURCE_DIRECTORY, basename)
48+
const adjusted = inNewSourceDir.startsWith('.')
49+
? inNewSourceDir
50+
: './' + inNewSourceDir
4651
return adjusted
4752
// For other files, re-point the src dir to "."
4853
} else {
4954
const flattenedBy1 = `${source}`.replace(
50-
flattenDirByAtLeast1Regex(srcDir),
51-
`$1${srcDir}$2`,
55+
rewriteSourceFolderDropGrandparentDir(srcDir),
56+
`$1${NEW_SOURCE_DIRECTORY}/${srcDir}$2`,
5257
)
5358
const directory = path.dirname(file)
5459
const adjusted = path.posix.relative(

src/TypesService.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ function sharedConfig(
351351
emitDeclarationOnly: true,
352352
sourceMap: true,
353353
declaration: true,
354-
declarationMap: true,
354+
declarationMap: config.emitDeclarationMaps,
355355
noEmit: false,
356356
rootDir: config.basePath,
357357
outDir: path.join(config.basePath, config.outDir),

0 commit comments

Comments
 (0)