-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(babel): alternative setup babel/typescript
- Loading branch information
Showing
14 changed files
with
512 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module.exports = api => ({ | ||
presets: [ | ||
'@babel/preset-env', | ||
'@babel/preset-typescript' | ||
], | ||
plugins: [ | ||
['@babel/plugin-proposal-decorators', {legacy: true}], | ||
['babel-plugin-transform-typescript-metadata', {loose: true}], | ||
['@babel/plugin-proposal-class-properties', {loose: true}], | ||
['jest-preset-angular/babel/inline-template.plugin'], | ||
['const-enum'] | ||
] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const { types } = require('@babel/core') | ||
|
||
module.exports = (_api) => ({ | ||
visitor: { | ||
ObjectProperty(path) { | ||
const node = path.node | ||
if (!types.isIdentifier(node.key)) return | ||
if (node.key.name !== 'templateUrl') return | ||
if (!types.isStringLiteral(node.value)) return | ||
const requireIdentifier = types.identifier('require') | ||
let templateUrl = node.value.value | ||
if (!templateUrl.match(/^(\.\/|\.\.\/|\/)/)) { | ||
templateUrl = `./${templateUrl}` | ||
} | ||
const templateUrlStringLiteral = types.stringLiteral(templateUrl) | ||
const templateCallExpr = types.callExpression(requireIdentifier, [templateUrlStringLiteral]) | ||
const templateIdentifier = types.identifier('template') | ||
const templateProperty = types.objectProperty(templateIdentifier, templateCallExpr) | ||
path.replaceWith(templateProperty) | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module.exports = { | ||
transform: { | ||
'^.+\\.(html)$': 'jest-preset-angular/babel/load-html-transformer', | ||
'^.+\\.(ts|js)$': 'babel-jest' | ||
}, | ||
testEnvironment: 'jest-environment-jsdom-fifteen', | ||
moduleFileExtensions: ['ts', 'html', 'js', 'json'], | ||
moduleNameMapper: { | ||
'^src/(.*)$': '<rootDir>/src/$1', | ||
'^app/(.*)$': '<rootDir>/src/app/$1', | ||
'^assets/(.*)$': '<rootDir>/src/assets/$1', | ||
'^environments/(.*)$': '<rootDir>/src/environments/$1', | ||
}, | ||
transformIgnorePatterns: ['node_modules/(?!@ngrx)'], | ||
snapshotSerializers: [ | ||
'jest-preset-angular/build/AngularNoNgAttributesSnapshotSerializer.js', | ||
'jest-preset-angular/build/AngularSnapshotSerializer.js', | ||
'jest-preset-angular/build/HTMLCommentSerializer.js', | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
process: src => ({ code: `module.exports=`+JSON.stringify(src) }) | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const babelAngularConfig = require('jest-preset-angular/babel/babel.config') | ||
module.exports = api => { | ||
api.cache(true) | ||
return babelAngularConfig(api) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Injectable } from "@angular/core"; | ||
import { HeroCategory, HeroColor } from "./hero-properties"; | ||
|
||
@Injectable({ | ||
providedIn: "root" | ||
}) | ||
export class HeroCategoryService { | ||
getCategoryForHero(heroName: string): HeroCategory { | ||
switch (heroName) { | ||
case "Joker": | ||
return HeroCategory.Evil; | ||
case "Batman": | ||
return HeroCategory.Good; | ||
default: | ||
return HeroCategory.Neutral; | ||
} | ||
} | ||
|
||
getColorForHero(heroName: string): HeroColor { | ||
switch (heroName) { | ||
case "Joker": | ||
return HeroColor.Purple; | ||
case "Batman": | ||
return HeroColor.Black; | ||
case "Catwoman": | ||
return HeroColor.Black; | ||
default: | ||
return HeroColor.Transparent; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { async } from "@angular/core/testing"; | ||
|
||
import { HeroCategoryService } from "./hero-category.service"; | ||
import { HeroCategory, HeroColor } from './hero-properties'; | ||
|
||
describe("HeroCategoryService", () => { | ||
let service: HeroCategoryService; | ||
|
||
beforeEach(() => (service = new HeroCategoryService())); | ||
|
||
it("should create", () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
|
||
it("should return the right category for heroes", () => { | ||
expect(service.getCategoryForHero("Batman")).toEqual(HeroCategory.Good); | ||
expect(service.getCategoryForHero("Joker")).toEqual(HeroCategory.Evil); | ||
expect(service.getCategoryForHero("Catwoman")).toEqual(HeroCategory.Neutral); | ||
}); | ||
|
||
it("should return the right color for heroes", () => { | ||
expect(service.getColorForHero("Batman")).toEqual(HeroColor.Black); | ||
expect(service.getColorForHero("Joker")).toEqual(HeroColor.Purple); | ||
expect(service.getColorForHero("The Penguin")).toEqual(HeroColor.Transparent); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
|
||
export enum HeroCategory { | ||
Evil, | ||
Good, | ||
Neutral | ||
} | ||
|
||
export const enum HeroColor { | ||
Black = "#000000", | ||
Purple = "#551A8B", | ||
Transparent = "#00000000" | ||
} |
Oops, something went wrong.