From 1523a4ff4579e170355c7607f445689b43229caf Mon Sep 17 00:00:00 2001 From: Alexander Farkas Date: Mon, 17 May 2021 11:26:41 +0200 Subject: [PATCH] [typescript]: Add workaround to include better typescript support. --- Gruntfile.js | 8 ++++++++ lazysizes-umd.js | 3 +-- lazysizes.d.ts | 5 ++--- lazysizes.js | 3 +-- package.json | 3 ++- src/lazysizes-core.js | 3 +-- types/global.d.ts | 5 +++-- 7 files changed, 18 insertions(+), 12 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 9f5d0a85..f81289a3 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -101,6 +101,14 @@ grunt.file.write('lazysizes-umd.js', umd.replace('{{ls}}', ls)); }); + grunt.registerTask('importTs', 'import global typescript.', function() { + const fileName = './lazysizes.d.ts'; + const importStr = `import './types/global';\n\n`; + const tsContent = grunt.file.read(fileName); + + grunt.file.write(fileName, importStr + tsContent); + }); + // Default task. grunt.registerTask("default", [ "wrapcore", "test", "uglify", "bytesize", "maxFilesize" ]); diff --git a/lazysizes-umd.js b/lazysizes-umd.js index 3c3b795d..62a3fbed 100644 --- a/lazysizes-umd.js +++ b/lazysizes-umd.js @@ -9,8 +9,7 @@ } }(window, /** - * import("./types/global") - * @typedef { import("./types/lazysizes-config").LazySizesConfigPartial } LazySizesConfigPartial + * @typedef { import("./types/global").LazySizesConfigPartial } LazySizesConfigPartial */ function l(window, document, Date) { // Pass in the window Date function also for SSR because the Date class can be lost 'use strict'; diff --git a/lazysizes.d.ts b/lazysizes.d.ts index 0102473a..e118b2ad 100644 --- a/lazysizes.d.ts +++ b/lazysizes.d.ts @@ -1,3 +1,5 @@ +import './types/global'; + export = lazySizes; declare var lazySizes: { init: () => void; @@ -53,9 +55,6 @@ declare var lazySizes: { declare namespace lazySizes { export { LazySizesConfigPartial }; } -/** - * import("./types/global") - */ type LazySizesConfigPartial = { [x: string]: any; lazyClass?: string; diff --git a/lazysizes.js b/lazysizes.js index 23a3f75e..467b5d34 100644 --- a/lazysizes.js +++ b/lazysizes.js @@ -7,8 +7,7 @@ }(typeof window != 'undefined' ? window : {}, /** - * import("./types/global") - * @typedef { import("./types/lazysizes-config").LazySizesConfigPartial } LazySizesConfigPartial + * @typedef { import("./types/global").LazySizesConfigPartial } LazySizesConfigPartial */ function l(window, document, Date) { // Pass in the window Date function also for SSR because the Date class can be lost 'use strict'; diff --git a/package.json b/package.json index fec5cb25..14e5c3fa 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,8 @@ "license": "MIT", "author": "Alexander Farkas ", "scripts": { - "prepublishOnly": "grunt && tsc" + "build": "grunt && tsc && grunt importTs", + "prepublishOnly": "npm run build" }, "repository": { "type": "git", diff --git a/src/lazysizes-core.js b/src/lazysizes-core.js index 76528d1c..685b9d03 100644 --- a/src/lazysizes-core.js +++ b/src/lazysizes-core.js @@ -1,7 +1,6 @@ /** - * import("./types/global") - * @typedef { import("./types/lazysizes-config").LazySizesConfigPartial } LazySizesConfigPartial + * @typedef { import("./types/global").LazySizesConfigPartial } LazySizesConfigPartial */ function l(window, document, Date) { // Pass in the window Date function also for SSR because the Date class can be lost 'use strict'; diff --git a/types/global.d.ts b/types/global.d.ts index a169c783..6ade9347 100644 --- a/types/global.d.ts +++ b/types/global.d.ts @@ -1,5 +1,6 @@ -// import type * as lazySizes from '../lazysizes'; -import { LazySizesConfigPartial } from './lazysizes-config'; +import { LazySizesConfigPartial, LazySizesConfig } from './lazysizes-config'; + +export { LazySizesConfigPartial, LazySizesConfig }; export interface LazyUnveilReadEvent extends CustomEvent { target: Element;