-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
94 lines (78 loc) · 2.49 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
/**
* Copyright 2017 aixigo AG
* Released under the MIT license.
* https://opensource.org/licenses/MIT
*/
'use strict';
const path = require( 'path' );
const java = require( 'java' );
const loaderUtils = require( 'loader-utils' );
const utils = require( './lib/utils' );
const STRING_WRITER = 'java.io.StringWriter';
module.exports = function( source ) {
const options = loaderUtils.getOptions( this ) || {};
const classpath = Array.isArray( options.classpath ) ? options.classpath : [ options.classpath ];
const baseDirectory = getBaseDirectory( this );
const template = loaderUtils.interpolateName( this, options.template, { content: source } );
this.cacheable();
this.async();
if( !java.isJvmCreated() ) {
java.registerClient( () => {
classpath.forEach( cp => {
if( cp ) {
java.classpath.push( path.resolve( this.context, cp ) );
}
} );
} );
}
java.ensureJvm( () => {
let fmConfig;
try {
fmConfig = utils.getFreeMarkerConfig( this, options, baseDirectory );
}
catch( err ) {
this.callback( err );
return;
}
fmConfig.getTemplate( path.relative( baseDirectory, template ), ( err, template ) => {
invokeBlocking( callback => {
try {
const untransformedModel = this.exec( source, this.resourcePath );
const fmData = utils.getModel( untransformedModel );
const fmWriter = java.newInstanceSync( STRING_WRITER );
template.process( fmData, fmWriter, () => {
callback( null, fmWriter.toStringSync() );
} );
}
catch( err ) {
callback( err );
}
}, this.callback );
} );
} );
};
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
let nextPromise = Promise.resolve();
function invokeBlocking( job, callback ) {
const previousNext = nextPromise;
nextPromise = new Promise( resolve => {
previousNext.then( () => {
job( ( ...args ) => {
callback( ...args );
resolve();
} );
} );
} );
}
function getBaseDirectory( webpack ) {
// Webpack 4
if( webpack.rootContext) {
return webpack.rootContext;
}
// Webpack 3
if( webpack.options && webpack.options.context ) {
return webpack.options.context;
}
// Fallback - Standalone
return process.cwd();
}