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(dynamic-import-vars): keep dynamic import assert arg intact #1504

Merged
merged 1 commit into from
Aug 11, 2023

Conversation

jorenbroekema
Copy link
Contributor

Rollup Plugin Name: dynamic-import-vars

This PR contains:

  • bugfix
  • feature
  • refactor
  • documentation
  • other

Are tests included?

  • yes (bugfixes and features will not be merged without tests)
  • no

Breaking Changes?

  • yes (breaking changes will not be merged unless absolutely necessary)
  • no

If yes, then include "BREAKING CHANGES:" in the first commit message body, followed by a description of what is breaking.

List any relevant issue numbers: #1503

Description

When transforming dynamic imports with variables in them, the second argument (import assertions) is stripped away and this information is lost. This Pull Request addresses that problem (see issue) by grabbing the argument Node and stringifying it and injecting it into the Magic String, so that this argument is kept as is.

This is needed for other plugins to correctly deal with imports that have import assertions, most notably https://github.com/jleeson/rollup-plugin-import-css which for CSS imports returns either a CSS string or CSSStyleSheet depending on whether or not an import assertion for CSS is present. If such an assertion is present, but dynamic-import-vars strips it away, the CSS plugin will bundle the CSS import as a CSS string rather than CSSStyleSheet, which would be different from local development, before bundling, since browsers natively do support import assertions now and return a CSSStyleSheet.

I might need some help with the testing, as it currently doesn't verify that the assertion is kept intact, I'm not sure how to test that, it seems in the final output[0].code the assertions are gone again, but when I test my changes in my local project, it does fix my issue.. and when I console.log the transform hook in this plugin with my changes, it does seem like it returns the correct code.

Copy link
Member

@lukastaegert lukastaegert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks mostly fine to me, but the comment about adding the acorn plugin makes me curious as this plugin is already added by Rollup.

Comment on lines 148 to 158
## Import Assertions

This plugin will keep your import assertions inside dynamic import statements intact, provided that you add the `acorn-import-assertions` syntax plugin to your Rollup config:

```js
import { importAssertions } from 'acorn-import-assertions';
import dynamicImportVars from '@rollup/plugin-dynamic-import-vars';

export default {
plugins: [dynamicImportVars()],
acornInjectPlugins: [importAssertions]
};
```

Will ensure the following import assertions remain intact:

```js
// Refer to rollup-plugin-import-css https://github.com/jleeson/rollup-plugin-import-css
function importLocale(sheet) {
return import(`./styles/${sheet}.css`, { assert: { type: 'css' } });
}
```

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be needed as Rollup includes that plugin by default: https://github.com/rollup/rollup/blob/6797d576bf5b221e6592882d8d905da8dc978479/src/utils/options/normalizeInputOptions.ts#L2-L2
Did you try this? Does updating the Rollup version in the tests help?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, it works without needing to add it manually. I guess I based that off of some other plugin that was based on an older version of Rollup where this was still required.

@shellscape
Copy link
Collaborator

@jorenbroekema please give the replies here a look 🙏 if we reach 60 days without a reply, we'll consider the PR to be abandoned.

@jorenbroekema
Copy link
Contributor Author

jorenbroekema commented Jun 19, 2023

Thanks for the reminder @shellscape , I addressed your comments, I might still need a hand on the test, the snapshot output seems to show the assert arg not appearing in the output, I'm not sure that's intended and I'm missing something, or my test is incorrect? Again, locally I tested this patch again, and it still does fix my issue, but I guess it would be nice if the test I am adding is a working regression test for my issue and not just a light smoke test

Copy link
Collaborator

@shellscape shellscape left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that test is sufficient for now. Thanks for adding it. @lukastaegert please have another look.

Copy link
Member

@lukastaegert lukastaegert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me 👍

@shellscape
Copy link
Collaborator

Sorry about the delay getting this merged. Thanks again!

@shellscape shellscape merged commit a240861 into rollup:master Aug 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants