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

Commit

Permalink
feat: add language target to control transpilation level
Browse files Browse the repository at this point in the history
  • Loading branch information
calebboyd authored and nchanged committed Sep 12, 2017
1 parent 9d0ddd9 commit ec41a95
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 10 deletions.
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,4 @@
"url": "https://opencollective.com/fuse-box",
"logo": "https://opencollective.com/opencollective/logo.txt"
}
}
}
13 changes: 7 additions & 6 deletions src/analysis/plugins/ImportDeclaration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { File } from "../../core/File";
import { File, ScriptTarget } from "../../core/File";
/**
* Handles require and ImportDeclarations
* At the moment does not transpile
Expand All @@ -25,7 +25,7 @@ export class ImportDeclaration {
}

if (node.async === true) {
file.es6module = true;
file.setLanguageLevel(ScriptTarget.ES2017)
}

if (node.type === "ExportDefaultDeclaration") {
Expand All @@ -50,17 +50,18 @@ export class ImportDeclaration {
}

public static onEnd(file: File) {
// We detect that imports are used
// Now we need to transpile the code
if (file.es6module) {
const target = file.context.languageLevel
const downlevelTranspile = file.languageLevel > target

if (file.es6module || downlevelTranspile) {
file.context.log.magicReason(
'used typescript to compile because an import was used',
file.info.fuseBoxPath);
const ts = require("typescript");
let tsconfg: any = {
compilerOptions: {
module: "commonjs",
target: "es5"
target
},
};;
let result = ts.transpileModule(file.contents, tsconfg);
Expand Down
19 changes: 19 additions & 0 deletions src/core/File.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ import * as fs from "fs";
import * as path from "path";
import { ensureFuseBoxPath, readFuseBoxModule, isStylesheetExtension } from "../Utils";

/**
* Same Target Enumerator used in TypeScript
*/
export enum ScriptTarget {
ES5 = 1,
ES2015 = 2,
ES2016 = 3,
ES2017 = 4,
ESNext = 5
}

/**
*
*
Expand All @@ -18,6 +29,8 @@ export class File {

public isFuseBoxBundle = false;

public languageLevel = ScriptTarget.ES5

public es6module = false;
/**
* In order to keep bundle in a bundle
Expand Down Expand Up @@ -166,6 +179,12 @@ export class File {
return file;
}

public setLanguageLevel (level: ScriptTarget) {
if (this.languageLevel < level) {
this.languageLevel = level
}
}

public addProperty(key: string, obj: any) {
this.properties.set(key, obj);
}
Expand Down
18 changes: 16 additions & 2 deletions src/core/FuseBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { UserOutput } from "./UserOutput";
import { BundleProducer } from "./BundleProducer";
import { Bundle } from "./Bundle";
import { SplitConfig } from "./BundleSplit";
import { ScriptTarget } from "./File";

const isWin = /^win/.test(process.platform);
const appRoot = require("app-root-path");
Expand All @@ -26,7 +27,16 @@ export interface FuseBoxOptions {
tsConfig?: string;
package?: any;
cache?: boolean;
target?: "browser" | "server" | "universal" | "electron",
/**
* "browser" | "server" | "universal" | "electron"
*
* Combine target and language version with an '@'
*
* eg. server@es2017
*
* default: "universal@es5"
*/
target?: string,
log?: boolean;
globals?: { [packageName: string]: /** Variable name */ string };
plugins?: Plugin[] | [Plugin[]];
Expand Down Expand Up @@ -91,7 +101,11 @@ export class FuseBox {
}

if (opts.target !== undefined) {
this.context.target = opts.target;
const [target, languageLevel] = opts.target.toLowerCase().split('@')
this.context.target = target
const level = languageLevel && Object.keys(ScriptTarget)
.find(t => t.toLowerCase() === languageLevel)
this.context.languageLevel = ScriptTarget[level] || ScriptTarget.ES5
}
if (opts.polyfillNonStandardDefaultUsage !== undefined) {
this.context.polyfillNonStandardDefaultUsage = opts.polyfillNonStandardDefaultUsage;
Expand Down
7 changes: 6 additions & 1 deletion src/core/WorkflowContext.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as path from "path";
import * as escodegen from "escodegen";
import { BundleSource } from "../BundleSource";
import { File } from "./File";
import { File, ScriptTarget } from "./File";
import { Log } from "../Log";
import { IPackageInformation, IPathInformation, AllowedExtenstions } from "./PathMaster";
import { ModuleCollection } from "./ModuleCollection";
Expand Down Expand Up @@ -97,6 +97,8 @@ export class WorkFlowContext {

public pendingPromises: Promise<any>[] = [];

public languageLevel: ScriptTarget;

public polyfillNonStandardDefaultUsage: boolean | string[] = false;

public customAPIFile: string;
Expand Down Expand Up @@ -558,6 +560,9 @@ export class WorkFlowContext {
}

config.compilerOptions.module = "commonjs";
if (!('target' in config.compilerOptions)) {
config.compilerOptions.target = this.languageLevel
}

if (this.useSourceMaps) {
config.compilerOptions.sourceMap = true;
Expand Down

0 comments on commit ec41a95

Please sign in to comment.