@@ -9,29 +9,23 @@ module.exports = (env, argv) => {
9
9
output : {
10
10
path : path . resolve ( __dirname , 'dist' ) ,
11
11
filename : 'scripts/[name].js' ,
12
- assetModuleFilename : 'assets/[hash][ext][query]' ,
12
+ } ,
13
+ externals : {
14
+ // Without declaring `schemas` as external, Webpack will attempt to look
15
+ // for the `schemas` library and bundle it. However, we want our schemas
16
+ // bundle to be separate from this one. This external config tells webpack
17
+ // that the schemas library will instead be supplied at runtime. External
18
+ // libraries can be provided in multiple ways, but we provide it through a
19
+ // script reference in the HTML file outputted by this bundle.
20
+ // https://webpack.js.org/configuration/externals/#externals
21
+ schemas : 'schemas' ,
13
22
} ,
14
23
plugins : [
15
24
new HtmlWebpackPlugin ( {
16
25
template : './index.html' ,
17
26
} ) ,
18
27
new CopyPlugin ( {
19
28
patterns : [
20
- {
21
- context : 'templates' ,
22
- from : '**/*.pdf' ,
23
- to : 'templates/[path][name][ext]' ,
24
- } ,
25
- {
26
- context : 'templates' ,
27
- from : '**/schema.yaml' ,
28
- to : 'templates/[path][name][ext]' ,
29
- } ,
30
- {
31
- context : 'templates' ,
32
- from : '**/exampleInput/*' ,
33
- to : 'templates/[path][name][ext]' ,
34
- } ,
35
29
{
36
30
from : 'main.html' ,
37
31
} ,
@@ -52,6 +46,12 @@ module.exports = (env, argv) => {
52
46
{
53
47
test : / \. ( p n g | s v g | j p g | j p e g | g i f ) $ / i,
54
48
type : 'asset/resource' ,
49
+ generator : {
50
+ // Will throw a file not found error if ``dist`` folder not in
51
+ // ``{project root}/web/``.
52
+ filename : '../[file]' ,
53
+ emit : false ,
54
+ } ,
55
55
} ,
56
56
] ,
57
57
} ,
@@ -61,8 +61,44 @@ module.exports = (env, argv) => {
61
61
} ,
62
62
} ;
63
63
64
+ // Difficult to run two webpack instances on a single dev server (i.e., this
65
+ // file, and the other webpack file that builds schemas). So for dev servers,
66
+ // we will stick to a singular build that concatenates both application and
67
+ // schema content. This is fine, because the whole point of having a separate
68
+ // build for schema content was to reduce production build times when users
69
+ // are only editing schema files (and not the rest of the application), but
70
+ // the dev server already gets around this problem through hot loading.
64
71
if ( argv . mode === 'development' ) {
65
72
config . devtool = 'eval-source-map' ;
73
+ // The external schemas lib is replaced by a direct reference to the
74
+ // schemas entrypoint. When you directly reference the schemas entrypoint in
75
+ // this bundle, a separate schemas library is not needed, because this
76
+ // bundle will now include all schema content as well.
77
+ config . resolve = {
78
+ alias : {
79
+ schemas : path . resolve ( __dirname , 'schemas.js' ) ,
80
+ } ,
81
+ } ;
82
+ delete config . externals ;
83
+ // Need pdf SOPs that schema build previously supplied
84
+ config . plugins . push (
85
+ new CopyPlugin ( {
86
+ patterns : [
87
+ {
88
+ context : 'templates' ,
89
+ from : '**/*.pdf' ,
90
+ to : 'templates/[path][name][ext]' ,
91
+ } ,
92
+ ] ,
93
+ } )
94
+ ) ;
95
+ // False emits don't play nice with dev servers either
96
+ for ( const rule of config . module . rules ) {
97
+ if ( rule . hasOwnProperty ( 'generator' ) ) {
98
+ delete rule . generator . filename ;
99
+ delete rule . generator . emit ;
100
+ }
101
+ }
66
102
}
67
103
68
104
return config ;
0 commit comments