UNPKG

travelm-agency-webpack-plugin

Version:

Run the travelm agency code generator as part of your webpack build

130 lines (129 loc) 6.59 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (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 () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const T = __importStar(require("travelm-agency")); const util_1 = require("util"); const webpack_1 = require("webpack"); class TravelmAgencyPlugin { constructor(options) { var _a, _b; this.ranOnce = false; this.runTravelmAgency = this.runTravelmAgency.bind(this); this.writeJsonFiles = this.writeJsonFiles.bind(this); const customHtmlModule = (_a = options.customHtmlModule) !== null && _a !== void 0 ? _a : "Html"; this.options = { translationDir: options.translationDir || "translations", elmPath: options.elmPath || "src/Translations.elm", generatorMode: options.generatorMode || "inline", i18nArgFirst: !!options.i18nArgFirst, addContentHash: options.addContentHash === undefined ? true : options.addContentHash, jsonPath: options.jsonPath || "i18n", prefixFileIdentifier: !!options.prefixFileIdentifier, customHtmlModule, customHtmlAttributesModule: (_b = options.customHtmlAttributesModule) !== null && _b !== void 0 ? _b : `${customHtmlModule}.Attributes`, }; } apply(compiler) { compiler.hooks.beforeRun.tapPromise(TravelmAgencyPlugin.name, () => __awaiter(this, void 0, void 0, function* () { if (this.ranOnce) { return; } this.ranOnce = true; return this.runTravelmAgency(yield this.getTranslationFilePaths(), false); })); compiler.hooks.compilation.tap(TravelmAgencyPlugin.name, (compilation) => { compilation.hooks.processAssets.tap(TravelmAgencyPlugin.name, this.writeJsonFiles); }); compiler.hooks.afterCompile.tap(TravelmAgencyPlugin.name, (compilation) => { compilation.contextDependencies.add(path_1.default.resolve(this.options.translationDir)); }); compiler.hooks.watchRun.tapPromise(TravelmAgencyPlugin.name, (compilation) => __awaiter(this, void 0, void 0, function* () { if (!compilation.modifiedFiles || compilation.modifiedFiles.has(path_1.default.resolve(this.options.translationDir))) { const translationFilePaths = yield this.getTranslationFilePaths(); return this.runTravelmAgency(translationFilePaths, true).catch((err) => { compilation .getInfrastructureLogger(TravelmAgencyPlugin.name) .error(err); }); } })); } getTranslationFilePaths() { return __awaiter(this, void 0, void 0, function* () { return (yield (0, util_1.promisify)(fs_1.default.readdir)(this.options.translationDir)).map((fileName) => path_1.default.resolve(this.options.translationDir, fileName)); }); } runTravelmAgency(translationFilePaths, devMode) { return __awaiter(this, void 0, void 0, function* () { const travelmAgency = T.createInstance(); yield travelmAgency.sendTranslations(translationFilePaths, devMode); this.responseContent = yield travelmAgency.finishModule(Object.assign(Object.assign({}, this.options), { devMode })); const shouldBeWritten = yield (0, util_1.promisify)(fs_1.default.readFile)(this.options.elmPath, { encoding: "utf-8", }) .then((data) => { var _a; return data !== ((_a = this.responseContent) === null || _a === void 0 ? void 0 : _a.elmFile); }) .catch(() => true); if (shouldBeWritten) { yield (0, util_1.promisify)(fs_1.default.writeFile)(this.options.elmPath, this.responseContent.elmFile); } }); } writeJsonFiles(assets) { const { options } = this; if (options.generatorMode === "dynamic" && this.responseContent) { this.responseContent.optimizedJson.forEach(({ filename, content }) => { if (!assets[path_1.default.join(options.jsonPath, filename)]) { assets[path_1.default.join(options.jsonPath, filename)] = new webpack_1.sources.RawSource(content); } }); } } } exports.default = TravelmAgencyPlugin;