Merge pull request #2754 from solgenomics/topic/fix_homepage_add_accessions_dialog
[sgn.git] / js / webpack_util / jsan-preprocess-loader.js
blobf1bcdef2032e96cc6888a0964f3b635d4bc8f3a8
1 // Handles JSAN imports and adds the JSAN adaptor as needed.
3 const path = require('path');
4 const fs = require('fs');
5 const loaderUtils = require('loader-utils');
6 const {
7         JSAN_adaptor,
8         common_js_regex,
9         import_regex,
10         src_path_to_JSAN,
11         isChildOf } = require("./utils.js");
12         
13 module.exports = function() {
14         this.cacheable();
15         const callback = this.async();  
16         const options = loaderUtils.getOptions(this);
17         var rpath = this.resourcePath;
18         fs.readFile(rpath, function read(err, data) {
19             if (err) {
20                 callback(err);
21             }
22                 var JSAN_adapt = "require('"+JSAN_adaptor+"');\n";
23                 var head = "";
24                 var replac_func = function(match,group1,group2){
25                         head = head || JSAN_adapt;
26                         var relpath = group1||group2;
27                         var src_path = path.resolve(path.dirname(rpath),relpath);
28                         return isChildOf(src_path,options.legacyPath) ? src_path_to_JSAN(options.legacyPath,src_path) : match;
29                 };
30                 var out = String(data).replace(import_regex,replac_func)
31                                                           .replace(common_js_regex,replac_func);
32                 callback(null, head+out);
33         });