Skip to content

Commit 8dc8768

Browse files
authored
Merge pull request #345 from gregjacobs/upgrade-to-typescript-4
Upgrade to TypeScript 4
2 parents 0acda93 + 4e7c5ec commit 8dc8768

File tree

9 files changed

+56
-84
lines changed

9 files changed

+56
-84
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
language: node_js
22
node_js:
33
- "lts/*" # latest long-term-support version of node.js
4-
- "9.0"
54
- "8.9"

gulpfile.js

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
231231
async 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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
"rollup-plugin-commonjs": "^9.2.0",
7979
"rollup-plugin-node-resolve": "^4.0.0",
8080
"through2": "^2.0.1",
81-
"typescript": "^3.2.2",
81+
"typescript": "^4.2.3",
8282
"webpack": "^4.28.2",
8383
"yarn": "^1.12.3"
8484
},

src/autolinker.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ import { parseHtml } from './htmlParser/parse-html';
121121
* - An {@link Autolinker.HtmlTag} instance, which can be used to build/modify
122122
* an HTML tag before writing out its HTML text.
123123
*/
124-
export default class Autolinker {
124+
export default class Autolinker { // NOTE: must be 'export default' here for UMD module
125125

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

988988
}
989989

990-
991990
export interface AutolinkerConfig {
992991
urls?: UrlsConfig;
993992
email?: boolean;

src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
// This is done by the buildSrcFixCommonJsIndexTask() function in the gulpfile.
44
// See that function for more details.
55

6-
export { default } from './autolinker';
7-
export { default as Autolinker } from './autolinker';
6+
import Autolinker from './autolinker';
7+
8+
export default Autolinker;
9+
export { Autolinker }
810

911
export * from './autolinker';
1012
export * from './anchor-tag-builder';

src/utils.ts

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

tests-integration/test-require.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
const Autolinker = require( 'autolinker' );
88
const NamedAutolinker = require( 'autolinker' ).Autolinker;
99

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

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

7069

7170
it( `The 'Match' classes should also continue to be in their 1.x namespace locations for backward compatibility`, () => {
71+
expect( Autolinker.match ).toEqual( jasmine.any(Object) );
72+
7273
expect( Autolinker.match.Match ).toEqual( jasmine.any( Function ) ); // constructor function
7374
expect( Autolinker.match.Match.name ).toBe( 'Match' ); // function name
7475
expect( Autolinker.match.Match.prototype.getMatchedText ).toEqual( jasmine.any( Function ) );

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/* Basic Options */
44
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
55
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
6-
"lib": [ "es2015", "dom" ], /* Specify library files to be included in the compilation. */
6+
"lib": [ "ES5", "dom" ], /* Specify library files to be included in the compilation. */
77
// "allowJs": true, /* Allow javascript files to be compiled. */
88
// "checkJs": true, /* Report errors in .js files. */
99
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */

yarn.lock

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,7 +1575,7 @@ debug@^4.1.0:
15751575
dependencies:
15761576
ms "^2.1.1"
15771577

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

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

3912-
lodash._baseindexof@*:
3913-
version "3.1.0"
3914-
resolved "https://registry.yarnpkg.com/lodash._baseindexof/-/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c"
3915-
integrity sha1-/lK1OhxnYeQmGNZU5KJXie1hgiw=
3916-
39173912
lodash._basetostring@^3.0.0:
39183913
version "3.0.1"
39193914
resolved "https://registry.yarnpkg.com/lodash._basetostring/-/lodash._basetostring-3.0.1.tgz#d1861d877f824a52f669832dcaf3ee15566a07d5"
@@ -3932,29 +3927,12 @@ lodash._basevalues@^3.0.0:
39323927
resolved "https://registry.yarnpkg.com/lodash._basevalues/-/lodash._basevalues-3.0.0.tgz#5b775762802bde3d3297503e26300820fdf661b7"
39333928
integrity sha1-W3dXYoAr3j0yl1A+JjAIIP32Ybc=
39343929

3935-
lodash._bindcallback@*:
3936-
version "3.0.1"
3937-
resolved "https://registry.yarnpkg.com/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e"
3938-
integrity sha1-5THCdkTPi1epnhftlbNcdIeJOS4=
3939-
3940-
lodash._cacheindexof@*:
3941-
version "3.0.2"
3942-
resolved "https://registry.yarnpkg.com/lodash._cacheindexof/-/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92"
3943-
integrity sha1-PcaayCSY0u5ePOVgkbr9Ktx73pI=
3944-
3945-
lodash._createcache@*:
3946-
version "3.1.2"
3947-
resolved "https://registry.yarnpkg.com/lodash._createcache/-/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093"
3948-
integrity sha1-VtagZAF2JeeevKa4AY4XRAvc8JM=
3949-
dependencies:
3950-
lodash._getnative "^3.0.0"
3951-
39523930
lodash._createset@~4.0.0:
39533931
version "4.0.3"
39543932
resolved "https://registry.yarnpkg.com/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26"
39553933
integrity sha1-D0ZZ+7CddRlPqeK4imZE02PJ/iY=
39563934

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

4018-
lodash.restparam@*, lodash.restparam@^3.0.0:
3996+
lodash.restparam@^3.0.0:
40193997
version "3.6.1"
40203998
resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
40213999
integrity sha1-k2pOMJ7zMKdkXtQUWYbIWuWyCAU=
@@ -4732,7 +4710,6 @@ npm@^6.14.5:
47324710
cmd-shim "^3.0.3"
47334711
columnify "~1.5.4"
47344712
config-chain "^1.1.12"
4735-
debuglog "*"
47364713
detect-indent "~5.0.0"
47374714
detect-newline "^2.1.0"
47384715
dezalgo "~1.0.3"
@@ -4747,7 +4724,6 @@ npm@^6.14.5:
47474724
has-unicode "~2.0.1"
47484725
hosted-git-info "^2.8.8"
47494726
iferr "^1.0.2"
4750-
imurmurhash "*"
47514727
infer-owner "^1.0.4"
47524728
inflight "~1.0.6"
47534729
inherits "^2.0.4"
@@ -4766,14 +4742,8 @@ npm@^6.14.5:
47664742
libnpx "^10.2.2"
47674743
lock-verify "^2.1.0"
47684744
lockfile "^1.0.4"
4769-
lodash._baseindexof "*"
47704745
lodash._baseuniq "~4.6.0"
4771-
lodash._bindcallback "*"
4772-
lodash._cacheindexof "*"
4773-
lodash._createcache "*"
4774-
lodash._getnative "*"
47754746
lodash.clonedeep "~4.5.0"
4776-
lodash.restparam "*"
47774747
lodash.union "~4.6.0"
47784748
lodash.uniq "~4.5.0"
47794749
lodash.without "~4.4.0"
@@ -6793,10 +6763,10 @@ typedarray@^0.0.6:
67936763
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
67946764
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=
67956765

6796-
typescript@^3.2.2:
6797-
version "3.2.2"
6798-
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.2.2.tgz#fe8101c46aa123f8353523ebdcf5730c2ae493e5"
6799-
integrity sha512-VCj5UiSyHBjwfYacmDuc/NOk4QQixbE+Wn7MFJuS0nRuPQbof132Pw4u53dm264O8LPc2MVsc7RJNml5szurkg==
6766+
typescript@^4.2.3:
6767+
version "4.2.3"
6768+
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.2.3.tgz#39062d8019912d43726298f09493d598048c1ce3"
6769+
integrity sha512-qOcYwxaByStAWrBf4x0fibwZvMRG+r4cQoTjbPtUlrWjBHbmCAww1i448U0GJ+3cNNEtebDteo/cHOR3xJ4wEw==
68006770

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

0 commit comments

Comments
 (0)