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

Access global.* using array notation #1144

Merged
merged 3 commits into from
Dec 18, 2016

Conversation

christopherthielen
Copy link
Contributor

Allow arbitrary moduleName (including names with dashes like ui-router or scoped npm packages like @angular/core) to be added to the global object.

Closes #582
Closes #584

this.foo.bar.baz = (function () {
this['foo'] = this['foo'] || {};
this['foo']['bar'] = this['foo']['bar'] || {};
this['foo']['bar']['baz'] = (function () {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Still processes namespaced exports by splitting on the dots

(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(factory((global['module-name-with-dashes'] = global['module-name-with-dashes'] || {})));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

allow dashes

(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(factory((global['@scoped/npm-package'] = global['@scoped/npm-package'] || {})));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

allow arbitrary strings, including npm scoped packages. Not sure this is/should be a requirement, but I added a test anyway because of #582

@Rich-Harris
Copy link
Contributor

Thanks @christopherthielen! Any chance you could modify this PR such that valid property names are left as-is? There are advantages to using dot notation, so I think it would be better if properties were only quoted as necessary since they're in the minority.

@christopherthielen
Copy link
Contributor Author

sure, no problem

This allows arbitrary `moduleName` (including names with dashes like `ui-router` or scoped npm packages like `@angular/core`) to be added to the global object.

Closes rollup#582
Closes rollup#584
@christopherthielen
Copy link
Contributor Author

Okey dokey, done.

I chose to be naive about when to quote/not quote properties. The actual rules for javascript property access are more complex: https://mathiasbynens.be/notes/javascript-identifiers . There are npm modules you could use to test if a property can be accessed using a dot or not, but it seemed overkill. This should catch 99% and quote when unsure.

Also I didn't want to force maintenance of a variable checker on the project. I did find some (low-stars) npm packages that could be used if you prefer something along those lines.

@Rich-Harris Rich-Harris merged commit dfb8a3f into rollup:master Dec 18, 2016
@Rich-Harris
Copy link
Contributor

Excellent, thank you!

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.

None yet

2 participants