@@ -11,8 +11,8 @@ import { WebpackConfigOptions } from '../build-options';
1111import { getSourceMapDevTool } from './utils' ;
1212
1313/**
14- * Returns a partial specific to creating a bundle for node
15- * @param wco Options which are include the build options and app config
14+ * Returns a partial Webpack configuration specific to creating a bundle for node
15+ * @param wco Options which include the build options and app config
1616 */
1717export function getServerConfig ( wco : WebpackConfigOptions ) : Configuration {
1818 const {
@@ -22,11 +22,14 @@ export function getServerConfig(wco: WebpackConfigOptions): Configuration {
2222 } = wco . buildOptions ;
2323
2424 const extraPlugins = [ ] ;
25- if ( sourceMap ) {
26- const { scripts, styles, hidden } = sourceMap ;
27- if ( scripts || styles ) {
28- extraPlugins . push ( getSourceMapDevTool ( scripts , styles , hidden ) ) ;
29- }
25+ const { scripts, styles, hidden } = sourceMap ;
26+ if ( scripts || styles ) {
27+ extraPlugins . push ( getSourceMapDevTool ( scripts , styles , hidden ) ) ;
28+ }
29+
30+ const externals : Configuration [ 'externals' ] = [ ...externalDependencies ] ;
31+ if ( ! bundleDependencies ) {
32+ externals . push ( externalizePackages ) ;
3033 }
3134
3235 const config : Configuration = {
@@ -44,31 +47,29 @@ export function getServerConfig(wco: WebpackConfigOptions): Configuration {
4447 ...extraPlugins ,
4548 ] ,
4649 node : false ,
50+ externals,
4751 } ;
4852
49- if ( bundleDependencies ) {
50- config . externals = [ ...externalDependencies ] ;
51- } else {
52- config . externals = [
53- ...externalDependencies ,
54- ( context : string , request : string , callback : ( error ?: null , result ?: string ) => void ) => {
55- // Absolute & Relative paths are not externals
56- if ( request . startsWith ( '.' ) || isAbsolute ( request ) ) {
57- callback ( ) ;
53+ return config ;
54+ }
5855
59- return ;
60- }
56+ function externalizePackages (
57+ context : string ,
58+ request : string ,
59+ callback : ( error ?: Error , result ?: string ) => void ,
60+ ) : void {
61+ // Absolute & Relative paths are not externals
62+ if ( request . startsWith ( '.' ) || isAbsolute ( request ) ) {
63+ callback ( ) ;
6164
62- try {
63- require . resolve ( request ) ;
64- callback ( null , request ) ;
65- } catch {
66- // Node couldn't find it, so it must be user-aliased
67- callback ( ) ;
68- }
69- } ,
70- ] ;
65+ return ;
7166 }
7267
73- return config ;
68+ try {
69+ require . resolve ( request , { paths : [ context ] } ) ;
70+ callback ( undefined , request ) ;
71+ } catch {
72+ // Node couldn't find it, so it must be user-aliased
73+ callback ( ) ;
74+ }
7475}
0 commit comments