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
lukastaegert
merged 9 commits into
rollup:master
from
wh1tevs:feat/keep-extension-for-amd
Aug 31, 2022
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b543795
feat: add option to keep extensions for amd
wh1tevs b331d2d
update extension resolving for amd
wh1tevs 913312a
add tests
wh1tevs 4289689
fix 'output-options-hook' test
wh1tevs f3a1d0a
add docs
wh1tevs d27fd4e
Update docs/999-big-list-of-options.md
lukastaegert a412009
Change extension name to forceJsExtensionForImports
lukastaegert 4427349
Improve coverage
lukastaegert 22a6dfb
Merge branch 'master' into feat/keep-extension-for-amd
lukastaegert File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function addJsExtension(name: string): string { | ||
return !name.endsWith('.js') ? name + '.js' : name; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import addJsExtension from './addJsExtension'; | ||
import removeJsExtension from './removeJsExtension'; | ||
|
||
// AMD resolution will only respect the AMD baseUrl if the .js extension is omitted. | ||
// The assumption is that this makes sense for all relative ids: | ||
// https://requirejs.org/docs/api.html#jsfiles | ||
export default function updateExtensionForRelativeAmdId( | ||
id: string, | ||
keepExtension: boolean | ||
): string { | ||
if (id[0] !== '.') { | ||
return id; | ||
} | ||
|
||
return keepExtension ? addJsExtension(id) : removeJsExtension(id); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module.exports = { | ||
description: 'keep extension for AMD modules', | ||
options: { | ||
external: ['./foo', 'baz/quux'], | ||
output: { interop: 'default', amd: { keepExtension: true } } | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
define(['./foo.js', 'baz/quux'], (function (foo, baz) { 'use strict'; | ||
|
||
const bar = 42; | ||
|
||
console.log(foo, bar, baz); | ||
|
||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
'use strict'; | ||
|
||
var foo = require('./foo'); | ||
var baz = require('baz/quux'); | ||
|
||
const bar = 42; | ||
|
||
console.log(foo, bar, baz); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import foo from './foo'; | ||
import baz from 'baz/quux'; | ||
|
||
const bar = 42; | ||
|
||
console.log(foo, bar, baz); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
(function (foo, baz) { | ||
'use strict'; | ||
|
||
const bar = 42; | ||
|
||
console.log(foo, bar, baz); | ||
|
||
})(foo, baz); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
System.register(['./foo', 'baz/quux'], (function () { | ||
'use strict'; | ||
var foo, baz; | ||
return { | ||
setters: [function (module) { | ||
foo = module["default"]; | ||
}, function (module) { | ||
baz = module["default"]; | ||
}], | ||
execute: (function () { | ||
|
||
const bar = 42; | ||
|
||
console.log(foo, bar, baz); | ||
|
||
}) | ||
}; | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('./foo'), require('baz/quux')) : | ||
typeof define === 'function' && define.amd ? define(['./foo.js', 'baz/quux'], factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.foo, global.baz)); | ||
})(this, (function (foo, baz) { 'use strict'; | ||
|
||
const bar = 42; | ||
|
||
console.log(foo, bar, baz); | ||
|
||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const bar = 42; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import foo from './foo'; | ||
import { bar } from './bar'; | ||
import baz from 'baz/quux'; | ||
|
||
console.log(foo, bar, baz); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 withdep.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
istrue
and module is local dependency (like./foo
), extension will be added, if it package, external or id (likebar
orlib/baz/quux
) extension will be removed,for example:
(
output.amd.keepExtension: true
)(
output.amd.keepExtension: false
) // defaultIt 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.
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, andb) 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.
I think it should be
amd.addJsExtension
oramd.foreceJsExtensionForImports
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.No, if
amd.keepExtension: false
. If true, result should be[name].amd.js
, i add test just in caseThere 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.
That is an even better name