Woongjae Lee
NHN Dooray - Frontend Team
2woongjae@gmail.com
function getNumWorkers() {
let cores;
try {
cores = require('physical-cpu-count');
} catch (err) {
cores = os.cpus().length;
}
return cores || 1;
}
class WorkerFarm extends Farm {
constructor(options) {
let opts = {
autoStart: true,
maxConcurrentWorkers: getNumWorkers()
};
super(opts, require.resolve('./worker'));
this.localWorker = this.promisifyWorker(require('./worker'));
this.remoteWorker = this.promisifyWorker(this.setup(['init', 'run']));
this.started = false;
this.init(options);
}
...
}
require('v8-compile-cache');
const Parser = require('./Parser');
let parser;
exports.init = function(options, callback) {
parser = new Parser(options || {});
Object.assign(process.env, options.env || {});
callback();
};
exports.run = async function(path, pkg, options, callback) {
try {
var asset = parser.getAsset(path, pkg, options);
await asset.process();
callback(null, {
dependencies: Array.from(asset.dependencies.values()),
generated: asset.generated,
hash: asset.hash,
cacheData: asset.cacheData
});
} catch (err) {
let returned = err;
if (asset) {
returned = asset.generateErrorMessage(returned);
}
returned.fileName = path;
callback(returned);
}
};
const fs = require('./utils/fs');
const path = require('path');
const md5 = require('./utils/md5');
const objectHash = require('./utils/objectHash');
const pkg = require('../package.json');
// These keys can affect the output, so if they differ, the cache should not match
const OPTION_KEYS = ['publicURL', 'minify', 'hmr'];
class FSCache {
constructor(options) {
this.dir = path.resolve(options.cacheDir || '.cache');
this.dirExists = false;
this.invalidated = new Set();
this.optionsHash = objectHash(
OPTION_KEYS.reduce((p, k) => ((p[k] = options[k]), p), {
version: pkg.version
})
);
}
async ensureDirExists() {
await fs.mkdirp(this.dir);
this.dirExists = true;
}
getCacheFile(filename) {
let hash = md5(this.optionsHash + filename);
return path.join(this.dir, hash + '.json');
}
async writeDepMtimes(data) {
// Write mtimes for each dependent file that is already compiled into this asset
for (let dep of data.dependencies) {
if (dep.includedInParent) {
let stats = await fs.stat(dep.name);
dep.mtime = stats.mtime.getTime();
}
}
}
async write(filename, data) {
try {
await this.ensureDirExists();
await this.writeDepMtimes(data);
await fs.writeFile(this.getCacheFile(filename), JSON.stringify(data));
this.invalidated.delete(filename);
} catch (err) {
console.error('Error writing to cache', err);
}
}
async checkDepMtimes(data) {
// Check mtimes for files that are already compiled into this asset
// If any of them changed, invalidate.
for (let dep of data.dependencies) {
if (dep.includedInParent) {
let stats = await fs.stat(dep.name);
if (stats.mtime > dep.mtime) {
return false;
}
}
}
return true;
}
async read(filename) {
if (this.invalidated.has(filename)) {
return null;
}
let cacheFile = this.getCacheFile(filename);
try {
let stats = await fs.stat(filename);
let cacheStats = await fs.stat(cacheFile);
if (stats.mtime > cacheStats.mtime) {
return null;
}
let json = await fs.readFile(cacheFile);
let data = JSON.parse(json);
if (!await this.checkDepMtimes(data)) {
return null;
}
return data;
} catch (err) {
return null;
}
}
invalidate(filename) {
this.invalidated.add(filename);
}
async delete(filename) {
try {
await fs.unlink(this.getCacheFile(filename));
this.invalidated.delete(filename);
} catch (err) {
// Fail silently
}
}
}
module.exports = FSCache;
{
"name": "parcel-react-js",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "parcel src/index.html",
"build": "parcel build src/index.html --public-url '/'",
"serve": "serve -s dist"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"babel-preset-react-app": "^6.24.1",
"parcel-bundler": "^1.4.1",
"react": "^16.2.0",
"react-dom": "^16.2.0"
},
"dependencies": {
"serve": "^6.4.4"
}
}
{
"presets": ["react-app"]
}
"dependencies": {
"babel-plugin-syntax-jsx": "^6.3.13",
"babel-plugin-transform-react-display-name": "^6.23.0",
"babel-plugin-transform-react-jsx": "^6.24.1",
"babel-plugin-transform-react-jsx-self": "^6.22.0",
"babel-plugin-transform-react-jsx-source": "^6.22.0",
"babel-preset-flow": "^6.23.0"
}
"dependencies": {
"babel-plugin-dynamic-import-node": "1.1.0",
"babel-plugin-syntax-dynamic-import": "6.18.0",
"babel-plugin-transform-class-properties": "6.24.1",
"babel-plugin-transform-object-rest-spread": "6.26.0",
"babel-plugin-transform-react-constant-elements": "6.23.0",
"babel-plugin-transform-react-jsx": "6.24.1",
"babel-plugin-transform-react-jsx-self": "6.22.0",
"babel-plugin-transform-react-jsx-source": "6.22.0",
"babel-plugin-transform-regenerator": "6.26.0",
"babel-plugin-transform-runtime": "6.23.0",
"babel-preset-env": "1.6.1",
"babel-preset-react": "6.24.1"
}
{
"name": "parcel-react-ts",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "parcel src/index.html",
"build": "parcel build src/index.html --public-url '/'",
"serve": "serve -s dist"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/react": "^16.0.34",
"@types/react-dom": "^16.0.3",
"parcel-bundler": "^1.4.1",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"typescript": "^2.6.2"
},
"dependencies": {
"serve": "^6.4.4"
}
}
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"jsx": "react",
"strict": true
}
}
By Woongjae Lee
μ½λλ²μ€νΉ νμΊμ€νΈ 1ν