Skip to content

Commit

Permalink
Another attempt and fixing up sourcemaps
Browse files Browse the repository at this point in the history
- Specify sourceRoot (since gulp-rev-all doesn't seem to add the prefix).
- Simplify source map generation

Co-authored-by: Jeff Hui <jeff@mayvenn.com>
  • Loading branch information
Justin Watt and jeffh committed Jun 21, 2019
1 parent 0203778 commit a918ec4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
49 changes: 24 additions & 25 deletions gulpfile.js
Expand Up @@ -148,10 +148,9 @@ function revAssets() {
}

var options = {
prefix: "//" + argv.host + "/cdn/",
prefix: "https://" + argv.host + "/cdn/",
includeFilesInManifest: ['.css', '.js', '.svg', '.png', '.gif', '.woff', '.cljs', '.cljc', '.map'],
dontSearchFile: ['.js'],
dontRenameFile: ['.map']
dontSearchFile: ['.js']
};

return hashedAssetSources()
Expand Down Expand Up @@ -197,13 +196,7 @@ function renameFile(oldFilename, newFilename) {
});
}

exports['fix-main-js-pointing-to-source-map'] = fixMainJsPointingToSourceMap;
async function fixMainJsPointingToSourceMap() {
// because .js files are excluded from search and replace of sha-ed versions (so that
// the js code doesn't become really wrong), we need to take special care to update
// main.js to have the sha-ed version of the sourcemap in the file
var revManifest = JSON.parse(await readFile("resources/rev-manifest.json"));

async function rootJSFiles() {
// probably should be production, but this is probably easier
var config = jsedn.toJS(jsedn.parse(await readFile('dev.cljs.edn')));
let outputDir = config[':output-dir'];
Expand All @@ -213,31 +206,37 @@ async function fixMainJsPointingToSourceMap() {
for (let [_, options] of Object.entries(config[':modules'])) {
jsRootFiles.push(assetPath + options[':output-to'].replace(outputDir, ''));
}
return jsRootFiles;
}

let base = "resources/public/cdn/";
exports['fix-main-js-pointing-to-source-map'] = fixMainJsPointingToSourceMap;
async function fixMainJsPointingToSourceMap() {
if (!argv.host) {
throw "missing --host";
}
var root = "https://" + argv.host + "/cdn/";

await Promise.all(jsRootFiles.map(async (jsKey) => {
var originalFullJsMapFile = base + jsKey + ".map";
var finalFullJsMapFile = base + revManifest[jsKey] + ".map";
// because .js files are excluded from search and replace of sha-ed versions (so that
// the js code doesn't become really wrong), we need to take special care to update
// main.js to have the sha-ed version of the sourcemap in the file
var revManifest = JSON.parse(await readFile("resources/rev-manifest.json"));

revManifest[jsKey + ".map"] = revManifest[jsKey] + ".map";
await renameFile(originalFullJsMapFile, finalFullJsMapFile);
}));
var jsRootFiles = await rootJSFiles();

let base = "resources/public/cdn/";

await Promise.all(jsRootFiles.map(async (jsKey) => {
var fullJsFile = "resources/public/cdn/" + revManifest[jsKey];
var data = await readFile(fullJsFile);
var result = data.replace(new RegExp(escapeRegExp(path.basename(jsKey)), 'g'), path.basename(revManifest[jsKey]));

var result = data.replace(new RegExp(escapeRegExp(path.basename(jsKey + '.map')), 'g'),
path.basename(revManifest[jsKey + '.map']));
await writeFile(fullJsFile, result);

let sourceMap = 'resources/public/cdn/' + revManifest[jsKey + ".map"];
data = await readFile(sourceMap);
result = data.replace(new RegExp(escapeRegExp(path.basename(jsKey)), 'g'),
path.basename(revManifest[jsKey]));
await writeFile(sourceMap, result);
fullJsFile = "resources/public/cdn/" + revManifest[jsKey + '.map'];
var sourceMap = JSON.parse(await readFile(fullJsFile));
sourceMap.sourceRoot = root;
await writeFile(fullJsFile, JSON.stringify(sourceMap));
}));
await writeFile("resources/rev-manifest.json", JSON.stringify(revManifest, null, 2));
}
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -23,7 +23,7 @@
"gulp-ignore": "^2.0.2",
"gulp-json-transform": "^0.4.6",
"gulp-postcss": "8.0.0",
"gulp-rev-all": "^2.0.1",
"gulp-rev-all": "^2.0.2",
"gulp-uglify": "^1.5.3",
"jsedn": "^0.4.1",
"merge-stream": "^0.1.7",
Expand Down

0 comments on commit a918ec4

Please sign in to comment.