-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
192 lines (171 loc) · 5.72 KB
/
index.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
"use strict";
const compileTypescript = require('./lib/compile-typescript');
const concat = require('broccoli-concat');
const fs = require('fs');
const funnel = require('broccoli-funnel');
const funnelLib = require('./lib/funnel-lib');
const getPackageName = require('./lib/get-package-name');
const mergeTrees = require('broccoli-merge-trees');
const path = require('path');
const toES5 = require('./lib/to-es5');
const toNamedAmd = require('./lib/to-named-amd');
const toNamedCommonJs = require('./lib/to-named-common-js');
const buildTestsIndex = require('./lib/build-tests-index');
const writeFile = require('broccoli-file-creator');
const packageDist = require('./lib/package-dist');
const envFlags = require('./lib/default-es5-plugins').envFlags;
const Babel = require('broccoli-babel-transpiler');
module.exports = function(options = {}) {
let env = process.env.EMBER_ENV || process.env.BROCCOLI_ENV;
let isTest = isTestBuild(options);
let projectPath = options.projectPath || process.cwd();
let projectName = getPackageName(projectPath);
let tsconfigPath = options.tsconfigPath || (isTest ? 'tsconfig.tests.json' : 'tsconfig.json');
let vendorPackages = options.vendorPackages;
let external = options.external || vendorPackages || [];
let testOptions = options.test || {};
let useES5 = testOptions.es5 === undefined || testOptions.es5;
console.log('Build project:', projectName);
console.log('Build env:', env);
console.log('Build path:', projectPath);
console.log('Build tsconfig: ', tsconfigPath);
let trees = [];
if (isTest) {
trees.push(funnelLib('loader.js', {
include: ['loader.js'],
annotation: 'loader.js'
}));
trees.push(funnelLib('qunit', {
include: ['qunit.js', 'qunit.css'],
annotation: 'test/qunit.{js|css}'
}));
trees.push(funnel(path.join(__dirname, 'test-support'), {
include: [
'test-loader.js',
'index.html'
],
annotation: 'test-support'
}));
let vendorTrees = options.vendorTrees;
if (!vendorTrees) {
vendorTrees = vendorPackages ? vendorPackages.map(packageDist) : [];
}
let vendorFile;
if (vendorTrees.length > 0) {
vendorFile = concat(mergeTrees(vendorTrees), {
inputFiles: ['**/*'],
outputFile: 'vendor.js',
sourceMapConfig: { enabled: false },
annotation: 'vendor.js'
});
} else {
vendorFile = writeFile('vendor.js', '');
}
trees.push(vendorFile);
let srcTrees = options.srcTrees;
let jsTrees = [];
if (!srcTrees) {
let srcPath = path.join(projectPath, 'src');
let testPath = path.join(projectPath, 'test');
if (fs.existsSync(srcPath) && fs.existsSync(testPath)) {
let testsIndex = buildTestsIndex('test', 'index.ts');
srcTrees = [];
srcTrees.push(funnel(srcPath, { destDir: 'src' }));
srcTrees.push(funnel(testPath, { destDir: 'test' }));
srcTrees.push(funnel(testsIndex, { destDir: 'test' }));
jsTrees.push(funnel(srcPath, {
destDir: 'src',
include: ['**/*.js']
}));
jsTrees.push(funnel(testPath, {
destDir: 'test',
include: ['**/*.js']
}));
}
}
let compiledTypescript = compileTypescript(
tsconfigPath,
projectPath,
srcTrees
);
jsTrees.push(filterTypescriptFromTree(compiledTypescript));
let modules = mergeTrees(jsTrees);
if (useES5) {
modules = toES5(modules, { sourceMap: 'inline'}, options.buildType);
}
let amd = toNamedAmd(modules, {
dest: 'tests.js',
namespace: 'tests',
entry: path.join('tests', 'test', 'index.js'),
external
});
trees.push(amd);
} else {
let srcTrees = options.srcTrees;
let jsTrees = [];
if (!srcTrees) {
let srcPath = path.join(projectPath, 'src');
if (fs.existsSync(srcPath)) {
srcTrees = [funnel(srcPath, { destDir: 'src' })];
jsTrees.push(funnel(srcPath, {
destDir: 'src',
include: ['**/*.js']
}));
}
}
let es2017ModulesAndTypes = compileTypescript(
tsconfigPath,
projectPath,
srcTrees
);
jsTrees.push(filterTypescriptFromTree(es2017ModulesAndTypes));
let es2017Modules = new Babel(mergeTrees(jsTrees), {
plugins: [envFlags(options.buildType || env)]
});
let types = selectTypesFromTree(es2017ModulesAndTypes);
let es5Modules = toES5(es2017Modules, { sourceMap: 'inline' }, options.buildType);
let es5Amd = toNamedAmd(es5Modules, { namespace: projectName, external });
let es2017Amd = toNamedAmd(es2017Modules, {
namespace: projectName,
external,
lang: 'es2017'
});
let es2017CommonJs = toNamedCommonJs(es2017Modules);
let es5CommonJs = toNamedCommonJs(es5Modules);
trees.push(funnel(types, {
destDir: 'types',
annotation: 'types'
}));
trees.push(funnel(es2017Modules, {
destDir: 'modules/es2017',
annotation: 'modules-es2017'
}));
trees.push(funnel(es5Modules, {
destDir: 'modules/es5',
annotation: 'modules-es5'
}));
trees.push(es5Amd);
trees.push(es2017Amd);
trees.push(funnel(es2017CommonJs, {
destDir: 'commonjs/es2017',
annotation: 'commonjs-es2017'
}));
trees.push(funnel(es5CommonJs, {
destDir: 'commonjs/es5',
annotation: 'commonjs-es5'
}));
}
return mergeTrees(trees);
};
function selectTypesFromTree(tree) {
return funnel(tree, { include: ['**/*.d.ts'] });
}
function filterTypescriptFromTree(tree) {
return funnel(tree, { exclude: ['**/*.ts'] });
}
function isTestBuild(options) {
if (options.buildType) {
return options.buildType === 'test';
}
return process.env.EMBER_ENV === 'test' || process.env.BROCCOLI_ENV === 'tests';
}