This repository has been archived by the owner on Apr 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
140 lines (112 loc) · 4.35 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
var path = require( 'path' ),
fs = require( 'fs' ),
assign = require( 'object-assign' )
var REGEXP_ESCAPE_RANGE = /([^\w\d])/g
function RedefinitionLevelsPlugin( base ) {
if( Array.prototype.some.call( arguments, function( arg ) {
return typeof arg !== 'string' && typeof arg !== 'boolean'
} ) )
throw new TypeError( '[webpack-redefinition-levels] Base path and levels must be a string.')
if( arguments.length < 2 || ( arguments.length === 2 && typeof arguments[ 1 ] === 'boolean' ) )
throw new TypeError( '[webpack-redefinition-levels] Invalid signature. At least one level argument is required.' )
if( !path.isAbsolute( base ) )
throw new Error( '[webpack-redefinition-levels] Base path must be absolute.' )
this.base = base
this.forceRedefinition = false;
var levels = Array.prototype.filter
.call( arguments, function( arg ) {
if( typeof arg === 'string' )
return true;
this.forceRedefinition = arg
return false;
}, this )
.map( function( level ) {
if( path.isAbsolute( level ) )
return level;
return path.resolve( base, level )
} )
this.levels = levels
this.baseRegexp = new RegExp( '^' + base.replace( REGEXP_ESCAPE_RANGE, '\\$1' ) )
if( this.forceRedefinition ) {
// /^(?:base|level\.1|…)
this.pathRegexp = new RegExp(
'^(?:' +
levels.map( function( level ) {
return level.replace( REGEXP_ESCAPE_RANGE, '\\$1' )
} ).join( '|' ) +
')'
)
} else {
this.pathRegexp = this.baseRegexp
}
}
RedefinitionLevelsPlugin.prototype.apply = function( compiler ) {
assign( this, {
extensions: compiler.options.extensions || this.extensions || [ '.jsx', '.js', '.json', '.node' ]
} )
compiler.resolvers.normal.apply( new ResolverPlugin( this ) )
}
module.exports = RedefinitionLevelsPlugin
function ResolverPlugin( options ) {
assign( this, options )
}
ResolverPlugin.canResolveModule = function( pathTo, extensions ) {
var pathParsed = path.parse( pathTo )
if( pathParsed.ext ) {
extensions = [ pathParsed.ext ]
pathTo = path.join( pathParsed.dir, pathParsed.name )
}
for( var i = 0, l = extensions.length; i < l; i++ ) {
try {
fs.accessSync( pathTo + extensions[ i ] )
return true;
} catch( e ) {}
}
return false;
}
ResolverPlugin.prototype.apply = function( resolver ) {
var self = this
resolver.plugin( "resolve", function( context, request ) {
// Dont touch modules like node_modules
if( request.path[0] !== '.' || request.module )
return;
// Module placed outside the base and levels folders
if( !self.baseRegexp.test( context ) )
return;
// Tries to find required component in current project dir
if( !self.forceRedefinition && ResolverPlugin.canResolveModule(
path.join( context, request.path ),
self.extensions
) )
return;
self.levels.some( function( level ) {
// Creates relative path to same folder in fallback project dir
var relative = path.relative(
context,
context.replace( self.pathRegexp, level )
)
// Ignore when cant conctruct relatibe path to fallback project dir
// But when forceRedefinition is true and context folder inside base folder,
// we'll try to find module by its original path (now request.path === newPath)
if( !relative && !self.forceRedefinition ) {
return true;
}
// Creates new relative path
var newPath = path.join(
relative,
request.path
)
// Keep path relative
if( newPath[ 0 ] !== '.' ) {
newPath = '.' + path.sep + newPath
}
if( ResolverPlugin.canResolveModule(
path.join( context, newPath ),
self.extensions
) ) {
request.path = newPath
return true;
}
} )
} );
}