1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const path = require("path");
|
4 | const fs = require("fs");
|
5 | const glob = require("glob");
|
6 | const utils_1 = require("../models/webpack-configs/utils");
|
7 | const webpack_test_config_1 = require("../models/webpack-test-config");
|
8 | const karma_webpack_throw_error_1 = require("./karma-webpack-throw-error");
|
9 | const getAppFromConfig = require('../utilities/app-utils').getAppFromConfig;
|
10 | function isDirectory(path) {
|
11 | try {
|
12 | return fs.statSync(path).isDirectory();
|
13 | }
|
14 | catch (_) {
|
15 | return false;
|
16 | }
|
17 | }
|
18 |
|
19 | function addKarmaFiles(files, newFiles, prepend = false) {
|
20 | const defaults = {
|
21 | included: true,
|
22 | served: true,
|
23 | watched: true
|
24 | };
|
25 | const processedFiles = newFiles
|
26 | .filter(file => glob.sync(file.pattern, { nodir: true }).length != 0)
|
27 | .map(file => (Object.assign({}, defaults, file)));
|
28 |
|
29 |
|
30 | if (prepend) {
|
31 | files.unshift(...processedFiles);
|
32 | }
|
33 | else {
|
34 | files.push(...processedFiles);
|
35 | }
|
36 | }
|
37 | const init = (config) => {
|
38 | const appConfig = getAppFromConfig(config.angularCli.app);
|
39 | const appRoot = path.join(config.basePath, appConfig.root);
|
40 | const testConfig = Object.assign({
|
41 | environment: 'dev',
|
42 | codeCoverage: false,
|
43 | sourcemaps: true,
|
44 | progress: true,
|
45 | }, config.angularCli);
|
46 |
|
47 | if (appConfig.assets) {
|
48 | config.proxies = config.proxies || {};
|
49 | appConfig.assets.forEach((pattern) => {
|
50 |
|
51 | pattern = typeof pattern === 'string' ? { glob: pattern } : pattern;
|
52 |
|
53 |
|
54 | pattern.input = path.resolve(appRoot, pattern.input || '');
|
55 | pattern.output = pattern.output || '';
|
56 | pattern.glob = pattern.glob || '';
|
57 |
|
58 | const assetPath = path.join(pattern.input, pattern.glob);
|
59 | const filePattern = isDirectory(assetPath) ? assetPath + '/**' : assetPath;
|
60 | addKarmaFiles(config.files, [{ pattern: filePattern, included: false }]);
|
61 |
|
62 |
|
63 | let relativePath, proxyPath;
|
64 | if (fs.existsSync(assetPath)) {
|
65 | relativePath = path.relative(config.basePath, assetPath);
|
66 | proxyPath = path.join(pattern.output, pattern.glob);
|
67 | }
|
68 | else {
|
69 |
|
70 | relativePath = path.relative(config.basePath, pattern.input);
|
71 | proxyPath = path.join(pattern.output);
|
72 | }
|
73 |
|
74 | proxyPath = proxyPath.replace(/\\/g, '/');
|
75 | config.proxies['/' + proxyPath] = '/base/' + relativePath;
|
76 | });
|
77 | }
|
78 |
|
79 | const webpackConfig = new webpack_test_config_1.WebpackTestConfig(testConfig, appConfig).buildConfig();
|
80 | const webpackMiddlewareConfig = {
|
81 | noInfo: true,
|
82 | stats: {
|
83 | assets: false,
|
84 | colors: true,
|
85 | version: false,
|
86 | hash: false,
|
87 | timings: false,
|
88 | chunks: false,
|
89 | chunkModules: false
|
90 | },
|
91 | watchOptions: {
|
92 | poll: testConfig.poll
|
93 | }
|
94 | };
|
95 |
|
96 | if (config.singleRun) {
|
97 | webpackConfig.plugins.push(new karma_webpack_throw_error_1.KarmaWebpackThrowError());
|
98 | }
|
99 | config.webpack = Object.assign(webpackConfig, config.webpack);
|
100 | config.webpackMiddleware = Object.assign(webpackMiddlewareConfig, config.webpackMiddleware);
|
101 |
|
102 | Object.keys(config.preprocessors)
|
103 | .filter((file) => config.preprocessors[file].indexOf('@angular/cli') !== -1)
|
104 | .map((file) => config.preprocessors[file])
|
105 | .map((arr) => arr.splice(arr.indexOf('@angular/cli'), 1, 'webpack', 'sourcemap'));
|
106 |
|
107 | if (appConfig.scripts && appConfig.scripts.length > 0) {
|
108 | const globalScriptPatterns = utils_1.extraEntryParser(appConfig.scripts, appRoot, 'scripts')
|
109 | .filter(script => !(script.output || script.lazy))
|
110 | .map(script => ({ pattern: path.resolve(appRoot, script.input) }));
|
111 | addKarmaFiles(config.files, globalScriptPatterns, true);
|
112 | }
|
113 |
|
114 | if (appConfig.polyfills) {
|
115 | const polyfillsFile = path.resolve(appRoot, appConfig.polyfills);
|
116 | config.preprocessors[polyfillsFile] = ['webpack', 'sourcemap'];
|
117 | addKarmaFiles(config.files, [{ pattern: polyfillsFile }], true);
|
118 | }
|
119 | };
|
120 | init.$inject = ['config'];
|
121 |
|
122 | const preprocessor = () => (content, _file, done) => done(null, content);
|
123 | preprocessor.$inject = [];
|
124 |
|
125 | module.exports = Object.assign({
|
126 | 'framework:@angular/cli': ['factory', init],
|
127 | 'preprocessor:@angular/cli': ['factory', preprocessor]
|
128 | }, require('karma-webpack'), require('karma-sourcemap-loader'));
|
129 |
|
\ | No newline at end of file |