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
feat: add option to keep extensions for amd #4607
Conversation
There was a problem hiding this 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
inumd.ts
. But as we want to observe this flag anyway wherever this function is used, maybe change the function to something likeupdateExtensionForAmd(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 forstripKnownJsExtensions
. - 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 intest/form/samples
with content similar to the other tests. Add your option in_config.js
viaoptions.output.amd
. If you put both an external static and an external dynamic import into yourmain.js
(can be the same target), then it should be possible to cover everything with a single test.
Also, there are some tests broken due to the new option, but they should be easy to fix. |
Codecov Report
@@ 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
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
There was a problem hiding this 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; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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"?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this 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 |
There was a problem hiding this comment.
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"
@@ -0,0 +1,3 @@ | |||
export default function addJsExtension(name: string): string { | |||
return !name.endsWith('.js') ? name + '.js' : name; |
There was a problem hiding this comment.
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"?
3ede154
to
a412009
Compare
I changed the option name for |
This PR contains:
Are tests included?
Breaking Changes?
List any relevant issue numbers:
resolves #4606
Description