Skip to content

Commit e1788f5

Browse files
committed
Add r.js optimization support
1 parent 1fedff9 commit e1788f5

File tree

8 files changed

+61
-23
lines changed

8 files changed

+61
-23
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,3 @@ Reference files via the es6! plugin name:
2929
// ...
3030
});
3131
```
32-

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "requirejs-babel",
3-
"version": "0.0.2",
3+
"version": "0.0.3",
44
"authors": [
55
"Michael <[email protected]>"
66
],

demo/index.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
<title>Document</title>
66
</head>
77
<body>
8-
8+
Open console tab in dev tools!
9+
910
<script src="libs/requirejs.min.js"></script>
1011
<script>
1112
requirejs.config({
1213
paths: {
13-
'es6': '../es6',
14-
'babel': '../babel-4.6.6.min'
14+
es6: '../es6',
15+
babel: '../babel-4.6.6.min'
1516
}
1617
});
17-
18-
require(['es6!src/main']);
19-
</script
18+
</script>
19+
<script src="src/index.js"></script>
2020
</body>
2121
</html>
File renamed without changes.

demo/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require(['es6!src/class']);

es6.js

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
1-
var fetchText = function (url, callback) {
2-
var xhr = new XMLHttpRequest();
3-
xhr.open('GET', url, true);
4-
xhr.onreadystatechange = function (evt) {
5-
//Do not explicitly handle errors, those should be
6-
//visible via console output in the browser.
7-
if (xhr.readyState === 4) {
8-
callback(xhr.responseText);
9-
}
1+
var getXhr, fetchText, _buildMap = {};
2+
3+
if (typeof window !== "undefined" && window.navigator && window.document) {
4+
getXhr = function () {
5+
return new XMLHttpRequest();
106
};
11-
xhr.send(null);
12-
};
7+
8+
fetchText = function (url, callback) {
9+
var xhr = getXhr();
10+
xhr.open('GET', url, true);
11+
xhr.onreadystatechange = function (evt) {
12+
//Do not explicitly handle errors, those should be
13+
//visible via console output in the browser.
14+
if (xhr.readyState === 4) {
15+
callback(xhr.responseText);
16+
}
17+
};
18+
xhr.send(null);
19+
};
20+
21+
} else if (typeof process !== "undefined" &&
22+
process.versions &&
23+
!!process.versions.node) {
24+
//Using special require.nodeRequire, something added by r.js.
25+
fs = require.nodeRequire('fs');
26+
fetchText = function (path, callback) {
27+
callback(fs.readFileSync(path, 'utf8'));
28+
};
29+
}
1330

1431
define(['babel'], function(babel) {
1532
return {
@@ -21,8 +38,20 @@ define(['babel'], function(babel) {
2138
code = babel.transform(text, {
2239
sourceMap: 'inline'
2340
}).code;
24-
onload.fromText(code);
41+
42+
if (config.isBuild) {
43+
_buildMap[name] = code;
44+
}
45+
46+
onload.fromText(code);
2547
});
48+
},
49+
50+
write: function (pluginName, moduleName, write) {
51+
if (moduleName in _buildMap) {
52+
// var text = jsEscape(buildMap[moduleName]);
53+
write.asModule(pluginName + '!' + moduleName, _buildMap[moduleName]);
54+
}
2655
}
2756
}
28-
});
57+
});

package.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
{
22
"name": "requirejs-babel",
3-
"version": "0.0.2",
3+
"version": "0.0.3",
44
"description": "An AMD loader plugin for Babel",
5+
"repository": {
6+
"type": "git",
7+
"url": "https://github.com/mikach/requirejs-babel"
8+
},
59
"main": "index.js",
610
"scripts": {
11+
"optimize": "r.js -o baseUrl=./demo paths.es6=../es6 paths.babel=../babel-4.6.6.min name=src/index out=demo/main-built.js optimize=none",
712
"test": "echo \"Error: no test specified\" && exit 1"
813
},
914
"author": "Michael <[email protected]>",
10-
"license": "MIT"
15+
"license": "MIT",
16+
"devDependencies": {
17+
"requirejs": "^2.1.16"
18+
}
1119
}

0 commit comments

Comments
 (0)