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

feat: add option to keep extensions for amd #4607

Merged
merged 9 commits into from Aug 31, 2022

Conversation

wh1tevs
Copy link
Contributor

@wh1tevs wh1tevs commented Aug 15, 2022

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

List any relevant issue numbers:
resolves #4606

Description

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.

Nice one! Not sure if you are done yet, three things:

  • There is another usage of removeExtensionFromRelativeAmdId in umd.ts. But as we want to observe this flag anyway wherever this function is used, maybe change the function to something like updateExtensionForAmd(id: string, keepExtension: boolean)?
  • And there is another place where we strip AMD extensions, this time for dynamic imports. This happens in Chunk.ts, look for stripKnownJsExtensions.
  • We need a test for this. Adding a test is really simple, in this case we should go for a form test. Basically add another folder in test/form/samples with content similar to the other tests. Add your option in _config.js via options.output.amd. If you put both an external static and an external dynamic import into your main.js (can be the same target), then it should be possible to cover everything with a single test.

@lukastaegert
Copy link
Member

Also, there are some tests broken due to the new option, but they should be easy to fix.

@codecov
Copy link

codecov bot commented Aug 15, 2022

Codecov Report

Merging #4607 (22a6dfb) into master (eb1fab7) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #4607   +/-   ##
=======================================
  Coverage   98.87%   98.87%           
=======================================
  Files         209      210    +1     
  Lines        7368     7373    +5     
  Branches     2103     2106    +3     
=======================================
+ Hits         7285     7290    +5     
  Misses         27       27           
  Partials       56       56           
Impacted Files Coverage Δ
src/Chunk.ts 100.00% <100.00%> (ø)
src/finalisers/amd.ts 100.00% <100.00%> (ø)
src/finalisers/shared/addJsExtension.ts 100.00% <100.00%> (ø)
...nalisers/shared/updateExtensionForRelativeAmdId.ts 100.00% <100.00%> (ø)
src/finalisers/umd.ts 100.00% <100.00%> (ø)
src/utils/options/normalizeOutputOptions.ts 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

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 really good, but one question remains, see my comment.
Beyond that, the only thing missing is documentation of the option. There are three places where it needs to be added:

  • In the big list of options in /docs next to the other amd options (should be sorted alphabetically),
  • In help.md (this is sorted alphabetically as well)
  • In the copy of help.md that is part of the CLI documentation in /docs

@@ -0,0 +1,3 @@
export default function addJsExtension(name: string): string {
return !name.endsWith('.js') ? name + '.js' : name;
Copy link
Member

Choose a reason for hiding this comment

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

You added a new logic here, was this intended? E.g. if you have an external dependency dep.amd you would end up with dep.amd.js. And if the user decides to use chunk names without extensions, the extensions would be added anyway in the import, which will probably not work. What is the rationale? Also, it is not tested.

Copy link
Contributor Author

@wh1tevs wh1tevs Aug 15, 2022

Choose a reason for hiding this comment

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

Extension depends on output.amd.keepExtension, it described in updateExtensionForRelativeAmdId.
If keepExtension is true and module is local dependency (like ./foo), extension will be added, if it package, external or id (like bar or lib/baz/quux) extension will be removed,

for example:

import foo from './foo';
import bar from 'bar';

(output.amd.keepExtension: true)

define(['./foo.js', 'bar'], function (foo, bar) {})

(output.amd.keepExtension: false) // default

define(['./foo', 'bar'], function (foo, bar) {})

It partially tested in this commit

Copy link
Member

Choose a reason for hiding this comment

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

partially

Ah I see, the case that is not tested (and which coverage complains about) is not what I thought but actually the combination relative id + js extension present + keep extension. Maybe you can add this for coverage.

The problem I saw is that
a) there may already be a different extension, so adding the extension .js is actually wrong, and
b) there are multiple chunks (e.g. a non-external dynamic import) and the user sets e.g. output.chunkFileNames: "[name].amd". Then "keepExtension" would probably add a wrong extension.

But then again, they should not use keepExtension in that case, so I convinced myself that you are right and this is irrelevant (also the way you wrote the docs supports this). Proposal to make this clearer: Change the name to "amd.keepJsExtension" to make clear this is about the presence or absence of ".js"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Proposal to make this clearer: Change the name to "amd.keepJsExtension" to make clear this is about the presence or absence of ".js"?

I think it should be amd.addJsExtension or amd.foreceJsExtensionForImports

a) there may already be a different extension, so adding the extension .js is actually wrong, and

I do not quite understand what is at stake. If you talking about dependencies in define, then only js modules will get there. Give me a example and i add tests.

b) there are multiple chunks (e.g. a non-external dynamic import) and the user sets e.g. output.chunkFileNames: "[name].amd". Then "keepExtension" would probably add a wrong extension.

No, if amd.keepExtension: false. If true, result should be [name].amd.js, i add test just in case

Copy link
Member

Choose a reason for hiding this comment

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

amd.foreceJsExtensionForImports

That is an even better name

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.

Just some small nits left, but this is nearly ready for release. Thanks for bearing with me!

cli/help.md Outdated
@@ -23,6 +23,7 @@ Basic options:
--amd.autoId Generate the AMD ID based off the chunk name
--amd.basePath <prefix> Path to prepend to auto generated AMD ID
--amd.define <name> Function to use in place of `define`
--amd.keepExtension Add `.js` extension for generated chunks and local AMD modules
Copy link
Member

Choose a reason for hiding this comment

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

This file should wrap at 80 characters, can we slightly shorten this? E.g. "Add .js extension in imports"

docs/999-big-list-of-options.md Outdated Show resolved Hide resolved
@@ -0,0 +1,3 @@
export default function addJsExtension(name: string): string {
return !name.endsWith('.js') ? name + '.js' : name;
Copy link
Member

Choose a reason for hiding this comment

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

partially

Ah I see, the case that is not tested (and which coverage complains about) is not what I thought but actually the combination relative id + js extension present + keep extension. Maybe you can add this for coverage.

The problem I saw is that
a) there may already be a different extension, so adding the extension .js is actually wrong, and
b) there are multiple chunks (e.g. a non-external dynamic import) and the user sets e.g. output.chunkFileNames: "[name].amd". Then "keepExtension" would probably add a wrong extension.

But then again, they should not use keepExtension in that case, so I convinced myself that you are right and this is irrelevant (also the way you wrote the docs supports this). Proposal to make this clearer: Change the name to "amd.keepJsExtension" to make clear this is about the presence or absence of ".js"?

@lukastaegert
Copy link
Member

I changed the option name for output.amd.forceJsExtensionForImports and added the missing test case so that coverage should be fine now. Will release this if CI runs through.

@lukastaegert lukastaegert enabled auto-merge (squash) August 31, 2022 04:44
@lukastaegert lukastaegert merged commit 71eefd1 into rollup:master Aug 31, 2022
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.

Add support to keep '.js' extension for amd
2 participants