UNPKG

4.39 kBJavaScriptView Raw
1import fs from 'fs';
2import path from 'path';
3import { green } from 'colors';
4import * as colors from 'colors';
5import replace from 'replace';
6import MagicString from 'magic-string';
7import { createFilter } from 'rollup-pluginutils';
8
9var moduleIdRegex = /moduleId\s*:(.*)/g;
10var componentRegex = /@Component\(\s?{([\s\S]*)}\s?\)$|type:\s?Component,\s?args:\s?\[\s?{([\s\S]*)},\s?\]/gm;
11var commentRegex = /\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm; // http://www.regextester.com/?fam=96247
12var templateUrlRegex = /templateUrl\s*:(.*)/g;
13var styleUrlsRegex = /styleUrls\s*:(\s*\[[\s\S]*?\])/g; // http://www.regextester.com/?fam=98594
14var stringRegex = /(['"`])((?:[^\\]\\\1|.)*?)\1/g;
15
16function insertText(str, dir, preprocessor, processFilename, sourceType) {
17 if ( preprocessor === void 0 ) preprocessor = function (res) { return res; };
18 if ( processFilename === void 0 ) processFilename = false;
19 if ( sourceType === void 0 ) sourceType = 'ts';
20
21 var quoteChar = sourceType === 'ts' ? '`' : '"';
22 return str.replace(stringRegex, function (match, quote, url) {
23 var includePath = path.join(dir, url);
24 if (processFilename) {
25 return quoteChar + preprocessor(includePath) + quoteChar;
26 }
27 var text = fs.readFileSync(includePath).toString();
28 return quoteChar + preprocessor(text, includePath) + quoteChar;
29 });
30}
31
32function angular(options) {
33 if ( options === void 0 ) options = {};
34
35 options.preprocessors = options.preprocessors || {};
36
37 // ignore @angular/** modules
38 options.exclude = options.exclude || [];
39 if (typeof options.exclude === 'string' || options.exclude instanceof String) { options.exclude = [options.exclude]; }
40 if (options.exclude.indexOf('node_modules/@angular/**') === -1) { options.exclude.push('node_modules/@angular/**'); }
41
42 var filter = createFilter(options.include, options.exclude);
43
44 return {
45 name: 'angular',
46 transform: function transform(source, map) {
47 if (!filter(map)) { return; }
48 source = source.replace(commentRegex, '');
49 var magicString = new MagicString(source);
50
51 var dir = path.parse(map).dir;
52 var fileExt = map.split('.').pop();
53
54 var hasReplacements = false;
55 var match;
56 var start, end, replacement;
57
58 while ((match = componentRegex.exec(source)) !== null) {
59 start = match.index;
60 end = start + match[0].length;
61
62 replacement = match[0]
63 .replace(templateUrlRegex, function (match, url) {
64 hasReplacements = true;
65 var toReplace = 'template:' + insertText(url, dir, options.preprocessors.template, options.processFilename, options.sourcetype);
66 if (fileExt === 'js') {
67 /* replace templateUrl in files generated by ngc */
68 replace({
69 regex: match,
70 replacement: toReplace,
71 paths: [map],
72 recursive: true,
73 silent: true,
74 });
75 console.info(("templateUrl in file " + map + " has been changed from " + (green(match)) + " to " + (green(toReplace))));
76 }
77 return toReplace;
78 })
79 .replace(styleUrlsRegex, function (match, urls) {
80 hasReplacements = true;
81 var toReplace = 'styles:' + insertText(urls, dir, options.preprocessors.style, options.processFilename, options.sourcetype);
82 /* replace styles in files generated by ngc */
83 if (fileExt === 'js') {
84 replace({
85 regex: styleUrlsRegex,
86 replacement: toReplace,
87 paths: [map],
88 recursive: true,
89 silent: true,
90 });
91 console.info(("styleUrls in file " + map + " has been changed from " + (green(match)) + " to " + (green(toReplace))));
92 }
93 return toReplace;
94 })
95 .replace(moduleIdRegex, function (match, moduleId) {
96 hasReplacements = true;
97 return '';
98 });
99 if (hasReplacements) {
100 magicString.overwrite(start, end, replacement);
101 }
102 }
103
104 if (!hasReplacements) {
105 return null;
106 }
107
108 var result = { code: magicString.toString() };
109
110 if (options.sourceMap !== false) {
111 result.map = magicString.generateMap({ hires: true });
112 }
113 return result;
114 }
115 };
116}
117
118export default angular;