Skip to content
This repository has been archived by the owner on Dec 8, 2024. It is now read-only.

Add support for passing options to the htmlbars precompiler #11

Closed
wants to merge 5 commits into from

Conversation

jasonmit
Copy link

@jasonmit jasonmit commented May 9, 2016

ember-cli-htmlbars also sets moduleName and other transformations may rely on this property.

https://github.com/ember-cli/ember-cli-htmlbars/blob/master/index.js#L69

This PR brings the ability to implement a method to construct a moduleName based on the babel options that the babel transform provides to plugins. It will also work with any future options that may be added to the precompiler.

});

assert.equal(transformed, "var compiled = Ember.HTMLBars.template(precompiled(hello));", "tagged template is replaced");
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like this test has been duplicated unintentionally?

Copy link
Author

Choose a reason for hiding this comment

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

Nope, and reverted :)

babel.transform(code, {
blacklist: ['strict', 'es6.modules'],
plugins: [HTMLBarsInlinePrecompile(precompile)],
filenameRelative: 'module-name-test.js'
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry to be such a complainer here, but I am currently not sure if the current test fully covers what the moduleName functionality does. Do you think there is a way we can use babel.transformFileSync or something, so it tests that the file.opts.filenameRelative is the correct value? I just want to make sure that if babel is transforming templates from a file, then we are reading the correct value. Using filenameRelative looks ok, but at the moment it looks to me like we are only stubbing the property and not ensuring that it is really the name of a file.

Copy link
Author

@jasonmit jasonmit May 10, 2016

Choose a reason for hiding this comment

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

sounds good, will update tomorrow.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks @jasonmit! Much appreciated!

@jasonmit
Copy link
Author

jasonmit commented May 11, 2016

@pangratz test updated to use transformFileSync and no longer stubbing the moduleName

var fullpath = path.join(__dirname, 'fixtures', 'hello-world.js');

var precompile = function(template, options) {
assert.equal(options.moduleName, fullpath);
Copy link
Contributor

Choose a reason for hiding this comment

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

I have just one more question: I have never used the moduleName option so forgive my ignorance, but since we're using relativeName, wouldn't we expect the moduleName be a relative path here? I have seen that there is the sourceRoot option for babel.transformFileSync, which might be the one to specify to which directory the file should be resolved relative to?

I am just asking. You said you used that successfully in one of your plugins and it works, so I am just asking because I want to understand what the moduleName option is used for...

Copy link
Author

@jasonmit jasonmit May 12, 2016

Choose a reason for hiding this comment

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

Ember uses it internally for hinting at deprecation warnings in a template. I use it for building scoped CSS modules.

As for the relativeName not being relative, it seems that way. The documentation says the opposite -- so I'm a bit confused. Lets hold off on merging until I can resolve this, we wouldn't want to leak full paths.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ember uses it internally for hinting at deprecation warnings in a template. I use it for building scoped CSS modules.

Thanks for the heads up!

Ok, waiting seems like a good idea. Thanks for looking into this!

Copy link
Author

@jasonmit jasonmit May 12, 2016

Choose a reason for hiding this comment

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

Did some digging, it looks like sourceRoot only knocks off the path when dealing with module transformations.

Instead of this PR, would you be for adding an optional second arg that has a precompilerOptions method who's return value is used to pass on to the precompiler?

HTMLBarsInlinePrecompilePlugin(precompile, {
  precompileOptions(opts) {
    // example of how someone might use it to construct their own moduleName
    var sourceRootRegEx = new RegExp("^" + opts.sourceRoot + "/?");

    return {
      moduleName: opts.filenameRelative.replace(sourceRootRegEx, "")
    };
  }
});

Not implementing the method will have the same behavior as today.

@jasonmit jasonmit changed the title Add support for moduleName Add support for passing options to the htmlbars precompiler May 12, 2016
sourceRoot: sourceRoot,
plugins: [HTMLBarsInlinePrecompile(precompile, {
precompileOptions: function(opts) {
// example of how someone might use it to construct their own moduleName
Copy link
Author

Choose a reason for hiding this comment

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

or any future options that get added

@pangratz
Copy link
Contributor

😍 that is an awesome idea! I like that API.

If you're in the mood, can you add a small section the README outlining this functionality?

I guess this only needs a squash and then it's good to go. Thanks for bearing with me here. This turned out great!

@jasonmit
Copy link
Author

Agree, I like this more and moves the responsibility away from this plugin. I'll update the README shortly.

@jasonmit
Copy link
Author

README updated. Feel free to squash on merge (relative new GitHub feature).

@rwjblue
Copy link
Member

rwjblue commented Aug 30, 2019

Replaced by #75.

@rwjblue rwjblue closed this Aug 30, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants