Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make source maps configurable, source files have their own folder #18

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -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
];
};
});
};
}
80 changes: 49 additions & 31 deletions src/BuildService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() =>
Expand All @@ -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<void>
> =>
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<void>
> =>
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
Expand Down
11 changes: 11 additions & 0 deletions src/ConfigService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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,
Expand Down
13 changes: 9 additions & 4 deletions src/SourceService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,31 @@ 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)
// If file is from the root directory,
// 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(
Expand Down
2 changes: 1 addition & 1 deletion src/TypesService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Loading