From ae76649967998f3556c0c95813b9dd4de84f14f3 Mon Sep 17 00:00:00 2001 From: Sergei Maertens Date: Wed, 1 May 2024 23:28:06 +0200 Subject: [PATCH 01/12] :tada: Add TS boilerplate Added boilerplate to create/bundle an npm package on Github, so that the build artifact(s) can be included in the static files of the django app for ease of use, and other projects can separately install the package for the JS with npm to bundle the JS with their own solution. --- .editorconfig | 5 ++++- .gitignore | 4 ++++ js/.nvmrc | 1 + js/package-lock.json | 29 +++++++++++++++++++++++++++++ js/package.json | 31 +++++++++++++++++++++++++++++++ js/src/index.ts | 1 + js/tsconfig.json | 22 ++++++++++++++++++++++ 7 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 js/.nvmrc create mode 100644 js/package-lock.json create mode 100644 js/package.json create mode 100644 js/src/index.ts create mode 100644 js/tsconfig.json diff --git a/.editorconfig b/.editorconfig index 39fd6e0..f7cf72f 100644 --- a/.editorconfig +++ b/.editorconfig @@ -13,7 +13,10 @@ indent_size = 2 [*.{yml,yaml}] indent_size = 2 -[*.js] +[*.{js,ts}] +indent_size = 2 + +[*.json] indent_size = 2 [Makefile] diff --git a/.gitignore b/.gitignore index 9751be2..4456f0c 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,7 @@ dist/ htmlcov/ reports/ testapp/*.db + +# frontend tooling / builds +js/lib/ +js/node_modules/ diff --git a/js/.nvmrc b/js/.nvmrc new file mode 100644 index 0000000..9a2a0e2 --- /dev/null +++ b/js/.nvmrc @@ -0,0 +1 @@ +v20 diff --git a/js/package-lock.json b/js/package-lock.json new file mode 100644 index 0000000..33ba680 --- /dev/null +++ b/js/package-lock.json @@ -0,0 +1,29 @@ +{ + "name": "django-cookie-consent", + "version": "0.5.0-beta0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "django-cookie-consent", + "version": "0.5.0-beta0", + "license": "BSD-2-Clause", + "devDependencies": { + "typescript": "^5.4.5" + } + }, + "node_modules/typescript": { + "version": "5.4.5", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.4.5.tgz", + "integrity": "sha512-vcI4UpRgg81oIRUFwR0WSIHKt11nJ7SAVlYNIu+QpqeyXP+gpQJy/Z4+F0aGxSE4MqwjyXvW/TzgkLAx2AGHwQ==", + "dev": true, + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=14.17" + } + } + } +} diff --git a/js/package.json b/js/package.json new file mode 100644 index 0000000..40c6b37 --- /dev/null +++ b/js/package.json @@ -0,0 +1,31 @@ +{ + "name": "django-cookie-consent", + "version": "0.5.0-beta0", + "description": "Frontend code for django-cookie-consent", + "main": "lib/index.js", + "type": "module", + "files": ["lib/"], + "scripts": { + "build": "tsc", + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/jazzband/django-cookie-consent.git" + }, + "keywords": [ + "django", + "typescript", + "cookie", + "consent" + ], + "author": "Sergei Maertens", + "license": "BSD-2-Clause", + "bugs": { + "url": "https://github.com/jazzband/django-cookie-consent/issues" + }, + "homepage": "https://github.com/jazzband/django-cookie-consent#readme", + "devDependencies": { + "typescript": "^5.4.5" + } +} diff --git a/js/src/index.ts b/js/src/index.ts new file mode 100644 index 0000000..c0f1d43 --- /dev/null +++ b/js/src/index.ts @@ -0,0 +1 @@ +console.log('ok') diff --git a/js/tsconfig.json b/js/tsconfig.json new file mode 100644 index 0000000..673c580 --- /dev/null +++ b/js/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "baseUrl": "src", + "target": "es2017", + "module": "esnext", + "outDir": "lib", + "declaration": true, + "noErrorTruncation": true, + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + "strict": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "noImplicitReturns": true, + "noImplicitThis": true, + "noImplicitAny": true, + "strictBindCallApply": true, + "strictNullChecks": true, + }, + "include": ["src"], + "exclude": ["node_modules", "lib"], +} From af0d31b216ea804037198c598ccf7483bb499801 Mon Sep 17 00:00:00 2001 From: Sergei Maertens Date: Thu, 2 May 2024 10:38:39 +0200 Subject: [PATCH 02/12] :recycle: Refactor cookiebar.module.js into TS module The added type safety helps catch bugs, and the setup is simple enough for people not too familiar with TS to be able to contribute. Small refactors have been done to avoid globals and localize state a bit more, which shouldn't affect anyone. --- js/src/cookiebar.ts | 325 ++++++++++++++++++++++++++++++++++++++++++++ js/src/index.ts | 2 +- 2 files changed, 326 insertions(+), 1 deletion(-) create mode 100644 js/src/cookiebar.ts diff --git a/js/src/cookiebar.ts b/js/src/cookiebar.ts new file mode 100644 index 0000000..2f99382 --- /dev/null +++ b/js/src/cookiebar.ts @@ -0,0 +1,325 @@ +/** + * Cookiebar functionality, as a TS/JS module. + * + * About modules: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules + * + * The code is organized here in a way to make the templates work with Django's page + * cache. This means that anything user-specific (so different django session and even + * cookie consent cookies) cannot be baked into the templates, as that breaks caches. + * + * The cookie bar operates on the following principles: + * + * - The developer using the library includes the desired template in their django + * templates, using the HTML