File tree Expand file tree Collapse file tree 2 files changed +49
-0
lines changed Expand file tree Collapse file tree 2 files changed +49
-0
lines changed Original file line number Diff line number Diff line change @@ -29,4 +29,19 @@ export default [
29
29
} ,
30
30
} ) ,
31
31
) ,
32
+ ...makeNPMConfigVariants (
33
+ makeBaseNPMConfig ( {
34
+ entrypoints : [ 'src/config/prefixLoader.ts' ] ,
35
+
36
+ packageSpecificConfig : {
37
+ output : {
38
+ // make it so Rollup calms down about the fact that we're doing `export { loader as default }`
39
+ exports : 'default' ,
40
+
41
+ // preserve the original file structure (i.e., so that everything is still relative to `src`)
42
+ entryFileNames : 'config/[name].js' ,
43
+ } ,
44
+ } ,
45
+ } ) ,
46
+ ) ,
32
47
] ;
Original file line number Diff line number Diff line change
1
+ import * as fs from 'fs' ;
2
+ import * as path from 'path' ;
3
+
4
+ type LoaderOptions = {
5
+ distDir : string ;
6
+ } ;
7
+ // TODO Use real webpack types
8
+ type LoaderThis = {
9
+ // Webpack 4
10
+ query ?: LoaderOptions ;
11
+ // Webpack 5
12
+ getOptions ?: ( ) => LoaderOptions ;
13
+ addDependency : ( filepath : string ) => void ;
14
+ } ;
15
+
16
+ /**
17
+ * Inject templated code into the beginning of a module.
18
+ */
19
+ function prefixLoader ( this : LoaderThis , userCode : string ) : string {
20
+ // We know one or the other will be defined, depending on the version of webpack being used
21
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
22
+ const { distDir } = this . getOptions ? this . getOptions ( ) : this . query ! ;
23
+
24
+ const templatePath = path . resolve ( __dirname , 'prefixLoaderTemplate.js' ) ;
25
+ this . addDependency ( templatePath ) ;
26
+
27
+ // Fill in the placeholder
28
+ let templateCode = fs . readFileSync ( templatePath ) . toString ( ) ;
29
+ templateCode = templateCode . replace ( '__DIST_DIR__' , distDir ) ;
30
+
31
+ return `${ templateCode } \n${ userCode } ` ;
32
+ }
33
+
34
+ export { prefixLoader as default } ;
You can’t perform that action at this time.
0 commit comments