UNPKG

hot-import

Version:

Hot Module Replacement (HMR) for Node.js

133 lines 5.53 kB
"use strict"; var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); } var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var g = generator.apply(thisArg, _arguments || []), i, q = []; return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i; function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; } function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } } function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); } function fulfill(value) { resume("next", value); } function reject(value) { resume("throw", value); } function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); } }; Object.defineProperty(exports, "__esModule", { value: true }); // https://github.com/Microsoft/TypeScript/issues/14151#issuecomment-280812617 if (!Symbol.asyncIterator) { Symbol.asyncIterator = Symbol.for('Symbol.asyncIterator'); } const fs = require("fs"); const os = require("os"); const path = require("path"); const rimraf = require("rimraf"); function* emptyObjectModuleFixture() { for (const workDir of tmpDir()) { const moduleFile = path.join(workDir, 'empty-module.ts'); const expectedValue = {}; try { fs.writeFileSync(moduleFile, `export = {}`); yield { file: moduleFile, returnValue: expectedValue, }; } catch (e) { console.error(e); } } } exports.emptyObjectModuleFixture = emptyObjectModuleFixture; function changingVariableModuleFixtures() { return __asyncGenerator(this, arguments, function* changingVariableModuleFixtures_1() { for (const workDir of tmpDir()) { const MODULE_FILE = path.join(workDir, 'changing-module.ts'); const ORIGINAL_TEXT = 'original'; const CHANGED_TEXT = 'changed'; try { fs.writeFileSync(MODULE_FILE, `export const answer = '${ORIGINAL_TEXT}'`); yield yield __await({ file: MODULE_FILE, returnValue: ORIGINAL_TEXT, }); yield __await(new Promise(resolve => { fs.writeFile(MODULE_FILE, `export const answer = '${CHANGED_TEXT}'`, resolve); })); yield yield __await({ file: MODULE_FILE, returnValue: CHANGED_TEXT, }); } catch (e) { console.error(e); } } }); } exports.changingVariableModuleFixtures = changingVariableModuleFixtures; function changingClassModuleFixtures() { return __asyncGenerator(this, arguments, function* changingClassModuleFixtures_1() { for (const workDir of tmpDir()) { const moduleFile = path.join(workDir, 'class-module.ts'); const MODULE_CODE1 = `export = class Test1 { public id = 1; constructor(public text: string) {}; }`; const MODULE_CODE2 = `export = class Test2 { public id = 2; constructor(public text: string) {}; }`; try { fs.writeFileSync(moduleFile, MODULE_CODE1); yield yield __await({ file: moduleFile, returnValue: 1, }); yield __await(new Promise(resolve => fs.writeFile(moduleFile, MODULE_CODE2, resolve))); yield yield __await({ file: moduleFile, returnValue: 2, }); } catch (e) { console.error(e); } } }); } exports.changingClassModuleFixtures = changingClassModuleFixtures; function changingRawFuncModuleFixtures() { return __asyncGenerator(this, arguments, function* changingRawFuncModuleFixtures_1() { for (const workDir of tmpDir()) { const MODULE_FILE = path.join(workDir, 'changing-module.ts'); const ORIGINAL_TEXT = 'original'; const CHANGED_TEXT = 'changed'; try { fs.writeFileSync(MODULE_FILE, `export = () => '${ORIGINAL_TEXT}'`); yield yield __await({ file: MODULE_FILE, returnValue: ORIGINAL_TEXT, }); yield __await(new Promise(resolve => { fs.writeFile(MODULE_FILE, `export = () => '${CHANGED_TEXT}'`, resolve); })); yield yield __await({ file: MODULE_FILE, returnValue: CHANGED_TEXT, }); } catch (e) { console.error(e); } } }); } exports.changingRawFuncModuleFixtures = changingRawFuncModuleFixtures; function* tmpDir() { const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'hot-import-')); try { yield dir; } finally { rimraf(dir, e => { if (e) { console.error('rimraf error: ', e); } }); } } exports.tmpDir = tmpDir; //# sourceMappingURL=fixtures.js.map