diff --git a/generators/app/index.js b/generators/app/index.js index fd89fe9..5b34e77 100644 --- a/generators/app/index.js +++ b/generators/app/index.js @@ -440,8 +440,7 @@ export default class extends Generator { type: 'confirm', name: 'typescript', message: 'Do you want to use TypeScript?', - default: false, - when: (answers) => answers.transpile + default: false }, { type: 'list', @@ -745,7 +744,11 @@ export default class extends Generator { rm('.babelrc'); } - if (answers.transpile || answers.typescript) { + if ( + answers.transpile && + (answers.bundleCjs || + (answers.browserModule && !answers.sassModule)) + ) { cp('rollup.config.js', 'rollup.config.js'); } else { rm('rollup.config.js'); @@ -765,13 +768,7 @@ 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'); - } + cp('tsconfig.build.json', 'tsconfig.build.json'); } else { rm('tsconfig.json'); rm('tsconfig.build.json'); diff --git a/generators/app/templates/_package.json b/generators/app/templates/_package.json index 1376000..600bc63 100644 --- a/generators/app/templates/_package.json +++ b/generators/app/templates/_package.json @@ -56,9 +56,10 @@ "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 || typescript ) { %>, - "build": "<% if ( !browserModule && !styles && (!bundleCjs || (!transpile && typescript)) ) { %><% if ( transpile ) { %>babel '{index,lib/**/*}.<%= extension || 'js' %>' --out-dir esm/<% if ( sourceMaps ) { %> --source-maps true<% } %><% } else { %><% if ( typescript ) { %><% if ( transpile ) { %> && <% } %>tsc -p tsconfig.build.json<% } %><% } %><% } else { %>rollup --config rollup.config.js<% } %>"<% if ( bundleCjs ) { %>, - "module-check": "node -e 'require(\"<%= moduleName %>\");' && node --input-type=module -e 'import \"<%= moduleName %>\";'"<% } %><% } %>, + "posttest:ci": "cat ./coverage/lcov.info | coveralls"<% } %><% } %><% if ( transpile && (bundleCjs || (browserModule && !sassModule)) ) { %>, + "build": "rollup --config rollup.config.js"<% if ( bundleCjs ) { %>, + "module-check": "node -e 'require(\"<%= moduleName %>\");' && node --input-type=module -e 'import \"<%= moduleName %>\";'"<% } %><% } else if (transpile || typescript) { %>, + "build": "<% if ( transpile ) { %>babel '{index,lib/**/*}.<%= extension || 'js' %>' --out-dir esm/<% if ( sourceMaps ) { %> --source-maps true<% } %><% } else { %><% if ( typescript ) { %><% if ( transpile ) { %> && <% } %>tsc -p tsconfig.build.json --declarationDir types<% } %><% } %>"<% } %>, "prerelease": "npm run lint<% if ( transpile ) { %><% if ( typescript ) { %> && npm run lint:types<% } %> && npm run build<% } %><% if ( !(browserModule && styles || sassModule || cssModule) ) { %><% if ( bundleCjs ) { %> && npm run module-check<% } %><% } %>"<% if ( transpile ) { %>, "prepublishOnly": "npm run build"<% } %> }, diff --git a/generators/app/templates/eslintrc b/generators/app/templates/eslintrc index c808714..f8948d9 100644 --- a/generators/app/templates/eslintrc +++ b/generators/app/templates/eslintrc @@ -1,7 +1,7 @@ <% var files = []; if ( manualTests || integrationTests ) { if ( bundlingTool === 'webpack' ) { files.push("test/manual/webpack.config.js"); } if ( bundlingTool === 'rollup' ) { files.push("test/manual/rollup.config.js"); } } if ( automatedTests && browserModule && !sassModule ) { files.push("karma.conf.js"); } if ( integrationTests ) { -files.push("wdio.conf.js"); } if ( transpile || typescript ) { +files.push("wdio.conf.js"); } if ( transpile && (bundleCjs || (browserModule && !sassModule)) ) { files.push("rollup.config.js"); } %>{ "root": true, "extends": [ diff --git a/generators/app/templates/rollup.config.js b/generators/app/templates/rollup.config.js index e7d2a1b..4d4f790 100644 --- a/generators/app/templates/rollup.config.js +++ b/generators/app/templates/rollup.config.js @@ -35,39 +35,17 @@ export default { prefix = 'esm'; } if (typeof prefix !== 'undefined') { - const tsconfig = { - extends: './tsconfig.json', - exclude: ['test/**/*.<%= extension || 'js' %>'], - compilerOptions: { - declaration: true, - declarationMap: true, - declarationDir: prefix, - emitDeclarationOnly: true, - noEmit: false, - listEmittedFiles: true + const { stdout } = await execa( + 'tsc', + ['-p', './tsconfig.build.json', '--declarationDir', prefix], + { + preferLocal: true } - }; - const file = `.${prefix}.tsconfig.json`; + ); try { - await fs.writeFile( - file, - JSON.stringify(tsconfig), - 'utf-8' - ); - const { stdout } = await execa( - 'tsc', - ['-p', file], - { - preferLocal: true - } - ); - try { - await cpy('types', `${prefix}/types`); - } catch (error) {} - console.log(stdout); - } finally { - await fs.unlink(file); - } + await cpy('types', `${prefix}/types`); + } catch (error) {} + console.log(stdout); } } }; diff --git a/generators/app/templates/tsconfig.build.json b/generators/app/templates/tsconfig.build.json index 1257b91..ee56950 100644 --- a/generators/app/templates/tsconfig.build.json +++ b/generators/app/templates/tsconfig.build.json @@ -3,7 +3,6 @@ "compilerOptions": { "declaration": true, "declarationMap": true, - "declarationDir": "types", "emitDeclarationOnly": true, "noEmit": false, "listEmittedFiles": true diff --git a/test/index.js b/test/index.js index ae7383f..6ead226 100644 --- a/test/index.js +++ b/test/index.js @@ -926,7 +926,7 @@ describe('Transpile, source maps', function () { }); it('should create necessary files', function () { - assert.file(['.babelrc', 'rollup.config.js']); + assert.file(['.babelrc']); }); it('should fill package.json with correct information', function () { @@ -1301,10 +1301,8 @@ describe('TypeScript, with comments', function () { return helpers .run(generatorPath) .withAnswers({ - transpile: true, typescript: true, - typescriptMode: 'comments', - automatedTests: true + typescriptMode: 'comments' }) .toPromise(); }); @@ -1317,7 +1315,7 @@ describe('TypeScript, with comments', function () { assert.jsonFileContent('package.json', { exports: { '.': { - types: 'esm/index.d.ts' + types: 'types/index.d.ts' } }, scripts: { @@ -1333,19 +1331,105 @@ describe('TypeScript, with comments', function () { }); describe('TypeScript, full', function () { + before(function () { + return helpers + .run(generatorPath) + .withAnswers({ + typescript: true, + typescriptMode: 'full' + }) + .toPromise(); + }); + + it('should create necessary files', function () { + assert.file(['tsconfig.json', 'tsconfig.build.json']); + }); + + it('should fill package.json with correct information', function () { + assert.jsonFileContent('package.json', { + exports: { + '.': { + types: 'types/index.d.ts' + } + }, + scripts: { + 'lint:types': 'tsc' + }, + devDependencies: { + 'typescript': '^5.4.3', + '@types/node': '^20.11.30', + '@types/mocha': '^10.0.6' + } + }); + }); +}); + +describe('TypeScript, transpile, with comments', function () { before(function () { return helpers .run(generatorPath) .withAnswers({ transpile: true, typescript: true, - typescriptMode: 'full', - automatedTests: true + typescriptMode: 'comments' }) .toPromise(); }); - it('should fill rollup.config.js with correct information', function () { - assert.fileContent('rollup.config.js', "input: 'index.ts'"); + it('should create necessary files', function () { + assert.file(['tsconfig.json']); + }); + + it('should fill package.json with correct information', function () { + assert.jsonFileContent('package.json', { + exports: { + '.': { + types: 'esm/index.d.ts' + } + }, + scripts: { + 'lint:types': 'tsc' + }, + devDependencies: { + 'typescript': '^5.4.3', + '@types/node': '^20.11.30', + '@types/mocha': '^10.0.6' + } + }); + }); +}); + +describe('TypeScript, transpile, full', function () { + before(function () { + return helpers + .run(generatorPath) + .withAnswers({ + transpile: true, + typescript: true, + typescriptMode: 'full' + }) + .toPromise(); + }); + + it('should create necessary files', function () { + assert.file(['tsconfig.json']); + }); + + it('should fill package.json with correct information', function () { + assert.jsonFileContent('package.json', { + exports: { + '.': { + types: 'esm/index.d.ts' + } + }, + scripts: { + 'lint:types': 'tsc' + }, + devDependencies: { + 'typescript': '^5.4.3', + '@types/node': '^20.11.30', + '@types/mocha': '^10.0.6' + } + }); }); });