Skip to content

Commit

Permalink
Add support for parsing import assertions & attributes to rsc & clien…
Browse files Browse the repository at this point in the history
…t loaders
  • Loading branch information
unstubbable committed May 14, 2024
1 parent f119bef commit faf0e5d
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/soft-garlics-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@mfng/webpack-rsc': minor
---

Add support for parsing import assertions & attributes to rsc & client loaders
1 change: 1 addition & 0 deletions packages/webpack-rsc/src/webpack-rsc-client-loader.cts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function webpackRscClientLoader(
const ast = parser.parse(source, {
sourceType: `module`,
sourceFilename: resourcePath,
plugins: [`importAssertions`],
});

let hasUseServerDirective = false;
Expand Down
28 changes: 28 additions & 0 deletions packages/webpack-rsc/src/webpack-rsc-client-loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,32 @@ export const foo = createServerReference("test#foo", callServer);

expect(output).toEqual(source);
});

test(`can parse import assertions`, async () => {
const resourcePath = path.resolve(
currentDirname,
`__fixtures__/import-assertions.js`,
);

const serverReferencesMap = new Map();
const output = await callLoader(resourcePath, {serverReferencesMap});

expect(output.toString().trim()).toEqual(
`await import('./foo.json', {assert: {type: 'json'}});`,
);
});

test(`can parse import attributes`, async () => {
const resourcePath = path.resolve(
currentDirname,
`__fixtures__/import-attributes.js`,
);

const serverReferencesMap = new Map();
const output = await callLoader(resourcePath, {serverReferencesMap});

expect(output.toString().trim()).toEqual(
`await import('./foo.json', {with: {type: 'json'}});`,
);
});
});
1 change: 1 addition & 0 deletions packages/webpack-rsc/src/webpack-rsc-server-loader.cts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const webpackRscServerLoader: webpack.LoaderDefinitionFunction<webpackRscServerL
const ast = parser.parse(source, {
sourceType: `module`,
sourceFilename: resourcePath,
plugins: [`importAssertions`],
});

let moduleDirective: 'use client' | 'use server' | undefined;
Expand Down
26 changes: 26 additions & 0 deletions packages/webpack-rsc/src/webpack-rsc-server-loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,30 @@ export { qux };

expect(output).toEqual(source);
});

test(`can parse import assertions`, async () => {
const resourcePath = path.resolve(
currentDirname,
`__fixtures__/import-assertions.js`,
);

const output = await callLoader(resourcePath, new Map(), new Map());

expect(output.toString().trim()).toEqual(
`await import('./foo.json', {assert: {type: 'json'}});`,
);
});

test(`can parse import attributes`, async () => {
const resourcePath = path.resolve(
currentDirname,
`__fixtures__/import-attributes.js`,
);

const output = await callLoader(resourcePath, new Map(), new Map());

expect(output.toString().trim()).toEqual(
`await import('./foo.json', {with: {type: 'json'}});`,
);
});
});

0 comments on commit faf0e5d

Please sign in to comment.