Skip to content

Commit

Permalink
Update Rollup and TypeScript configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
niksy committed Mar 26, 2024
1 parent 51ead58 commit 3105765
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 55 deletions.
17 changes: 7 additions & 10 deletions generators/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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');
Expand All @@ -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');
Expand Down
7 changes: 4 additions & 3 deletions generators/app/templates/_package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"<% } %>
},
Expand Down
2 changes: 1 addition & 1 deletion generators/app/templates/eslintrc
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
40 changes: 9 additions & 31 deletions generators/app/templates/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
};
Expand Down
1 change: 0 additions & 1 deletion generators/app/templates/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"compilerOptions": {
"declaration": true,
"declarationMap": true,
"declarationDir": "types",
"emitDeclarationOnly": true,
"noEmit": false,
"listEmittedFiles": true
Expand Down
102 changes: 93 additions & 9 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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();
});
Expand All @@ -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: {
Expand All @@ -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'
}
});
});
});

0 comments on commit 3105765

Please sign in to comment.