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: support external star exports on namespace objects #3474
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d7ec1e6
feat: support external star exports on namespace objects
guybedford 2033087
fixup test description, form test
guybedford 35a88be
fixup form tests
guybedford 5f4310d
fixup Object.create assignment, __PURE__ tweaks
guybedford 6b43b23
remove invalid test changes
guybedford 8ff2966
Add pure annotations again, small refactor
lukastaegert c140efc
Simplify namespace handling logic and extend tests
lukastaegert a390b33
Improve coverage
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import { RESERVED_NAMES } from '../../utils/reservedNames'; | |
import { InclusionContext } from '../ExecutionContext'; | ||
import Identifier from '../nodes/Identifier'; | ||
import { UNKNOWN_PATH } from '../utils/PathTracker'; | ||
import ExternalVariable from './ExternalVariable'; | ||
import Variable from './Variable'; | ||
|
||
export default class NamespaceVariable extends Variable { | ||
|
@@ -12,7 +13,7 @@ export default class NamespaceVariable extends Variable { | |
memberVariables: { [name: string]: Variable } = Object.create(null); | ||
module: Module; | ||
|
||
private containsExternalNamespace = false; | ||
private reexportedExternalNamespaces: ExternalVariable[] = []; | ||
private referencedEarly = false; | ||
private references: Identifier[] = []; | ||
private syntheticNamedExports: boolean; | ||
|
@@ -41,20 +42,14 @@ export default class NamespaceVariable extends Variable { | |
|
||
include(context: InclusionContext) { | ||
if (!this.included) { | ||
if (this.containsExternalNamespace) { | ||
return this.context.error({ | ||
code: 'NAMESPACE_CANNOT_CONTAIN_EXTERNAL', | ||
id: this.module.id, | ||
message: `Cannot create an explicit namespace object for module "${this.context.getModuleName()}" because it contains a reexported external namespace` | ||
}); | ||
} | ||
this.included = true; | ||
for (const identifier of this.references) { | ||
if (identifier.context.getModuleExecIndex() <= this.context.getModuleExecIndex()) { | ||
this.referencedEarly = true; | ||
break; | ||
} | ||
} | ||
this.reexportedExternalNamespaces = this.context.includeAndGetReexportedExternalNamespaces(); | ||
if (this.context.preserveModules) { | ||
for (const memberName of Object.keys(this.memberVariables)) | ||
this.memberVariables[memberName].include(context); | ||
|
@@ -67,8 +62,9 @@ export default class NamespaceVariable extends Variable { | |
|
||
initialise() { | ||
for (const name of this.context.getExports().concat(this.context.getReexports())) { | ||
if (name[0] === '*' && name.length > 1) this.containsExternalNamespace = true; | ||
this.memberVariables[name] = this.context.traceExport(name); | ||
if (name[0] !== '*') { | ||
this.memberVariables[name] = this.context.traceExport(name); | ||
} | ||
} | ||
} | ||
|
||
|
@@ -91,21 +87,32 @@ export default class NamespaceVariable extends Variable { | |
return `${t}${safeName}: ${original.getName()}`; | ||
}); | ||
|
||
members.unshift(`${t}__proto__:${_}null`); | ||
|
||
if (options.namespaceToStringTag) { | ||
members.unshift(`${t}[Symbol.toStringTag]:${_}'Module'`); | ||
} | ||
|
||
const name = this.getName(); | ||
const hasExternalReexports = this.reexportedExternalNamespaces.length > 0; | ||
if (!hasExternalReexports) members.unshift(`${t}__proto__:${_}null`); | ||
|
||
let output = `{${n}${members.join(`,${n}`)}${n}}`; | ||
if (this.syntheticNamedExports) { | ||
output = `/*#__PURE__*/Object.assign(${output}, ${this.module.getDefaultExport().getName()})`; | ||
if (hasExternalReexports || this.syntheticNamedExports) { | ||
const assignmentArgs = members.length > 0 ? [output] : []; | ||
if (hasExternalReexports) { | ||
assignmentArgs.unshift( | ||
'/*#__PURE__*/Object.create(null)', | ||
...this.reexportedExternalNamespaces.map(variable => variable.getName()) | ||
); | ||
} | ||
if (this.syntheticNamedExports) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is a little nicer to have synthetic exports in the same assignment |
||
assignmentArgs.push(this.module.getDefaultExport().getName()); | ||
} | ||
output = `/*#__PURE__*/Object.assign(${assignmentArgs.join(`,${_}`)})`; | ||
} | ||
if (options.freeze) { | ||
output = `/*#__PURE__*/Object.freeze(${output})`; | ||
} | ||
|
||
const name = this.getName(); | ||
output = `${options.varOrConst} ${name}${_}=${_}${output};`; | ||
|
||
if (options.format === 'system' && this.exportName) { | ||
|
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,6 @@ | ||
define(['external'],function(x){'use strict';x=x&&Object.prototype.hasOwnProperty.call(x,'default')?x['default']:x;var self=/*#__PURE__*/Object.freeze({[Symbol.toStringTag]:'Module',__proto__:null,get default(){return foo}});console.log(self); | ||
define(['external'],function(x){'use strict';x=x&&Object.prototype.hasOwnProperty.call(x,'default')?x['default']:x;var self=/*#__PURE__*/Object.freeze({__proto__:null,[Symbol.toStringTag]:'Module',get default(){return foo}});console.log(self); | ||
function foo () { | ||
console.log( x ); | ||
} | ||
// trailing comment | ||
return foo;}); | ||
return foo;}); |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
var foo=(function(x){'use strict';x=x&&Object.prototype.hasOwnProperty.call(x,'default')?x['default']:x;var self=/*#__PURE__*/Object.freeze({[Symbol.toStringTag]:'Module',__proto__:null,get default(){return foo}});console.log(self); | ||
var foo=(function(x){'use strict';x=x&&Object.prototype.hasOwnProperty.call(x,'default')?x['default']:x;var self=/*#__PURE__*/Object.freeze({__proto__:null,[Symbol.toStringTag]:'Module',get default(){return foo}});console.log(self); | ||
function foo () { | ||
console.log( x ); | ||
} | ||
// trailing comment | ||
return foo;}(x)); | ||
return foo;}(x)); |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
(function(g,f){typeof exports==='object'&&typeof module!=='undefined'?module.exports=f(require('external')):typeof define==='function'&&define.amd?define(['external'],f):(g=g||self,g.foo=f(g.x));}(this,(function(x){'use strict';x=x&&Object.prototype.hasOwnProperty.call(x,'default')?x['default']:x;var self=/*#__PURE__*/Object.freeze({[Symbol.toStringTag]:'Module',__proto__:null,get default(){return foo}});console.log(self); | ||
(function(g,f){typeof exports==='object'&&typeof module!=='undefined'?module.exports=f(require('external')):typeof define==='function'&&define.amd?define(['external'],f):(g=g||self,g.foo=f(g.x));}(this,(function(x){'use strict';x=x&&Object.prototype.hasOwnProperty.call(x,'default')?x['default']:x;var self=/*#__PURE__*/Object.freeze({__proto__:null,[Symbol.toStringTag]:'Module',get default(){return foo}});console.log(self); | ||
function foo () { | ||
console.log( x ); | ||
} | ||
// trailing comment | ||
return foo;}))); | ||
return foo;}))); |
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
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,24 @@ | ||
module.exports = { | ||
description: 'supports namespaces with external star reexports', | ||
options: { | ||
external: ['external1', 'external2'], | ||
plugins: { | ||
transform(code, id) { | ||
if (id.endsWith('override.js')) { | ||
return { | ||
code, | ||
syntheticNamedExports: true | ||
}; | ||
} | ||
return null; | ||
} | ||
}, | ||
output: { | ||
globals: { | ||
external1: 'external1', | ||
external2: 'external2' | ||
}, | ||
name: 'bundle' | ||
} | ||
} | ||
}; |
21 changes: 21 additions & 0 deletions
21
test/form/samples/ns-external-star-reexport/_expected/amd.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,21 @@ | ||
define(['exports', 'external1', 'external2'], function (exports, external1, external2) { 'use strict'; | ||
|
||
var reexportExternal = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.assign(/*#__PURE__*/Object.create(null), external1)); | ||
|
||
const extra = 'extra'; | ||
|
||
const override = 'override'; | ||
var reexportExternalsWithOverride = { synthetic: 'synthetic' }; | ||
|
||
var reexportExternalsWithOverride$1 = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.assign(/*#__PURE__*/Object.create(null), external1, external2, { | ||
override: override, | ||
'default': reexportExternalsWithOverride, | ||
extra: extra | ||
}, reexportExternalsWithOverride)); | ||
|
||
exports.external = reexportExternal; | ||
exports.externalOverride = reexportExternalsWithOverride$1; | ||
|
||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
|
||
}); |
22 changes: 22 additions & 0 deletions
22
test/form/samples/ns-external-star-reexport/_expected/cjs.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,22 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
|
||
var external1 = require('external1'); | ||
var external2 = require('external2'); | ||
|
||
var reexportExternal = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.assign(/*#__PURE__*/Object.create(null), external1)); | ||
|
||
const extra = 'extra'; | ||
|
||
const override = 'override'; | ||
var reexportExternalsWithOverride = { synthetic: 'synthetic' }; | ||
|
||
var reexportExternalsWithOverride$1 = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.assign(/*#__PURE__*/Object.create(null), external1, external2, { | ||
override: override, | ||
'default': reexportExternalsWithOverride, | ||
extra: extra | ||
}, reexportExternalsWithOverride)); | ||
|
||
exports.external = reexportExternal; | ||
exports.externalOverride = reexportExternalsWithOverride$1; |
Oops, something went wrong.
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.
I found it a little more straightforward to not push all external namespaces individually to the namespace variable but rather have the context function take care of all that.