-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow defining the moduleIds-related option in the transform p… (#11194)
* Update "moduleIds" tests * Allow defining the moduleIds related options in the transform plugins - moduleIds - moduleId - getModuleId - moduleRoot * Sort deps
- Loading branch information
1 parent
3ce7c2e
commit a875560
Showing
52 changed files
with
342 additions
and
71 deletions.
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
52 changes: 52 additions & 0 deletions
52
packages/babel-helper-module-transforms/src/get-module-name.js
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,52 @@ | ||
// @flow | ||
|
||
export default function getModuleName( | ||
rootOpts: Object, | ||
pluginOpts: Object, | ||
): ?string { | ||
const { | ||
filename, | ||
filenameRelative = filename, | ||
|
||
sourceRoot = pluginOpts.moduleRoot ?? rootOpts.moduleRoot, | ||
} = rootOpts; | ||
|
||
const { | ||
moduleId = rootOpts.moduleId, | ||
moduleIds = rootOpts.moduleIds ?? !!moduleId, | ||
|
||
getModuleId = rootOpts.getModuleId, | ||
|
||
moduleRoot = rootOpts.moduleRoot ?? sourceRoot, | ||
} = pluginOpts; | ||
|
||
if (!moduleIds) return null; | ||
|
||
// moduleId is n/a if a `getModuleId()` is provided | ||
if (moduleId != null && !getModuleId) { | ||
return moduleId; | ||
} | ||
|
||
let moduleName = moduleRoot != null ? moduleRoot + "/" : ""; | ||
|
||
if (filenameRelative) { | ||
const sourceRootReplacer = | ||
sourceRoot != null ? new RegExp("^" + sourceRoot + "/?") : ""; | ||
|
||
moduleName += filenameRelative | ||
// remove sourceRoot from filename | ||
.replace(sourceRootReplacer, "") | ||
// remove extension | ||
.replace(/\.(\w*?)$/, ""); | ||
} | ||
|
||
// normalize path separators | ||
moduleName = moduleName.replace(/\\/g, "/"); | ||
|
||
if (getModuleId) { | ||
// If return is falsy, assume they want us to use our generated default name | ||
return getModuleId(moduleName) || moduleName; | ||
} else { | ||
return moduleName; | ||
} | ||
} |
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
Empty file.
5 changes: 5 additions & 0 deletions
5
...plugin-transform-modules-amd/test/fixtures/amd/get-module-name-option-compat/options.json
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 @@ | ||
{ | ||
"sourceType": "module", | ||
"moduleIds": true, | ||
"moduleId": "my custom module name" | ||
} |
3 changes: 3 additions & 0 deletions
3
...el-plugin-transform-modules-amd/test/fixtures/amd/get-module-name-option-compat/output.js
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 @@ | ||
define("my custom module name", [], function () { | ||
"use strict"; | ||
}); |
9 changes: 7 additions & 2 deletions
9
.../babel-plugin-transform-modules-amd/test/fixtures/amd/get-module-name-option/options.json
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 |
---|---|---|
@@ -1,5 +1,10 @@ | ||
{ | ||
"sourceType": "module", | ||
"moduleIds": true, | ||
"moduleId": "my custom module name" | ||
"plugins": [ | ||
"external-helpers", | ||
["transform-modules-amd", { | ||
"moduleIds": true, | ||
"moduleId": "my custom module name" | ||
}] | ||
] | ||
} |
1 change: 1 addition & 0 deletions
1
packages/babel-plugin-transform-modules-amd/test/fixtures/amd/module-name-compat/input.js
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 @@ | ||
foobar(); |
4 changes: 4 additions & 0 deletions
4
...ages/babel-plugin-transform-modules-amd/test/fixtures/amd/module-name-compat/options.json
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,4 @@ | ||
{ | ||
"sourceType": "module", | ||
"moduleIds": true | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/babel-plugin-transform-modules-amd/test/fixtures/amd/module-name-compat/output.js
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 @@ | ||
define("amd/module-name-compat/input", [], function () { | ||
"use strict"; | ||
|
||
foobar(); | ||
}); |
5 changes: 4 additions & 1 deletion
5
packages/babel-plugin-transform-modules-amd/test/fixtures/amd/module-name/options.json
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 |
---|---|---|
@@ -1,4 +1,7 @@ | ||
{ | ||
"sourceType": "module", | ||
"moduleIds": true | ||
"plugins": [ | ||
"external-helpers", | ||
["transform-modules-amd", { "moduleIds": true }] | ||
] | ||
} |
Empty file.
6 changes: 6 additions & 0 deletions
6
...ugin-transform-modules-amd/test/fixtures/loose/get-module-name-option-compat/options.json
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 @@ | ||
{ | ||
"sourceType": "module", | ||
"moduleIds": true, | ||
"moduleId": "my custom module name", | ||
"plugins": ["external-helpers", ["transform-modules-amd", { "loose": true }]] | ||
} |
3 changes: 3 additions & 0 deletions
3
...-plugin-transform-modules-amd/test/fixtures/loose/get-module-name-option-compat/output.js
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 @@ | ||
define("my custom module name", [], function () { | ||
"use strict"; | ||
}); |
11 changes: 8 additions & 3 deletions
11
...abel-plugin-transform-modules-amd/test/fixtures/loose/get-module-name-option/options.json
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 |
---|---|---|
@@ -1,6 +1,11 @@ | ||
{ | ||
"sourceType": "module", | ||
"moduleIds": true, | ||
"moduleId": "my custom module name", | ||
"plugins": ["external-helpers", ["transform-modules-amd", { "loose": true }]] | ||
"plugins": [ | ||
"external-helpers", | ||
["transform-modules-amd", { | ||
"loose": true, | ||
"moduleIds": true, | ||
"moduleId": "my custom module name" | ||
}] | ||
] | ||
} |
1 change: 1 addition & 0 deletions
1
packages/babel-plugin-transform-modules-amd/test/fixtures/loose/module-name-compat/input.js
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 @@ | ||
foobar(); |
5 changes: 5 additions & 0 deletions
5
...es/babel-plugin-transform-modules-amd/test/fixtures/loose/module-name-compat/options.json
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 @@ | ||
{ | ||
"sourceType": "module", | ||
"moduleIds": true, | ||
"plugins": ["external-helpers", ["transform-modules-amd", { "loose": true }]] | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/babel-plugin-transform-modules-amd/test/fixtures/loose/module-name-compat/output.js
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 @@ | ||
define("loose/module-name-compat/input", [], function () { | ||
"use strict"; | ||
|
||
foobar(); | ||
}); |
9 changes: 7 additions & 2 deletions
9
packages/babel-plugin-transform-modules-amd/test/fixtures/loose/module-name/options.json
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 |
---|---|---|
@@ -1,5 +1,10 @@ | ||
{ | ||
"sourceType": "module", | ||
"moduleIds": true, | ||
"plugins": ["external-helpers", ["transform-modules-amd", { "loose": true }]] | ||
"plugins": [ | ||
"external-helpers", | ||
["transform-modules-amd", { | ||
"loose": true, | ||
"moduleIds": 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
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
Empty file.
4 changes: 4 additions & 0 deletions
4
...nsform-modules-systemjs/test/fixtures/systemjs/get-module-name-option-compat/options.json
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,4 @@ | ||
{ | ||
"moduleIds": true, | ||
"moduleId": "my custom module name" | ||
} |
8 changes: 8 additions & 0 deletions
8
...transform-modules-systemjs/test/fixtures/systemjs/get-module-name-option-compat/output.js
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 @@ | ||
System.register("my custom module name", [], function (_export, _context) { | ||
"use strict"; | ||
|
||
return { | ||
setters: [], | ||
execute: function () {} | ||
}; | ||
}); |
9 changes: 7 additions & 2 deletions
9
...gin-transform-modules-systemjs/test/fixtures/systemjs/get-module-name-option/options.json
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 |
---|---|---|
@@ -1,4 +1,9 @@ | ||
{ | ||
"moduleIds": true, | ||
"moduleId": "my custom module name" | ||
"plugins": [ | ||
"external-helpers", | ||
["transform-modules-systemjs", { | ||
"moduleIds": true, | ||
"moduleId": "my custom module name" | ||
}] | ||
] | ||
} |
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
Empty file.
6 changes: 6 additions & 0 deletions
6
...ugin-transform-modules-umd/test/fixtures/loose/get-module-name-option-compat/options.json
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 @@ | ||
{ | ||
"sourceType": "module", | ||
"moduleIds": true, | ||
"moduleId": "my custom module name", | ||
"plugins": ["external-helpers", ["transform-modules-umd", { "loose": true }]] | ||
} |
15 changes: 15 additions & 0 deletions
15
...-plugin-transform-modules-umd/test/fixtures/loose/get-module-name-option-compat/output.js
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,15 @@ | ||
(function (global, factory) { | ||
if (typeof define === "function" && define.amd) { | ||
define("my custom module name", [], factory); | ||
} else if (typeof exports !== "undefined") { | ||
factory(); | ||
} else { | ||
var mod = { | ||
exports: {} | ||
}; | ||
factory(); | ||
global.myCustomModuleName = mod.exports; | ||
} | ||
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function () { | ||
"use strict"; | ||
}); |
11 changes: 8 additions & 3 deletions
11
...abel-plugin-transform-modules-umd/test/fixtures/loose/get-module-name-option/options.json
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 |
---|---|---|
@@ -1,6 +1,11 @@ | ||
{ | ||
"sourceType": "module", | ||
"moduleIds": true, | ||
"moduleId": "my custom module name", | ||
"plugins": ["external-helpers", ["transform-modules-umd", { "loose": true }]] | ||
"plugins": [ | ||
"external-helpers", | ||
["transform-modules-umd", { | ||
"loose": true, | ||
"moduleIds": true, | ||
"moduleId": "my custom module name" | ||
}] | ||
] | ||
} |
1 change: 1 addition & 0 deletions
1
packages/babel-plugin-transform-modules-umd/test/fixtures/loose/module-name-compat/input.js
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 @@ | ||
foobar(); |
5 changes: 5 additions & 0 deletions
5
...es/babel-plugin-transform-modules-umd/test/fixtures/loose/module-name-compat/options.json
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 @@ | ||
{ | ||
"sourceType": "module", | ||
"moduleIds": true, | ||
"plugins": ["external-helpers", ["transform-modules-umd", { "loose": true }]] | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/babel-plugin-transform-modules-umd/test/fixtures/loose/module-name-compat/output.js
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,17 @@ | ||
(function (global, factory) { | ||
if (typeof define === "function" && define.amd) { | ||
define("loose/module-name-compat/input", [], factory); | ||
} else if (typeof exports !== "undefined") { | ||
factory(); | ||
} else { | ||
var mod = { | ||
exports: {} | ||
}; | ||
factory(); | ||
global.looseModuleNameCompatInput = mod.exports; | ||
} | ||
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function () { | ||
"use strict"; | ||
|
||
foobar(); | ||
}); |
1 change: 1 addition & 0 deletions
1
...sform-modules-umd/test/fixtures/loose/module-name-with-overridden-global-compat/input.mjs
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 default 42; |
Oops, something went wrong.