Skip to content

Commit 05a1e25

Browse files
committed
Initial commit
0 parents  commit 05a1e25

File tree

5 files changed

+103
-0
lines changed

5 files changed

+103
-0
lines changed

.editorconfig

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
indent_style = space
8+
indent_size = 2
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
trim_trailing_whitespace = false

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
dist/

package.json

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "rollup-plugin-angular",
3+
"version": "0.1.0",
4+
"description": "Angular2 template/style inliner",
5+
"main": "dist/rollup-plugin-angular.js",
6+
"jsnext:main": "dist/rollup-plugin-angular.es2015.js",
7+
"scripts": {
8+
"build": "rollup -c",
9+
"prepublish": "npm run build"
10+
},
11+
"keywords": [
12+
"angular2",
13+
"template",
14+
"styles",
15+
"inliner"
16+
],
17+
"author": "Felix Itzenplitz",
18+
"repository": {
19+
"type": "git",
20+
"url": "https://github.com/cebor/rollup-plugin-angular.git"
21+
},
22+
"license": "MIT",
23+
"dependencies": {
24+
"rollup-pluginutils": "^1.5.1"
25+
},
26+
"devDependencies": {
27+
"buble": "^0.12.5",
28+
"rollup": "^0.34.1",
29+
"rollup-plugin-buble": "^0.12.1"
30+
}
31+
}

rollup.config.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import buble from 'rollup-plugin-buble';
2+
3+
var external = Object.keys(require('./package.json').dependencies).concat(['fs', 'path']);
4+
5+
export default {
6+
entry: 'src/index.js',
7+
plugins: [ buble() ],
8+
external: external,
9+
targets: [
10+
{ dest: 'dist/rollup-plugin-angular.js', format: 'cjs' },
11+
{ dest: 'dist/rollup-plugin-angular.esm.js', format: 'es' }
12+
]
13+
};

src/index.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
4+
import { createFilter } from 'rollup-pluginutils';
5+
6+
const componentRegex = /^\s*@(Component)\({([\s\S]*)}\)\s*$/gm;
7+
const templateUrlRegex = /templateUrl\s*:(.*)/g;
8+
const styleUrlsRegex = /styleUrls\s*:(\s*\[[\s\S]*?\])/g;
9+
const stringRegex = /(['"])(.*?)\1/g;
10+
11+
function insertText(str, dir) {
12+
str = str.replace(stringRegex, function (match, quote, url) {
13+
var text = fs.readFileSync(path.join(dir, url)).toString();
14+
return '`' + text + '`';
15+
});
16+
console.log(str);
17+
return str;
18+
}
19+
20+
export default function angular (options = {}) {
21+
var filter = createFilter(options.include, options.exclude);
22+
23+
return {
24+
name: 'angular',
25+
transform(source, map) {
26+
if ( !filter( id ) ) return;
27+
28+
var dir = path.parse(map).dir;
29+
30+
source = source.replace(componentRegex, function (match, decorator, metadata) {
31+
metadata = metadata
32+
.replace(templateUrlRegex, function (match, url) {
33+
return 'template:' + insertText(url, dir);
34+
})
35+
.replace(styleUrlsRegex, function (match, urls) {
36+
return 'styles:' + insertText(urls, dir);
37+
});
38+
39+
return '@' + decorator + '({' + metadata + '})';
40+
});
41+
42+
return source;
43+
}
44+
};
45+
}

0 commit comments

Comments
 (0)