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

Export format options, accept different import formats, fixes #63 #64

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ and `prefixRegExp` which apply to single parameters, or `regExp` which will appl

Make sure you use the same path separator for the `prefix` and `relativeTo` parameters, all templateUrls and in your webpack.config.js file.

### Export formats

There are different export formats available:

+ ```module.exports``` (default, cjs format). "myTemplateUrl" becomes ```module.exports = "myTemplateUrl";```
+ ```exports.default``` (when ```exportAsDefault``` param is set, es6to5 format). "myTemplateUrl" becomes ```exports.default = "myTemplateUrl";```
+ ```export default``` (when ```exportAsEs6Default``` param is set, es6 format). "myTemplateUrl" becomes ```export default "myTemplateUrl";```

### Using with npm requires

This module relies on angular being available on `window` object. However, in cases angular is connected from `node_modules` via `require('angular')`, option to force this module to get the angular should be used:
Expand Down
13 changes: 11 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ module.exports = function (content) {
var pathSep = options.pathSep || '/';
var resource = this.resource;
var pathSepRegex = new RegExp(escapeRegExp(path.sep), 'g');
var exportAsEs6Default = options.exportAsEs6Default;
var exportAsDefault = options.exportAsDefault;

// if a unix path starts with // we treat is as an absolute path e.g. //Users/wearymonkey
// if we're on windows, then we ignore the / prefix as windows absolute paths are unique anyway e.g. C:\Users\wearymonkey
Expand Down Expand Up @@ -45,19 +47,26 @@ module.exports = function (content) {
.replace(new RegExp(escapeRegExp(pathSep) + '+', 'g'), pathSep);
var html;

if (content.match(/^module\.exports/)) {
if (content.match(/(?:^module\.exports)|(?:^export\s+default)|(?:^exports.default)/)) {
Copy link
Owner

Choose a reason for hiding this comment

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

/^(module\.exports|export\s+default|exports.default)/ is a bit easier to read.

var firstQuote = findQuote(content, false);
var secondQuote = findQuote(content, true);
html = content.substr(firstQuote, secondQuote - firstQuote + 1);
} else {
html = content;
}

var exportsString = "module.exports = ";
Copy link
Owner

Choose a reason for hiding this comment

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

Would a simpler solution be to just keep the same export expression of the content? That way only html-loader would need to be configured.

I'm thinking something like:

var exportExprMatch = content.match(/^(module\.exports\s+=|export\s+default|exports.default\s+=)\s+/);
var exportExpr;
if (exportExprMatch) {
  var firstQuote = findQuote(content, false); 
  var secondQuote = findQuote(content, true);
  html = content.substr(firstQuote, secondQuote - firstQuote + 1);
  exportExpr = exportExprMatch[0];
} else {
  html = content;
  exportExpr = 'module.exports = ';
}

return "var path = '"+jsesc(filePath)+"';\n" +
        "var html = " + html + ";\n" +
        (requireAngular ? "var angular = require('angular');\n" : "window.") +
        "angular.module('" + ngModule + "').run(['$templateCache', function(c) { c.put(path, html) }]);\n" +
        exportExpr + "path;";

The README change would also then not be required.

Copy link
Author

Choose a reason for hiding this comment

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

Works for me!
OTOH it feels more natural to me to specify the export format at that loader whose output is actually consumed by the app.
Also that would not work with raw-loader, because that loader is hard coded to commonjs exports.
I don't know how important that scenario is, because I am new at webpack. Are there valid reasons for chossing the raw-loader over the html-loader?

if (exportAsDefault) {
exportsString = "exports.default = ";
} else if (exportAsEs6Default) {
exportsString = "export default ";
}

return "var path = '"+jsesc(filePath)+"';\n" +
"var html = " + html + ";\n" +
(requireAngular ? "var angular = require('angular');\n" : "window.") +
"angular.module('" + ngModule + "').run(['$templateCache', function(c) { c.put(path, html) }]);\n" +
"module.exports = path;";
exportsString + " path;";

function getAndInterpolateOption(optionKey, def) {
return options[optionKey]
Expand Down