Skip to content

Commit

Permalink
Add typescript file compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
John Tran committed Sep 25, 2017
1 parent 9f682fb commit a12b931
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 65 deletions.
122 changes: 62 additions & 60 deletions gulpfile.js
Expand Up @@ -52,16 +52,18 @@ const babelOptions = require('./scripts/getBabelOptions')({
'relay-debugger-react-native-runtime',
signedsource: 'signedsource',
util: 'util',
yargs: 'yargs'
yargs: 'yargs',
typescript: 'typescript',
'tsconfig.json': 'tsconfig.json',
},
plugins: [
'transform-flow-strip-types',
['transform-runtime', {polyfill: false}]
['transform-runtime', { polyfill: false }],
],
postPlugins: [
'transform-async-to-generator',
'transform-es2015-modules-commonjs'
]
'transform-es2015-modules-commonjs',
],
});
const del = require('del');
const derequire = require('gulp-derequire');
Expand Down Expand Up @@ -92,7 +94,7 @@ const PRODUCTION_HEADER =
' * LICENSE file in the root directory of this source tree. An additional grant',
' * of patent rights can be found in the PATENTS file in the same directory.',
' *',
' */'
' */',
].join('\n') + '\n';

const buildDist = function(filename, opts, isProduction) {
Expand All @@ -105,32 +107,32 @@ const buildDist = function(filename, opts, isProduction) {
net: 'empty',
path: 'empty',
child_process: 'empty',
util: 'empty'
util: 'empty',
},
output: {
filename: filename,
libraryTarget: opts.libraryTarget,
library: opts.libraryName
library: opts.libraryName,
},
plugins: [
new webpackStream.webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(
isProduction ? 'production' : 'development'
)
isProduction ? 'production' : 'development',
),
}),
new webpackStream.webpack.optimize.OccurenceOrderPlugin(),
new webpackStream.webpack.optimize.DedupePlugin()
]
new webpackStream.webpack.optimize.DedupePlugin(),
],
};
if (isProduction && !opts.noMinify) {
webpackOpts.plugins.push(
new webpackStream.webpack.optimize.UglifyJsPlugin({
compress: {
hoist_vars: true,
screw_ie8: true,
warnings: false
}
})
warnings: false,
},
}),
);
}
return webpackStream(webpackOpts, null, function(err, stats) {
Expand All @@ -151,50 +153,50 @@ const builds = [
{
package: 'babel-plugin-relay',
exports: {
index: 'BabelPluginRelay.js'
index: 'BabelPluginRelay.js',
},
bundles: [
{
entry: 'BabelPluginRelay.js',
output: 'babel-plugin-relay',
libraryName: 'BabelPluginRelay',
libraryTarget: 'commonjs2',
target: 'node'
}
]
target: 'node',
},
],
},
{
package: 'react-relay',
exports: {
classic: 'ReactRelayClassicExports.js',
compat: 'ReactRelayCompatPublic.js',
index: 'ReactRelayPublic.js'
index: 'ReactRelayPublic.js',
},
bundles: [
{
entry: 'ReactRelayClassicExports.js',
output: 'react-relay-classic',
libraryName: 'ReactRelayClassic',
libraryTarget: 'umd'
libraryTarget: 'umd',
},
{
entry: 'ReactRelayCompatPublic.js',
output: 'react-relay-compat',
libraryName: 'ReactRelayCompat',
libraryTarget: 'umd'
libraryTarget: 'umd',
},
{
entry: 'ReactRelayPublic.js',
output: 'react-relay',
libraryName: 'ReactRelay',
libraryTarget: 'umd'
}
]
libraryTarget: 'umd',
},
],
},
{
package: 'relay-compiler',
exports: {
index: 'RelayCompilerPublic.js'
index: 'RelayCompilerPublic.js',
},
bundles: [
{
Expand All @@ -203,32 +205,32 @@ const builds = [
libraryName: 'RelayCompiler',
libraryTarget: 'commonjs2',
target: 'node',
noMinify: true // Note: uglify can't yet handle modern JS
}
noMinify: true, // Note: uglify can't yet handle modern JS
},
],
bins: [
{
entry: 'RelayCompilerBin.js',
output: 'relay-compiler',
libraryTarget: 'commonjs2',
target: 'node'
}
]
target: 'node',
},
],
},
{
package: 'relay-runtime',
exports: {
index: 'RelayRuntime.js'
index: 'RelayRuntime.js',
},
bundles: [
{
entry: 'RelayRuntime.js',
output: 'relay-runtime',
libraryName: 'RelayRuntime',
libraryTarget: 'umd'
}
]
}
libraryTarget: 'umd',
},
],
},
];

gulp.task('clean', function() {
Expand All @@ -245,12 +247,12 @@ gulp.task('modules', function() {
'*' + PACKAGES + '/react-relay/classic/util/*.js',
'*' + PACKAGES + '/relay-runtime/util/*.js',
'!' + PACKAGES + '/**/__tests__/**/*.js',
'!' + PACKAGES + '/**/__mocks__/**/*.js'
'!' + PACKAGES + '/**/__mocks__/**/*.js',
])
.pipe(babel(babelOptions))
.pipe(flatten())
.pipe(gulp.dest(path.join(DIST, build.package, 'lib')))
)
.pipe(gulp.dest(path.join(DIST, build.package, 'lib'))),
),
);
});

Expand All @@ -262,11 +264,11 @@ gulp.task('copy-files', function() {
'LICENSE',
'PATENTS',
'*' + PACKAGES + '/' + build.package + '/*',
'!' + PACKAGES + '/' + build.package + '/**/*.js'
'!' + PACKAGES + '/' + build.package + '/**/*.js',
])
.pipe(flatten())
.pipe(gulp.dest(path.join(DIST, build.package)))
)
.pipe(gulp.dest(path.join(DIST, build.package))),
),
);
});

Expand All @@ -276,9 +278,9 @@ gulp.task('exports', ['copy-files', 'modules'], function() {
fs.writeFileSync(
path.join(DIST, build.package, exportName + '.js'),
PRODUCTION_HEADER +
`\nmodule.exports = require('./lib/${build.exports[exportName]}');`
)
)
`\nmodule.exports = require('./lib/${build.exports[exportName]}');`,
),
),
);
});

Expand All @@ -293,10 +295,10 @@ gulp.task('bins', ['modules'], function() {
.pipe(buildDist(bin.output, bin, /* isProduction */ false))
.pipe(header(SCRIPT_HASHBANG + PRODUCTION_HEADER))
.pipe(chmod(0o755))
.pipe(gulp.dest(path.join(DIST, build.package, 'bin')))
)
)
)
.pipe(gulp.dest(path.join(DIST, build.package, 'bin'))),
),
),
),
);
});

Expand All @@ -311,15 +313,15 @@ gulp.task('bundles', ['modules'], function() {
buildDist(
bundle.output + '.js',
bundle,
/* isProduction */ false
)
/* isProduction */ false,
),
)
.pipe(derequire())
.pipe(header(DEVELOPMENT_HEADER))
.pipe(gulp.dest(path.join(DIST, build.package)))
)
)
)
.pipe(gulp.dest(path.join(DIST, build.package))),
),
),
),
);
});

Expand All @@ -334,14 +336,14 @@ gulp.task('bundles:min', ['modules'], function() {
buildDist(
bundle.output + '.min.js',
bundle,
/* isProduction */ true
)
/* isProduction */ true,
),
)
.pipe(header(PRODUCTION_HEADER))
.pipe(gulp.dest(path.join(DIST, build.package)))
)
)
)
.pipe(gulp.dest(path.join(DIST, build.package))),
),
),
),
);
});

Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -68,6 +68,7 @@
"relay-debugger-react-native-runtime": "0.0.10",
"run-sequence": "1.2.2",
"signedsource": "^1.0.0",
"typescript": "^2.5.2",
"webpack": "1.13.1",
"webpack-stream": "3.2.0",
"yargs": "^9.0.0"
Expand Down
18 changes: 13 additions & 5 deletions packages/relay-compiler/core/RelayJSModuleParser.js
Expand Up @@ -21,15 +21,23 @@ const fs = require('fs');
const invariant = require('invariant');
const path = require('path');

const {ASTCache} = require('../graphql-compiler/GraphQLCompilerPublic');
const { ASTCache } = require('../graphql-compiler/GraphQLCompilerPublic');

import type {File, FileFilter} from '../graphql-compiler/GraphQLCompilerPublic';
import type {DocumentNode} from 'graphql';
import type {
File,
FileFilter,
} from '../graphql-compiler/GraphQLCompilerPublic';
import type { DocumentNode } from 'graphql';

// Throws an error if parsing the file fails
function parseFile(baseDir: string, file: File): ?DocumentNode {
const text = fs.readFileSync(path.join(baseDir, file.relPath), 'utf8');
let text = fs.readFileSync(path.join(baseDir, file.relPath), 'utf8');

if (/^\.tsx?$/.test(path.extname(file.relPath))) {
const tsconfig = __non_webpack_require__(path.resolve('tsconfig.json'));
const ts = require('typescript');
text = ts.transpileModule(text, tsconfig).outputText;
}
invariant(
text.indexOf('graphql') >= 0,
'RelayJSModuleParser: Files should be filtered before passed to the ' +
Expand All @@ -42,7 +50,7 @@ function parseFile(baseDir: string, file: File): ?DocumentNode {
text,
baseDir,
file,
).forEach(({tag, template}) => {
).forEach(({ tag, template }) => {
if (!(tag === 'graphql' || tag === 'graphql.experimental')) {
throw new Error(
`Invalid tag ${tag} in ${file.relPath}. Expected graphql\`\`.`,
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Expand Up @@ -5020,6 +5020,10 @@ typedarray@^0.0.6:
version "0.0.6"
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"

typescript@^2.5.2:
version "2.5.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.5.2.tgz#038a95f7d9bbb420b1bf35ba31d4c5c1dd3ffe34"

ua-parser-js@^0.7.9:
version "0.7.14"
resolved "https://registry.yarnpkg.com/ua-parser-js/-/ua-parser-js-0.7.14.tgz#110d53fa4c3f326c121292bbeac904d2e03387ca"
Expand Down

0 comments on commit a12b931

Please sign in to comment.