Skip to content

Commit

Permalink
Add prettier (#409)
Browse files Browse the repository at this point in the history
* Add prettier

* Add test/helpers
  • Loading branch information
danez committed Apr 21, 2017
1 parent dbec80d commit 2204871
Show file tree
Hide file tree
Showing 16 changed files with 494 additions and 211 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"
}
}
29 changes: 28 additions & 1 deletion 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": "^1.2.2",
"react": "^15.1.0",
"react-intl": "^2.1.2",
"react-intl-webpack-plugin": "^0.0.3",
Expand All @@ -41,9 +44,11 @@
"scripts": {
"clean": "rimraf lib/",
"build": "babel src/ --out-dir lib/",
"format": "prettier --write --trailing-comma all \"src/**/*.js\" \"test/*.test.js\" \"test/helpers/*.js\" && prettier --write --trailing-comma es5 \"scripts/*.js\"",
"lint": "eslint src test",
"preversion": "yarn run test",
"precommit": "lint-staged",
"prepublish": "yarn run clean && yarn run build",
"preversion": "yarn run test",
"test": "yarn run lint && cross-env BABEL_ENV=test yarn run build && yarn run test-only",
"test-only": "nyc ava"
},
Expand Down Expand Up @@ -90,5 +95,27 @@
"src/**/*.js"
],
"babel": "inherit"
},
"lint-staged": {
"scripts/*.js": [
"prettier --trailing-comma es5 --write",
"git add"
],
"src/**/*.js": [
"prettier --trailing-comma all --write",
"git add"
],
"test/**/*.test.js": [
"prettier --trailing-comma all --write",
"git add"
],
"test/helpers/*.js": [
"prettier --trailing-comma all --write ",
"git add"
],
"package.json": [
"node ./scripts/yarn-install.js",
"git add yarn.lock"
]
}
}
13 changes: 13 additions & 0 deletions scripts/yarn-install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"use strict";

const exec = require("child_process").exec;

const runIfYarn = fn => {
exec("yarn -V", error => {
if (error === null) fn();
});
};
runIfYarn(() => {
console.log("`package.json` was changed. Running yarn...🐈");
exec("yarn");
});
28 changes: 17 additions & 11 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); }
let result = {};

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,16 @@ 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 +124,10 @@ 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 +176,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 @@ -59,7 +59,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 @@ -97,7 +102,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 @@ -110,10 +117,13 @@ module.exports = function(source, inputSourceMap) {
cacheIdentifier: JSON.stringify({
"babel-loader": pkg.version,
"babel-core": babel.version,
babelrc: exists(loaderOptions.babelrc) ?
read(loaderOptions.babelrc) :
resolveRc(path.dirname(filename)),
env: loaderOptions.forceEnv || process.env.BABEL_ENV || process.env.NODE_ENV || "development",
babelrc: exists(loaderOptions.babelrc)
? read(loaderOptions.babelrc)
: resolveRc(path.dirname(filename)),
env: loaderOptions.forceEnv ||
process.env.BABEL_ENV ||
process.env.NODE_ENV ||
"development",
}),
};

Expand All @@ -124,10 +134,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 @@ -140,24 +147,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
7 changes: 4 additions & 3 deletions src/utils/exists.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ module.exports = function(cache) {
cache = cache || {};

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

if (!filename) { return false; }

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

return cache[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 2204871

Please sign in to comment.