-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.js
116 lines (106 loc) · 3.69 KB
/
plugin.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
/**
* Copyright 2017 aixigo AG
* Released under the MIT license.
* https://opensource.org/licenses/MIT
*/
const path = require( 'path' );
const http = require( 'http' );
const createServer = require( './lib/server' );
const createApp = require( './lib/app' );
const loaderPath = require.resolve( __dirname );
module.exports = function( options ) {
const server = createServer( options || {} );
return {
apply( compiler ) {
const context = ( options && options.context ) || compiler.context;
const middleware = [];
let watching = false;
if( Array.isArray( options.middleware ) ) {
middleware.push.apply( middleware, options.middleware );
}
else if( 'middleware' in options ) {
middleware.push( options.middleware );
}
else {
middleware.push( createApp( options ) );
}
server.notify = ( entry, callback ) => {
const address = server.address();
const req = http.request( {
method: 'POST',
port: address.port,
family: address.family,
path: options.api || '/',
headers: {
'content-type': 'application/json'
}
}, () => callback( null ) );
req.on( 'error', callback );
req.write( JSON.stringify( entry ) );
req.end();
};
compiler.plugin( 'after-environment', () => {
middleware.unshift( requestHandler( compiler.inputFileSystem, context ) );
middleware.forEach( server.use );
} );
compiler.plugin( 'watch-run', ( watcher, callback ) => {
watching = true;
server.listen( err => {
const baseUrl = `http://localhost:${server.address().port}/`;
if( !err ) {
// eslint-disable-next-line no-console
console.log( `PDFreactor server is running at \u001b[1m${baseUrl}\u001b[0m` );
}
callback( err );
} );
} );
compiler.plugin( 'watch-close', ( watcher, callback ) => {
server.close();
callback();
} );
compiler.plugin( 'before-run', ( compiler, callback ) => {
if( !watching ) {
server.listen( callback );
}
} );
compiler.plugin( 'done', stats => {
if( !watching ) {
server.close();
}
} );
compiler.plugin( 'compilation', compilation => {
compilation.plugin( 'normal-module-loader', ( loaderContext, module ) => {
const index = module.loaders.findIndex( obj => obj.loader === loaderPath );
if( index >= 0 ) {
loaderContext.options = Object.assign(
loaderContext.options || {},
options,
{ server, context }
);
module.loaders.splice(
index,
1,
{ loader: require.resolve( './lib/post' ) },
module.loaders[ index ],
{ loader: require.resolve( './lib/pre' ) }
);
}
} );
} );
}
};
};
function requestHandler( fs, context ) {
return ( req, res, next ) => {
const resource = path.resolve( context, req.url.replace( /^\//, '' ) );
fs.readFile( resource, ( err, content ) => {
if( err ) {
next();
return;
}
res.statusCode = 200;
res.statusMessage = 'Ok';
res.end( content );
} );
};
}