Skip to content

Commit 8585533

Browse files
filipesilvahansl
authored andcommitted
fix(@angular-devkit/build-webpack): fix karma asset paths
1 parent 3d978e9 commit 8585533

File tree

5 files changed

+18
-32
lines changed

5 files changed

+18
-32
lines changed

packages/angular_devkit/build_webpack/src/angular-cli-files/models/webpack-configs/common.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,7 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
9191

9292
// process asset entries
9393
if (appConfig.assets) {
94-
const copyWebpackPluginPatterns = appConfig.assets.map((asset: string | AssetPattern) => {
95-
// Convert all string assets to object notation.
96-
asset = typeof asset === 'string' ? { glob: asset } : asset;
94+
const copyWebpackPluginPatterns = appConfig.assets.map((asset: AssetPattern) => {
9795
// Add defaults.
9896
// Input is always resolved relative to the projectRoot.
9997
// TODO: add smart defaults to schema to use project root as default.

packages/angular_devkit/build_webpack/src/angular-cli-files/models/webpack-configs/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,6 @@ export function getOutputHashFormat(option: string, length = 20): HashFormat {
9999
export interface AssetPattern {
100100
glob: string;
101101
input?: string;
102-
output?: string;
103-
allowOutsideOutDir?: boolean;
102+
output: string;
103+
allowOutsideOutDir: boolean;
104104
}

packages/angular_devkit/build_webpack/src/angular-cli-files/plugins/karma.ts

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import * as webpack from 'webpack';
88
const webpackDevMiddleware = require('webpack-dev-middleware');
99

1010
import { AssetPattern } from '../models/webpack-configs/utils';
11-
import { isDirectory } from '../utilities/is-directory';
1211
import { KarmaWebpackFailureCb } from './karma-webpack-failure-cb';
1312

1413
/**
@@ -50,7 +49,7 @@ function addKarmaFiles(files: any[], newFiles: any[], prepend = false) {
5049

5150
const init: any = (config: any, emitter: any, customFileHandlers: any) => {
5251
const options = config.buildWebpack.options;
53-
const projectRoot = config.buildWebpack.projectRoot;
52+
const projectRoot = config.buildWebpack.projectRoot as string;
5453
successCb = config.buildWebpack.successCb;
5554
failureCb = config.buildWebpack.failureCb;
5655

@@ -71,38 +70,26 @@ const init: any = (config: any, emitter: any, customFileHandlers: any) => {
7170
], true);
7271
}
7372

74-
// Add assets. This logic is mimics the one present in GlobCopyWebpackPlugin.
73+
// Add assets. This logic is mimics the one present in GlobCopyWebpackPlugin, but proxies
74+
// asset requests to the Webpack server.
7575
if (options.assets) {
7676
config.proxies = config.proxies || {};
7777
options.assets.forEach((pattern: AssetPattern) => {
78-
// Convert all string patterns to Pattern type.
79-
pattern = typeof pattern === 'string' ? { glob: pattern } : pattern;
80-
// Add defaults.
81-
// Input is always resolved relative to the projectRoot.
82-
pattern.input = path.resolve(projectRoot, pattern.input || '');
83-
pattern.output = pattern.output || '';
84-
pattern.glob = pattern.glob || '';
85-
86-
// Build karma file pattern.
87-
const assetPath = path.join(pattern.input, pattern.glob);
88-
const filePattern = isDirectory(assetPath) ? assetPath + '/**' : assetPath;
89-
addKarmaFiles(config.files, [{ pattern: filePattern, included: false }]);
90-
91-
// The `files` entry serves the file from `/base/{asset.input}/{asset.glob}`.
92-
// We need to add a URL rewrite that exposes the asset as `/{asset.output}/{asset.glob}`.
93-
let relativePath: string, proxyPath: string;
94-
if (fs.existsSync(assetPath)) {
95-
relativePath = path.relative(config.basePath, assetPath);
78+
// TODO: use smart defaults in schema to do this instead.
79+
// Default input to be projectRoot.
80+
pattern.input = pattern.input || projectRoot;
81+
82+
// Determine Karma proxy path.
83+
let proxyPath: string;
84+
if (fs.existsSync(path.join(pattern.input, pattern.glob))) {
9685
proxyPath = path.join(pattern.output, pattern.glob);
9786
} else {
9887
// For globs (paths that don't exist), proxy pattern.output to pattern.input.
99-
relativePath = path.relative(config.basePath, pattern.input);
10088
proxyPath = path.join(pattern.output);
101-
10289
}
10390
// Proxy paths must have only forward slashes.
10491
proxyPath = proxyPath.replace(/\\/g, '/');
105-
config.proxies['/' + proxyPath] = '/base/' + relativePath;
92+
config.proxies['/' + proxyPath] = '/_karma_webpack_/' + proxyPath;
10693
});
10794
}
10895

packages/angular_devkit/build_webpack/src/karma/schema.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,7 @@
122122
},
123123
"input": {
124124
"type": "string",
125-
"description": "The input path dir in which to apply 'glob'.",
126-
"default": "./"
125+
"description": "The input path dir in which to apply 'glob'."
127126
},
128127
"output": {
129128
"type": "string",

packages/angular_devkit/build_webpack/test/karma/assets_spec_large.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import { tap } from 'rxjs/operators';
910
import { host, karmaTargetSpec, runTargetSpec } from '../utils';
1011

1112

@@ -88,11 +89,12 @@ describe('Karma Builder assets', () => {
8889
assets: [
8990
{ glob: 'glob-asset.txt' },
9091
{ glob: 'output-asset.txt', output: 'output-folder' },
91-
{ glob: '**/*', input: 'folder', output: 'folder' },
92+
{ glob: '**/*', input: 'src/folder', output: 'folder' },
9293
],
9394
};
9495

9596
runTargetSpec(host, karmaTargetSpec, overrides).pipe(
97+
tap(buildEvent => expect(buildEvent.success).toBe(true)),
9698
).subscribe(undefined, done.fail, done);
9799
}, 45000);
98100
});

0 commit comments

Comments
 (0)