Skip to content

Commit

Permalink
Add prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
danez committed Mar 6, 2017
1 parent ca3318b commit f919f60
Show file tree
Hide file tree
Showing 17 changed files with 522 additions and 184 deletions.
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
},
"extends": [
"eslint-config-babel"
]
],
"rules": {
"arrow-parens": "off"
}
}
19 changes: 17 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@
"eslint": "^3.8.1",
"eslint-config-babel": "^6.0.0",
"eslint-plugin-flowtype": "^2.25.0",
"husky": "^0.13.2",
"lint-staged": "^3.3.1",
"nyc": "^10.0.0",
"prettier": "^0.21.0",
"react": "^15.1.0",
"react-intl": "^2.1.2",
"react-intl-webpack-plugin": "^0.0.3",
Expand All @@ -43,8 +46,9 @@
"build": "babel src/ --out-dir lib/",
"coverage": "nyc report --reporter=json && codecov -f coverage/coverage-final.json",
"lint": "eslint src test",
"preversion": "npm test",
"precommit": "lint-staged",
"prepublish": "npm run clean && npm run build",
"preversion": "npm test",
"test": "npm run lint && cross-env BABEL_ENV=test npm run build && npm run test-only",
"test-ci": "cross-env BABEL_ENV=test npm run build && npm run test-only",
"test-only": "nyc ava"
Expand Down Expand Up @@ -88,5 +92,16 @@
"src/**/*.js"
],
"babel": "inherit"
}
},
"lint-staged": {
"src/**/*.js": [
"prettier --trailing-comma all --write ",
"git add"
],
"test/**/*.test.js": [
"prettier --trailing-comma all --write ",
"git add"
]
},
"pre-commit": "lint-staged"
}
28 changes: 18 additions & 10 deletions src/fs-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const os = require("os");
const path = require("path");
const zlib = require("zlib");

let defaultCacheDirectory = null; // Lazily instantiated when needed
let defaultCacheDirectory = null; // Lazily instantiated when needed

/**
* Read the contents from the compressed file.
Expand All @@ -26,12 +26,12 @@ let defaultCacheDirectory = null; // Lazily instantiated when needed
*/
const read = function(filename, callback) {
return fs.readFile(filename, function(err, data) {
if (err) { return callback(err); }
if (err) return callback(err);

return zlib.gunzip(data, function(err, content) {
let result = {};

if (err) { return callback(err); }
if (err) return callback(err);

try {
result = JSON.parse(content);
Expand All @@ -44,7 +44,6 @@ const read = function(filename, callback) {
});
};


/**
* Write contents into a compressed file.
*
Expand All @@ -57,13 +56,12 @@ const write = function(filename, result, callback) {
const content = JSON.stringify(result);

return zlib.gzip(content, function(err, data) {
if (err) { return callback(err); }
if (err) return callback(err);

return fs.writeFile(filename, data, callback);
});
};


/**
* Build the filename for the cached file
*
Expand Down Expand Up @@ -97,12 +95,17 @@ const handleCache = function(directory, params, callback) {
const options = params.options || {};
const transform = params.transform;
const identifier = params.identifier;
const shouldFallback = typeof params.directory !== "string" && directory !== os.tmpdir();
const shouldFallback = typeof params.directory !== "string" &&
directory !== os.tmpdir();

// Make sure the directory exists.
mkdirp(directory, function(err) {
// Fallback to tmpdir if node_modules folder not writable
if (err) return shouldFallback ? handleCache(os.tmpdir(), params, callback) : callback(err);
if (err) {
return shouldFallback
? handleCache(os.tmpdir(), params, callback)
: callback(err);
}

const file = path.join(directory, filename(source, identifier, options));

Expand All @@ -122,7 +125,11 @@ const handleCache = function(directory, params, callback) {

return write(file, result, function(err) {
// Fallback to tmpdir if node_modules folder not writable
if (err) return shouldFallback ? handleCache(os.tmpdir(), params, callback) : callback(err);
if (err) {
return shouldFallback
? handleCache(os.tmpdir(), params, callback)
: callback(err);
}

callback(null, result);
});
Expand Down Expand Up @@ -171,7 +178,8 @@ module.exports = function(params, callback) {
directory = params.directory;
} else {
if (defaultCacheDirectory === null) {
defaultCacheDirectory = findCacheDir({ name: "babel-loader" }) || os.tmpdir();
defaultCacheDirectory = findCacheDir({ name: "babel-loader" }) ||
os.tmpdir();
}
directory = defaultCacheDirectory;
}
Expand Down
58 changes: 34 additions & 24 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ const transpile = function(source, options) {
hideStack = true;
}
throw new BabelLoaderError(
name, message, error.codeFrame, hideStack, error);
name,
message,
error.codeFrame,
hideStack,
error,
);
} else {
throw error;
}
Expand Down Expand Up @@ -90,7 +95,9 @@ function passMetadata(s, context, metadata) {

module.exports = function(source, inputSourceMap) {
// Handle filenames (#106)
const webpackRemainingChain = loaderUtils.getRemainingRequest(this).split("!");
const webpackRemainingChain = loaderUtils
.getRemainingRequest(this)
.split("!");
const filename = webpackRemainingChain[webpackRemainingChain.length - 1];

// Handle options
Expand All @@ -105,10 +112,13 @@ module.exports = function(source, inputSourceMap) {
cacheIdentifier: JSON.stringify({
"babel-loader": pkg.version,
"babel-core": babel.version,
babelrc: exists(userOptions.babelrc) ?
read(userOptions.babelrc) :
resolveRc(path.dirname(filename)),
env: userOptions.forceEnv || process.env.BABEL_ENV || process.env.NODE_ENV || "development",
babelrc: exists(userOptions.babelrc)
? read(userOptions.babelrc)
: resolveRc(path.dirname(filename)),
env: userOptions.forceEnv ||
process.env.BABEL_ENV ||
process.env.NODE_ENV ||
"development",
}),
};

Expand All @@ -119,10 +129,7 @@ module.exports = function(source, inputSourceMap) {
}

if (options.sourceFileName === undefined) {
options.sourceFileName = relative(
options.sourceRoot,
options.filename
);
options.sourceFileName = relative(options.sourceRoot, options.filename);
}

const cacheDirectory = options.cacheDirectory;
Expand All @@ -137,24 +144,27 @@ module.exports = function(source, inputSourceMap) {

if (cacheDirectory) {
const callback = this.async();
return cache({
directory: cacheDirectory,
identifier: cacheIdentifier,
source: source,
options: options,
transform: transpile,
}, (err, { code, map, metadata } = {}) => {
if (err) return callback(err);

metadataSubscribers.forEach((s) => passMetadata(s, this, metadata));

return callback(null, code, map);
});
return cache(
{
directory: cacheDirectory,
identifier: cacheIdentifier,
source: source,
options: options,
transform: transpile,
},
(err, { code, map, metadata } = {}) => {
if (err) return callback(err);

metadataSubscribers.forEach(s => passMetadata(s, this, metadata));

return callback(null, code, map);
},
);
}

const { code, map, metadata } = transpile(source, options);

metadataSubscribers.forEach((s) => passMetadata(s, this, metadata));
metadataSubscribers.forEach(s => passMetadata(s, this, metadata));

this.callback(null, code, map);
};
1 change: 0 additions & 1 deletion src/resolve-rc.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const find = function find(start, rel) {
// Reached root
return find(up, rel);
}

};

module.exports = function(loc, rel) {
Expand Down
3 changes: 1 addition & 2 deletions src/utils/exists.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ module.exports = function(cache) {
cache = cache || {};

return function(filename) {

if (!filename) { return false; }
if (!filename) return false;

cache[filename] = cache[filename] || fs.existsSync(filename);

Expand Down
1 change: 0 additions & 1 deletion src/utils/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ module.exports = function(cache) {
cache = cache || {};

return function(filename) {

if (!filename) {
throw new Error("filename must be a string");
}
Expand Down
Loading

0 comments on commit f919f60

Please sign in to comment.