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

Implement babel 7 support #763

Merged
merged 14 commits into from
Apr 12, 2019
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: 5 additions & 0 deletions build.proj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ of patent rights can be found in the PATENTS file in the same directory.
WorkingDirectory="src/React.Core"
Command="npm install"
/>
<Exec
WorkingDirectory="src/React.Core/Resources/babel-legacy"
Command="npm install"
/>
<Exec
WorkingDirectory="src/React.Sample.Webpack.CoreMvc"
Command="npm install"
Expand Down Expand Up @@ -93,6 +97,7 @@ of patent rights can be found in the PATENTS file in the same directory.

<Target Name="Build" DependsOnTargets="RestorePackages;UpdateVersion">
<Exec WorkingDirectory="src/React.Core" Command="node_modules/.bin/webpack" />
<Exec WorkingDirectory="src/React.Core/Resources/babel-legacy" Command="node_modules/.bin/webpack" />
<MSBuild Projects="$(SolutionFile)" Targets="Rebuild" Properties="Configuration=Release;Platform=Any CPU;NoWarn=1607,7035,1701;Version=$(VersionString)" />
<Exec WorkingDirectory="src/React.Sample.Webpack.CoreMvc" Command="node_modules/.bin/webpack" />
</Target>
Expand Down
17 changes: 14 additions & 3 deletions src/React.AspNet.Middleware/BabelFileOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
Expand All @@ -7,8 +7,10 @@
using System.Collections.Generic;
#if OWIN
using Microsoft.Owin.StaticFiles;
using Microsoft.Owin.StaticFiles.ContentTypes;
#else
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.StaticFiles;
#endif

#if OWIN
Expand Down Expand Up @@ -37,8 +39,17 @@ public class BabelFileOptions
/// </summary>
public BabelFileOptions()
{
Extensions = new[] { ".jsx" };
StaticFileOptions = new StaticFileOptions();
Extensions = new[] { ".jsx", ".tsx" };
StaticFileOptions = new StaticFileOptions() { ContentTypeProvider = new JsxContentTypeProvider() } ;
}

private class JsxContentTypeProvider : IContentTypeProvider
{
public bool TryGetContentType(string subpath, out string contentType)
{
contentType = "text/javascript";
return true;
}
}
}
}
4 changes: 2 additions & 2 deletions src/React.Core/Babel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
Expand Down Expand Up @@ -74,7 +74,7 @@ public Babel(IReactEnvironment environment, ICache cache, IFileSystem fileSystem
_fileSystem = fileSystem;
_fileCacheHash = fileCacheHash;
_config = siteConfig;
_babelConfig = siteConfig.BabelConfig.Serialize();
_babelConfig = siteConfig.BabelConfig.Serialize(_config.BabelVersion);
}

/// <summary>
Expand Down
44 changes: 26 additions & 18 deletions src/React.Core/BabelConfig.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using React.Exceptions;

namespace React
{
Expand All @@ -22,28 +24,34 @@ public class BabelConfig
/// </summary>
public ISet<string> Presets { get; set; }

/// <summary>
/// Creates a new instance of <see cref="BabelConfig" />.
/// </summary>
public BabelConfig()
{
// Use es2015-no-commonjs by default so Babel doesn't prepend "use strict" to the start of the
// output. This messes with the top-level "this", as we're not actually using JavaScript modules
// in ReactJS.NET yet.
Presets = new HashSet<string> { "es2015-no-commonjs", "stage-1", "react" };
Plugins = new HashSet<string>();
}

/// <summary>
/// Serializes this Babel configuration into the format required for Babel.
/// </summary>
/// <returns></returns>
public string Serialize()
public string Serialize(string babelVersion)
{
return JsonConvert.SerializeObject(this, new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
});
ISet<string> defaultPresets = babelVersion == BabelVersions.Babel7
? new HashSet<string> { "typescript", "react" }
: babelVersion == BabelVersions.Babel6 || babelVersion == null
? new HashSet<string> { "es2015-no-commonjs", "stage-1", "react" }
: throw new ArgumentException(nameof(babelVersion));

ISet<string> defaultPlugins = babelVersion == BabelVersions.Babel7
? new HashSet<string> { "proposal-class-properties", "proposal-object-rest-spread" }
: babelVersion == BabelVersions.Babel6 || babelVersion == null
? new HashSet<string>()
: throw new ArgumentException(nameof(babelVersion));

return JsonConvert.SerializeObject(
new BabelConfig
{
Plugins = Plugins ?? defaultPlugins,
Presets = Presets ?? defaultPresets,
},
new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
});
}
}
}
23 changes: 23 additions & 0 deletions src/React.Core/BabelVersions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace React
{
/// <summary>
/// Public string values for babel versions
/// </summary>
public static class BabelVersions
{
/// <summary>
/// Babel 6
/// </summary>
public static readonly string Babel6 = "babel-6";

/// <summary>
/// Babel 7
/// </summary>
public static readonly string Babel7 = "babel-7";
}
}
11 changes: 11 additions & 0 deletions src/React.Core/IReactSiteConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,17 @@ public interface IReactSiteConfiguration
/// <returns>The configuration, for chaining</returns>
IReactSiteConfiguration SetBabelConfig(BabelConfig value);

/// <summary>
/// Gets or sets the Babel version to use. Supports "babel@6 or babel@7".
/// </summary>
string BabelVersion { get; set; }

/// <summary>
/// Sets the Babel configuration to use.
/// </summary>
/// <returns>The configuration, for chaining</returns>
IReactSiteConfiguration SetBabelVersion(string value);

/// <summary>
/// Gets or sets whether to use the debug version of React. This is slower, but gives
/// useful debugging tips.
Expand Down
10 changes: 9 additions & 1 deletion src/React.Core/React.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Description>ReactJS and Babel tools for .NET. Important: This package does not do much on its own; you probably want an integration package (like React.Web.Mvc4) as well. Please refer to project site (http://reactjs.net/) for more details, usage examples and sample code.</Description>
Expand All @@ -21,13 +21,21 @@

<ItemGroup>
<Compile Remove="node_modules\**" />
<Compile Remove="Resources\babel-legacy\node_modules\**" />
<EmbeddedResource Remove="node_modules\**" />
<EmbeddedResource Remove="Resources\babel-legacy\node_modules\**" />
<None Remove="node_modules\**" />
<None Remove="Resources\babel-legacy\node_modules\**" />
</ItemGroup>

<ItemGroup>
<None Remove="Resources\babel-legacy.generated.min.js" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\SharedAssemblyInfo.cs" />
<Compile Include="..\SharedAssemblyVersionInfo.cs" />
<EmbeddedResource Include="Resources\babel-legacy.generated.min.js" />
<EmbeddedResource Include="Resources\shims.js;Resources\react.generated.js;Resources\react.generated.min.js;Resources\babel.generated.min.js" />
</ItemGroup>

Expand Down
6 changes: 5 additions & 1 deletion src/React.Core/ReactEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,11 @@ private void EnsureBabelLoaded(IJsEngine engine)
#else
var assembly = typeof(ReactEnvironment).GetTypeInfo().Assembly;
#endif
const string resourceName = "React.Core.Resources.babel.generated.min.js";
string resourceName = _config.BabelVersion == BabelVersions.Babel7
? "React.Core.Resources.babel.generated.min.js"
: _config.BabelVersion == BabelVersions.Babel6 || _config.BabelVersion == null
? "React.Core.Resources.babel-legacy.generated.min.js"
: throw new ReactConfigurationException("BabelVersion was not null, but did not contain a valid value.");

if (_config.AllowJavaScriptPrecompilation
&& engine.TryExecuteResourceWithPrecompilation(_cache, resourceName, assembly))
Expand Down
15 changes: 15 additions & 0 deletions src/React.Core/ReactSiteConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,21 @@ public IReactSiteConfiguration SetBabelConfig(BabelConfig value)
return this;
}

/// <summary>
/// Gets or sets the Babel version to use. Supports "babel@6 or babel@7".
/// </summary>
public string BabelVersion { get; set; }

/// <summary>
/// Sets the Babel version to use.
/// </summary>
/// <returns>The configuration, for chaining</returns>
public IReactSiteConfiguration SetBabelVersion(string value)
{
BabelVersion = value;
return this;
}

/// <summary>
/// Gets or sets whether to use the debug version of React. This is slower, but gives
/// useful debugging tips.
Expand Down
42 changes: 42 additions & 0 deletions src/React.Core/Resources/babel-legacy/babel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import {transform as babelTransform, version as babelVersion} from 'babel-standalone';

export function ReactNET_transform(input, babelConfig, filename) {
babelConfig = {
...JSON.parse(babelConfig),
ast: false,
filename,
}
try {
return babelTransform(input, babelConfig).code;
} catch (ex) {
// Parsing stack is extremely long and not very useful, so just rethrow the message.
throw new Error(ex.message);
}
}

export function ReactNET_transform_sourcemap(input, babelConfig, filename) {
babelConfig = {
...JSON.parse(babelConfig),
ast: false,
filename,
sourceMaps: true,
};
try {
var result = babelTransform(input, babelConfig);
return JSON.stringify({
babelVersion,
code: result.code,
sourceMap: result.map
});
} catch (ex) {
// Parsing stack is extremely long and not very useful, so just rethrow the message.
throw new Error(ex.message);
}
}
Loading