Skip to content

Instantly share code, notes, and snippets.

@fkchang
Created June 21, 2018 21:36
Show Gist options
  • Save fkchang/9f766dac2f43819a7a2b4607fac3ad3e to your computer and use it in GitHub Desktop.
Save fkchang/9f766dac2f43819a7a2b4607fac3ad3e to your computer and use it in GitHub Desktop.
'use strict';
const fs = require('fs');
const child_process = require('child_process');
module.exports = class OpalWebpackResolverPlugin {
constructor(source, target) {
const gemfile_path = 'Gemfile';
const gemfile_lock_path = 'Gemfile.lock';
const owl_cache_path = '.owl_cache/load_paths.json';
var gemfile_mtime = fs.statSync(gemfile_path).mtimeMs;
var gemfile_lock_mtime = fs.statSync(gemfile_lock_path).mtimeMs;
var owl_cache_mtime = fs.statSync(owl_cache_path).mtimeMs;
if (gemfile_mtime > gemfile_lock_mtime) {
console.error("Gemfile is newer than Gemfile.lock, please run 'bundle install' or 'bundle update'!");
}
if (gemfile_lock_mtime > owl_cache_mtime) {
console.error("Gemfile.lock is newer than load path cache, please restart opal-webpack-compile-server and webpack");
}
if (!this.owl_cache_fetched) {
var owl_cache_from_file = fs.readFileSync(owl_cache_path);
var owl_cache = JSON.parse(owl_cache_from_file.toString());
this.opal_load_paths = owl_cache.opal_load_paths;
this.opal_load_path_entries = owl_cache.opal_load_path_entries;
this.owl_cache_fetched = true;
}
this.source = source;
this.target = target;
}
apply(resolver) {
const target = resolver.ensureHook(this.target);
resolver.getHook(this.source).tapAsync("OpalWebpackResolverPlugin", (request, resolveContext, callback) => {
if (request.request.endsWith('.rb') || request.request.endsWith('.js')) {
var absolute_path = this.get_absolute_path(request.path, request.request);
if (absolute_path) {
var result = Object.assign({}, request, {path: absolute_path});
resolver.doResolve(target, result, "opal-webpack-resolver-plugin found: " + absolute_path, resolveContext, callback);
} else {
// continue pipeline
return callback();
}
} else {
// continue pipeline
return callback();
}
});
}
is_file(path) {
if(path.includes('paggio')) {
console.log('is_file: LOOKING for ' + path + ' IS_FILE: = ' + fs.statSync(path).isFile());
var bla = fs.statSync(path);
//console.log('ISFILE: %O', bla);
}
return fs.statSync(path).isFile();
}
get_absolute_path(path, request) {
var logical_filename_rb;
var logical_filename_js;
var absolute_filename;
var module;
if(path.includes('paggio') || request.includes('paggio')) {
console.log("\nget_absolute_path: LOOKING 2 for |" + path + '| REQUEST: ' + request);
}
// cleanup request, comes like './module.rb', we want '/module.rb'
if (request.startsWith('./')) {
module = request.slice(1);
} else if (request.startsWith('/')) {
module = request;
} else {
module = '/' + request;
}
// opal allows for require of
// .rb, .js, .js.rb, look up all of them
if (module.endsWith('.rb')) {
logical_filename_rb = module;
logical_filename_js = module.slice(0,module.length-2) + 'js';
} else if (module.endsWith('.js')) {
logical_filename_rb = module + '.rb';
logical_filename_js = module;
}
var l = this.opal_load_paths.length;
// in general, to avoid conflicts, we need to lookup .rb first, once all .rb
// possibilities are checked, check .js
// try .rb
// look up known entries
for (var i = 0; i < l; i++) {
absolute_filename = this.opal_load_paths[i] + logical_filename_rb;
if (this.opal_load_path_entries.includes(absolute_filename)) {
// check if file exists?
if(absolute_filename.includes('paggio.rb')){
console.log("\n\n looking for paggio.rb " + this.opal_load_path_entries);
}
if (fs.existsSync(absolute_filename) && this.is_file(absolute_filename)) {
if(path.includes('paggio')) {
console.log('get_absolute_path: RETURNING ' + absolute_filename );
}
return absolute_filename;
}
}
}
// look up file system of app
for (var i = 0; i < l; i++) {
if (this.opal_load_paths[i].startsWith(process.cwd())) {
absolute_filename = this.opal_load_paths[i] + logical_filename_rb;
if (fs.existsSync(absolute_filename) && this.is_file(absolute_filename)) {
return absolute_filename;
}
}
}
// check current path
absolute_filename = path + logical_filename_rb;
if (absolute_filename.startsWith(process.cwd())) {
if (fs.existsSync(absolute_filename) && this.is_file(absolute_filename)) {
return absolute_filename;
}
}
// try .js
// look up known entries
for (var i = 0; i < l; i++) {
absolute_filename = this.opal_load_paths[i] + logical_filename_js;
if (this.opal_load_path_entries.includes(absolute_filename)) {
// check if file exists?
if (fs.existsSync(absolute_filename) && this.is_file(absolute_filename)) {
return absolute_filename;
}
}
}
// look up file system of app
for (var i = 0; i < l; i++) {
if (this.opal_load_paths[i].startsWith(process.cwd())) {
absolute_filename = this.opal_load_paths[i] + logical_filename_js;
if (fs.existsSync(absolute_filename) && this.is_file(absolute_filename)) {
return absolute_filename;
}
}
}
if(path.includes('paggio')) {
console.log('get_absolute_path: NULL for ' + absolute_filename );
}
return null;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment