-
Notifications
You must be signed in to change notification settings - Fork 42
/
rollup-plugin-angularjs-template-loader.js
82 lines (66 loc) · 2.89 KB
/
rollup-plugin-angularjs-template-loader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const fs = require('fs');
const path = require('path');
import MagicString from 'magic-string';
// This rollup plugin finds AngularJS templates and loads them into the $templateCache
// This is a rollup replacement for the ngtemplate-loader webpack loader
export default function angularJsTemplateLoader(options = {}) {
function isSourceMapEnabled() {
return options.sourceMap !== false && options.sourcemap !== false;
}
return {
transform(originalCode, id) {
let code = originalCode;
// look for things like templateUrl: require('./template.html')
if (!code.includes('Url: require')) {
return false;
}
if (!fs.existsSync(id)) {
throw new Error('Unable to load AngularJS template; could not find source file ' + id);
}
// Find the directory the JS source file is in
const baseDir = fs.lstatSync(id).isDirectory() ? id : path.dirname(id);
const templateRegex = /Url: require\(['"]([^'"]+\.html)['"]\)/g;
let match = templateRegex.exec(code);
if (!match) {
return;
}
let magicString = new MagicString(code);
while (match) {
// i.e., './template.html'
const templatePath = match[1];
// Absolute path to the actual template.html file
const contentPath = path.resolve(baseDir, templatePath);
if (!fs.existsSync(contentPath)) {
throw new Error('Unable to load AngularJS template; could not find template file ' + contentPath);
}
const startIdx = match.index;
const endIdx = startIdx + match[0].length;
// read the contents of template.html
const content = fs.readFileSync(contentPath, { encoding: 'UTF8' }).replace(/`/g, '\\`');
// Replace: templateUrl: require('./template.html')
// With: templateUrl: './template.html'
magicString.overwrite(startIdx, endIdx, `Url: '${templatePath}'`);
// Append a run block that adds the HTML content into the $templateCache (used by angularjs when loading templates)
magicString.append(`
/*********************************************************************
* angularjs-template-loader rollup plugin -- ${templatePath} start *
********************************************************************/
window.angular.module('ng').run(['$templateCache', function(templateCache) {
templateCache.put('${templatePath}',
\`${content}\`)
}]);
/*********************************************************************
* angularjs-template-loader rollup plugin -- ${templatePath} end *
********************************************************************/
`);
// look for another match
match = templateRegex.exec(code);
}
if (isSourceMapEnabled()) {
return { code: magicString.toString(), map: magicString.generateMap({ hires: true }) };
} else {
return { code: magicString.toString() };
}
},
};
}