Skip to content

Commit d0b14d3

Browse files
committed
Upgrade to Babel 6. Closes reactjs#193
1 parent aff723a commit d0b14d3

File tree

16 files changed

+122
-149
lines changed

16 files changed

+122
-149
lines changed

build.proj

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ of patent rights can be found in the PATENTS file in the same directory.
1010
<Project ToolsVersion="4.0" DefaultTargets="Build;Test;Package" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
1111
<PropertyGroup>
1212
<Major>2</Major>
13-
<Minor>1</Minor>
14-
<Build>3</Build>
13+
<Minor>2</Minor>
14+
<Build>0</Build>
1515
<Revision>0</Revision>
1616
<DevNuGetServer>http://reactjs.net/packages/</DevNuGetServer>
1717
<MSBuildCommunityTasksPath>$(MSBuildProjectDirectory)\tools\MSBuildTasks</MSBuildCommunityTasksPath>

src/React.AspNet/project.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "2.1.3-*",
2+
"version": "2.2.0-*",
33
"configurations": {
44
"Debug": {
55
"compilationOptions": {

src/React.Core/Babel.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ protected virtual JavaScriptWithSourceMap TransformWithHeader(
228228
hash = _fileCacheHash.CalculateHash(contents);
229229
}
230230
// Prepend header to generated code
231-
var header = GetFileHeader(hash);
231+
var header = GetFileHeader(hash, result.BabelVersion);
232232
result.Code = header + result.Code;
233233
result.Hash = hash;
234234

@@ -302,16 +302,17 @@ public virtual JavaScriptWithSourceMap TransformWithSourceMap(
302302
/// validate the cache.
303303
/// </summary>
304304
/// <param name="hash">Hash of the input</param>
305+
/// <param name="babelVersion">Version of Babel used to perform this transformation</param>
305306
/// <returns>Header for the cache</returns>
306-
protected virtual string GetFileHeader(string hash)
307+
protected virtual string GetFileHeader(string hash, string babelVersion)
307308
{
308309
return string.Format(
309310
@"{0}
310311
// Automatically generated by ReactJS.NET. Do not edit, your changes will be overridden.
311-
// Version: {1}
312+
// Version: {1} with Babel {3}
312313
// Generated at: {2}
313314
///////////////////////////////////////////////////////////////////////////////
314-
", _fileCacheHash.AddPrefix(hash), _environment.Version, DateTime.Now);
315+
", _fileCacheHash.AddPrefix(hash), _environment.Version, DateTime.Now, babelVersion);
315316
}
316317

317318
/// <summary>

src/React.Core/BabelConfig.cs

+15-51
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using Newtonsoft.Json;
3+
using Newtonsoft.Json.Serialization;
34

45
namespace React
56
{
@@ -10,52 +11,27 @@ namespace React
1011
public class BabelConfig
1112
{
1213
/// <summary>
13-
/// Gets or sets whether Babel's "loose mode" is used for all transformers. See
14-
/// http://babeljs.io/docs/advanced/loose/ for more information. Only one of
15-
/// <see cref="AllLoose"/> or <see cref="Loose"/> can be used at a time.
14+
/// Gets or sets the Babel plugins to use. See http://babeljs.io/docs/plugins/ for a full
15+
/// list of plugins.
1616
/// </summary>
17-
public bool AllLoose { get; set; }
17+
public ISet<string> Plugins { get; set; }
1818

1919
/// <summary>
20-
/// Gets or sets the transformers to blacklist
20+
/// Gets or sets the Babel presets to use. See http://babeljs.io/docs/plugins/ for a full
21+
/// list of presets.
2122
/// </summary>
22-
public IEnumerable<string> Blacklist { get; set; }
23-
24-
/// <summary>
25-
/// Gets or sets whether Babel should use a reference to babelHelpers instead of placing
26-
/// helpers at the top of your code. Meant to be used in conjunction with external
27-
/// helpers (http://babeljs.io/docs/advanced/external-helpers/)
28-
/// </summary>
29-
public bool ExternalHelpers { get; set; }
30-
31-
/// <summary>
32-
/// Gets or sets the transformers to use in Babel's "loose mode". See
33-
/// http://babeljs.io/docs/advanced/loose/ for more information. Only one of
34-
/// <see cref="AllLoose"/> or <see cref="Loose"/> can be used at a time.
35-
/// </summary>
36-
public IEnumerable<string> Loose { get; set; }
37-
38-
/// <summary>
39-
/// Gets or sets an transformers to optionally use. See
40-
/// http://babeljs.io/docs/advanced/transformers/#optional for a full list of transformers
41-
/// </summary>
42-
public IEnumerable<string> Optional { get; set; }
43-
44-
/// <summary>
45-
/// Gets or sets the experimental proposal stage (http://babeljs.io/docs/usage/experimental/).
46-
/// </summary>
47-
public int Stage { get; set; }
23+
public ISet<string> Presets { get; set; }
4824

4925
/// <summary>
5026
/// Creates a new instance of <see cref="BabelConfig" />.
5127
/// </summary>
5228
public BabelConfig()
5329
{
54-
// By default, we blacklist the "strict" transform, as it messes with the top-level "this".
55-
// This is required since we're not actually using JavaScript modules directly in ReactJS.NET yet.
56-
// See https://babeljs.io/docs/faq/#why-is-this-being-remapped-to-undefined-
57-
Blacklist = new[] {"strict"};
58-
Stage = 2;
30+
// Use es2015-no-commonjs by default so Babel doesn't prepend "use strict" to the start of the
31+
// output. This messes with the top-level "this", as we're not actually using JavaScript modules
32+
// in ReactJS.NET yet.
33+
Presets = new HashSet<string> { "es2015-no-commonjs", "stage-1", "react" };
34+
Plugins = new HashSet<string>();
5935
}
6036

6137
/// <summary>
@@ -64,22 +40,10 @@ public BabelConfig()
6440
/// <returns></returns>
6541
public string Serialize()
6642
{
67-
var config = new Dictionary<string, object>
68-
{
69-
{"blacklist", Blacklist},
70-
{"externalHelpers", ExternalHelpers},
71-
{"optional", Optional},
72-
{"stage", Stage},
73-
};
74-
if (AllLoose)
75-
{
76-
config.Add("loose", "all");
77-
}
78-
else if (Loose != null)
43+
return JsonConvert.SerializeObject(this, new JsonSerializerSettings
7944
{
80-
config.Add("loose", Loose);
81-
}
82-
return JsonConvert.SerializeObject(config);
45+
ContractResolver = new CamelCasePropertyNamesContractResolver(),
46+
});
8347
}
8448
}
8549
}

src/React.Core/JavaScriptWithSourceMap.cs

+5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ namespace React
1818
[Serializable]
1919
public class JavaScriptWithSourceMap
2020
{
21+
/// <summary>
22+
/// Gets or sets the version of Babel used to perform this transformation.
23+
/// </summary>
24+
public string BabelVersion { get; set; }
25+
2126
/// <summary>
2227
/// The transformed result
2328
/// </summary>

src/React.Core/React.Core.csproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -131,20 +131,20 @@
131131
<Compile Include="TinyIoC\TinyIoC.cs" />
132132
</ItemGroup>
133133
<ItemGroup>
134+
<None Include="package.json" />
134135
<None Include="packages.config" />
135136
<Compile Include="SimpleFileSystem.cs" />
136137
<None Include="React.Core.nutrans" />
137138
</ItemGroup>
138139
<ItemGroup>
139140
<EmbeddedResource Include="Resources\shims.js" />
140141
</ItemGroup>
141-
<ItemGroup>
142-
<EmbeddedResource Include="node_modules\babel-core\browser.js" />
143-
</ItemGroup>
144142
<ItemGroup>
145143
<Content Include="gulpfile.js" />
146144
<EmbeddedResource Include="Resources\react.generated.js" />
147145
<EmbeddedResource Include="Resources\react.generated.min.js" />
146+
<EmbeddedResource Include="Resources\babel.generated.min.js" />
147+
<Content Include="Resources\babel.js" />
148148
<Content Include="Resources\react.js" />
149149
</ItemGroup>
150150
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

src/React.Core/ReactEnvironment.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -434,10 +434,10 @@ private void EnsureBabelLoaded(IJsEngine engine)
434434
throw new BabelNotLoadedException();
435435
}
436436

437-
var babelLoaded = engine.Evaluate<bool>("typeof global.Babel !== 'undefined'");
437+
var babelLoaded = engine.Evaluate<bool>("typeof ReactNET_transform !== 'undefined'");
438438
if (!babelLoaded)
439439
{
440-
engine.ExecuteResource("React.node_modules.babel_core.browser.js", typeof(ReactEnvironment).Assembly);
440+
engine.ExecuteResource("React.Resources.babel.generated.min.js", typeof(ReactEnvironment).Assembly);
441441
}
442442
}
443443
}

src/React.Core/Resources/babel.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (c) 2015, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
import {transform as babelTransform, version as babelVersion} from 'babel-standalone';
11+
12+
export function ReactNET_transform(input, babelConfig, filename) {
13+
babelConfig = {
14+
...JSON.parse(babelConfig),
15+
ast: false,
16+
filename,
17+
}
18+
try {
19+
return babelTransform(input, babelConfig).code;
20+
} catch (ex) {
21+
// Parsing stack is extremely long and not very useful, so just rethrow the message.
22+
throw new Error(ex.message);
23+
}
24+
}
25+
26+
export function ReactNET_transform_sourcemap(input, babelConfig, filename) {
27+
babelConfig = {
28+
...JSON.parse(babelConfig),
29+
ast: false,
30+
filename,
31+
sourceMaps: true,
32+
};
33+
try {
34+
var result = babelTransform(input, babelConfig);
35+
return JSON.stringify({
36+
babelVersion,
37+
code: result.code,
38+
sourceMap: result.map
39+
});
40+
} catch (ex) {
41+
// Parsing stack is extremely long and not very useful, so just rethrow the message.
42+
throw new Error(ex.message);
43+
}
44+
}

src/React.Core/Resources/shims.js

-27
Original file line numberDiff line numberDiff line change
@@ -64,31 +64,4 @@ function ReactNET_initReact() {
6464
}
6565
// :'(
6666
return false;
67-
}
68-
69-
function ReactNET_transform(input, babelConfig, filename) {
70-
babelConfig = JSON.parse(babelConfig);
71-
babelConfig.filename = filename;
72-
try {
73-
return global.babel.transform(input, babelConfig).code;
74-
} catch (ex) {
75-
// Parsing stack is extremely long and not very useful, so just rethrow the message.
76-
throw new Error(ex.message);
77-
}
78-
}
79-
80-
function ReactNET_transform_sourcemap(input, babelConfig, filename) {
81-
babelConfig = JSON.parse(babelConfig);
82-
babelConfig.filename = filename;
83-
babelConfig.sourceMap = true;
84-
try {
85-
var result = global.babel.transform(input, babelConfig);
86-
return JSON.stringify({
87-
code: result.code,
88-
sourceMap: result.map
89-
});
90-
} catch (ex) {
91-
// Parsing stack is extremely long and not very useful, so just rethrow the message.
92-
throw new Error(ex.message);
93-
}
9467
}

src/React.Core/gulpfile.js

+33-13
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@
88
*/
99

1010
var gulp = require('gulp');
11+
var named = require('vinyl-named');
1112
var webpack = require('webpack');
1213
var webpackStream = require('webpack-stream');
1314
var uglify = require('gulp-uglify');
1415

16+
var OUTPUT_DIR = 'Resources/';
17+
1518
gulp.task('default', ['build']);
16-
gulp.task('build', ['build-react-dev', 'build-react-prod']);
19+
gulp.task('build', ['build-react-dev', 'build-deps-prod']);
1720

1821
gulp.task('build-react-dev', function() {
1922
return gulp.src('Resources/react.js')
@@ -23,27 +26,44 @@ gulp.task('build-react-dev', function() {
2326
libraryTarget: 'this'
2427
},
2528
plugins: [
26-
new webpack.DefinePlugin({
27-
'process.env.NODE_ENV': '"development"'
28-
})
29+
new webpack.DefinePlugin({
30+
'process.env.NODE_ENV': '"development"'
31+
}),
32+
new webpack.optimize.OccurenceOrderPlugin(),
33+
new webpack.optimize.DedupePlugin()
2934
]
3035
}))
31-
.pipe(gulp.dest('Resources/'));
36+
.pipe(gulp.dest(OUTPUT_DIR));
3237
});
3338

34-
gulp.task('build-react-prod', function () {
35-
return gulp.src('Resources/react.js')
39+
gulp.task('build-deps-prod', function () {
40+
return gulp.src(['Resources/react.js', 'Resources/babel.js'])
41+
.pipe(named())
3642
.pipe(webpackStream({
43+
module: {
44+
loaders: [
45+
{
46+
exclude: /node_modules/,
47+
test: /\.js$/,
48+
loader: 'babel',
49+
query: {
50+
presets: ['es2015', 'stage-0']
51+
}
52+
},
53+
]
54+
},
3755
output: {
38-
filename: 'react.generated.min.js',
56+
filename: '[name].generated.min.js',
3957
libraryTarget: 'this'
4058
},
4159
plugins: [
42-
new webpack.DefinePlugin({
43-
'process.env.NODE_ENV': '"production"'
44-
})
60+
new webpack.DefinePlugin({
61+
'process.env.NODE_ENV': '"production"'
62+
}),
63+
new webpack.optimize.OccurenceOrderPlugin(),
64+
new webpack.optimize.DedupePlugin()
4565
]
4666
}))
4767
.pipe(uglify())
48-
.pipe(gulp.dest('Resources/'));
49-
});
68+
.pipe(gulp.dest(OUTPUT_DIR));
69+
});

src/React.Core/package.json

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@
33
"name": "React.Core",
44
"private": true,
55
"devDependencies": {
6-
"babel-core": "^5.8.25",
6+
"babel-core": "^6.3.13",
7+
"babel-loader": "^6.2.0",
8+
"babel-preset-es2015": "^6.3.13",
9+
"babel-preset-stage-0": "^6.3.13",
10+
"babel-standalone": "^1.0.0",
711
"gulp": "^3.9.0",
812
"gulp-uglify": "^1.5.1",
13+
"json-loader": "^0.5.4",
914
"react": "^0.14.3",
15+
"vinyl-named": "^1.1.0",
1016
"webpack": "^1.12.9",
11-
"webpack-stream": "^2.2.0"
17+
"webpack-stream": "^2.3.0"
1218
}
1319
}

src/React.Sample.Mvc6/project.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"webroot": "wwwroot",
3-
"version": "2.1.3-*",
3+
"version": "2.2.0-*",
44
"dependencies": {
55
"Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
66
"Microsoft.AspNet.Diagnostics": "1.0.0-rc1-final",

0 commit comments

Comments
 (0)