1 | const _ = require('lodash');
|
2 | const { createFilter } = require('rollup-pluginutils');
|
3 | const MagicString = require('magic-string');
|
4 | const utils = require('./translation-utils');
|
5 |
|
6 | module.exports = (opts = {}) => {
|
7 | const include = opts.include || '**/*.js';
|
8 | const { exclude } = opts;
|
9 | const filter = createFilter(include, exclude);
|
10 | const sourcemap = opts.sourcemap !== false;
|
11 | const subdirectory = opts.subdirectory || './';
|
12 | const filtering = opts.filtering !== false;
|
13 | return {
|
14 | name: 'translation-ui-router',
|
15 | transform(code, id) {
|
16 | if (filtering && !filter(id)) return null;
|
17 | const magicString = new MagicString(code);
|
18 | this.parse(code, {
|
19 | onComment: (block, text, start, end) => {
|
20 | const match = text.match(/@ngTranslationsInject:?([a-zA-Z]+)?(.*)/);
|
21 | if (match) {
|
22 | const translations = _.chain(match)
|
23 | .get(2)
|
24 | .split(/\s+/)
|
25 | .value();
|
26 |
|
27 | const format = _.get(match, '[1]');
|
28 |
|
29 | if (_(translations).isArray() && !_(translations).isEmpty()) {
|
30 | const inject = utils.injectTranslationImports(translations, id, subdirectory, format);
|
31 | magicString.overwrite(start, end, `/* @ngInject */ ($translate, $q, asyncLoader) => { ${inject} }`);
|
32 | } else {
|
33 | magicString.overwrite(start, end, 'angular.noop');
|
34 | }
|
35 | }
|
36 | },
|
37 | });
|
38 | return {
|
39 | code: magicString.toString(),
|
40 | map: sourcemap ? magicString.generateMap({ hires: true }) : null,
|
41 | };
|
42 | },
|
43 | };
|
44 | };
|