Skip to content

Commit

Permalink
Fix remix-routes not transforming source maps
Browse files Browse the repository at this point in the history
  • Loading branch information
IgnusG committed Mar 4, 2024
1 parent 1455009 commit cd919f3
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 56 deletions.
14 changes: 12 additions & 2 deletions packages/remix-dev/vite/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1602,9 +1602,19 @@ export const remixVitePlugin: RemixVitePlugin = (remixUserConfig = {}) => {
}
}

let [filepath] = id.split("?");

let removedExports = removeExports(
code,
SERVER_ONLY_ROUTE_EXPORTS
)({
filename: id,
sourceFileName: filepath,
});

return {
code: removeExports(code, SERVER_ONLY_ROUTE_EXPORTS),
map: null,
code: removedExports.code,
map: removedExports.map,
};
},
},
Expand Down
102 changes: 51 additions & 51 deletions packages/remix-dev/vite/remove-exports-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ describe("removeExports", () => {
export const clientExport_2 = () => {}
`,
["serverExport_1", "serverExport_2"]
);
expect(result).toMatchInlineSnapshot(`
)({});
expect(result.code).toMatchInlineSnapshot(`
"export const clientExport_1 = () => {};
export const clientExport_2 = () => {};"
`);
expect(result).not.toMatch(/server/i);
expect(result.code).not.toMatch(/server/i);
});

test("arrow function with dependencies", () => {
Expand All @@ -39,16 +39,16 @@ describe("removeExports", () => {
export const clientExport_2 = () => clientUtil()
`,
["serverExport_1", "serverExport_2"]
);
expect(result).toMatchInlineSnapshot(`
)({});
expect(result.code).toMatchInlineSnapshot(`
"import { clientLib } from 'client-lib';
import { sharedLib } from 'shared-lib';
const sharedUtil = () => sharedLib();
const clientUtil = () => sharedUtil(clientLib());
export const clientExport_1 = () => clientUtil();
export const clientExport_2 = () => clientUtil();"
`);
expect(result).not.toMatch(/server/i);
expect(result.code).not.toMatch(/server/i);
});

test("function statement", () => {
Expand All @@ -61,12 +61,12 @@ describe("removeExports", () => {
export function clientExport_2(){}
`,
["serverExport_1", "serverExport_2"]
);
expect(result).toMatchInlineSnapshot(`
)({});
expect(result.code).toMatchInlineSnapshot(`
"export function clientExport_1() {}
export function clientExport_2() {}"
`);
expect(result).not.toMatch(/server/i);
expect(result.code).not.toMatch(/server/i);
});

test("function statement with dependencies", () => {
Expand All @@ -89,8 +89,8 @@ describe("removeExports", () => {
export function clientExport_2() { return clientUtil() }
`,
["serverExport_1", "serverExport_2"]
);
expect(result).toMatchInlineSnapshot(`
)({});
expect(result.code).toMatchInlineSnapshot(`
"import { clientLib } from 'client-lib';
import { sharedLib } from 'shared-lib';
function sharedUtil() {
Expand All @@ -106,7 +106,7 @@ describe("removeExports", () => {
return clientUtil();
}"
`);
expect(result).not.toMatch(/server/i);
expect(result.code).not.toMatch(/server/i);
});

test("object", () => {
Expand All @@ -119,12 +119,12 @@ describe("removeExports", () => {
export const clientExport_2 = {}
`,
["serverExport_1", "serverExport_2"]
);
expect(result).toMatchInlineSnapshot(`
)({});
expect(result.code).toMatchInlineSnapshot(`
"export const clientExport_1 = {};
export const clientExport_2 = {};"
`);
expect(result).not.toMatch(/server/i);
expect(result.code).not.toMatch(/server/i);
});

test("object with dependencies", () => {
Expand All @@ -147,8 +147,8 @@ describe("removeExports", () => {
export const clientExport_2 = { value: clientUtil() }
`,
["serverExport_1", "serverExport_2"]
);
expect(result).toMatchInlineSnapshot(`
)({});
expect(result.code).toMatchInlineSnapshot(`
"import { clientLib } from 'client-lib';
import { sharedLib } from 'shared-lib';
const sharedUtil = () => sharedLib();
Expand All @@ -160,7 +160,7 @@ describe("removeExports", () => {
value: clientUtil()
};"
`);
expect(result).not.toMatch(/server/i);
expect(result.code).not.toMatch(/server/i);
});

test("function call", () => {
Expand All @@ -173,12 +173,12 @@ describe("removeExports", () => {
export const clientExport_2 = globalFunction()
`,
["serverExport_1", "serverExport_2"]
);
expect(result).toMatchInlineSnapshot(`
)({});
expect(result.code).toMatchInlineSnapshot(`
"export const clientExport_1 = globalFunction();
export const clientExport_2 = globalFunction();"
`);
expect(result).not.toMatch(/server/i);
expect(result.code).not.toMatch(/server/i);
});

test("function call with dependencies", () => {
Expand All @@ -201,16 +201,16 @@ describe("removeExports", () => {
export const clientExport_2 = clientUtil()
`,
["serverExport_1", "serverExport_2"]
);
expect(result).toMatchInlineSnapshot(`
)({});
expect(result.code).toMatchInlineSnapshot(`
"import { clientLib } from 'client-lib';
import { sharedLib } from 'shared-lib';
const sharedUtil = () => sharedLib();
const clientUtil = () => sharedUtil(clientLib());
export const clientExport_1 = clientUtil();
export const clientExport_2 = clientUtil();"
`);
expect(result).not.toMatch(/server/i);
expect(result.code).not.toMatch(/server/i);
});

test("iife", () => {
Expand All @@ -223,12 +223,12 @@ describe("removeExports", () => {
export const clientExport_2 = (() => {})()
`,
["serverExport_1", "serverExport_2"]
);
expect(result).toMatchInlineSnapshot(`
)({});
expect(result.code).toMatchInlineSnapshot(`
"export const clientExport_1 = (() => {})();
export const clientExport_2 = (() => {})();"
`);
expect(result).not.toMatch(/server/i);
expect(result.code).not.toMatch(/server/i);
});

test("iife with dependencies", () => {
Expand All @@ -251,16 +251,16 @@ describe("removeExports", () => {
export const clientExport_2 = (() => clientUtil())()
`,
["serverExport_1", "serverExport_2"]
);
expect(result).toMatchInlineSnapshot(`
)({});
expect(result.code).toMatchInlineSnapshot(`
"import { clientLib } from 'client-lib';
import { sharedLib } from 'shared-lib';
const sharedUtil = () => sharedLib();
const clientUtil = () => sharedUtil(clientLib());
export const clientExport_1 = (() => clientUtil())();
export const clientExport_2 = (() => clientUtil())();"
`);
expect(result).not.toMatch(/server/i);
expect(result.code).not.toMatch(/server/i);
});

test("re-export", () => {
Expand All @@ -273,12 +273,12 @@ describe("removeExports", () => {
export { clientExport_2 } from './client/2'
`,
["serverExport_1", "serverExport_2"]
);
expect(result).toMatchInlineSnapshot(`
)({});
expect(result.code).toMatchInlineSnapshot(`
"export { clientExport_1 } from './client/1';
export { clientExport_2 } from './client/2';"
`);
expect(result).not.toMatch(/server/i);
expect(result.code).not.toMatch(/server/i);
});

test("re-export multiple", () => {
Expand All @@ -289,11 +289,11 @@ describe("removeExports", () => {
export { clientExport_1, clientExport_2 } from './client'
`,
["serverExport_1", "serverExport_2"]
);
expect(result).toMatchInlineSnapshot(
)({});
expect(result.code).toMatchInlineSnapshot(
"\"export { clientExport_1, clientExport_2 } from './client';\""
);
expect(result).not.toMatch(/server/i);
expect(result.code).not.toMatch(/server/i);
});

test("re-export manual", () => {
Expand All @@ -311,14 +311,14 @@ describe("removeExports", () => {
export { clientExport_2 }
`,
["serverExport_1", "serverExport_2"]
);
expect(result).toMatchInlineSnapshot(`
)({});
expect(result.code).toMatchInlineSnapshot(`
"import { clientExport_1 } from './client/1';
import { clientExport_2 } from './client/2';
export { clientExport_1 };
export { clientExport_2 };"
`);
expect(result).not.toMatch(/server/i);
expect(result.code).not.toMatch(/server/i);
});

test("number", () => {
Expand All @@ -331,12 +331,12 @@ describe("removeExports", () => {
export const clientExport_2 = 123
`,
["serverExport_1", "serverExport_2"]
);
expect(result).toMatchInlineSnapshot(`
)({});
expect(result.code).toMatchInlineSnapshot(`
"export const clientExport_1 = 123;
export const clientExport_2 = 123;"
`);
expect(result).not.toMatch(/server/i);
expect(result.code).not.toMatch(/server/i);
});

test("string", () => {
Expand All @@ -349,12 +349,12 @@ describe("removeExports", () => {
export const clientExport_2 = 'string'
`,
["serverExport_1", "serverExport_2"]
);
expect(result).toMatchInlineSnapshot(`
)({});
expect(result.code).toMatchInlineSnapshot(`
"export const clientExport_1 = 'string';
export const clientExport_2 = 'string';"
`);
expect(result).not.toMatch(/server/i);
expect(result.code).not.toMatch(/server/i);
});

test("string reference", () => {
Expand All @@ -370,13 +370,13 @@ describe("removeExports", () => {
export const clientExport_2 = CLIENT_STRING
`,
["serverExport_1", "serverExport_2"]
);
expect(result).toMatchInlineSnapshot(`
)({});
expect(result.code).toMatchInlineSnapshot(`
"const CLIENT_STRING = 'CLIENT_STRING';
export const clientExport_1 = CLIENT_STRING;
export const clientExport_2 = CLIENT_STRING;"
`);
expect(result).not.toMatch(/server/i);
expect(result.code).not.toMatch(/server/i);
});

test("null", () => {
Expand All @@ -389,11 +389,11 @@ describe("removeExports", () => {
export const clientExport_2 = null
`,
["serverExport_1", "serverExport_2"]
);
expect(result).toMatchInlineSnapshot(`
)({});
expect(result.code).toMatchInlineSnapshot(`
"export const clientExport_1 = null;
export const clientExport_2 = null;"
`);
expect(result).not.toMatch(/server/i);
expect(result.code).not.toMatch(/server/i);
});
});
9 changes: 6 additions & 3 deletions packages/remix-dev/vite/remove-exports.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Adapted from https://github.com/egoist/babel-plugin-eliminator/blob/d29859396b7708b7f7abbacdd951cbbc80902f00/src/index.ts
// Which was originally adapted from https://github.com/vercel/next.js/blob/574fe0b582d5cc1b13663121fd47a3d82deaaa17/packages/next/build/babel/plugins/next-ssg-transform.ts
import type { GeneratorOptions } from "@babel/generator";

import {
type BabelTypes,
type NodePath,
Expand Down Expand Up @@ -63,7 +65,8 @@ function isIdentifierReferenced(

export const removeExports = (source: string, exportsToRemove: string[]) => {
let document = parse(source, { sourceType: "module" });
let generateCode = () => generate(document).code;
let generateCodeAndSourceMaps = (opts: Partial<GeneratorOptions>) =>
generate(document, { ...opts, sourceMaps: true });

let referencedIdentifiers = new Set<NodePath<BabelTypes.Identifier>>();
let removedExports = new Set<string>();
Expand Down Expand Up @@ -213,7 +216,7 @@ export const removeExports = (source: string, exportsToRemove: string[]) => {
if (removedExports.size === 0) {
// No server-specific exports found so there's
// no need to remove unused references
return generateCode();
return generateCodeAndSourceMaps;
}

let referencesRemovedInThisPass: number;
Expand Down Expand Up @@ -359,5 +362,5 @@ export const removeExports = (source: string, exportsToRemove: string[]) => {
});
} while (referencesRemovedInThisPass);

return generateCode();
return generateCodeAndSourceMaps;
};

0 comments on commit cd919f3

Please sign in to comment.