Skip to content

Commit

Permalink
Merge pull request #326 from ryoppippi/feature/fix-sourcemap
Browse files Browse the repository at this point in the history
 feat: improve sourcemap
  • Loading branch information
ryoppippi authored Sep 12, 2024
2 parents b0188bb + 5862e7a commit ec8c1e6
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
Binary file modified bun.lockb
Binary file not shown.
3 changes: 2 additions & 1 deletion examples/vite-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"ts-patch": "^3.2.1",
"typescript": "~5.5.4",
"typia": "^6.9.0",
"vite": "^5.4.1"
"vite": "^5.4.1",
"vite-plugin-inspect": "^0.8.7"
}
}
2 changes: 2 additions & 0 deletions examples/vite-react/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import UnpluginTypia from "@ryoppippi/unplugin-typia/vite";
import Inspect from 'vite-plugin-inspect'

// https://vitejs.dev/config/
export default defineConfig({
plugins: [
Inspect(),
UnpluginTypia({}),
react(),
],
Expand Down
2 changes: 2 additions & 0 deletions packages/unplugin-typia/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"@rollup/pluginutils": "^5.1.0",
"consola": "^3.2.3",
"defu": "^6.1.4",
"diff-match-patch": "^1.0.5",
"find-cache-dir": "^5.0.0",
"magic-string": "^0.30.11",
"pathe": "^1.1.2",
Expand All @@ -57,6 +58,7 @@
"@ryoppippi/eslint-config": "npm:@jsr/ryoppippi__eslint-config@^0.0.23",
"@std/collections": "npm:@jsr/std__collections@1.0.5",
"@types/bun": "^1.1.8",
"@types/diff-match-patch": "^1.0.36",
"@types/node": "^20.14.15",
"@vue-macros/test-utils": "^1.6.0",
"bumpp": "^9.5.1",
Expand Down
53 changes: 52 additions & 1 deletion packages/unplugin-typia/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { UnpluginFactory, UnpluginInstance } from 'unplugin';
import { createUnplugin } from 'unplugin';
import { createFilter as rollupCreateFilter } from '@rollup/pluginutils';
import MagicString from 'magic-string';
import Diff from 'diff-match-patch';
import { resolve } from 'pathe';

import type { ResolvedConfig } from 'vite';
Expand All @@ -16,6 +17,8 @@ import { isSvelteFile, preprocess as sveltePreprocess } from './languages/svelte

const name = `unplugin-typia`;

const dmp = new Diff();

/**
* Create a filter function from the given include and exclude patterns.
*/
Expand Down Expand Up @@ -48,7 +51,55 @@ const unpluginFactory: UnpluginFactory<
function generateCodeWithMap({ source, code, id }: { source: Source; code: Data; id: ID }) {
/** generate Magic string */
const s = new MagicString(source);
s.overwrite(0, -1, code);

/** generate diff */
const diff = dmp.diff_main(source, code);

/** cleanup diff */
dmp.diff_cleanupSemantic(diff);

let offset = 0;
for (let index = 0; index < diff.length; index++) {
const [type, text] = diff[index];
const textLength = text.length;
/** skip */
if (type === 0) {
/* offset is increased */
offset += textLength;
}

/** add text */
else if (type === 1) {
s.prependLeft(offset, text);

/* offset is not increased because text is prepended */
}

/** remove text */
else if (type === -1) {
const next = diff.at(index + 1);

/** if next is equal to 1, then overwrite */
if (next != null && next[0] === 1) {
const replaceText = next[1];

/** get first non-whitespace character of text (maybe bug of magic-string) */
const firstNonWhitespaceIndexOfText = text.search(/\S/);
const offsetStart = offset + (firstNonWhitespaceIndexOfText > 0 ? firstNonWhitespaceIndexOfText : 0);

s.update(offsetStart, offset + textLength, replaceText);

/** skip next */
index += 1;
}
else {
s.remove(offset, offset + textLength);
}

/* offset is increased */
offset += textLength;
}
}

if (!s.hasChanged()) {
return;
Expand Down

0 comments on commit ec8c1e6

Please sign in to comment.