UNPKG

5.79 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const path = require("path");
4const fs = require("fs");
5const glob = require("glob");
6const utils_1 = require("../models/webpack-configs/utils");
7const webpack_test_config_1 = require("../models/webpack-test-config");
8const karma_webpack_throw_error_1 = require("./karma-webpack-throw-error");
9const getAppFromConfig = require('../utilities/app-utils').getAppFromConfig;
10function isDirectory(path) {
11 try {
12 return fs.statSync(path).isDirectory();
13 }
14 catch (_) {
15 return false;
16 }
17}
18// Add files to the Karma files array.
19function 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 // It's important to not replace the array, because
29 // karma already has a reference to the existing array.
30 if (prepend) {
31 files.unshift(...processedFiles);
32 }
33 else {
34 files.push(...processedFiles);
35 }
36}
37const 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 // Add assets. This logic is mimics the one present in GlobCopyWebpackPlugin.
47 if (appConfig.assets) {
48 config.proxies = config.proxies || {};
49 appConfig.assets.forEach((pattern) => {
50 // Convert all string patterns to Pattern type.
51 pattern = typeof pattern === 'string' ? { glob: pattern } : pattern;
52 // Add defaults.
53 // Input is always resolved relative to the appRoot.
54 pattern.input = path.resolve(appRoot, pattern.input || '');
55 pattern.output = pattern.output || '';
56 pattern.glob = pattern.glob || '';
57 // Build karma file pattern.
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 // The `files` entry serves the file from `/base/{asset.input}/{asset.glob}`.
62 // We need to add a URL rewrite that exposes the asset as `/{asset.output}/{asset.glob}`.
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 // For globs (paths that don't exist), proxy pattern.output to pattern.input.
70 relativePath = path.relative(config.basePath, pattern.input);
71 proxyPath = path.join(pattern.output);
72 }
73 // Proxy paths must have only forward slashes.
74 proxyPath = proxyPath.replace(/\\/g, '/');
75 config.proxies['/' + proxyPath] = '/base/' + relativePath;
76 });
77 }
78 // Add webpack config.
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 // If Karma is being ran in single run mode, throw errors.
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 // Replace the @angular/cli preprocessor with webpack+sourcemap.
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 // Add global scripts. This logic mimics the one in webpack-configs/common.
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 // Add polyfills file before everything else
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};
120init.$inject = ['config'];
121// Dummy preprocessor, just to keep karma from showing a warning.
122const preprocessor = () => (content, _file, done) => done(null, content);
123preprocessor.$inject = [];
124// Also export karma-webpack and karma-sourcemap-loader.
125module.exports = Object.assign({
126 'framework:@angular/cli': ['factory', init],
127 'preprocessor:@angular/cli': ['factory', preprocessor]
128}, require('karma-webpack'), require('karma-sourcemap-loader'));
129//# sourceMappingURL=/users/hansl/sources/angular-cli/plugins/karma.js.map
\No newline at end of file