mmir-webpack
Version:
Integration for MMIR library in webpack builds
55 lines (54 loc) • 3.49 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
const filepath_utils_1 = __importDefault(require("mmir-tooling/utils/filepath-utils"));
const grammar_gen_1 = __importDefault(require("mmir-tooling/grammar/grammar-gen"));
const compat_utils_1 = require("../utils/compat-utils");
const grammarLoader = function (content, map, meta) {
var callback = this.async();
var options = (0, compat_utils_1.getLoaderOptions)(this) || {};
// log('mmir-grammer-loader: options -> ', options);//DEBUG
if (!options || !options.mapping) {
callback('failed to parse JSON grammar: missing list for grammar settings [{id: "the ID", file: "the file path", ...}, ...]');
return; /////////////// EARLY EXIT /////////////////
}
var grammarFile = filepath_utils_1.default.normalizePath(this.resource);
grammar_gen_1.default.compile(content, grammarFile, options, callback, map, meta);
return;
};
//HACK force prevention of json-loader
var jsonLoaderPath;
grammarLoader.pitch = function (_remainingRequest, _precedingRequest, _data) {
// log('mmir-grammer-loader: PITCHing | remaining: ', _remainingRequest, ' | preceding: ', _precedingRequest, ' | data: ', _data);//DEBUG
// log('mmir-grammer-loader: PITCHing options -> ',loaderUtils.getOptions(this));//DEBUG
var options = (0, compat_utils_1.getLoaderOptions)(this) || {};
grammar_gen_1.default.initPendingAsyncGrammarInfo(options);
//HACK for webpack < 4.x the Rule.type property for indicating the conversion JSON -> javascript is not allowed
// -> for webpack >= 2.x the json-loader may register itself for the JSON grammar which would produce errors
// since it will receive the javascript code emitted by the grammar-loader
// WORKAROUND/HACK: try to detect json-loader, and remove it if present:
var options = (0, compat_utils_1.getLoaderOptions)(this) || {};
if (options && options.isRuleTypeDisabled) { //<- this will be set, if Rule.type had to be removed due to webpack version < 4.x
if (!jsonLoaderPath) {
try {
jsonLoaderPath = filepath_utils_1.default.normalizePath(require.resolve('json-loader'));
}
catch (err) {
//-> json-loader is not available
// log('mmir-grammer-loader: PITCHing phase, [WARN] json-loader prevention WORKAROUND - options.isRuleTypeDisabled is set, but json-loader module cannot be resolved, arguments: | remainingRequest: ', remainingRequest, ' | precedingRequest: ', precedingRequest, ' | data: ', data);
//-> ignore: no use trying to remove json-loader from this.loaders...
return; ////////////// EARLY EXIT /////////////////////////
}
}
for (var i = this.loaders.length - 1; i >= 0; --i) {
// log('mmir-grammer-loader: checking loaders at ', i, ' -> ', this.loaders[i]);//DEBUG
// for(var n in this.loaders[i]) log('mmir-grammer-loader: loaders ', i, '['+n+'] -> ', this.loaders[i][n])//DEBUG
if (filepath_utils_1.default.normalizePath(this.loaders[i].path) === jsonLoaderPath) {
// log('mmir-grammer-loader: removing default json-loader at ', i);//DEBUG
this.loaders.splice(i, 1);
}
}
}
};
module.exports = grammarLoader;