Skip to content

Commit

Permalink
fix(cli): fix build outputs (#604)
Browse files Browse the repository at this point in the history
  • Loading branch information
pluvrt authored Apr 15, 2024
1 parent 2bd26d0 commit 1a134ae
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 72 deletions.
5 changes: 5 additions & 0 deletions .changeset/curvy-tables-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"pluv": patch
---

Fix the build command incorrectly outputting build artifacts it shouldn't have.
9 changes: 5 additions & 4 deletions packages/cli/src/utils/buildApp/buildApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import typescript from "@rollup/plugin-typescript";
import dotenv from "dotenv";
import fs from "fs-extra";
import path from "node:path";
import { OutputAsset, OutputChunk, Plugin, rollup } from "rollup";
import type { OutputAsset, OutputChunk, Plugin } from "rollup";
import { rollup } from "rollup";
import nodePolyfills from "rollup-plugin-node-polyfills";
import { resolveDependenciesTransformer } from "./resolveDependenciesTransformer.js";
import { resolveDependencies } from "./resolveDependencies.js";

const isOutputValid = (chunks: (OutputChunk | OutputAsset)[]): boolean => {
return chunks.some((chunk) => {
Expand Down Expand Up @@ -110,8 +111,8 @@ export const buildApp = async (options: BuildAppOptions) => {
skipLibCheck: true,
strict: true,
target: "es2021",
transformers: { before: [resolveDependenciesTransformer] },
include: input,
include: resolveDependencies({ input }),
compilerOptions: { sourceMap: false },
}),
// Converts CommonJS modules to ES6.
(commonjs as unknown as typeof commonjs.default)({
Expand Down
127 changes: 127 additions & 0 deletions packages/cli/src/utils/buildApp/resolveDependencies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import path from "node:path";
import type { CompilerOptions, Program } from "typescript";
import ts from "typescript";

export interface ResolveDependenciesParams {
input: string;
}

const crawlDependencies = (program: Program): readonly string[] => {
const [entryFileName] = program.getRootFileNames();

if (!entryFileName) return [];

const dependencies = new Set<string>();
const visited = new Set<string>();

const visit = (filePath: string): void => {
if (visited.has(filePath)) return;

visited.add(filePath);

const sourceText = ts.sys.readFile(filePath);

if (!sourceText) return;

const sourceFile = ts.createSourceFile(
filePath,
sourceText,
ts.ScriptTarget.ESNext,
true,
);

ts.forEachChild(sourceFile, (node) => {
if (!ts.isImportDeclaration(node)) return;

const moduleName = node.moduleSpecifier
.getText()
.replace(/(^("|'|`))|(("|'|`)$)/g, "");

const resolvedModule = ts.resolveModuleName(
moduleName,
sourceFile.fileName,
program.getCompilerOptions(),
ts.sys,
);

const resolvedFile =
resolvedModule.resolvedModule?.resolvedFileName;

if (!resolvedFile) return;

dependencies.add(resolvedFile);

visit(resolvedFile);
});
};

visit(entryFileName);

return Array.from(dependencies.values())
.map((dependency) => {
const sourceFile = program.getSourceFile(dependency);

return sourceFile?.fileName;
})
.filter((fileName): fileName is string => typeof fileName === "string");
};

const getTsConfigPath = () => {
const tsConfigPath = path.resolve(process.cwd(), "tsconfig.json");

if (!ts.sys.fileExists(tsConfigPath)) return null;

return tsConfigPath;
};

export const resolveDependencies = (
params: ResolveDependenciesParams,
): readonly string[] => {
const { input } = params;

const tsConfigPath = getTsConfigPath();
const basePath = tsConfigPath ? path.dirname(tsConfigPath) : process.cwd();

const compilerOptions: CompilerOptions = {
allowJs: true,
allowSyntheticDefaultImports: true,
checkJs: false,
esModuleInterop: true,
forceConsistentCasingInFileNames: true,
incremental: false,
inlineSources: false,
isolatedModules: true,
jsx: ts.JsxEmit.Preserve,
lib: ["es2021"],
module: ts.ModuleKind.ESNext,
moduleResolution: ts.ModuleResolutionKind.Bundler,
noEmit: true,
noUnusedLocals: false,
noUnusedParameters: false,
preserveWatchOutput: true,
resolveJsonModule: true,
skipLibCheck: true,
sourceMap: false,
strict: true,
target: ts.ScriptTarget.ESNext,
baseUrl: basePath,
};

const parsedConfig = ts.parseJsonConfigFileContent(
compilerOptions,
ts.sys,
basePath,
compilerOptions,
tsConfigPath ?? undefined,
);

const program = ts.createProgram({
options: parsedConfig.options,
projectReferences: parsedConfig.projectReferences,
rootNames: [path.resolve(basePath, input)],
});

const dependencies = crawlDependencies(program);

return dependencies;
};
68 changes: 0 additions & 68 deletions packages/cli/src/utils/buildApp/resolveDependenciesTransformer.ts

This file was deleted.

0 comments on commit 1a134ae

Please sign in to comment.