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 MagicString = _interopDefault(require('magic-string'));
|
8 | var rollupPluginutils = require('rollup-pluginutils');
|
9 |
|
10 | var moduleIdRegex = /moduleId\s*:(.*)/g;
|
11 | var componentRegex = /@Component\(\s?{([\s\S]*)}\s?\)$/gm;
|
12 | var templateUrlRegex = /templateUrl\s*:(.*)/g;
|
13 | var styleUrlsRegex = /styleUrls\s*:(\s*\[[\s\S]*?\])/g;
|
14 | var stringRegex = /(['"`])((?:[^\\]\\\1|.)*?)\1/g;
|
15 |
|
16 | function insertText(str, dir, preprocessor, processFilename) {
|
17 | if ( preprocessor === void 0 ) preprocessor = function (res) { return res; };
|
18 | if ( processFilename === void 0 ) processFilename = false;
|
19 |
|
20 | return str.replace(stringRegex, function (match, quote, url) {
|
21 | var includePath = path.join(dir, url);
|
22 | if (processFilename) {
|
23 | return '`' + preprocessor(includePath) + '`';
|
24 | }
|
25 | var text = fs.readFileSync(includePath).toString();
|
26 | return '`' + preprocessor(text, includePath) + '`';
|
27 | });
|
28 | }
|
29 |
|
30 | function angular(options) {
|
31 | if ( options === void 0 ) options = {};
|
32 |
|
33 | options.preprocessors = options.preprocessors || {};
|
34 |
|
35 |
|
36 | options.exclude = options.exclude || [];
|
37 | if (typeof options.exclude === 'string' || options.exclude instanceof String) { options.exclude = [options.exclude]; }
|
38 | if (options.exclude.indexOf('node_modules/@angular/**') === -1) { options.exclude.push('node_modules/@angular/**'); }
|
39 |
|
40 | var filter = rollupPluginutils.createFilter(options.include, options.exclude);
|
41 |
|
42 | return {
|
43 | name: 'angular',
|
44 | transform: function transform(source, map) {
|
45 | if (!filter(map)) { return; }
|
46 |
|
47 | var magicString = new MagicString(source);
|
48 | var dir = path.parse(map).dir;
|
49 |
|
50 | var hasReplacements = false;
|
51 | var match;
|
52 | var start, end, replacement;
|
53 |
|
54 | while ((match = componentRegex.exec(source)) !== null) {
|
55 | start = match.index;
|
56 | end = start + match[0].length;
|
57 |
|
58 | replacement = match[0]
|
59 | .replace(templateUrlRegex, function (match, url) {
|
60 | hasReplacements = true;
|
61 | return 'template:' + insertText(url, dir, options.preprocessors.template, options.processFilename);
|
62 | })
|
63 | .replace(styleUrlsRegex, function (match, urls) {
|
64 | hasReplacements = true;
|
65 | return 'styles:' + insertText(urls, dir, options.preprocessors.style, options.processFilename);
|
66 | })
|
67 | .replace(moduleIdRegex, function (match, moduleId) {
|
68 | hasReplacements = true;
|
69 | return '';
|
70 | });
|
71 |
|
72 | if (hasReplacements) { magicString.overwrite(start, end, replacement); }
|
73 | }
|
74 |
|
75 | if (!hasReplacements) { return null; }
|
76 |
|
77 | var result = { code: magicString.toString() };
|
78 | if (options.sourceMap !== false) { result.map = magicString.generateMap({ hires: true }); }
|
79 |
|
80 | return result;
|
81 | }
|
82 | };
|
83 | }
|
84 |
|
85 | module.exports = angular;
|