Skip to content

Commit

Permalink
Merge pull request #345 from gregjacobs/upgrade-to-typescript-4
Browse files Browse the repository at this point in the history
Upgrade to TypeScript 4
  • Loading branch information
gregjacobs authored Mar 14, 2021
2 parents 0acda93 + 4e7c5ec commit 8dc8768
Showing 9 changed files with 56 additions and 84 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
language: node_js
node_js:
- "lts/*" # latest long-term-support version of node.js
- "9.0"
- "8.9"
74 changes: 36 additions & 38 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -184,60 +184,58 @@ function buildSrcTypeScript( tsProject, outputDir ) {
*
* const Autolinker = require( 'autolinker' );
*
* In order to get this to work, we need to change the generated output index.js
* line:
* exports.default = autolinker_1.default;
* to:
* exports = autolinker_1.default; // make the Autolinker class the actual export
* In order to get this to work, we need to redefine the `exports` object of
* dist/commonjs/index.js to be the Autolinker class itself. To do this, this
* line is prepended to the file:
*
* exports = module.exports = require('./autolinker').default;
*
* Then TypeScript will happily assign the `.default` and `.Autolinker`
* properties to that new `exports` object.
*
* This function essentially changes the generated index.js from its original
* content of:
*
* "use strict";
* function __export(m) {
* for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
* }
* Object.defineProperty(exports, "__esModule", { value: true });
* var autolinker_1 = require("./autolinker");
* exports.default = autolinker_1.default; // <-- target of change
* var autolinker_2 = require("./autolinker");
* exports.Autolinker = autolinker_2.default;
* __export(require("./anchor-tag-builder"));
* __export(require("./html-tag"));
* __export(require("./match/index"));
* __export(require("./matcher/index"));
* exports.Autolinker = void 0;
* var tslib_1 = require("tslib");
* var autolinker_1 = tslib_1.__importDefault(require("./autolinker"));
* exports.Autolinker = autolinker_1.default;
* exports.default = autolinker_1.default;
* tslib_1.__exportStar(require("./autolinker"), exports);
* tslib_1.__exportStar(require("./anchor-tag-builder"), exports);
* tslib_1.__exportStar(require("./html-tag"), exports);
* tslib_1.__exportStar(require("./match/index"), exports);
* tslib_1.__exportStar(require("./matcher/index"), exports);
*
* to this:
*
* "use strict";
* function __export(m) {
* for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
* }
* Object.defineProperty(exports, "__esModule", { value: true });
* var autolinker_1 = require("./autolinker");
*
* // Note: the following two lines are added by gulpfile.js's buildSrcFixCommonJsIndexTask() to allow require('autolinker') to work correctly
* exports = module.exports = autolinker_1.default; // redefine 'exports' object as the Autolinker class itself
* Object.defineProperty( exports, "__esModule", { value: true } ); // redeclare '__esModule' on new 'exports' object
* // Note: the following line is added by gulpfile.js's buildSrcFixCommonJsIndexTask() to allow require('autolinker') to work correctly
* exports = module.exports = require('./autolinker').default; // redefine 'exports' object as the Autolinker class itself
*
* exports.default = autolinker_1.default; // continue to allow 'default' property import for ES6 default import
* var autolinker_2 = require("./autolinker");
* exports.Autolinker = autolinker_2.default;
* __export(require("./anchor-tag-builder"));
* __export(require("./html-tag"));
* __export(require("./match/index"));
* __export(require("./matcher/index"));
* Object.defineProperty(exports, "__esModule", { value: true });
* exports.Autolinker = void 0;
* var tslib_1 = require("tslib");
* var autolinker_1 = tslib_1.__importDefault(require("./autolinker"));
* exports.Autolinker = autolinker_1.default;
* exports.default = autolinker_1.default;
* tslib_1.__exportStar(require("./autolinker"), exports);
* tslib_1.__exportStar(require("./anchor-tag-builder"), exports);
* tslib_1.__exportStar(require("./html-tag"), exports);
* tslib_1.__exportStar(require("./match/index"), exports);
* tslib_1.__exportStar(require("./matcher/index"), exports);
*/
async function buildSrcFixCommonJsIndexTask() {
const indexJsContents = fs.readFileSync( './dist/commonjs/index.js', 'utf-8' )
.replace( 'exports.default =', `
// Note: the following two lines are added by gulpfile.js's buildSrcFixCommonJsIndexTask() to allow require('autolinker') to work correctly
exports = module.exports = autolinker_1.default; // redefine 'exports' object as the Autolinker class itself
Object.defineProperty( exports, "__esModule", { value: true } ); // redeclare '__esModule' on new 'exports' object
exports.default =
.replace( '"use strict";', `
"use strict";
// Note: the following line is added by gulpfile.js's buildSrcFixCommonJsIndexTask() to allow require('autolinker') to work correctly
exports = module.exports = require('./autolinker').default; // redefine 'exports' object as the Autolinker class itself
`.trimRight().replace( /^\t{3}/gm, '' ) );

fs.writeFileSync( './dist/commonjs/index.js', indexJsContents );
}

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -78,7 +78,7 @@
"rollup-plugin-commonjs": "^9.2.0",
"rollup-plugin-node-resolve": "^4.0.0",
"through2": "^2.0.1",
"typescript": "^3.2.2",
"typescript": "^4.2.3",
"webpack": "^4.28.2",
"yarn": "^1.12.3"
},
3 changes: 1 addition & 2 deletions src/autolinker.ts
Original file line number Diff line number Diff line change
@@ -121,7 +121,7 @@ import { parseHtml } from './htmlParser/parse-html';
* - An {@link Autolinker.HtmlTag} instance, which can be used to build/modify
* an HTML tag before writing out its HTML text.
*/
export default class Autolinker {
export default class Autolinker { // NOTE: must be 'export default' here for UMD module

/**
* @static
@@ -987,7 +987,6 @@ export default class Autolinker {

}


export interface AutolinkerConfig {
urls?: UrlsConfig;
email?: boolean;
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -3,8 +3,10 @@
// This is done by the buildSrcFixCommonJsIndexTask() function in the gulpfile.
// See that function for more details.

export { default } from './autolinker';
export { default as Autolinker } from './autolinker';
import Autolinker from './autolinker';

export default Autolinker;
export { Autolinker }

export * from './autolinker';
export * from './anchor-tag-builder';
3 changes: 3 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -52,6 +52,9 @@ export function ellipsis( str: string, truncateLen: number, ellipsisChars?: stri
* @return {Number} The index of the `element`, or -1 if it was not found.
*/
export function indexOf<T>( arr: T[], element: T ) {
// @ts-ignore - As far as TypeScript is concerned, this method will always
// exist (lowest "lib" in TS is "ES5"). Hence we need to ignore this error
// to support IE8 which only implements ES3 and doesn't have this method
if( Array.prototype.indexOf ) {
return arr.indexOf( element );

3 changes: 2 additions & 1 deletion tests-integration/test-require.spec.ts
Original file line number Diff line number Diff line change
@@ -7,7 +7,6 @@
const Autolinker = require( 'autolinker' );
const NamedAutolinker = require( 'autolinker' ).Autolinker;


describe( 'Autolinker require() tests - ', () => {

it( `Autolinker should be the default export of 'autolinker'`, () => {
@@ -69,6 +68,8 @@ describe( 'Autolinker require() tests - ', () => {


it( `The 'Match' classes should also continue to be in their 1.x namespace locations for backward compatibility`, () => {
expect( Autolinker.match ).toEqual( jasmine.any(Object) );

expect( Autolinker.match.Match ).toEqual( jasmine.any( Function ) ); // constructor function
expect( Autolinker.match.Match.name ).toBe( 'Match' ); // function name
expect( Autolinker.match.Match.prototype.getMatchedText ).toEqual( jasmine.any( Function ) );
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
/* Basic Options */
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"lib": [ "es2015", "dom" ], /* Specify library files to be included in the compilation. */
"lib": [ "ES5", "dom" ], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
46 changes: 8 additions & 38 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -1575,7 +1575,7 @@ debug@^4.1.0:
dependencies:
ms "^2.1.1"

debuglog@*, debuglog@^1.0.1:
debuglog@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"
integrity sha1-qiT/uaw9+aI1GDfPstJ5NgzXhJI=
@@ -3094,7 +3094,7 @@ import-lazy@^2.1.0:
resolved "https://registry.yarnpkg.com/import-lazy/-/import-lazy-2.1.0.tgz#05698e3d45c88e8d7e9d92cb0584e77f096f3e43"
integrity sha1-BWmOPUXIjo1+nZLLBYTnfwlvPkM=

imurmurhash@*, imurmurhash@^0.1.4:
imurmurhash@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
integrity sha1-khi5srkoojixPcT7a21XbyMUU+o=
@@ -3909,11 +3909,6 @@ lodash._basecopy@^3.0.0:
resolved "https://registry.yarnpkg.com/lodash._basecopy/-/lodash._basecopy-3.0.1.tgz#8da0e6a876cf344c0ad8a54882111dd3c5c7ca36"
integrity sha1-jaDmqHbPNEwK2KVIghEd08XHyjY=

lodash._baseindexof@*:
version "3.1.0"
resolved "https://registry.yarnpkg.com/lodash._baseindexof/-/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c"
integrity sha1-/lK1OhxnYeQmGNZU5KJXie1hgiw=

lodash._basetostring@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/lodash._basetostring/-/lodash._basetostring-3.0.1.tgz#d1861d877f824a52f669832dcaf3ee15566a07d5"
@@ -3932,29 +3927,12 @@ lodash._basevalues@^3.0.0:
resolved "https://registry.yarnpkg.com/lodash._basevalues/-/lodash._basevalues-3.0.0.tgz#5b775762802bde3d3297503e26300820fdf661b7"
integrity sha1-W3dXYoAr3j0yl1A+JjAIIP32Ybc=

lodash._bindcallback@*:
version "3.0.1"
resolved "https://registry.yarnpkg.com/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e"
integrity sha1-5THCdkTPi1epnhftlbNcdIeJOS4=

lodash._cacheindexof@*:
version "3.0.2"
resolved "https://registry.yarnpkg.com/lodash._cacheindexof/-/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92"
integrity sha1-PcaayCSY0u5ePOVgkbr9Ktx73pI=

lodash._createcache@*:
version "3.1.2"
resolved "https://registry.yarnpkg.com/lodash._createcache/-/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093"
integrity sha1-VtagZAF2JeeevKa4AY4XRAvc8JM=
dependencies:
lodash._getnative "^3.0.0"

lodash._createset@~4.0.0:
version "4.0.3"
resolved "https://registry.yarnpkg.com/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26"
integrity sha1-D0ZZ+7CddRlPqeK4imZE02PJ/iY=

lodash._getnative@*, lodash._getnative@^3.0.0:
lodash._getnative@^3.0.0:
version "3.9.1"
resolved "https://registry.yarnpkg.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5"
integrity sha1-VwvH3t5G1hzc3mh9ZdPuy6o6r/U=
@@ -4015,7 +3993,7 @@ lodash.keys@^3.0.0:
lodash.isarguments "^3.0.0"
lodash.isarray "^3.0.0"

lodash.restparam@*, lodash.restparam@^3.0.0:
lodash.restparam@^3.0.0:
version "3.6.1"
resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
integrity sha1-k2pOMJ7zMKdkXtQUWYbIWuWyCAU=
@@ -4732,7 +4710,6 @@ npm@^6.14.5:
cmd-shim "^3.0.3"
columnify "~1.5.4"
config-chain "^1.1.12"
debuglog "*"
detect-indent "~5.0.0"
detect-newline "^2.1.0"
dezalgo "~1.0.3"
@@ -4747,7 +4724,6 @@ npm@^6.14.5:
has-unicode "~2.0.1"
hosted-git-info "^2.8.8"
iferr "^1.0.2"
imurmurhash "*"
infer-owner "^1.0.4"
inflight "~1.0.6"
inherits "^2.0.4"
@@ -4766,14 +4742,8 @@ npm@^6.14.5:
libnpx "^10.2.2"
lock-verify "^2.1.0"
lockfile "^1.0.4"
lodash._baseindexof "*"
lodash._baseuniq "~4.6.0"
lodash._bindcallback "*"
lodash._cacheindexof "*"
lodash._createcache "*"
lodash._getnative "*"
lodash.clonedeep "~4.5.0"
lodash.restparam "*"
lodash.union "~4.6.0"
lodash.uniq "~4.5.0"
lodash.without "~4.4.0"
@@ -6793,10 +6763,10 @@ typedarray@^0.0.6:
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=

typescript@^3.2.2:
version "3.2.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.2.2.tgz#fe8101c46aa123f8353523ebdcf5730c2ae493e5"
integrity sha512-VCj5UiSyHBjwfYacmDuc/NOk4QQixbE+Wn7MFJuS0nRuPQbof132Pw4u53dm264O8LPc2MVsc7RJNml5szurkg==
typescript@^4.2.3:
version "4.2.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.2.3.tgz#39062d8019912d43726298f09493d598048c1ce3"
integrity sha512-qOcYwxaByStAWrBf4x0fibwZvMRG+r4cQoTjbPtUlrWjBHbmCAww1i448U0GJ+3cNNEtebDteo/cHOR3xJ4wEw==

uglify-js@3.4.x:
version "3.4.10"

0 comments on commit 8dc8768

Please sign in to comment.