Skip to content

Commit

Permalink
Add support for parsing import assertions & attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
unstubbable committed May 14, 2024
1 parent 074fa3a commit 2576ed1
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/swift-eyes-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
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ dist
lib
manifests.js
cdk.out
packages/webpack-rsc/src/__fixtures__/import-attributes.js
packages/webpack-rsc/src/__fixtures__/import-assertions.js
1 change: 1 addition & 0 deletions packages/webpack-rsc/src/__fixtures__/foo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions packages/webpack-rsc/src/__fixtures__/import-assertions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
await import('./foo.json', {assert: {type: 'json'}});
1 change: 1 addition & 0 deletions packages/webpack-rsc/src/__fixtures__/import-attributes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
await import('./foo.json', {with: {type: 'json'}});
1 change: 1 addition & 0 deletions packages/webpack-rsc/src/webpack-rsc-ssr-loader.cts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const webpackRscSsrLoader: webpack.LoaderDefinitionFunction<webpackRscSsrLoader.
const ast = parser.parse(source, {
sourceType: `module`,
sourceFilename: resourcePath,
plugins: [`importAssertions`],
});

let hasUseServerDirective = false;
Expand Down
26 changes: 26 additions & 0 deletions packages/webpack-rsc/src/webpack-rsc-ssr-loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,30 @@ export function ClientComponent({action}) {
}`.trim(),
);
});

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

const output = await callLoader(resourcePath, 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());

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

0 comments on commit 2576ed1

Please sign in to comment.