File tree 7 files changed +63
-4
lines changed
7 files changed +63
-4
lines changed Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ import asyncMapSeries from 'neo-async/mapSeries';
7
7
8
8
import readBuffer from './readBuffer' ;
9
9
import WorkerError from './WorkerError' ;
10
+ import { replacer , reviver } from './serializer' ;
10
11
11
12
const workerPath = require . resolve ( './worker' ) ;
12
13
@@ -107,7 +108,7 @@ class PoolWorker {
107
108
108
109
writeJson ( data ) {
109
110
const lengthBuffer = Buffer . alloc ( 4 ) ;
110
- const messageBuffer = Buffer . from ( JSON . stringify ( data ) , 'utf-8' ) ;
111
+ const messageBuffer = Buffer . from ( JSON . stringify ( data , replacer ) , 'utf-8' ) ;
111
112
lengthBuffer . writeInt32BE ( messageBuffer . length , 0 ) ;
112
113
this . writePipe . write ( lengthBuffer ) ;
113
114
this . writePipe . write ( messageBuffer ) ;
@@ -141,7 +142,7 @@ class PoolWorker {
141
142
}
142
143
this . state = 'message read' ;
143
144
const messageString = messageBuffer . toString ( 'utf-8' ) ;
144
- const message = JSON . parse ( messageString ) ;
145
+ const message = JSON . parse ( messageString , reviver ) ;
145
146
this . state = 'process message' ;
146
147
this . onWorkerMessage ( message , ( err ) => {
147
148
if ( err ) {
Original file line number Diff line number Diff line change
1
+ export function replacer ( _key , value ) {
2
+ if ( value instanceof RegExp ) {
3
+ return {
4
+ __serialized_type : 'RegExp' ,
5
+ source : value . source ,
6
+ flags : value . flags ,
7
+ } ;
8
+ }
9
+ return value ;
10
+ }
11
+
12
+ export function reviver ( _key , value ) {
13
+ if ( typeof value === 'object' && value !== null ) {
14
+ // eslint-disable-next-line no-underscore-dangle
15
+ if ( value . __serialized_type === 'RegExp' ) {
16
+ return new RegExp ( value . source , value . flags ) ;
17
+ }
18
+ }
19
+
20
+ return value ;
21
+ }
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ import loaderRunner from 'loader-runner';
6
6
import asyncQueue from 'neo-async/queue' ;
7
7
8
8
import readBuffer from './readBuffer' ;
9
+ import { replacer , reviver } from './serializer' ;
9
10
10
11
const writePipe = fs . createWriteStream ( null , { fd : 3 } ) ;
11
12
const readPipe = fs . createReadStream ( null , { fd : 4 } ) ;
@@ -93,7 +94,7 @@ function writeJson(data) {
93
94
} ) ;
94
95
95
96
const lengthBuffer = Buffer . alloc ( 4 ) ;
96
- const messageBuffer = Buffer . from ( JSON . stringify ( data ) , 'utf-8' ) ;
97
+ const messageBuffer = Buffer . from ( JSON . stringify ( data , replacer ) , 'utf-8' ) ;
97
98
lengthBuffer . writeInt32BE ( messageBuffer . length , 0 ) ;
98
99
99
100
writePipeWrite ( lengthBuffer ) ;
@@ -319,7 +320,7 @@ function readNextMessage() {
319
320
return ;
320
321
}
321
322
const messageString = messageBuffer . toString ( 'utf-8' ) ;
322
- const message = JSON . parse ( messageString ) ;
323
+ const message = JSON . parse ( messageString , reviver ) ;
323
324
324
325
onMessage ( message ) ;
325
326
setImmediate ( ( ) => readNextMessage ( ) ) ;
Original file line number Diff line number Diff line change
1
+ $white : #FFFFFF ;
Original file line number Diff line number Diff line change 1
1
@import ' _shared' ;
2
+ @import ' color_palette' ;
2
3
3
4
body {
4
5
background : red ;
Original file line number Diff line number Diff line change @@ -16,6 +16,9 @@ module.exports = (env) => {
16
16
} ;
17
17
const sassLoaderOptions = {
18
18
sourceMap : true ,
19
+ sassOptions : {
20
+ includePaths : [ path . resolve ( __dirname , 'assets' ) ] ,
21
+ } ,
19
22
} ;
20
23
if ( + env . threads > 0 ) {
21
24
threadLoader . warmup ( workerPool , [ 'babel-loader' , 'babel-preset-env' ] ) ;
Original file line number Diff line number Diff line change
1
+ const { replacer, reviver } = require ( '../src/serializer' ) ;
2
+
3
+ test ( 'round-trips plain objects' , ( ) => {
4
+ const json = JSON . stringify (
5
+ {
6
+ a : 1 ,
7
+ b : 'foo' ,
8
+ c : [ null , false ] ,
9
+ } ,
10
+ replacer
11
+ ) ;
12
+ expect ( JSON . parse ( json , reviver ) ) . toEqual ( {
13
+ a : 1 ,
14
+ b : 'foo' ,
15
+ c : [ null , false ] ,
16
+ } ) ;
17
+ } ) ;
18
+
19
+ test ( 'round-trips regular expressions' , ( ) => {
20
+ const json = JSON . stringify (
21
+ {
22
+ r : / h o g e / g,
23
+ s : / ^ ( \w \s ) + $ / m,
24
+ } ,
25
+ replacer
26
+ ) ;
27
+ expect ( JSON . parse ( json , reviver ) ) . toEqual ( {
28
+ r : / h o g e / g,
29
+ s : / ^ ( \w \s ) + $ / m,
30
+ } ) ;
31
+ } ) ;
You can’t perform that action at this time.
0 commit comments