Skip to content

Commit

Permalink
Add support for TypeScript in non-transpiled configurations
Browse files Browse the repository at this point in the history
Closes #201.
  • Loading branch information
niksy committed Mar 25, 2024
1 parent 03a58ef commit a2ee721
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 10 deletions.
10 changes: 9 additions & 1 deletion generators/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
},
{
Expand Down Expand Up @@ -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) {
Expand Down
9 changes: 5 additions & 4 deletions generators/app/templates/_package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) { %>,
Expand Down Expand Up @@ -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"<% } %>
Expand Down Expand Up @@ -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' ) { %>,
Expand Down
2 changes: 1 addition & 1 deletion generators/app/templates/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default {
}
if (typeof prefix !== 'undefined') {
const tsconfig = {
extends: './tsconfig',
extends: './tsconfig.json',
exclude: ['test/**/*.<%= extension || 'js' %>'],
compilerOptions: {
declaration: true,
Expand Down
12 changes: 12 additions & 0 deletions generators/app/templates/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"declaration": true,
"declarationMap": true,
"declarationDir": "types",
"emitDeclarationOnly": true,
"noEmit": false,
"listEmittedFiles": true
},
"exclude": ["test/**/*.<%= extension || 'js' %>"],
}
8 changes: 4 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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 ./'
}
});
});
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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 () {
Expand Down

0 comments on commit a2ee721

Please sign in to comment.