Skip to content

Commit 3b34deb

Browse files
literatOndraM
authored andcommitted
Refactor(typescript): Introduce support for Typescript language #CCM-40
1 parent 34fc685 commit 3b34deb

File tree

8 files changed

+178
-26
lines changed

8 files changed

+178
-26
lines changed

.eslintrc.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,21 @@ module.exports = {
1010
sourceType: 'module',
1111
},
1212

13+
parser: '@typescript-eslint/parser',
14+
1315
ignorePatterns: ['node_modules', '!.*.js'],
1416

1517
extends: ['@lmc-eu/eslint-config-base', 'prettier'],
1618

17-
plugins: ['prettier', 'jest'],
19+
plugins: ['prettier', 'jest', '@typescript-eslint'],
20+
21+
"settings": {
22+
"import/resolver": {
23+
"node": {
24+
"extensions": [".js", ".ts"]
25+
}
26+
}
27+
},
1828

1929
rules: {
2030
'no-use-before-define': ['error', 'nofunc'],

config/jest.config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"rootDir": "../",
33
"transform": {
4-
"\\.js?$": "babel-jest"
4+
"\\.[j|t]s?$": "ts-jest"
55
}
66
}

package.json

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"./LmcCookieConsentManager.cjs": "./LmcCookieConsentManager.cjs",
2020
"./LmcCookieConsentManager.mjs": "./LmcCookieConsentManager.mjs"
2121
},
22+
"types": "./LmcCookieConsentManager.d.ts",
2223
"repository": {
2324
"type": "git",
2425
"url": "https://github.com/lmc-eu/cookie-consent-manager.git"
@@ -36,10 +37,11 @@
3637
"css:minify": "cleancss --format breaksWith=lf --source-map-inline-sources --batch --batch-suffix \".min\" \"dist/*.css\" \"!dist/*.min.css\"",
3738
"css:lint": "stylelint --config .stylelintrc \"src/**/*.scss\" \"examples/assets/*.css\" --cache --cache-location .cache/.stylelintcache",
3839
"prejs": "yarn js:lint",
39-
"js": "yarn js:compile",
40+
"js": "npm-run-all --serial js:compile js:compile:types",
4041
"js:lint": "eslint ./",
4142
"js:lint:fix": "yarn js:lint --fix",
4243
"js:compile": "node scripts/build.js",
44+
"js:compile:types": "tsc -p ./tsconfig.build.json --outDir ./dist",
4345
"lint": "npm-run-all --serial js:lint css:lint lint:commit",
4446
"lint:commit": "yarn commitlint --from $(git describe --always --first-parent) --verbose",
4547
"lint:commit:last": "yarn commitlint --from HEAD~1 --to HEAD --verbose",
@@ -51,7 +53,7 @@
5153
"format": "yarn format:check",
5254
"format:check": "prettier --check 'src/**/*.{js,jsx,ts,tsx,scss}' 'scripts/*'",
5355
"format:fix": "prettier --write 'src/**/*.{js,jsx,ts,tsx,scss}' 'scripts/*'",
54-
"test": "npm-run-all --serial lint test:unit format",
56+
"test": "npm-run-all --serial lint test:unit format types",
5557
"test:unit": "jest --config ./config/jest.config.json",
5658
"test:unit:watch": "yarn test:unit --watchAll",
5759
"prepare": "husky install",
@@ -62,7 +64,8 @@
6264
"version": "yarn changelog && yarn replace-version && git status && git add CHANGELOG.md README.md package.json ./dist && git status",
6365
"postversion": "echo 'Check and push: `git push --set-upstream origin main && git push --tags`'",
6466
"release": "yarn version --`./bin/ci/semver.sh`",
65-
"replace-version": "node scripts/readme-replace-version.js"
67+
"replace-version": "node scripts/readme-replace-version.js",
68+
"types": "tsc -p ./tsconfig.json"
6669
},
6770
"dependencies": {
6871
"@lmc-eu/spirit-design-tokens": "^0.4.5",
@@ -78,6 +81,9 @@
7881
"@lmc-eu/eslint-config-base": "1.0.0",
7982
"@lmc-eu/prettier-config": "1.2.1",
8083
"@lmc-eu/stylelint-config": "2.0.2",
84+
"@types/jest": "^27.0.3",
85+
"@typescript-eslint/eslint-plugin": "^5.4.0",
86+
"@typescript-eslint/parser": "^5.4.0",
8187
"autoprefixer": "10.4.0",
8288
"babel-jest": "27.4.4",
8389
"clean-css-cli": "5.5.0",
@@ -96,6 +102,8 @@
96102
"replace-in-file": "6.3.2",
97103
"sass": "1.45.0",
98104
"stylelint": "13.13.1",
99-
"stylelint-order": "5.0.0"
105+
"stylelint-order": "5.0.0",
106+
"ts-jest": "^27.0.7",
107+
"typescript": "^4.5.2"
100108
}
101109
}

scripts/build.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,32 @@ const fs = require('fs');
44

55
// iife
66
build({
7-
entryPoints: ['src/init.js', 'src/LmcCookieConsentManager.js'],
7+
entryPoints: ['src/init.ts', 'src/LmcCookieConsentManager.ts'],
88
bundle: true,
99
target: 'es2017',
1010
outdir: 'dist',
11+
tsconfig: 'tsconfig.build.json',
1112
}).catch((error) => {
1213
console.error(error);
1314
process.exit(1);
1415
});
1516

1617
// esm
1718
build({
18-
entryPoints: ['src/LmcCookieConsentManager.js'],
19+
entryPoints: ['src/LmcCookieConsentManager.ts'],
1920
bundle: true,
2021
target: 'es2017',
2122
outfile: 'dist/LmcCookieConsentManager.mjs',
2223
format: 'esm',
24+
tsconfig: 'tsconfig.build.json',
2325
}).catch((error) => {
2426
console.error(error);
2527
process.exit(1);
2628
});
2729

2830
// cjs
2931
build({
30-
entryPoints: ['src/LmcCookieConsentManager.js'],
32+
entryPoints: ['src/LmcCookieConsentManager.ts'],
3133
bundle: true,
3234
target: 'es6',
3335
outfile: 'dist/LmcCookieConsentManager.cjs',
@@ -38,6 +40,7 @@ build({
3840
* because vanilla-cookie-consent is set as `main` in package.json
3941
*/
4042
mainFields: ['main'],
43+
tsconfig: 'tsconfig.build.json',
4144
}).then(() => {
4245
// Annotate the CommonJS export names for ESM import in node
4346
fs.appendFile(

scripts/serve.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ serve(
99
servedir: './',
1010
},
1111
{
12-
entryPoints: ['src/init.js', 'src/LmcCookieConsentManager.js'],
12+
entryPoints: ['src/init.ts', 'src/LmcCookieConsentManager.ts'],
1313
bundle: true,
1414
target: 'es2017',
1515
outdir: 'dist',
16+
tsconfig: 'tsconfig.build.json',
1617
},
1718
)
1819
.then((result) => console.log(result))

tsconfig.build.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"noEmit": false,
5+
"declaration": true,
6+
"emitDeclarationOnly": true
7+
},
8+
"exclude": [
9+
"./src/**/__tests__/**"
10+
]
11+
}

tsconfig.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es6",
4+
"lib": [
5+
"dom",
6+
"dom.iterable",
7+
"esnext"
8+
],
9+
"allowJs": true,
10+
"skipLibCheck": true,
11+
"esModuleInterop": true,
12+
"allowSyntheticDefaultImports": true,
13+
"strict": true,
14+
"forceConsistentCasingInFileNames": true,
15+
"module": "esnext",
16+
"moduleResolution": "node",
17+
"resolveJsonModule": true,
18+
"isolatedModules": true,
19+
"noEmit": true,
20+
},
21+
"include": [
22+
"src"
23+
],
24+
}

0 commit comments

Comments
 (0)