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

Add type-checking to unplugin #689

Merged
merged 16 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion build/esbuild.civet
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
esbuild = require "esbuild"
heraPlugin = require "@danielx/hera/esbuild-plugin"
# Need to use the packaged version because we may not have built our own yet
civetPlugin = (require "../node_modules/@danielx/civet/dist/esbuild.js").default
civetPlugin = (require "../node_modules/@danielx/civet/dist/esbuild-plugin.js").default

watch = process.argv.includes '--watch'
minify = false
Expand Down
84 changes: 74 additions & 10 deletions integration/unplugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import { TransformResult, createUnplugin } from 'unplugin';
import civet from '@danielx/civet';
import {
TransformResult,
createUnplugin,
SourceMapCompact as UnpluginSourceMap,
} from 'unplugin';
import civet, { SourceMap } from '@danielx/civet';
import {
remapRange,
flattenDiagnosticMessageText,
rangeFromTextSpan,
// @ts-ignore
STRd6 marked this conversation as resolved.
Show resolved Hide resolved
// using ts-ignore because the version of @danielx/civet typescript is checking against
// is the one published to npm, not the one in the repo
} from '@danielx/civet/ts-diagnostic';
import * as fs from 'fs';
import path from 'path';
import ts from 'typescript';
Expand Down Expand Up @@ -42,6 +54,7 @@ const civetUnplugin = createUnplugin((options: PluginOptions = {}) => {
const outExt = options.outputExtension ?? (transpileToJS ? '.jsx' : '.tsx');

let fsMap: Map<string, string> = new Map();
const sourceMaps = new Map<string, SourceMap>();
let compilerOptions: any;

return {
Expand Down Expand Up @@ -92,7 +105,34 @@ const civetUnplugin = createUnplugin((options: PluginOptions = {}) => {
host: host.compilerHost,
});

const diagnostics = program.getGlobalDiagnostics();
const diagnostics: ts.Diagnostic[] = program
.getGlobalDiagnostics()
.map(diagnostic => {
const file = diagnostic.file;
if (!file) return diagnostic;

const sourceMap = sourceMaps.get(file.fileName);
if (!sourceMap) return diagnostic;

const sourcemapLines = sourceMap.data.lines;
const range = remapRange(
rangeFromTextSpan(
{
start: diagnostic.start || 0,
length: diagnostic.length ?? 1,
},
document
),
sourcemapLines
);

return {
...diagnostic,
messageText: flattenDiagnosticMessageText(diagnostic.messageText),
length: diagnostic.length,
start: range.start,
};
});

if (diagnostics.length > 0) {
console.error(
Expand All @@ -104,7 +144,12 @@ const civetUnplugin = createUnplugin((options: PluginOptions = {}) => {
const sourceFile = program.getSourceFile(file)!;
program.emit(
sourceFile,
(filePath, content) => {
async (filePath, content) => {
const dir = path.dirname(filePath);
if (!pathExists(dir)) {
await fs.promises.mkdir(dir, { recursive: true });
}
Mokshit06 marked this conversation as resolved.
Show resolved Hide resolved

this.emitFile({
source: content,
fileName: path.relative(process.cwd(), filePath),
Expand Down Expand Up @@ -142,13 +187,23 @@ const civetUnplugin = createUnplugin((options: PluginOptions = {}) => {
// but for some reason, webpack seems to be running them in the order
// of `resolveId` -> `loadInclude` -> `transform` -> `load`
// so we have to do transformation here instead
const compiled = civet.compile(code, {
// inlineMap: true,
filename: id,
js: transpileToJS,
sourceMap: true,
});

sourceMaps.set(path.resolve(process.cwd(), id), compiled.sourceMap);

const jsonSourceMap = compiled.sourceMap.json(
path.basename(id.replace(/\.tsx$/, '')),
path.basename(id)
);

let transformed: TransformResult = {
code: civet.compile(code, {
inlineMap: true,
filename: id,
js: transpileToJS,
} as any) as string,
map: null,
code: compiled.code,
map: jsonSourceMap as UnpluginSourceMap,
};

if (options.transformOutput)
Expand Down Expand Up @@ -183,4 +238,13 @@ const civetUnplugin = createUnplugin((options: PluginOptions = {}) => {
};
});

async function pathExists(path: string) {
try {
await fs.promises.access(path);
return true;
} catch {
return false;
}
}

export default civetUnplugin;