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

Commit

Permalink
make source extensions configurable in rn-cli.config.js
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredru committed May 9, 2017
1 parent 113ebb3 commit b9f1e8b
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 15 deletions.
3 changes: 3 additions & 0 deletions local-cli/bundle/buildBundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const outputBundle = require('./output/bundle');
const path = require('path');
const saveAssets = require('./saveAssets');
const defaultAssetExts = require('../../packager/defaults').assetExts;
const defaultSourceExts = require('../../packager/defaults').sourceExts;
const defaultPlatforms = require('../../packager/defaults').platforms;
const defaultProvidesModuleNodeModules = require('../../packager/defaults').providesModuleNodeModules;

Expand Down Expand Up @@ -59,6 +60,7 @@ function buildBundle(
var shouldClosePackager = false;
if (!packagerInstance) {
const assetExts = (config.getAssetExts && config.getAssetExts()) || [];
const sourceExts = (config.getSourceExts && config.getSourceExts()) || [];
const platforms = (config.getPlatforms && config.getPlatforms()) || [];

const transformModulePath =
Expand All @@ -81,6 +83,7 @@ function buildBundle(
providesModuleNodeModules: providesModuleNodeModules,
resetCache: args.resetCache,
reporter: new TerminalReporter(),
sourceExts: defaultSourceExts.concat(sourceExts),
transformModulePath: transformModulePath,
watch: false,
};
Expand Down
3 changes: 3 additions & 0 deletions local-cli/core/default.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ const config: ConfigT = {
getAssetExts() {
return [];
},
getSourceExts() {
return [];
},
getPlatforms() {
return [];
},
Expand Down
8 changes: 8 additions & 0 deletions local-cli/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ export type ConfigT = {
* from here and use `require('./fonts/example.ttf')` inside your app.
*/
getAssetExts?: () => Array<string>,
/**
* Specify any additional source file extentions to be used by the packager.
* For example, if you want to include a .ts file, you would return ['ts']
* from here and use `require('./module/example')` to require the file with
* path 'module/example.ts' inside your app.
*/
getSourceExts?: () => Array<string>,
/**
* Specify any additional platforms to be used by the packager.
* For example, if you want to add a "custom" platform, and use modules
Expand All @@ -54,6 +61,7 @@ export type ConfigT = {
getBlacklistRE(): RegExp,
getProjectRoots(): Array<string>,
getAssetExts(): Array<string>,
getSourceExts(): Array<string>,
/**
* Returns an array of project commands used by the CLI to load
*/
Expand Down
2 changes: 2 additions & 0 deletions local-cli/server/runServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const connect = require('connect');
const copyToClipBoardMiddleware = require('./middleware/copyToClipBoardMiddleware');
const cpuProfilerMiddleware = require('./middleware/cpuProfilerMiddleware');
const defaultAssetExts = require('../../packager/defaults').assetExts;
const defaultSourceExts = require('../../packager/defaults').sourceExts;
const defaultPlatforms = require('../../packager/defaults').platforms;
const defaultProvidesModuleNodeModules = require('../../packager/defaults').providesModuleNodeModules;
const getDevToolsMiddleware = require('./middleware/getDevToolsMiddleware');
Expand Down Expand Up @@ -100,6 +101,7 @@ function getPackagerServer(args, config) {
providesModuleNodeModules: providesModuleNodeModules,
reporter: new TerminalReporter(),
resetCache: args.resetCache,
sourceExts: defaultSourceExts.concat(args.sourceExts),
transformModulePath: transformModulePath,
verbose: args.verbose,
watch: !args.nonPersistent,
Expand Down
5 changes: 5 additions & 0 deletions local-cli/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ module.exports = {
description: 'Specify any additional asset extentions to be used by the packager',
parse: (val) => val.split(','),
default: (config) => config.getAssetExts(),
}, {
command: '--sourceExts [list]',
description: 'Specify any additional source extentions to be used by the packager',
parse: (val) => val.split(','),
default: (config) => config.getSourceExts(),
}, {
command: '--platforms [list]',
description: 'Specify any additional platforms to be used by the packager',
Expand Down
2 changes: 2 additions & 0 deletions packager/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ exports.assetExts = [
'html', 'pdf', // Document formats
];

exports.sourceExts = ['js', 'json'];

exports.moduleSystem = require.resolve('./src/Resolver/polyfills/require.js');

exports.platforms = ['ios', 'android', 'windows', 'web'];
Expand Down
4 changes: 4 additions & 0 deletions packager/rn-cli.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ module.exports = {
return [];
},

getSourceExts() {
return [];
},

getBlacklistRE() {
return blacklist();
},
Expand Down
1 change: 1 addition & 0 deletions packager/src/Bundler/__tests__/Bundler-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var commonOptions = {
extraNodeModules: {},
platforms: defaults.platforms,
resetCache: false,
sourceExts: defaults.sourceExts,
watch: false,
};

Expand Down
2 changes: 2 additions & 0 deletions packager/src/Bundler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ type Options = {
providesModuleNodeModules?: Array<string>,
reporter: Reporter,
resetCache: boolean,
sourceExts: Array<string>,
transformModulePath?: string,
transformTimeoutInterval: ?number,
watch: boolean,
Expand Down Expand Up @@ -161,6 +162,7 @@ class Bundler {
providesModuleNodeModules: opts.providesModuleNodeModules,
reporter: opts.reporter,
resetCache: opts.resetCache,
sourceExts: opts.sourceExts,
transformCacheKey,
transformCode:
(module, code, transformCodeOptions) => this._transformer.transformFile(
Expand Down
5 changes: 4 additions & 1 deletion packager/src/ModuleGraph/node-haste/node-haste.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const defaults = require('../../../defaults');

type ResolveOptions = {|
assetExts: Extensions,
sourceExts: Extensions,
extraNodeModules: {[id: string]: string},
transformedFiles: {[path: Path]: TransformedFile},
|};
Expand All @@ -43,6 +44,7 @@ exports.createResolveFn = function(options: ResolveOptions): ResolveFn {
assetExts,
extraNodeModules,
transformedFiles,
sourceExts,
} = options;
const files = Object.keys(transformedFiles);
const getTransformedFile =
Expand All @@ -61,7 +63,7 @@ exports.createResolveFn = function(options: ResolveOptions): ResolveFn {
getTransformedFile,
);
const hasteMap = new HasteMap({
extensions: ['js', 'json'],
extensions: sourceExts,
files,
helpers,
moduleCache,
Expand All @@ -86,6 +88,7 @@ exports.createResolveFn = function(options: ResolveOptions): ResolveFn {
platform,
platforms,
preferNativePlatform: true,
sourceExts,
});
}

Expand Down
2 changes: 2 additions & 0 deletions packager/src/Resolver/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type Options = {
providesModuleNodeModules?: Array<string>,
reporter: Reporter,
resetCache: boolean,
sourceExts: Array<string>,
transformCacheKey: string,
transformCode: TransformCode,
watch?: boolean,
Expand Down Expand Up @@ -73,6 +74,7 @@ class Resolver {
reporter: opts.reporter,
resetCache: opts.resetCache,
roots: opts.projectRoots,
sourceExts: opts.sourceExts,
transformCacheKey: opts.transformCacheKey,
transformCode: opts.transformCode,
watch: opts.watch || false,
Expand Down
3 changes: 3 additions & 0 deletions packager/src/Server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type Options = {
reporter: Reporter,
resetCache?: boolean,
silent?: boolean,
sourceExts?: Array<string>,
transformModulePath?: string,
transformTimeoutInterval?: number,
watch?: boolean,
Expand Down Expand Up @@ -185,6 +186,7 @@ class Server {
reporter: Reporter,
resetCache: boolean,
silent: boolean,
sourceExts: Array<string>,
transformModulePath: ?string,
transformTimeoutInterval: ?number,
watch: boolean,
Expand Down Expand Up @@ -218,6 +220,7 @@ class Server {
reporter: options.reporter,
resetCache: options.resetCache || false,
silent: options.silent || false,
sourceExts: options.sourceExts || defaults.sourceExts,
transformModulePath: options.transformModulePath,
transformTimeoutInterval: options.transformTimeoutInterval,
watch: options.watch || false,
Expand Down
30 changes: 22 additions & 8 deletions packager/src/node-haste/DependencyGraph/ResolutionRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type Options = {
platform: string,
platforms: Set<string>,
preferNativePlatform: boolean,
sourceExts: Array<string>,
};

class ResolutionRequest {
Expand All @@ -55,6 +56,7 @@ class ResolutionRequest {
_platform: string;
_platforms: Set<string>;
_preferNativePlatform: boolean;
_sourceExts: Array<string>;
static emptyModule: string;

constructor({
Expand All @@ -68,6 +70,7 @@ class ResolutionRequest {
platform,
platforms,
preferNativePlatform,
sourceExts,
}: Options) {
this._dirExists = dirExists;
this._entryPath = entryPath;
Expand All @@ -79,6 +82,7 @@ class ResolutionRequest {
this._platform = platform;
this._platforms = platforms;
this._preferNativePlatform = preferNativePlatform;
this._sourceExts = sourceExts;
this._resetResolutionCache();
}

Expand Down Expand Up @@ -417,17 +421,27 @@ class ResolutionRequest {
let file;
if (this._hasteFS.exists(potentialModulePath)) {
file = potentialModulePath;
} else if (this._platform != null &&
this._hasteFS.exists(potentialModulePath + '.' + this._platform + '.js')) {
file = potentialModulePath + '.' + this._platform + '.js';
} else if (this._preferNativePlatform &&
this._hasteFS.exists(potentialModulePath + '.native.js')) {
file = potentialModulePath + '.native.js';
} else if (this._hasteFS.exists(potentialModulePath + '.js')) {
file = potentialModulePath + '.js';
} else if (this._hasteFS.exists(potentialModulePath + '.json')) {
file = potentialModulePath + '.json';
} else {
for (let i = 0; i < this._sourceExts.length; i++) {
const ext = this._sourceExts[i];
if (this._platform != null &&
this._hasteFS.exists(potentialModulePath + '.' + this._platform + '.' + ext)) {
file = potentialModulePath + '.' + this._platform + '.' + ext;
break;
} else if (this._preferNativePlatform &&
this._hasteFS.exists(potentialModulePath + '.native.' + ext)) {
file = potentialModulePath + '.native.' + ext;
break;
} else if (this._hasteFS.exists(potentialModulePath + '.' + ext)) {
file = potentialModulePath + '.' + ext;
break;
}
}
}

if (file === undefined) {
throw new UnableToResolveError(
fromModule,
toModule,
Expand Down
13 changes: 7 additions & 6 deletions packager/src/node-haste/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ const ERROR_BUILDING_DEP_GRAPH = 'DependencyGraphError';
class DependencyGraph {
_opts: {|
assetExts: Array<string>,
extensions: Array<string>,
extraNodeModules: ?Object,
forceNodeFilesystemAPI: boolean,
globalTransformCache: ?GlobalTransformCache,
Expand All @@ -65,6 +64,7 @@ class DependencyGraph {
resetCache: boolean,
roots: Array<string>,
shouldThrowOnUnresolvedErrors: () => boolean,
sourceExts: Array<string>,
transformCacheKey: string,
transformCode: TransformCode,
useWatchman: boolean,
Expand All @@ -86,7 +86,6 @@ class DependencyGraph {
assetDependencies,
assetExts,
cache,
extensions,
extraNodeModules,
forceNodeFilesystemAPI,
globalTransformCache,
Expand All @@ -100,6 +99,7 @@ class DependencyGraph {
resetCache,
roots,
shouldThrowOnUnresolvedErrors = () => true,
sourceExts,
transformCacheKey,
transformCode,
useWatchman,
Expand All @@ -109,7 +109,6 @@ class DependencyGraph {
assetDependencies: Array<string>,
assetExts: Array<string>,
cache: Cache,
extensions?: ?Array<string>,
extraNodeModules: ?Object,
forceNodeFilesystemAPI?: boolean,
globalTransformCache: ?GlobalTransformCache,
Expand All @@ -123,6 +122,7 @@ class DependencyGraph {
resetCache: boolean,
roots: Array<string>,
shouldThrowOnUnresolvedErrors?: () => boolean,
sourceExts: Array<string>,
transformCacheKey: string,
transformCode: TransformCode,
useWatchman?: ?boolean,
Expand All @@ -131,7 +131,6 @@ class DependencyGraph {
}) {
this._opts = {
assetExts: assetExts || [],
extensions: extensions || ['js', 'json'],
extraNodeModules,
forceNodeFilesystemAPI: !!forceNodeFilesystemAPI,
globalTransformCache,
Expand All @@ -147,6 +146,7 @@ class DependencyGraph {
resetCache,
roots,
shouldThrowOnUnresolvedErrors,
sourceExts,
transformCacheKey,
transformCode,
useWatchman: useWatchman !== false,
Expand All @@ -167,7 +167,7 @@ class DependencyGraph {

const mw = this._opts.maxWorkers;
this._haste = new JestHasteMap({
extensions: this._opts.extensions.concat(this._opts.assetExts),
extensions: this._opts.sourceExts.concat(this._opts.assetExts),
forceNodeFilesystemAPI: this._opts.forceNodeFilesystemAPI,
ignorePattern: {test: this._opts.ignoreFilePath},
maxWorkers: typeof mw === 'number' && mw >= 1 ? mw : getMaxWorkers(),
Expand Down Expand Up @@ -213,7 +213,7 @@ class DependencyGraph {

this._hasteMap = new HasteMap({
files: hasteFSFiles,
extensions: this._opts.extensions,
extensions: this._opts.sourceExts,
moduleCache: this._moduleCache,
preferNativePlatform: this._opts.preferNativePlatform,
helpers: this._helpers,
Expand Down Expand Up @@ -310,6 +310,7 @@ class DependencyGraph {
platform,
platforms: this._opts.platforms,
preferNativePlatform: this._opts.preferNativePlatform,
sourceExts: this._opts.sourceExts,
});

const response = new ResolutionResponse({transformOptions});
Expand Down

0 comments on commit b9f1e8b

Please sign in to comment.