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

fix(nuxt): Re-export 'default' exports with rollup plugin #13984

Merged
merged 2 commits into from
Oct 15, 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
7 changes: 4 additions & 3 deletions packages/nuxt/src/vite/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function extractFunctionReexportQueryParameters(query: string): string[]
return match && match[1]
? match[1]
.split(',')
.filter(param => param !== '' && param !== 'default')
.filter(param => param !== '')
// Sanitize, as code could be injected with another rollup plugin
.map((str: string) => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'))
: [];
Expand All @@ -72,10 +72,11 @@ export function constructFunctionReExport(pathWithQuery: string, entryId: string
return functionNames.reduce(
(functionsCode, currFunctionName) =>
functionsCode.concat(
`export async function ${currFunctionName}(...args) {\n` +
'async function reExport(...args) {\n' +
` const res = await import(${JSON.stringify(entryId)});\n` +
` return res.${currFunctionName}.call(this, ...args);\n` +
'}\n',
'}\n' +
`export { reExport as ${currFunctionName} };\n`,
),
'',
);
Expand Down
26 changes: 22 additions & 4 deletions packages/nuxt/test/vite/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe('removeSentryQueryFromPath', () => {
describe('extractFunctionReexportQueryParameters', () => {
it.each([
[`${SENTRY_FUNCTIONS_REEXPORT}foo,bar,${QUERY_END_INDICATOR}`, ['foo', 'bar']],
[`${SENTRY_FUNCTIONS_REEXPORT}foo,bar,default${QUERY_END_INDICATOR}`, ['foo', 'bar']],
[`${SENTRY_FUNCTIONS_REEXPORT}foo,bar,default${QUERY_END_INDICATOR}`, ['foo', 'bar', 'default']],
[
`${SENTRY_FUNCTIONS_REEXPORT}foo,a.b*c?d[e]f(g)h|i\\\\j(){hello},${QUERY_END_INDICATOR}`,
['foo', 'a\\.b\\*c\\?d\\[e\\]f\\(g\\)h\\|i\\\\\\\\j\\(\\)\\{hello\\}'],
Expand All @@ -111,18 +111,36 @@ describe('constructFunctionReExport', () => {
const result2 = constructFunctionReExport(query2, entryId);

const expected = `
export async function foo(...args) {
async function reExport(...args) {
const res = await import("./module");
return res.foo.call(this, ...args);
}
export async function bar(...args) {
export { reExport as foo };
async function reExport(...args) {
const res = await import("./module");
return res.bar.call(this, ...args);
}`;
}
export { reExport as bar };
`;
expect(result.trim()).toBe(expected.trim());
expect(result2.trim()).toBe(expected.trim());
});

it('constructs re-export code for a "default" query parameters and entry ID', () => {
const query = `${SENTRY_FUNCTIONS_REEXPORT}default${QUERY_END_INDICATOR}}`;
const entryId = './index';
const result = constructFunctionReExport(query, entryId);

const expected = `
async function reExport(...args) {
const res = await import("./index");
return res.default.call(this, ...args);
}
export { reExport as default };
`;
expect(result.trim()).toBe(expected.trim());
});

it('returns an empty string if the query string is empty', () => {
const query = '';
const entryId = './module';
Expand Down
Loading