Skip to content

Add the vue template compiler onto any template loader specified in the vue.loaders configuration #595

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

Merged
merged 4 commits into from
Jan 31, 2017

Conversation

asselin
Copy link

@asselin asselin commented Jan 26, 2017

This enables, for example, using a markdown loader via in a .vue file. Without this tweak, the webpack config has to specify both the loader that converts the markdown (e.g.) to HTML and template-compiler to compile it. That, however doesn't fully work because the webpack config can't know the moduleId generated by lib/loader.js and pass it in via the id parameter, so scoped styles won't work.

…he vue.loaders configuration.

This enables, for example, using a markdown loader via <template lang="md"> in a .vue file.  Without this tweak, the webpack config has
to specify both the loader that converts the markdown (e.g.) to HTML AND template-compiler to compile it.  That, however doesn't fully work
because the webpack config can't know the moduleId generated by lib/loader.js and pass it in via the `id` parameter, so scoped styles won't
work.
lib/loader.js Outdated
css: (isServer ? '' : styleLoaderPath + '!') + 'css-loader' + (needCssSourceMap ? '?sourceMap' : ''),
js: hasBuble ? ('buble-loader' + bubleOptions) : hasBabel ? 'babel-loader' : ''
}
var templateCompilerLoaderCommand = templateCompilerPath + '?id=' + moduleId
Copy link
Member

Choose a reason for hiding this comment

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

Why assign this as a separate variable here? We can just use defaultLoaders.html in both places

Copy link
Author

Choose a reason for hiding this comment

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

If defaultLoaders.html is left as-is, then for the default case, var loader = loaders[lang] on line 140 grabs that string and the new code on 151-153 below also add the same string, resulting in template-compiler?id=abc!template-compiler?id=abc for the final loader string.

My rationale for pulling it out into a separate variable was to make the 2 cases similar:

  • In the default case, the format of the template is HTML, which doesn't need a loader to translate it to HTML ('')
  • In the case where a user specifies a lang, it's entry will be the loader specified in the config.

In both cases, var loader = loaders[lang] will grab the loader necessary to translate the input language to HTML, and then we can prepend template-compiler?id=abc to it to handle compiling the HTML spit out by the loader.

Having said that, if you prefer a different style, let me know.

Thanks!

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the explanation - I think we can simply do this for line 151:

if (type === 'template' && loader !== defaultLoaders.html) {
  loader = defaultLoaders.html + '!' + loader
}

@yyx990803 yyx990803 merged commit f1df374 into vuejs:master Jan 31, 2017
@asselin
Copy link
Author

asselin commented Jan 31, 2017

@yyx990803 Thank you for merging my 2 PRs!

@yyx990803
Copy link
Member

@asselin thank you for contributing!

xalexec pushed a commit to Transnal/vue-loader that referenced this pull request Mar 26, 2017
…he vue.loaders configuration (vuejs#595)

* Tweak to add the vue template compiler onto any loader specified in the vue.loaders configuration.

This enables, for example, using a markdown loader via <template lang="md"> in a .vue file.  Without this tweak, the webpack config has
to specify both the loader that converts the markdown (e.g.) to HTML AND template-compiler to compile it.  That, however doesn't fully work
because the webpack config can't know the moduleId generated by lib/loader.js and pass it in via the `id` parameter, so scoped styles won't
work.

* adjustments

* add tests for custom html loader
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.

2 participants