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