From a2ee7219824933236de3fafde666b5aec1ee6c5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Nikoli=C4=87?= Date: Mon, 25 Mar 2024 09:15:49 +0100 Subject: [PATCH] Add support for TypeScript in non-transpiled configurations Closes #201. --- generators/app/index.js | 10 +++++++++- generators/app/templates/_package.json | 9 +++++---- generators/app/templates/rollup.config.js | 2 +- generators/app/templates/tsconfig.build.json | 12 ++++++++++++ test/index.js | 8 ++++---- 5 files changed, 31 insertions(+), 10 deletions(-) create mode 100644 generators/app/templates/tsconfig.build.json diff --git a/generators/app/index.js b/generators/app/index.js index 0818535..158f9aa 100644 --- a/generators/app/index.js +++ b/generators/app/index.js @@ -368,7 +368,7 @@ export default class extends Generator { 'Do you want to create CommonJS bundle (browser-only packages are safe to be built as ESM only)?', default: false, when: (answers) => { - return !isSassModule(answers); + return !isSassModule(answers) && answers.transpile; } }, { @@ -776,8 +776,16 @@ export default class extends Generator { if (answers.typescript) { cp('tsconfig.json', 'tsconfig.json'); + if ( + !answers.browserModule && + !answers.styles && + !answers.bundleCjs + ) { + cp('tsconfig.build.json', 'tsconfig.build.json'); + } } else { rm('tsconfig.json'); + rm('tsconfig.build.json'); } if (answers.cli) { diff --git a/generators/app/templates/_package.json b/generators/app/templates/_package.json index 902eaa2..b46e623 100644 --- a/generators/app/templates/_package.json +++ b/generators/app/templates/_package.json @@ -7,7 +7,7 @@ "module": "<% if ( transpile ) { %>esm/<% } %>index.js", "exports": { ".": {<% if ( typescript ) { %> - "types": "<% if ( transpile ) { %>esm/<% } %>index.d.ts",<% } %><% if ( browserModule && styles || sassModule || cssModule ) { %><% if ( sassModule ) { %> + "types": "<% if ( transpile ) { %>esm/<% } else { %>types/<% } %>index.d.ts",<% } %><% if ( browserModule && styles || sassModule || cssModule ) { %><% if ( sassModule ) { %> "sass": "./_index.scss",<% } %> "style": "./<% if ( sassModule ) { %>_index.scss<% } else if ( cssModule ) { %>index.css<% } else { %><% if ( transpile ) { %>dist/<% } %>index.css<% } %>",<% } %> "import": "./<% if ( transpile ) { %>esm/<% } %>index.js"<% if ( bundleCjs ) { %>, @@ -55,8 +55,8 @@ "test:watch": "npm test -- --watch"<% } %><% } else { %>, "test": "<% if ( automatedTests ) { %><% if ( codeCoverage ) { %>NODE_OPTIONS='--experimental-loader=@istanbuljs/esm-loader-hook --no-warnings' <% if ( transpile ) { %>BABEL_ENV=test <% } %>nyc mocha<% } else { %>mocha<% } %><% if ( transpile ) { %> --require @babel/register<% } %><% if ( !transpile && typescript && typescriptMode === 'full' ) { %> --require ts-node/register --extension ts<% } %> 'test/**/*.<%= extension || 'js' %>'<% if ( codeCoverage ) { %> && nyc check-coverage<% } %><% } else { %><% } %>"<% if ( automatedTests ) { %>, "test:watch": "<% if ( codeCoverage ) { %>nodemon<% if ( typescript && typescriptMode === 'full' ) { %> --ext js,json,ts<% } %> --exec npm test<% } else { %>npm test -- --watch<% } %>"<% } %><% if ( codeCoverageService ) { %>, - "posttest:ci": "cat ./coverage/lcov.info | coveralls"<% } %><% } %><% if ( transpile ) { %>, - "build": "rollup --config rollup.config.js", + "posttest:ci": "cat ./coverage/lcov.info | coveralls"<% } %><% } %><% if ( transpile || typescript ) { %>, + "build": "<% if ( !browserModule && !styles && (!bundleCjs || (!transpile && typescript)) ) { %><% if ( transpile ) { %>babel <% if ( complexTranspile ) { %>src<% } else { %>'{index,lib/**/*}.<%= extension || 'js' %>'<% } %> --out-dir <% if ( complexTranspile ) { %>./<% } else { %>esm/<% } %><% if ( sourceMaps ) { %> --source-maps true<% } %><% } else { %><% if ( typescript ) { %><% if ( transpile ) { %> && <% } %>tsc -p tsconfig.build.json<% } %><% } %><% } else { %>rollup --config rollup.config.js<% } %>", "module-check": "<% if ( bundleCjs ) { %>node -e 'require(\"<%= moduleName %>\");' && <% } %>node --input-type=module -e 'import \"<%= moduleName %>\";'"<% } %>, "prerelease": "npm run lint<% if ( transpile ) { %><% if ( typescript ) { %> && npm run lint:types<% } %> && npm run build<% } %><% if ( !(browserModule && styles || sassModule || cssModule) ) { %> && npm run module-check<% } %>"<% if ( transpile ) { %>, "prepublishOnly": "npm run build"<% } %> @@ -125,7 +125,8 @@ "webdriverio": "^6.6.8", "local-web-server": "^4.2.1", "http-shutdown": "^1.0.3"<% } %><% if ( transpile ) { %>, - "@babel/core": "^7.2.2", + "@babel/core": "^7.2.2",<% if ( !browserModule && !styles && !bundleCjs ) { %> + "@babel/cli": "^7.2.3",<% } %> "core-js": "^2.6.5", "@babel/preset-env": "^7.12.1"<% if ( typescript && typescriptMode === 'full' ) { %>, "@babel/preset-typescript": "^7.14.5"<% } %><% if ( browserModule && !sassModule ) { %><% if ( bundlingTool === 'webpack' ) { %>, diff --git a/generators/app/templates/rollup.config.js b/generators/app/templates/rollup.config.js index 059a17e..9a81510 100644 --- a/generators/app/templates/rollup.config.js +++ b/generators/app/templates/rollup.config.js @@ -36,7 +36,7 @@ export default { } if (typeof prefix !== 'undefined') { const tsconfig = { - extends: './tsconfig', + extends: './tsconfig.json', exclude: ['test/**/*.<%= extension || 'js' %>'], compilerOptions: { declaration: true, diff --git a/generators/app/templates/tsconfig.build.json b/generators/app/templates/tsconfig.build.json new file mode 100644 index 0000000..1257b91 --- /dev/null +++ b/generators/app/templates/tsconfig.build.json @@ -0,0 +1,12 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "declaration": true, + "declarationMap": true, + "declarationDir": "types", + "emitDeclarationOnly": true, + "noEmit": false, + "listEmittedFiles": true + }, + "exclude": ["test/**/*.<%= extension || 'js' %>"], +} diff --git a/test/index.js b/test/index.js index b8ad0de..b0fe853 100644 --- a/test/index.js +++ b/test/index.js @@ -876,7 +876,7 @@ describe('Transpile', function () { main: 'esm/index.js', files: ['esm/', 'LICENSE.md', 'README.md'], scripts: { - build: 'rollup --config rollup.config.js', + build: "babel '{index,lib/**/*}.js' --out-dir esm/", prerelease: 'npm run lint && npm run build && npm run module-check', prepublishOnly: 'npm run build' @@ -1027,7 +1027,7 @@ describe('Transpile, complex', function () { main: 'esm/index.js', files: ['esm/'], scripts: { - build: 'rollup --config rollup.config.js' + build: 'babel src --out-dir ./' } }); }); @@ -1119,7 +1119,7 @@ describe('ES Modules, transpile', function () { sideEffects: false, files: ['esm/'], scripts: { - build: 'rollup --config rollup.config.js', + build: "babel '{index,lib/**/*}.js' --out-dir esm/ --source-maps true", prerelease: 'npm run lint && npm run build && npm run module-check', prepublishOnly: 'npm run build' @@ -1465,7 +1465,7 @@ describe('TypeScript, with comments', function () { }); it('should create necessary files', function () { - assert.file(['tsconfig.json']); + assert.file(['tsconfig.json', 'tsconfig.build.json']); }); it('should fill package.json with correct information', function () {