3
3
Author Tobias Koppers @sokra
4
4
*/
5
5
6
- import path from 'path' ;
7
- import url from 'url' ;
8
-
9
6
import {
10
7
getOptions ,
11
8
stringifyRequest ,
@@ -17,7 +14,7 @@ import validateOptions from 'schema-utils';
17
14
18
15
import schema from './options.json' ;
19
16
20
- import { modifyUserRequest , getExposes } from './utils' ;
17
+ import { getExposes , contextify , getNewUserRequest } from './utils' ;
21
18
22
19
export default function loader ( ) {
23
20
const options = getOptions ( this ) ;
@@ -27,31 +24,14 @@ export default function loader() {
27
24
baseDataPath : 'options' ,
28
25
} ) ;
29
26
30
- // Change the request from an /abolute/path.js to a relative ./path.js
31
- // This prevents [chunkhash] values from changing when running webpack
32
- // builds in different directories.
33
- const newRequestPath = getRemainingRequest ( this )
34
- . split ( '!' )
35
- . map ( ( currentUrl ) => {
36
- const result = `./${ path . relative (
37
- this . context ,
38
- url . parse ( currentUrl ) . pathname
39
- ) } `;
40
-
41
- return process . platform === 'win32'
42
- ? result . split ( path . sep ) . join ( '/' )
43
- : result ;
44
- } )
45
- . join ( '!' ) ;
46
-
47
27
/*
48
28
* Workaround until module.libIdent() in webpack/webpack handles this correctly.
49
29
*
50
30
* fixes:
51
31
* - https://github.com/webpack-contrib/expose-loader/issues/55
52
32
* - https://github.com/webpack-contrib/expose-loader/issues/49
53
33
*/
54
- this . _module . userRequest = modifyUserRequest ( this . _module . userRequest ) ;
34
+ this . _module . userRequest = getNewUserRequest ( this . _module . userRequest ) ;
55
35
56
36
const callback = this . async ( ) ;
57
37
@@ -65,21 +45,12 @@ export default function loader() {
65
45
return ;
66
46
}
67
47
68
- const isModule = options . type !== 'commonjs' ;
48
+ // Change the request from an /abolute/path.js to a relative ./path.js.
49
+ // This prevents [chunkhash] values from changing when running webpack builds in different directories.
50
+ const newRequest = contextify ( this . rootContext , getRemainingRequest ( this ) ) ;
51
+ const stringifiedNewRequest = stringifyRequest ( this , `-!${ newRequest } ` ) ;
69
52
70
- let code = '' ;
71
-
72
- if ( isModule ) {
73
- code = `import * as ___EXPOSE_LOADER_IMPORT___ from ${ stringifyRequest (
74
- this ,
75
- `-!${ newRequestPath } `
76
- ) } ;\n`;
77
- } else {
78
- code = `var ___EXPOSE_LOADER_IMPORT___ = require(${ stringifyRequest (
79
- this ,
80
- `-!${ newRequestPath } `
81
- ) } );\n`;
82
- }
53
+ let code = `var ___EXPOSE_LOADER_IMPORT___ = require(${ stringifiedNewRequest } );\n` ;
83
54
84
55
code += `var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = require(${ stringifyRequest (
85
56
this ,
@@ -89,9 +60,9 @@ export default function loader() {
89
60
90
61
for ( const expose of exposes ) {
91
62
const { globalName, moduleLocalName } = expose ;
92
- const globalNameInterpolated = globalName . map ( ( item ) => {
93
- return interpolateName ( this , item , { } ) ;
94
- } ) ;
63
+ const globalNameInterpolated = globalName . map ( ( item ) =>
64
+ interpolateName ( this , item , { } )
65
+ ) ;
95
66
96
67
if ( typeof moduleLocalName !== 'undefined' ) {
97
68
code += `var ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___ = ___EXPOSE_LOADER_IMPORT___.${ moduleLocalName } \n` ;
@@ -113,15 +84,7 @@ export default function loader() {
113
84
: `${ propertyString } = ___EXPOSE_LOADER_IMPORT___;\n` ;
114
85
}
115
86
116
- if ( isModule ) {
117
- code += `export { default } from ${ stringifyRequest (
118
- this ,
119
- `-!${ newRequestPath } `
120
- ) } ;\n`;
121
- code += `export * from ${ stringifyRequest ( this , `-!${ newRequestPath } ` ) } ;\n` ;
122
- } else {
123
- code += `module.exports = ___EXPOSE_LOADER_IMPORT___;` ;
124
- }
87
+ code += `module.exports = ___EXPOSE_LOADER_IMPORT___;\n` ;
125
88
126
89
callback ( null , code ) ;
127
90
}
0 commit comments