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

Allow addons to customize es6 module prefix #2189

Merged
merged 1 commit into from
Oct 5, 2014
Merged

Allow addons to customize es6 module prefix #2189

merged 1 commit into from
Oct 5, 2014

Conversation

bcardarella
Copy link
Contributor

Addons can now customize the module prefix they are compiled under:

// index.js

module.exports = {
  name: 'some-addon',
  modulePrefix: 'other-addon'
}

Normally addons would use name to compile under. If modulePrefix is
declared that is used instead.

Use Case!!

During the ember-validations rewrite I've realized that I'd like to have
another library: ember-validations-addons or something to that effect.
It will contain additional validators that don't need to be part of the
original library. (like the URI validator) But I still need to lookup
validators in the ember-validations namespace rather than attempting
to manage additional namespaces. This would allow others to also write
their own addons in the same namespace.

@@ -27,4 +27,4 @@
"express": "^4.1.1",
"glob": "^3.2.9"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

no idea why this has a diff on it, I didn't touch the file

Copy link
Member

Choose a reason for hiding this comment

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

you removed the newline character at the end of the last line :)

@ahacking
Copy link

ahacking commented Oct 4, 2014

I did something similar in my brunch es6 module transpiler plugin at https://github.com/ahacking/es6-module-transpiler-js-brunch/blob/master/README.md but I made the option a function so that it can transform the module name arbitrarily. This was useful for being able to make a module appear under any prefix I wanted independent of the file layout and allowed my interim brunch solution to comply with the ember-cli naming conventions.

@bcardarella
Copy link
Contributor Author

@ahacking this is possible via overriding the moduleName() function that is in the Addon model. This PR just makes more use of that function.


this._includedModules = glob.sync(path.join(treePath, this._jsFileGlob())).reduce(function(ignoredModules, filePath) {
ignoredModules[filePath.replace(treePath, moduleName).slice(0, -3)] = ['default'];
modulePath = filePath.replace(treePath, _this.moduleName());
modulePath = modulePath.replace(path.extname(modulePath), '');
Copy link
Member

Choose a reason for hiding this comment

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

Considering the following:

modulePath = 'bower_components/loader.js/loader.js'
modulePath = modulePath.replace('.js', '')

// => "bower_components/loader/loader.js"

This might need to be tweaked to:

modulePath = modulePath.replace(new RegExp(path.extname(modulePath) + '$'), '');

Addons can now customize the module prefix they are compiled under:

```js
// index.js

module.exports = {
  name: 'some-addon',
  modulePrefix: 'other-addon'
}
```

Normally addons would use `name` to compile under. If `modulePrefix` is
declared that is used instead.

Use Case!!

During the ember-validations rewrite I've realized that I'd like to have
another library: ember-validations-addons or something to that effect.
It will contain additional validators that don't need to be part of the
original library. (like the URI validator) But I still need to lookup
validators in the `ember-validations` namespace rather than attempting
to manage additional namespaces. This would allow others to also write
their own addons in the same namespace.
@rwjblue
Copy link
Member

rwjblue commented Oct 5, 2014

👍

rwjblue added a commit that referenced this pull request Oct 5, 2014
…ixes

Allow addons to customize es6 module prefix
@rwjblue rwjblue merged commit 57c8b6b into ember-cli:master Oct 5, 2014
@bcardarella bcardarella deleted the bc-custom-addon-module-prefixes branch October 5, 2014 01:48
@ahacking
Copy link

ahacking commented Oct 5, 2014

@bcardarella. Thanks for the clarification.

@kristianmandrup
Copy link

@bcardarella Great idea :)

"This would allow others to also write their own addons in the same namespace."

Looking forward to your ember-validations rewrite.

Also check out webmake, if you need to turn node.js code into web compatible browser code which resolves in exactly the same way as in node ;) Looks pretty cool to me.

https://github.com/medikoo/modules-webmake#comparison-with-other-solutions

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.

6 participants