Skip to content

Commit aa74914

Browse files
committed
fix(nuxt): Re-export 'default' exports with rollup plugin (#13984)
While the preset for Netlify exports the serverless handler function as `export { D as default }`, the Vercel preset exports this handler as `export { D as handler };`. This PR makes some adaptions to the code generation in the plugin to make it possible to re-export `default` functions. The previous snippet did not work as `default` is obviously not allowed as a function name.
1 parent 3c1829f commit aa74914

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

packages/nuxt/src/vite/utils.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export function extractFunctionReexportQueryParameters(query: string): string[]
5757
return match && match[1]
5858
? match[1]
5959
.split(',')
60-
.filter(param => param !== '' && param !== 'default')
60+
.filter(param => param !== '')
6161
// Sanitize, as code could be injected with another rollup plugin
6262
.map((str: string) => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'))
6363
: [];
@@ -72,10 +72,11 @@ export function constructFunctionReExport(pathWithQuery: string, entryId: string
7272
return functionNames.reduce(
7373
(functionsCode, currFunctionName) =>
7474
functionsCode.concat(
75-
`export async function ${currFunctionName}(...args) {\n` +
75+
'async function reExport(...args) {\n' +
7676
` const res = await import(${JSON.stringify(entryId)});\n` +
7777
` return res.${currFunctionName}.call(this, ...args);\n` +
78-
'}\n',
78+
'}\n' +
79+
`export { reExport as ${currFunctionName} };\n`,
7980
),
8081
'',
8182
);

packages/nuxt/test/vite/utils.test.ts

+22-4
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ describe('removeSentryQueryFromPath', () => {
8888
describe('extractFunctionReexportQueryParameters', () => {
8989
it.each([
9090
[`${SENTRY_FUNCTIONS_REEXPORT}foo,bar,${QUERY_END_INDICATOR}`, ['foo', 'bar']],
91-
[`${SENTRY_FUNCTIONS_REEXPORT}foo,bar,default${QUERY_END_INDICATOR}`, ['foo', 'bar']],
91+
[`${SENTRY_FUNCTIONS_REEXPORT}foo,bar,default${QUERY_END_INDICATOR}`, ['foo', 'bar', 'default']],
9292
[
9393
`${SENTRY_FUNCTIONS_REEXPORT}foo,a.b*c?d[e]f(g)h|i\\\\j(){hello},${QUERY_END_INDICATOR}`,
9494
['foo', 'a\\.b\\*c\\?d\\[e\\]f\\(g\\)h\\|i\\\\\\\\j\\(\\)\\{hello\\}'],
@@ -111,18 +111,36 @@ describe('constructFunctionReExport', () => {
111111
const result2 = constructFunctionReExport(query2, entryId);
112112

113113
const expected = `
114-
export async function foo(...args) {
114+
async function reExport(...args) {
115115
const res = await import("./module");
116116
return res.foo.call(this, ...args);
117117
}
118-
export async function bar(...args) {
118+
export { reExport as foo };
119+
async function reExport(...args) {
119120
const res = await import("./module");
120121
return res.bar.call(this, ...args);
121-
}`;
122+
}
123+
export { reExport as bar };
124+
`;
122125
expect(result.trim()).toBe(expected.trim());
123126
expect(result2.trim()).toBe(expected.trim());
124127
});
125128

129+
it('constructs re-export code for a "default" query parameters and entry ID', () => {
130+
const query = `${SENTRY_FUNCTIONS_REEXPORT}default${QUERY_END_INDICATOR}}`;
131+
const entryId = './index';
132+
const result = constructFunctionReExport(query, entryId);
133+
134+
const expected = `
135+
async function reExport(...args) {
136+
const res = await import("./index");
137+
return res.default.call(this, ...args);
138+
}
139+
export { reExport as default };
140+
`;
141+
expect(result.trim()).toBe(expected.trim());
142+
});
143+
126144
it('returns an empty string if the query string is empty', () => {
127145
const query = '';
128146
const entryId = './module';

0 commit comments

Comments
 (0)