Skip to content
Closed
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
18 changes: 18 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,23 @@ module.exports = {
'import/no-extraneous-dependencies': 'off',
},
},
{
files: ['packages/**/*.js'],
rules: {
'import/no-commonjs': 'error',
},
},
{
files: [
'packages/metro/src/integration_tests/**/*.js',
'packages/metro-runtime/**/*.js',
'**/__tests__/**/*.js',
'**/__mocks__/**/*.js',
'**/__fixtures__/**/*.js',
],
rules: {
'import/no-commonjs': 'off',
},
},
],
};
6 changes: 1 addition & 5 deletions packages/buck-worker-tool/src/CommandFailedError.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,15 @@
* @oncall react_native
*/

'use strict';

/**
* Thrown to indicate the command failed and already output relevant error
* information on the console.
*/
class CommandFailedError extends Error {
export default class CommandFailedError extends Error {
constructor() {
super(
'The Buck worker-tool command failed. Diagnostics should have ' +
'been printed on the standard error output.',
);
}
}

module.exports = CommandFailedError;
14 changes: 4 additions & 10 deletions packages/buck-worker-tool/src/profiling.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
* @oncall react_native
*/

'use strict';

const fs = require('fs');
import fs from 'fs';

let currentInspectorSession;
let isProfiling = false;
Expand All @@ -20,13 +18,14 @@ function getInspectorSession() {
if (currentInspectorSession) {
return currentInspectorSession;
}
// eslint-disable-next-line import/no-commonjs
const inspector = require('inspector');
currentInspectorSession = new inspector.Session();
currentInspectorSession.connect();
return currentInspectorSession;
}

async function startProfiling() {
export async function startProfiling() {
if (isProfiling) {
return;
}
Expand All @@ -37,7 +36,7 @@ async function startProfiling() {
isProfiling = true;
}

async function stopProfilingAndWrite(workerName: ?string) {
export async function stopProfilingAndWrite(workerName: ?string) {
if (!isProfiling) {
return;
}
Expand All @@ -56,8 +55,3 @@ async function stopProfilingAndWrite(workerName: ?string) {
);
isProfiling = false;
}

module.exports = {
stopProfilingAndWrite,
startProfiling,
};
2 changes: 2 additions & 0 deletions packages/buck-worker-tool/src/third-party/JSONStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* LICENSE file in the root directory of this source tree.
*/

/* eslint-disable import/no-commonjs */

/**
* Copyright (c) 2011 Dominic Tarr.
* Based on the JSONStream package: https://github.com/dominictarr/JSONStream
Expand Down
16 changes: 7 additions & 9 deletions packages/buck-worker-tool/src/worker-tool.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@
* @oncall react_native
*/

'use strict';

import type {Writable} from 'stream';

const {startProfiling, stopProfilingAndWrite} = require('./profiling');
const JSONStream = require('./third-party/JSONStream');
const {Console} = require('console');
const duplexer = require('duplexer');
const fs = require('fs');
const invariant = require('invariant');
import {startProfiling, stopProfilingAndWrite} from './profiling';
import JSONStream from './third-party/JSONStream';
import {Console} from 'console';
import duplexer from 'duplexer';
import fs from 'fs';
import invariant from 'invariant';

export type Command = (
argv: Array<string>,
Expand Down Expand Up @@ -294,4 +292,4 @@ const success = (id: number) => ({
exit_code: 0 as const,
});

module.exports = {buckWorker};
export {buckWorker};
2 changes: 2 additions & 0 deletions packages/metro-babel-register/src/babel-register.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* @oncall react_native
*/

/* eslint-disable import/no-commonjs */

'use strict';

/*::
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
* @oncall react_native
*/

/* eslint-disable import/no-commonjs */

'use strict';

// Replace `import thing = require('thing')` with `const thing = require('thing')` which allows us to keep CJS semantics
Expand Down
27 changes: 17 additions & 10 deletions packages/metro-babel-transformer/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
* @oncall react_native
*/

'use strict';

import type {BabelCoreOptions, BabelFileMetadata} from '@babel/core';

const {parseSync, transformFromAstSync} = require('@babel/core');
const nullthrows = require('nullthrows');
import {parseSync, transformFromAstSync} from '@babel/core';
import nullthrows from 'nullthrows';

export type CustomTransformOptions = {
[string]: mixed,
Expand Down Expand Up @@ -81,7 +79,12 @@ export type BabelTransformer = $ReadOnly<{
getCacheKey?: () => string,
}>;

function transform({filename, options, plugins, src}: BabelTransformerArgs) {
function transform({
filename,
options,
plugins,
src,
}: BabelTransformerArgs): ReturnType<BabelTransformer['transform']> {
const OLD_BABEL_ENV = process.env.BABEL_ENV;
process.env.BABEL_ENV = options.dev
? 'development'
Expand All @@ -105,15 +108,16 @@ function transform({filename, options, plugins, src}: BabelTransformerArgs) {
// You get this behavior by default when using Babel's `transform` method directly.
cloneInputAst: false,
};
const sourceAst: BabelNodeFile = options.hermesParser
? // $FlowFixMe[incompatible-exact]
const sourceAst = options.hermesParser
? // eslint-disable-next-line import/no-commonjs
require('hermes-parser').parse(src, {
babel: true,
sourceType: babelConfig.sourceType,
})
: parseSync(src, babelConfig);

const transformResult = transformFromAstSync<MetroBabelFileMetadata>(
// $FlowFixMe[incompatible-call] BabelFile vs BabelNodeFile
sourceAst,
src,
babelConfig,
Expand All @@ -130,6 +134,9 @@ function transform({filename, options, plugins, src}: BabelTransformerArgs) {
}
}

module.exports = ({
transform,
}: BabelTransformer);
// Type check exports
/*::
({transform}) as BabelTransformer;
*/

export {transform};
10 changes: 3 additions & 7 deletions packages/metro-cache-key/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,14 @@
* @oncall react_native
*/

'use strict';
import crypto from 'crypto';
import fs from 'fs';

const crypto = require('crypto');
const fs = require('fs');

function getCacheKey(files: Array<string>): string {
export function getCacheKey(files: Array<string>): string {
return files
.reduce(
(hash, file) => hash.update('\0', 'utf8').update(fs.readFileSync(file)),
crypto.createHash('md5'),
)
.digest('hex');
}

module.exports = {getCacheKey};
8 changes: 2 additions & 6 deletions packages/metro-cache/src/Cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
* @oncall react_native
*/

'use strict';

import type {CacheStore} from 'metro-cache';

const {Logger} = require('metro-core');
import {Logger} from 'metro-core';

/**
* Main cache class. Receives an array of cache instances, and sequentially
Expand All @@ -22,7 +20,7 @@ const {Logger} = require('metro-core');
*
* All get/set operations are logged via Metro's logger.
*/
class Cache<T> {
export default class Cache<T> {
_stores: $ReadOnlyArray<CacheStore<T>>;

_hits: WeakMap<Buffer, CacheStore<T>>;
Expand Down Expand Up @@ -138,5 +136,3 @@ class Cache<T> {
return this._stores.length === 0;
}
}

module.exports = Cache;
2 changes: 1 addition & 1 deletion packages/metro-cache/src/__tests__/Cache-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('Cache', () => {

beforeEach(() => {
Logger = require('metro-core').Logger;
Cache = require('../Cache');
Cache = require('../Cache').default;

Logger.on('log', item => {
log.push({
Expand Down
4 changes: 1 addition & 3 deletions packages/metro-cache/src/__tests__/stableHash-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
* @oncall react_native
*/

'use strict';

const stableHash = require('../stableHash');
import stableHash from '../stableHash';

describe('stableHash', () => {
test('ensures that the hash implementation supports switched order properties', () => {
Expand Down
29 changes: 14 additions & 15 deletions packages/metro-cache/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,22 @@
* @oncall react_native
*/

'use strict';

const Cache = require('./Cache');
const stableHash = require('./stableHash');
const AutoCleanFileStore = require('./stores/AutoCleanFileStore');
const FileStore = require('./stores/FileStore');
const HttpGetStore = require('./stores/HttpGetStore');
const HttpStore = require('./stores/HttpStore');
import Cache from './Cache';
import stableHash from './stableHash';
import AutoCleanFileStore from './stores/AutoCleanFileStore';
import FileStore from './stores/FileStore';
import HttpGetStore from './stores/HttpGetStore';
import HttpStore from './stores/HttpStore';

export type {Options as FileOptions} from './stores/FileStore';
export type {Options as HttpOptions} from './stores/HttpStore';
export type {CacheStore} from './types';

module.exports.AutoCleanFileStore = AutoCleanFileStore;
module.exports.Cache = Cache;
module.exports.FileStore = FileStore;
module.exports.HttpGetStore = HttpGetStore;
module.exports.HttpStore = HttpStore;

module.exports.stableHash = stableHash;
export {
AutoCleanFileStore,
Cache,
FileStore,
HttpGetStore,
HttpStore,
stableHash,
};
10 changes: 3 additions & 7 deletions packages/metro-cache/src/stableHash.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
* @oncall react_native
*/

'use strict';
import crypto from 'crypto';
import canonicalize from 'metro-core/private/canonicalize';

const crypto = require('crypto');
const canonicalize = require('metro-core/private/canonicalize');

function stableHash(value: mixed): Buffer {
export default function stableHash(value: mixed): Buffer {
return (
crypto
.createHash('md5')
Expand All @@ -25,5 +23,3 @@ function stableHash(value: mixed): Buffer {
.digest('buffer')
);
}

module.exports = stableHash;
12 changes: 4 additions & 8 deletions packages/metro-cache/src/stores/AutoCleanFileStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
* @flow
*/

'use strict';

import type {Options} from './FileStore';

const FileStore = require('./FileStore');
const fs = require('fs');
const path = require('path');
import FileStore from './FileStore';
import fs from 'fs';
import path from 'path';

type CleanOptions = {
...Options,
Expand Down Expand Up @@ -59,7 +57,7 @@ function get<T>(property: ?T, defaultValue: T): T {
/**
* A FileStore that cleans itself up in a given interval
*/
class AutoCleanFileStore<T> extends FileStore<T> {
export default class AutoCleanFileStore<T> extends FileStore<T> {
_intervalMs: number;
_cleanupThresholdMs: number;
_root: string;
Expand Down Expand Up @@ -103,5 +101,3 @@ class AutoCleanFileStore<T> extends FileStore<T> {
this._scheduleCleanup();
}
}

module.exports = AutoCleanFileStore;
10 changes: 3 additions & 7 deletions packages/metro-cache/src/stores/FileStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
* @flow
*/

'use strict';

const fs = require('fs');
const path = require('path');
import fs from 'fs';
import path from 'path';

const NULL_BYTE = 0x00;
const NULL_BYTE_BUFFER = Buffer.from([NULL_BYTE]);
Expand All @@ -20,7 +18,7 @@ export type Options = {
root: string,
};

class FileStore<T> {
export default class FileStore<T> {
_root: string;

constructor(options: Options) {
Expand Down Expand Up @@ -90,5 +88,3 @@ class FileStore<T> {
}
}
}

module.exports = FileStore;
Loading
Loading