From faf0e5d825b84d1f8f31ab2c8eeb2d012922309e Mon Sep 17 00:00:00 2001 From: Hendrik Liebau Date: Tue, 14 May 2024 17:56:06 +0200 Subject: [PATCH] Add support for parsing import assertions & attributes to rsc & client loaders --- .changeset/soft-garlics-do.md | 5 ++++ .../src/webpack-rsc-client-loader.cts | 1 + .../src/webpack-rsc-client-loader.test.ts | 28 +++++++++++++++++++ .../src/webpack-rsc-server-loader.cts | 1 + .../src/webpack-rsc-server-loader.test.ts | 26 +++++++++++++++++ 5 files changed, 61 insertions(+) create mode 100644 .changeset/soft-garlics-do.md diff --git a/.changeset/soft-garlics-do.md b/.changeset/soft-garlics-do.md new file mode 100644 index 0000000..caa077b --- /dev/null +++ b/.changeset/soft-garlics-do.md @@ -0,0 +1,5 @@ +--- +'@mfng/webpack-rsc': minor +--- + +Add support for parsing import assertions & attributes to rsc & client loaders diff --git a/packages/webpack-rsc/src/webpack-rsc-client-loader.cts b/packages/webpack-rsc/src/webpack-rsc-client-loader.cts index abb2a6a..c557b06 100644 --- a/packages/webpack-rsc/src/webpack-rsc-client-loader.cts +++ b/packages/webpack-rsc/src/webpack-rsc-client-loader.cts @@ -32,6 +32,7 @@ function webpackRscClientLoader( const ast = parser.parse(source, { sourceType: `module`, sourceFilename: resourcePath, + plugins: [`importAssertions`], }); let hasUseServerDirective = false; diff --git a/packages/webpack-rsc/src/webpack-rsc-client-loader.test.ts b/packages/webpack-rsc/src/webpack-rsc-client-loader.test.ts index 1ae38a1..1efc698 100644 --- a/packages/webpack-rsc/src/webpack-rsc-client-loader.test.ts +++ b/packages/webpack-rsc/src/webpack-rsc-client-loader.test.ts @@ -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'}});`, + ); + }); }); diff --git a/packages/webpack-rsc/src/webpack-rsc-server-loader.cts b/packages/webpack-rsc/src/webpack-rsc-server-loader.cts index 73e022f..473c574 100644 --- a/packages/webpack-rsc/src/webpack-rsc-server-loader.cts +++ b/packages/webpack-rsc/src/webpack-rsc-server-loader.cts @@ -50,6 +50,7 @@ const webpackRscServerLoader: webpack.LoaderDefinitionFunction { + 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'}});`, + ); + }); });