1 // Creates JSON dependency mapping from webpack compilation.
3 const path = require('path');
4 const fs = require('fs');
6 function path_inside(parent, dir) {
7 var relative_path = path.relative(parent, dir);
8 return !!relative_path && !relative_path.startsWith('..') && !path.isAbsolute(relative_path);
11 function FileMapPlugin(options) {
12 this.jsan_re = RegExp(
16 ).replace(/^[\s\n]+|[\s\n]+$/,""),
21 FileMapPlugin.prototype.apply = function(compiler) {
23 compiler.hooks.emit.tapAsync('SGNFileMapPlugin', function(compilation,callback) {
25 var legacy_lists = {};
26 [...compilation.entrypoints].forEach((kvpair)=>{
27 entrypoints[kvpair[0]] = {
28 'files': kvpair[1].chunks.reduce((a,chunk)=>a.concat(chunk.files),[]).filter(f=>f.endsWith(".js")),
31 entrypoints[kvpair[0]].files.forEach(f=>{
32 legacy_lists[f] = legacy_lists[f] || [];
33 legacy_lists[f].push(entrypoints[kvpair[0]].legacy);
36 compilation.chunks.forEach(chunk=>{
37 chunk.files.forEach(f=>{
38 compilation.assets[f].source().replace(self.jsan_re,function(m,g1,g2){
39 legacy_lists[f].forEach(leg_list=>leg_list.push(g1||g2));
43 var entrypoints_string = JSON.stringify(entrypoints,null,2);
44 compilation.assets['mapping.json'] = {
46 return entrypoints_string;
49 return entrypoints_string.length;
56 module.exports = FileMapPlugin;