@@ -184,60 +184,58 @@ function buildSrcTypeScript( tsProject, outputDir ) {
184184 *
185185 * const Autolinker = require( 'autolinker' );
186186 *
187- * In order to get this to work, we need to change the generated output index.js
188- * line:
189- * exports.default = autolinker_1.default;
190- * to:
191- * exports = autolinker_1.default; // make the Autolinker class the actual export
187+ * In order to get this to work, we need to redefine the `exports` object of
188+ * dist/commonjs/index.js to be the Autolinker class itself. To do this, this
189+ * line is prepended to the file:
190+ *
191+ * exports = module.exports = require('./autolinker').default;
192+ *
193+ * Then TypeScript will happily assign the `.default` and `.Autolinker`
194+ * properties to that new `exports` object.
192195 *
193196 * This function essentially changes the generated index.js from its original
194197 * content of:
195198 *
196199 * "use strict";
197- * function __export(m) {
198- * for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
199- * }
200200 * Object.defineProperty(exports, "__esModule", { value: true });
201- * var autolinker_1 = require("./autolinker");
202- * exports.default = autolinker_1.default; // <-- target of change
203- * var autolinker_2 = require("./autolinker");
204- * exports.Autolinker = autolinker_2.default;
205- * __export(require("./anchor-tag-builder"));
206- * __export(require("./html-tag"));
207- * __export(require("./match/index"));
208- * __export(require("./matcher/index"));
201+ * exports.Autolinker = void 0;
202+ * var tslib_1 = require("tslib");
203+ * var autolinker_1 = tslib_1.__importDefault(require("./autolinker"));
204+ * exports.Autolinker = autolinker_1.default;
205+ * exports.default = autolinker_1.default;
206+ * tslib_1.__exportStar(require("./autolinker"), exports);
207+ * tslib_1.__exportStar(require("./anchor-tag-builder"), exports);
208+ * tslib_1.__exportStar(require("./html-tag"), exports);
209+ * tslib_1.__exportStar(require("./match/index"), exports);
210+ * tslib_1.__exportStar(require("./matcher/index"), exports);
209211 *
210212 * to this:
211213 *
212214 * "use strict";
213- * function __export(m) {
214- * for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
215- * }
216- * Object.defineProperty(exports, "__esModule", { value: true });
217- * var autolinker_1 = require("./autolinker");
218215 *
219- * // Note: the following two lines are added by gulpfile.js's buildSrcFixCommonJsIndexTask() to allow require('autolinker') to work correctly
220- * exports = module.exports = autolinker_1.default; // redefine 'exports' object as the Autolinker class itself
221- * Object.defineProperty( exports, "__esModule", { value: true } ); // redeclare '__esModule' on new 'exports' object
216+ * // Note: the following line is added by gulpfile.js's buildSrcFixCommonJsIndexTask() to allow require('autolinker') to work correctly
217+ * exports = module.exports = require('./autolinker').default; // redefine 'exports' object as the Autolinker class itself
222218 *
223- * exports.default = autolinker_1.default; // continue to allow 'default' property import for ES6 default import
224- * var autolinker_2 = require("./autolinker");
225- * exports.Autolinker = autolinker_2.default;
226- * __export(require("./anchor-tag-builder"));
227- * __export(require("./html-tag"));
228- * __export(require("./match/index"));
229- * __export(require("./matcher/index"));
219+ * Object.defineProperty(exports, "__esModule", { value: true });
220+ * exports.Autolinker = void 0;
221+ * var tslib_1 = require("tslib");
222+ * var autolinker_1 = tslib_1.__importDefault(require("./autolinker"));
223+ * exports.Autolinker = autolinker_1.default;
224+ * exports.default = autolinker_1.default;
225+ * tslib_1.__exportStar(require("./autolinker"), exports);
226+ * tslib_1.__exportStar(require("./anchor-tag-builder"), exports);
227+ * tslib_1.__exportStar(require("./html-tag"), exports);
228+ * tslib_1.__exportStar(require("./match/index"), exports);
229+ * tslib_1.__exportStar(require("./matcher/index"), exports);
230230 */
231231async function buildSrcFixCommonJsIndexTask ( ) {
232232 const indexJsContents = fs . readFileSync ( './dist/commonjs/index.js' , 'utf-8' )
233- . replace ( 'exports.default =' , `
234- // Note: the following two lines are added by gulpfile.js's buildSrcFixCommonJsIndexTask() to allow require('autolinker') to work correctly
235- exports = module.exports = autolinker_1.default; // redefine 'exports' object as the Autolinker class itself
236- Object.defineProperty( exports, "__esModule", { value: true } ); // redeclare '__esModule' on new 'exports' object
237-
238- exports.default =
233+ . replace ( '"use strict";' , `
234+ "use strict";
235+ // Note: the following line is added by gulpfile.js's buildSrcFixCommonJsIndexTask() to allow require('autolinker') to work correctly
236+ exports = module.exports = require('./autolinker').default; // redefine 'exports' object as the Autolinker class itself
239237 ` . trimRight ( ) . replace ( / ^ \t { 3 } / gm, '' ) ) ;
240-
238+
241239 fs . writeFileSync ( './dist/commonjs/index.js' , indexJsContents ) ;
242240}
243241
0 commit comments