Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save TheLarkInn/774289cfed6909143a31d99bd19a55c2 to your computer and use it in GitHub Desktop.
Save TheLarkInn/774289cfed6909143a31d99bd19a55c2 to your computer and use it in GitHub Desktop.
convertES6StyleAndHTMLImportsToTypeScriptFriendlyRequires.js
module.exports = function convertES6StyleAndHTMLImportsToTypeScriptFriendlyRequires(source) {
this.cacheable && this.cacheable();
var hasES6StyleAndHTMLImportRegex = new RegExp(/import\s+(.+)\s+from\s+([\'|\"].+\.(html|jade|haml|twig|erb|css|scss|less|yaml|sass|styl)[\'\"]);?/, 'g');
var results;
try {
results = source.replace(hasES6StyleAndHTMLImportRegex,
"let $1 = require($2);\n"
);
} catch(e) {
var err = "";
if (e.location == null || e.location.first_column == null || e.location.first_line == null) {
err += "Template Loader Error. The original exception was: " + e + "\n";
} else {
err += "ERROR ERROR ERROR ERROR";
}
throw new Error(err);
}
return results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment