Skip to content
This repository was archived by the owner on May 22, 2023. It is now read-only.

Commit 0f4066e

Browse files
committed
(#34) add webpack 4 compatibility
1 parent 5cd0719 commit 0f4066e

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22

33
## Last Changes
44

5+
- [#34](https://github.com/LaxarJS/laxar-loader/issues/34): add webpack 4 compatibility
6+
+ NEW FEATURE: see tickert for details
7+
58

69
## v2.0.2
710

811
- [#33](https://github.com/LaxarJS/laxar-loader/issues/33): fixed loading laxar assets on windows
912

13+
1014
## v2.0.1
1115

1216
- [#32](https://github.com/LaxarJS/laxar-loader/issues/32): fixed `debug-info` not being bundled separately

src/index.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ import loaderUtils from 'loader-utils';
1010
import laxarTooling from 'laxar-tooling';
1111

1212
const DEFAULT_CONFIG = {};
13+
const JSONISH = /^\s*[{[]/;
1314

1415
module.exports = function( source /*, map */ ) {
1516
const loaderContext = this;
17+
const rootContext = loaderContext.rootContext ||
18+
( loaderContext.options ? loaderContext.options.context : '' );
1619
const query = loaderUtils.parseQuery( loaderContext.query );
1720
const entries = query.entries && buildEntries( query );
1821

@@ -29,7 +32,7 @@ module.exports = function( source /*, map */ ) {
2932

3033
const configPath = typeof query.laxarConfig === 'string' ?
3134
resolveRelative( loaderContext.context, query.laxarConfig ) :
32-
resolveRelative( loaderContext.options.context || '', './laxar.config' );
35+
resolveRelative( rootContext, './laxar.config' );
3336

3437
const loaders = {
3538
json: require.resolve( './json' ),
@@ -108,7 +111,13 @@ module.exports = function( source /*, map */ ) {
108111
);
109112

110113
function readJson( filename ) {
111-
return loadModule( `${loaders.json}!${filename}` );
114+
return loadSource( filename )
115+
.then( code => {
116+
if( JSONISH.test( code ) ) {
117+
return JSON.parse( code );
118+
}
119+
return loaderContext.exec( code, filename );
120+
} );
112121
}
113122

114123
function requireFile( module, type, name ) {
@@ -126,25 +135,24 @@ module.exports = function( source /*, map */ ) {
126135

127136
function loadModule( request ) {
128137
const filename = request.split( '!' ).pop().split( '?' ).shift();
138+
return loadSource( request )
139+
.then( code => loaderContext.exec( code, filename ) );
140+
}
129141

142+
function loadSource( request ) {
130143
return new Promise( ( resolve, reject ) => {
131144
loaderContext.loadModule( request, ( err, code ) => {
132145
if( err ) {
133146
reject( err );
134147
return;
135148
}
136-
try {
137-
resolve( loaderContext.exec( code, filename ) );
138-
}
139-
catch( err ) {
140-
reject( err );
141-
}
149+
resolve( code );
142150
} );
143151
} );
144152
}
145153

146154
function resolve( ref ) {
147-
return resolveRelative( loaderContext.options.context, ref );
155+
return resolveRelative( rootContext, ref );
148156
}
149157

150158
function resolveRelative( context, ref ) {

0 commit comments

Comments
 (0)