Skip to content

Commit

Permalink
Refactor(typescript): Introduce support for Typescript language #CCM-40
Browse files Browse the repository at this point in the history
  • Loading branch information
literat authored and OndraM committed Dec 15, 2021
1 parent 34fc685 commit 3b34deb
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 26 deletions.
12 changes: 11 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,21 @@ module.exports = {
sourceType: 'module',
},

parser: '@typescript-eslint/parser',

ignorePatterns: ['node_modules', '!.*.js'],

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

plugins: ['prettier', 'jest'],
plugins: ['prettier', 'jest', '@typescript-eslint'],

"settings": {
"import/resolver": {
"node": {
"extensions": [".js", ".ts"]
}
}
},

rules: {
'no-use-before-define': ['error', 'nofunc'],
Expand Down
2 changes: 1 addition & 1 deletion config/jest.config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"rootDir": "../",
"transform": {
"\\.js?$": "babel-jest"
"\\.[j|t]s?$": "ts-jest"
}
}
16 changes: 12 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"./LmcCookieConsentManager.cjs": "./LmcCookieConsentManager.cjs",
"./LmcCookieConsentManager.mjs": "./LmcCookieConsentManager.mjs"
},
"types": "./LmcCookieConsentManager.d.ts",
"repository": {
"type": "git",
"url": "https://github.com/lmc-eu/cookie-consent-manager.git"
Expand All @@ -36,10 +37,11 @@
"css:minify": "cleancss --format breaksWith=lf --source-map-inline-sources --batch --batch-suffix \".min\" \"dist/*.css\" \"!dist/*.min.css\"",
"css:lint": "stylelint --config .stylelintrc \"src/**/*.scss\" \"examples/assets/*.css\" --cache --cache-location .cache/.stylelintcache",
"prejs": "yarn js:lint",
"js": "yarn js:compile",
"js": "npm-run-all --serial js:compile js:compile:types",
"js:lint": "eslint ./",
"js:lint:fix": "yarn js:lint --fix",
"js:compile": "node scripts/build.js",
"js:compile:types": "tsc -p ./tsconfig.build.json --outDir ./dist",
"lint": "npm-run-all --serial js:lint css:lint lint:commit",
"lint:commit": "yarn commitlint --from $(git describe --always --first-parent) --verbose",
"lint:commit:last": "yarn commitlint --from HEAD~1 --to HEAD --verbose",
Expand All @@ -51,7 +53,7 @@
"format": "yarn format:check",
"format:check": "prettier --check 'src/**/*.{js,jsx,ts,tsx,scss}' 'scripts/*'",
"format:fix": "prettier --write 'src/**/*.{js,jsx,ts,tsx,scss}' 'scripts/*'",
"test": "npm-run-all --serial lint test:unit format",
"test": "npm-run-all --serial lint test:unit format types",
"test:unit": "jest --config ./config/jest.config.json",
"test:unit:watch": "yarn test:unit --watchAll",
"prepare": "husky install",
Expand All @@ -62,7 +64,8 @@
"version": "yarn changelog && yarn replace-version && git status && git add CHANGELOG.md README.md package.json ./dist && git status",
"postversion": "echo 'Check and push: `git push --set-upstream origin main && git push --tags`'",
"release": "yarn version --`./bin/ci/semver.sh`",
"replace-version": "node scripts/readme-replace-version.js"
"replace-version": "node scripts/readme-replace-version.js",
"types": "tsc -p ./tsconfig.json"
},
"dependencies": {
"@lmc-eu/spirit-design-tokens": "^0.4.5",
Expand All @@ -78,6 +81,9 @@
"@lmc-eu/eslint-config-base": "1.0.0",
"@lmc-eu/prettier-config": "1.2.1",
"@lmc-eu/stylelint-config": "2.0.2",
"@types/jest": "^27.0.3",
"@typescript-eslint/eslint-plugin": "^5.4.0",
"@typescript-eslint/parser": "^5.4.0",
"autoprefixer": "10.4.0",
"babel-jest": "27.4.4",
"clean-css-cli": "5.5.0",
Expand All @@ -96,6 +102,8 @@
"replace-in-file": "6.3.2",
"sass": "1.45.0",
"stylelint": "13.13.1",
"stylelint-order": "5.0.0"
"stylelint-order": "5.0.0",
"ts-jest": "^27.0.7",
"typescript": "^4.5.2"
}
}
9 changes: 6 additions & 3 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,32 @@ const fs = require('fs');

// iife
build({
entryPoints: ['src/init.js', 'src/LmcCookieConsentManager.js'],
entryPoints: ['src/init.ts', 'src/LmcCookieConsentManager.ts'],
bundle: true,
target: 'es2017',
outdir: 'dist',
tsconfig: 'tsconfig.build.json',
}).catch((error) => {
console.error(error);
process.exit(1);
});

// esm
build({
entryPoints: ['src/LmcCookieConsentManager.js'],
entryPoints: ['src/LmcCookieConsentManager.ts'],
bundle: true,
target: 'es2017',
outfile: 'dist/LmcCookieConsentManager.mjs',
format: 'esm',
tsconfig: 'tsconfig.build.json',
}).catch((error) => {
console.error(error);
process.exit(1);
});

// cjs
build({
entryPoints: ['src/LmcCookieConsentManager.js'],
entryPoints: ['src/LmcCookieConsentManager.ts'],
bundle: true,
target: 'es6',
outfile: 'dist/LmcCookieConsentManager.cjs',
Expand All @@ -38,6 +40,7 @@ build({
* because vanilla-cookie-consent is set as `main` in package.json
*/
mainFields: ['main'],
tsconfig: 'tsconfig.build.json',
}).then(() => {
// Annotate the CommonJS export names for ESM import in node
fs.appendFile(
Expand Down
3 changes: 2 additions & 1 deletion scripts/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ serve(
servedir: './',
},
{
entryPoints: ['src/init.js', 'src/LmcCookieConsentManager.js'],
entryPoints: ['src/init.ts', 'src/LmcCookieConsentManager.ts'],
bundle: true,
target: 'es2017',
outdir: 'dist',
tsconfig: 'tsconfig.build.json',
},
)
.then((result) => console.log(result))
Expand Down
11 changes: 11 additions & 0 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"noEmit": false,
"declaration": true,
"emitDeclarationOnly": true
},
"exclude": [
"./src/**/__tests__/**"
]
}
24 changes: 24 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"compilerOptions": {
"target": "es6",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
},
"include": [
"src"
],
}
Loading

0 comments on commit 3b34deb

Please sign in to comment.