@@ -8,7 +8,6 @@ import * as webpack from 'webpack';
88const webpackDevMiddleware = require ( 'webpack-dev-middleware' ) ;
99
1010import { AssetPattern } from '../models/webpack-configs/utils' ;
11- import { isDirectory } from '../utilities/is-directory' ;
1211import { KarmaWebpackFailureCb } from './karma-webpack-failure-cb' ;
1312
1413/**
@@ -50,7 +49,7 @@ function addKarmaFiles(files: any[], newFiles: any[], prepend = false) {
5049
5150const 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
0 commit comments