rollup-plugin-angular
Version:
Angular2 template and styles inliner
126 lines (110 loc) • 4.71 kB
JavaScript
import fs from 'fs';
import path from 'path';
import { green } from 'colors';
import replace from 'replace';
import MagicString from 'magic-string';
import { createFilter } from 'rollup-pluginutils';
var moduleIdRegex = /moduleId\s*:(.*)/g;
var componentRegex = /@Component\(\s?{([\s\S]*)}\s?\)$|type:\s?Component,\s?args:\s?\[\s?{([\s\S]*)},\s?\]/gm;
var commentRegex = /\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm; // http://www.regextester.com/?fam=96247
var templateUrlRegex = /templateUrl\s*:(.*)/g;
var styleUrlsRegex = /styleUrls\s*:(\s*\[[\s\S]*?\])/g; // http://www.regextester.com/?fam=98594
var stringRegex = /(['"`])((?:[^\\]\\\1|.)*?)\1/g;
function insertText(str, dir, preprocessor, processFilename, sourceType) {
if ( preprocessor === void 0 ) preprocessor = function (res) { return res; };
if ( processFilename === void 0 ) processFilename = false;
if ( sourceType === void 0 ) sourceType = 'ts';
var quoteChar = sourceType === 'ts' ? '`' : '"';
return str.replace(stringRegex, function (match, quote, url) {
var includePath = path.join(dir, url);
if (processFilename) {
return quoteChar + preprocessor(includePath) + quoteChar;
}
var text = fs.readFileSync(includePath).toString();
return quoteChar + preprocessor(text, includePath) + quoteChar;
});
}
function angular(options) {
if ( options === void 0 ) options = {};
options.preprocessors = options.preprocessors || {}; // set default preprocessors to `{}`
options.replace = (typeof options.replace === 'boolean') ? options.replace : true; // set default replace to `true`
// ignore @angular/** modules
options.exclude = options.exclude || [];
if (typeof options.exclude === 'string' || options.exclude instanceof String) {
options.exclude = [options.exclude];
}
if (options.exclude.indexOf('node_modules/@angular/**') === -1) {
options.exclude.push('node_modules/@angular/**');
}
var filter = createFilter(options.include, options.exclude);
return {
name: 'angular',
transform: function transform(source, map) {
if (!filter(map)) { return; }
// replace comments in source
source = source.replace(commentRegex, '');
// use MagicString
var magicString = new MagicString(source);
// get dir from `map`
var dir = path.parse(map).dir;
// get file extension from `map`
var fileExt = map.split('.').pop();
var hasReplacements = false;
var match;
var start, end, replacement;
while ((match = componentRegex.exec(source)) !== null) {
start = match.index;
end = start + match[0].length;
replacement = match[0]
.replace(templateUrlRegex, function (match, url) {
hasReplacements = true;
var toReplace = 'template:' + insertText(url, dir, options.preprocessors.template, options.processFilename, options.sourcetype);
if (fileExt === 'js' && options.replace === true) {
/* replace templateUrl in files generated by ngc */
replace({
regex: match,
replacement: toReplace,
paths: [map],
recursive: true,
silent: true,
});
console.info(("templateUrl in file " + map + " has been changed from " + (green(match)) + " to " + (green(toReplace))));
}
return toReplace;
})
.replace(styleUrlsRegex, function (match, urls) {
hasReplacements = true;
var toReplace = 'styles:' + insertText(urls, dir, options.preprocessors.style, options.processFilename, options.sourcetype);
/* replace styles in files generated by ngc */
if (fileExt === 'js' && options.replace === true) {
replace({
regex: styleUrlsRegex,
replacement: toReplace,
paths: [map],
recursive: true,
silent: true,
});
console.info(("styleUrls in file " + map + " has been changed from " + (green(match)) + " to " + (green(toReplace))));
}
return toReplace;
})
.replace(moduleIdRegex, function (match, moduleId) {
hasReplacements = true;
return '';
});
if (hasReplacements) {
magicString.overwrite(start, end, replacement);
}
}
if (!hasReplacements) {
return null;
}
var result = { code: magicString.toString() };
if (options.sourceMap !== false) {
result.map = magicString.generateMap({ hires: true });
}
return result;
}
};
}
export default angular;