forked from tildeio/router.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Brocfile.js
156 lines (127 loc) · 4.09 KB
/
Brocfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
var concat = require('broccoli-concat');
var filterES6Modules = require('broccoli-es6-module-filter');
var mergeTrees = require('broccoli-merge-trees');
var moveFile = require('broccoli-file-mover');
var pickFiles = require('broccoli-static-compiler');
var uglifyJavaScript = require('broccoli-uglify-js');
var wrapFiles = require('broccoli-wrap');
var concatFilenames = require('broccoli-concat-filenames');
var jshint = require('broccoli-jshint');
var recast = require('broccoli-es3-safe-recast');
var trees = [
createAMDTree(),
createCommonJSTree(),
createStandaloneTree(),
// TODO only add tests when Broccoli environment is development ...
makeTests()
];
trees = trees.map(recast);
module.exports = mergeTrees(trees);
function makeTests() {
// Concatenate all dependencies into tests/deps.js
var deps = concat('vendor/deps', {
inputFiles: ['*.js'],
outputFile: '/tests/deps.js'
});
var jshintLib = jshint('lib');
var jshintTests = jshint('test/tests');
// Create AMD module 'tests' containing all tests in 'test/tests' and concatenate them into tests/tests.js
var tests = filterES6Modules('test/tests', {
moduleType: 'amd',
packageName: 'tests',
anonymous: false
});
tests = mergeTrees([jshintTests, jshintLib, tests]);
tests = concat(tests, {
inputFiles: ['**/*.js'],
outputFile: '/tests/tests.js'
});
// Create /tests/tests_main.js which requires all tests (all test/tests/**/*_test.js files)
var testsMain = concatFilenames("test", {
inputFiles: ["**/*_test.js"],
outputFile: "/tests/tests_main.js",
transform: function(fileName) {
return "require('" + fileName + "');";
}
});
// Copy files needed for QUnit
var qunit = pickFiles('test', {
files: ['index.html', 'vendor/*'],
srcDir: '/',
destDir: '/tests'
});
// Copy vendor/loader.js to test/loader.js
var loader = concat('vendor', {
inputFiles: ['loader.js'],
outputFile: '/tests/loader.js'
});
// Merge all test related stuff into tests tree
return mergeTrees([deps, qunit, loader, tests, testsMain]);
}
function createAMDTree() {
// dist/router.amd.js: all AMD compiled modules concatenated into 1 file
var amd = filterES6Modules('lib', {
moduleType: 'amd',
anonymous: false
});
amd = concat(amd, {
// to be consinstent with old behavior, we include 'router.js' at the end
inputFiles: ['router/**/*.js', 'router.js'],
outputFile: '/router.amd.js'
});
return amd;
}
function createCommonJSTree() {
// CommonJS version of router.js; will be located in 'dist/commonjs'
var commonJs = pickFiles('lib', {
srcDir: '/',
destDir: '/commonjs'
});
commonJs = filterES6Modules(commonJs, {
moduleType: 'cjs'
});
// rename router.js to main.js
commonJs = moveFile(commonJs, {
srcFile: '/commonjs/router.js',
destFile: '/commonjs/main.js'
});
return commonJs;
}
function createStandaloneTree() {
// dist/router.js: IIFE version of router.js, using RSVP and RouteRecognizer globals
var begin = '(function(globals, RSVP, RouteRecognizer) {\n';
var end = [];
end.push('define("route-recognizer", [], function() { return {"default": RouteRecognizer}; });');
end.push('define("rsvp", [], function() { return RSVP;});');
end.push('define("rsvp/promise", [], function() { return {"default": RSVP.Promise}; });');
end.push("window.Router = requireModule('router');");
end.push('}(window, window.RSVP, window.RouteRecognizer));');
end = end.join('\n');
var browser = pickFiles('vendor', {
files: ['loader.js'],
srcDir: '/',
destDir: '/'
});
browser = mergeTrees([browser, createAMDTree()]);
browser = concat(browser, {
inputFiles: ['loader.js', '*.js'],
outputFile: '/router.js'
});
browser = wrapFiles(browser, {
wrapper: [begin, end],
extensions: ['js']
});
// dist/router.min.js
var minified = pickFiles(browser, {
srcDir: '/',
destDir: '/'
});
minified = moveFile(minified, {
srcFile: '/router.js',
destFile: '/router.min.js'
});
minified = uglifyJavaScript(minified, {
mangle: true
});
return mergeTrees([browser, minified]);
}