UNPKG

browser-store-test-data

Version:
175 lines (144 loc) 6.75 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _fs = _interopRequireDefault(require("fs")); var _path = _interopRequireDefault(require("path")); var _webpack = require("webpack"); var _constants = require("../core/constants"); var _ConsoleLogger = _interopRequireDefault(require("../core/logger/ConsoleLogger.js")); var BrowserStoreTestDataPlugin = /*#__PURE__*/function () { function BrowserStoreTestDataPlugin(options) { (0, _classCallCheck2["default"])(this, BrowserStoreTestDataPlugin); this.options = options || {}; this.testLoaderScript = 'test-loader.js'; this.testDataLoadUri = _constants.TEST_DATA_LOAD_URI; this.testBaseUri = _constants.TEST_BASE_URI; this.testDataUri = "".concat(this.testBaseUri, "/data"); } (0, _createClass2["default"])(BrowserStoreTestDataPlugin, [{ key: "apply", value: function apply(compiler) { this.logger = new _ConsoleLogger["default"](compiler.getInfrastructureLogger(_constants.PLUGIN_NAME)); this.addTestLoader(compiler); this.injectTestLoader(compiler); this.copyTestDataFilesAndCreateLoad(compiler); this.reportEmittedAssets(compiler); } }, { key: "addTestLoader", value: function addTestLoader(compiler) { var _this = this; compiler.hooks.thisCompilation.tap(_constants.PLUGIN_NAME, function (compilation) { compilation.hooks.processAssets.tap({ name: _constants.PLUGIN_NAME, stage: _webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL }, function () { var loaderScriptUri = "".concat(_this.testBaseUri, "/").concat(_this.testLoaderScript); var loaderFileAbsolutePath = require.resolve('../loader/loader.bundle.js'); var emittedAssetUri = _this.emitFileAsAsset(compilation, loaderFileAbsolutePath, loaderScriptUri); _this.logger.log("ADDED loader [".concat(emittedAssetUri, "]")); }); }); } }, { key: "copyTestDataFilesAndCreateLoad", value: function copyTestDataFilesAndCreateLoad(compiler) { var _this2 = this; compiler.hooks.thisCompilation.tap(_constants.PLUGIN_NAME, function (compilation) { compilation.hooks.processAssets.tap({ name: _constants.PLUGIN_NAME, stage: _webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS }, function (assets) { var browserDatabase = _this2.options.browserDatabase; var testDataConfig = _this2.options.testData; var testDataLoad = { database: browserDatabase, testData: [] }; testDataConfig.forEach(function (config) { var testDataFile = config.dataFile; var copiedFileUri = _this2.copyTestDataFile(compiler, compilation, testDataFile); _this2.logger.log("COPIED test data file [".concat(copiedFileUri, "]")); testDataLoad.testData.push({ collectionName: config.collectionName, testDataUri: "/".concat(copiedFileUri) }); }); var testDataLoadContent = JSON.stringify(testDataLoad, undefined, 2); var testDataLoadFileUri = _this2.testDataLoadUri; var emittedAssetUri = _this2.emitAssetFile(compilation, testDataLoadContent, testDataLoadFileUri); _this2.logger.log("CREATED test data load [".concat(emittedAssetUri, "]")); }); }); } }, { key: "injectTestLoader", value: function injectTestLoader(compiler) { var _this3 = this; compiler.hooks.compilation.tap(_constants.PLUGIN_NAME, function (compilation) { compilation.hooks.processAssets.tap({ name: _constants.PLUGIN_NAME, stage: _webpack.Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE }, function (assets) { var publicPath = compilation.outputOptions.publicPath; var loaderFile = "".concat(_this3.testBaseUri, "/").concat(_this3.testLoaderScript); Object.entries(assets).forEach(function (_ref) { var _ref2 = (0, _slicedToArray2["default"])(_ref, 2), pathname = _ref2[0], source = _ref2[1]; if (pathname === 'index.html') { var htmlPortionToUpdate = '<script'; var htmlUpdate = "<script src=\"".concat(publicPath).concat(loaderFile, "\"></script>").concat(htmlPortionToUpdate); var fileContent = source.source().replace(htmlPortionToUpdate, htmlUpdate); var webpackFileSource = new _webpack.sources.RawSource(fileContent); compilation.updateAsset('index.html', webpackFileSource); } }); }); }); } }, { key: "reportEmittedAssets", value: function reportEmittedAssets(compiler) { var _this4 = this; compiler.hooks.assetEmitted.tap(_constants.PLUGIN_NAME, function (file, _ref3) { var content = _ref3.content, source = _ref3.source, outputPath = _ref3.outputPath, compilation = _ref3.compilation, targetPath = _ref3.targetPath; _this4.logger.log("EMITTED [".concat(file, "]")); }); } }, { key: "copyTestDataFile", value: function copyTestDataFile(compiler, compilation, testDataFilePath) { var contextPath = compiler.context; var absoluteTestDataFilePath = _path["default"].join(contextPath, testDataFilePath); var testDataAssetFileName = _path["default"].basename(absoluteTestDataFilePath); var testDataAssetUri = "".concat(this.testDataUri, "/").concat(testDataAssetFileName); return this.emitFileAsAsset(compilation, absoluteTestDataFilePath, testDataAssetUri); } }, { key: "emitFileAsAsset", value: function emitFileAsAsset(compilation, absoluteFilePath, fileUri) { var contents = _fs["default"].readFileSync(absoluteFilePath); return this.emitAssetFile(compilation, contents, fileUri); } }, { key: "emitAssetFile", value: function emitAssetFile(compilation, contents, assetFileUri) { var webpackFileSource = new _webpack.sources.RawSource(contents); compilation.emitAsset(assetFileUri, webpackFileSource); return assetFileUri; } }]); return BrowserStoreTestDataPlugin; }(); exports["default"] = BrowserStoreTestDataPlugin;