Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add typescript template for lwc #590

Merged
merged 5 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,8 @@
"@typescript-eslint/no-non-null-assertion": ["off"],
"@typescript-eslint/no-var-requires": ["off"]
},
"ignorePatterns": ["*.js"]
"ignorePatterns": [
"*.js",
"src/templates/lightningcomponent/lwc/typeScript/*"
]
}
19 changes: 15 additions & 4 deletions src/generators/lightningComponentGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,18 @@ export default class LightningComponentGenerator extends BaseGenerator<Lightning
)!
.join('-')
.toLowerCase();
const ext = template === 'typeScript' ? 'ts' : 'js';

this.sourceRootWithPartialPath(
path.join('lightningcomponent', 'lwc', template)
);
await this.render(
this.templatePath(`${template}.js`),
this.templatePath(`${template}.${ext}`),
this.destinationPath(
path.join(
this.outputdir,
camelCaseComponentName,
`${camelCaseComponentName}.js`
`${camelCaseComponentName}.${ext}`
mingxuanzhangsfdx marked this conversation as resolved.
Show resolved Hide resolved
)
),
{ pascalCaseComponentName }
Expand All @@ -177,13 +178,13 @@ export default class LightningComponentGenerator extends BaseGenerator<Lightning
);

await this.render(
this.templatePath(path.join(`__tests__`, `${template}.test.js`)),
this.templatePath(path.join(`__tests__`, `${template}.test.${ext}`)),
this.destinationPath(
path.join(
this.outputdir,
camelCaseComponentName,
`__tests__`,
`${camelCaseComponentName}.test.js`
`${camelCaseComponentName}.test.${ext}`
)
),
{
Expand All @@ -193,6 +194,16 @@ export default class LightningComponentGenerator extends BaseGenerator<Lightning
}
);

if (template === 'typeScript') {
await this.render(
this.templatePath('gitignore'),
this.destinationPath(
path.join(this.outputdir, camelCaseComponentName, '.gitignore')
),
{}
);
}

if (!internal) {
const masterLabel = camelCaseToTitleCase(componentname)
.replace(/</g, '&lt;')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { createElement } from 'lwc';
import <%= pascalCaseComponentName %> from 'c/<%= camelCaseComponentName %>';

describe('c-<%= kebabCaseComponentName %>', () => {
afterEach(() => {
// The jsdom instance is shared across test cases in a single file so reset the DOM
while (document.body.firstChild) {
document.body.removeChild(document.body.firstChild);
}
});

it('TODO: test case generated by CLI command, please fill in test logic', () => {
// Arrange
const element = createElement('c-<%= kebabCaseComponentName %>', {
is: <%= pascalCaseComponentName %>
});

// Act
document.body.appendChild(element);

// Assert
// const div = element.shadowRoot.querySelector('div');
expect(1).toBe(1);
});
});
1 change: 1 addition & 0 deletions src/templates/lightningcomponent/lwc/typeScript/gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>

</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion><%= apiVersion %></apiVersion>
<isExposed>false</isExposed>
</LightningComponentBundle>
3 changes: 3 additions & 0 deletions src/templates/lightningcomponent/lwc/typeScript/typeScript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { LightningElement } from 'lwc';

export default class <%= pascalCaseComponentName %> extends LightningElement {}
6 changes: 5 additions & 1 deletion src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ export interface LightningAppOptions extends TemplateOptions {

export interface LightningComponentOptions extends TemplateOptions {
componentname: string;
template: 'default' | 'analyticsDashboard' | 'analyticsDashboardWithStep';
template:
| 'default'
| 'analyticsDashboard'
| 'analyticsDashboardWithStep'
| 'typeScript';
type: 'aura' | 'lwc';
internal: boolean;
}
Expand Down
28 changes: 28 additions & 0 deletions test/service/templateService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,34 @@ describe('TemplateService', () => {
);
});

it('should create LightningComponent (lwc) with TypeScript', async () => {
await remove(path.join('testsoutput', 'libraryCreate', 'lwc'));
const templateService = TemplateService.getInstance();
const result = await templateService.create(
TemplateType.LightningComponent,
{
componentname: 'LibraryCreateComponent',
outputdir: path.join('testsoutput', 'libraryCreate', 'lwc'),
template: 'typeScript',
type: 'lwc',
}
);

chai
.expect(result.created.sort())
.to.deep.equal(
[
'testsoutput/libraryCreate/lwc/libraryCreateComponent/libraryCreateComponent.ts',
'testsoutput/libraryCreate/lwc/libraryCreateComponent/libraryCreateComponent.html',
'testsoutput/libraryCreate/lwc/libraryCreateComponent/__tests__/libraryCreateComponent.test.ts',
'testsoutput/libraryCreate/lwc/libraryCreateComponent/libraryCreateComponent.js-meta.xml',
mingxuanzhangsfdx marked this conversation as resolved.
Show resolved Hide resolved
'testsoutput/libraryCreate/lwc/libraryCreateComponent/.gitignore',
]
.map((p) => path.normalize(p))
.sort()
);
});

it('should create LightningEvent', async () => {
await remove(path.join('testsoutput', 'libraryCreate', 'aura'));
const templateService = TemplateService.getInstance();
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"allowSyntheticDefaultImports": true,
"skipLibCheck": true
},
"include": ["./src/**/*"]
"include": ["./src/**/*"],
"exclude": ["./src/templates/lightningcomponent/lwc/typeScript/*"]
}
Loading