Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Jun 20, 2023. It is now read-only.

Commit 812fa63

Browse files
marcobusemannnchanged
authored andcommitted
fix($target): fluent api method target() does not split target and language level
A target can be specified by init() or using the fluent api method target(). The first one splitts target and language level for further processing. The later one does not split them. This leads to warnings for packages requireing a browser target. 1376
1 parent 82e6b88 commit 812fa63

File tree

4 files changed

+79
-9
lines changed

4 files changed

+79
-9
lines changed

src/core/Bundle.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { PackageAbstraction } from "../quantum/core/PackageAbstraction";
1414
import { EventEmitter } from "../EventEmitter";
1515
import { ExtensionOverrides } from "./ExtensionOverrides";
1616
import { QuantumBit } from "../quantum/plugin/QuantumBit";
17+
import { CombinedTargetAndLanguageLevel } from './CombinedTargetAndLanguageLevel';
1718

1819
type WatchFilterFn = (path: string) => boolean
1920
export interface HMROptions {
@@ -201,7 +202,10 @@ export class Bundle {
201202
}
202203

203204
public target(target: string): Bundle {
204-
this.context.target = target;
205+
const combination = new CombinedTargetAndLanguageLevel(target);
206+
this.context.target = combination.target();
207+
this.context.forcedLanguageLevel = combination.languageLevel();
208+
this.context.languageLevel = combination.languageLevelOrDefault();
205209
return this;
206210
}
207211

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { ScriptTarget } from './File';
2+
3+
export class CombinedTargetAndLanguageLevel {
4+
5+
constructor(private combination: string) {
6+
this.combination = this.combination || "browser";
7+
}
8+
9+
target(): string {
10+
const [target,] = this.splittedCombination();
11+
return target;
12+
}
13+
14+
languageLevel(): ScriptTarget {
15+
const [, languageLevel] = this.splittedCombination();
16+
const level = languageLevel && Object.keys(ScriptTarget).find(t => t.toLowerCase() === languageLevel);
17+
return level ? ScriptTarget[level] : undefined;
18+
}
19+
20+
languageLevelOrDefault(defaultLanguageLevel: ScriptTarget = ScriptTarget.ES2016) {
21+
const languageLevel = this.languageLevel();
22+
return languageLevel ? languageLevel : defaultLanguageLevel;
23+
}
24+
25+
private splittedCombination() {
26+
return this.combination.toLowerCase().split("@");
27+
}
28+
}

src/core/FuseBox.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { Bundle } from "./Bundle";
1717
import { File, ScriptTarget } from "./File";
1818
import { ExtensionOverrides } from "./ExtensionOverrides";
1919
import { TypescriptConfig } from "./TypescriptConfig";
20+
import { CombinedTargetAndLanguageLevel } from './CombinedTargetAndLanguageLevel';
2021

2122
const appRoot = require("app-root-path");
2223

@@ -115,14 +116,10 @@ export class FuseBox {
115116
}
116117

117118
// setting targets
118-
opts.target = opts.target || "browser";
119-
const [target, languageLevel] = opts.target.toLowerCase().split("@");
120-
this.context.target = target;
121-
const level = languageLevel && Object.keys(ScriptTarget).find(t => t.toLowerCase() === languageLevel);
122-
if (level) {
123-
this.context.forcedLanguageLevel = ScriptTarget[level];
124-
}
125-
this.context.languageLevel = ScriptTarget[level] || ScriptTarget.ES2016;
119+
const combination = new CombinedTargetAndLanguageLevel(opts.target);
120+
this.context.target = combination.target();
121+
this.context.forcedLanguageLevel = combination.languageLevel();
122+
this.context.languageLevel = combination.languageLevelOrDefault();
126123

127124
if (opts.polyfillNonStandardDefaultUsage !== undefined) {
128125
this.context.deprecation(
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { should } from "fuse-test-runner";
2+
import { CombinedTargetAndLanguageLevel } from '../core/CombinedTargetAndLanguageLevel';
3+
import { ScriptTarget } from '../core/File';
4+
5+
const TARGET_BROWSER = "browser";
6+
const TARGET_DUMMY = "asd";
7+
8+
export class CombinedTargetAndLanguageLevelTest {
9+
10+
"Should default to browser target"() {
11+
const combination = new CombinedTargetAndLanguageLevel(null);
12+
should(combination.target()).equal(TARGET_BROWSER);
13+
}
14+
15+
"Should detect target without language level"() {
16+
const combination = new CombinedTargetAndLanguageLevel(TARGET_DUMMY);
17+
should(combination.target()).equal(TARGET_DUMMY);
18+
}
19+
20+
"Should provide undefined for missing language level"() {
21+
const combination = new CombinedTargetAndLanguageLevel(TARGET_DUMMY);
22+
should(combination.languageLevel()).beUndefined();
23+
}
24+
25+
"Should detect target and language level"() {
26+
const combination = new CombinedTargetAndLanguageLevel(`${TARGET_DUMMY}@es5`);
27+
should(combination.target()).equal(TARGET_DUMMY);
28+
should(combination.languageLevel()).equal(ScriptTarget.ES5);
29+
}
30+
31+
"Should default to language level es2016"() {
32+
const combination = new CombinedTargetAndLanguageLevel(TARGET_DUMMY);
33+
should(combination.languageLevelOrDefault()).equal(ScriptTarget.ES2016);
34+
}
35+
36+
"Should detect target and language level (for default variant)"() {
37+
const combination = new CombinedTargetAndLanguageLevel(`${TARGET_DUMMY}@es5`);
38+
should(combination.target()).equal(TARGET_DUMMY);
39+
should(combination.languageLevelOrDefault()).equal(ScriptTarget.ES5);
40+
}
41+
}

0 commit comments

Comments
 (0)