webpack-angular-translate
Version:
Webpack plugin that extracts the translation-ids with the default texts.
98 lines • 4.49 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AngularTranslatePlugin = void 0;
var webpack_sources_1 = require("webpack-sources");
var translations_registry_1 = __importStar(require("./translations-registry"));
/**
* Stateful plugin that keeps the found translations stored in this.translations.
* @property translations object hash where the translation id is used to retrieve the Translation object.
* @constructor
*/
var AngularTranslatePlugin = /** @class */ (function () {
function AngularTranslatePlugin(options) {
this.translationsRegistry = new translations_registry_1.default();
this.options = __assign({ fileName: "translations.json" }, options);
}
/**
* Entry function from webpack that registers the plugin in the required-build-phases
* @param compiler
*/
AngularTranslatePlugin.prototype.apply = function (compiler) {
var _this = this;
compiler.hooks.compilation.tap("webpack-angular-translate", function (compilation) {
_this.compilation = compilation;
/**
* Register the plugin to the normal-module-loader and expose the registerTranslation function in the loaderContext.
* This way the loader can communicate with the plugin and pass the translations to the plugin.
*/
compilation.hooks.normalModuleLoader.tap("webpack-angular-translate", function (loaderContext) {
loaderContext.registerTranslation = _this.registerTranslation.bind(_this);
loaderContext.pruneTranslations = _this.translationsRegistry.pruneTranslations.bind(_this.translationsRegistry);
});
});
compiler.hooks.emit.tap("webpack-angular-translate", this.emitResult.bind(this));
};
/**
* Registers a new translation that should be included in the output
* @param translation {Translation} the translation to register
*/
AngularTranslatePlugin.prototype.registerTranslation = function (translation) {
try {
this.translationsRegistry.registerTranslation(translation);
}
catch (e) {
if (e instanceof translations_registry_1.EmptyTranslationIdError) {
this.compilation.warnings.push(e);
}
else if (e instanceof translations_registry_1.TranslationRegistrationError) {
this.compilation.errors.push(e);
}
else {
throw e;
}
}
};
AngularTranslatePlugin.prototype.emitResult = function (compilation) {
// Only create the file if it is not empty.
// Fixes an issue with karma-webpack where the build fails when the asset is emitted.
if (!this.translationsRegistry.empty) {
var translations = this.translationsRegistry.toJSON();
var content = JSON.stringify(translations, null, "\t");
compilation.assets[this.options.fileName] = new webpack_sources_1.RawSource(content);
}
};
return AngularTranslatePlugin;
}());
exports.AngularTranslatePlugin = AngularTranslatePlugin;
exports.default = AngularTranslatePlugin;
//# sourceMappingURL=angular-translate-plugin.js.map