Skip to content

Commit

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

* Add prettierignore

* Prettier all the files

* Remove console.log
  • Loading branch information
jamiebuilds authored and devongovett committed Dec 8, 2017
1 parent d2ba17e commit 548eef9
Show file tree
Hide file tree
Showing 57 changed files with 1,315 additions and 534 deletions.
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
src/builtins
test/integration
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"singleQuote": true,
"bracketSpacing": false,
}
52 changes: 39 additions & 13 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,67 @@ const version = require('../package.json').version;

program.version(version);

program.command('serve [input]')
program
.command('serve [input]')
.description('starts a development server')
.option('-p, --port <port>', 'set the port to serve on. defaults to 1234')
.option('-d, --out-dir <path>', 'set the output directory. defaults to "dist"')
.option('--public-url <url>', 'set the public URL to serve on. defaults to the same as the --out-dir option')
.option(
'-d, --out-dir <path>',
'set the output directory. defaults to "dist"'
)
.option(
'--public-url <url>',
'set the public URL to serve on. defaults to the same as the --out-dir option'
)
.option('--no-hmr', 'disable hot module replacement')
.option('--no-cache', 'disable the filesystem cache')
.action(bundle);

program.command('watch [input]')
program
.command('watch [input]')
.description('starts the bundler in watch mode')
.option('-d, --out-dir <path>', 'set the output directory. defaults to "dist"')
.option('--public-url <url>', 'set the public URL to serve on. defaults to the same as the --out-dir option')
.option(
'-d, --out-dir <path>',
'set the output directory. defaults to "dist"'
)
.option(
'--public-url <url>',
'set the public URL to serve on. defaults to the same as the --out-dir option'
)
.option('--no-hmr', 'disable hot module replacement')
.option('--no-cache', 'disable the filesystem cache')
.action(bundle);

program.command('build [input]')
program
.command('build [input]')
.description('bundles for production')
.option('-d, --out-dir <path>', 'set the output directory. defaults to "dist"')
.option('--public-url <url>', 'set the public URL to serve on. defaults to the same as the --out-dir option')
.option(
'-d, --out-dir <path>',
'set the output directory. defaults to "dist"'
)
.option(
'--public-url <url>',
'set the public URL to serve on. defaults to the same as the --out-dir option'
)
.option('--no-minify', 'disable minification')
.option('--no-cache', 'disable the filesystem cache')
.action(bundle);

program.command('help [command]')
program
.command('help [command]')
.description('display help information for a command')
.action(function (command) {
.action(function(command) {
let cmd = program.commands.find(c => c.name() === command) || program;
cmd.help();
});

program.on('--help', function () {
program.on('--help', function() {
console.log('');
console.log(' Run `' + chalk.bold('parcel help <command>') + '` for more information on specific commands');
console.log(
' Run `' +
chalk.bold('parcel help <command>') +
'` for more information on specific commands'
);
console.log('');
});

Expand Down
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,35 @@
"babel-cli": "^6.26.0",
"babel-preset-env": "^1.6.1",
"cross-env": "^5.1.1",
"husky": "^0.14.3",
"less": "^2.7.2",
"lint-staged": "^6.0.0",
"mocha": "^3.5.0",
"ncp": "^2.0.0",
"nib": "^1.1.2",
"node-sass": "^4.5.3",
"nyc": "^11.1.0",
"postcss-modules": "^0.8.0",
"posthtml-include": "^1.1.0",
"prettier": "^1.9.1",
"rimraf": "^2.6.1",
"stylus": "^0.54.5",
"typescript": "^2.6.2"
},
"scripts": {
"test": "cross-env NODE_ENV=test mocha",
"format": "prettier --write './{src,bin,test}/**/*.{js,json,md}'",
"build": "babel src -d lib",
"prepublish": "yarn build"
"prepublish": "yarn build",
"precommit": "lint-staged"
},
"bin": {
"parcel": "bin/cli.js"
},
"engines": {
"node": ">= 6.0.0"
},
"lint-staged": {
"*.{js,json,md}": ["prettier --write", "git add"]
}
}
26 changes: 18 additions & 8 deletions src/Asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ class Asset {
this.ast = null;
this.generated = null;
this.hash = null;
this.parentDeps = new Set;
this.dependencies = new Map;
this.depAssets = new Map;
this.parentDeps = new Set();
this.dependencies = new Map();
this.depAssets = new Map();
this.parentBundle = null;
this.bundles = new Set;
this.bundles = new Set();
}

async loadIfNeeded() {
Expand Down Expand Up @@ -71,9 +71,16 @@ class Asset {
from = this.name;
}

let resolved = path.resolve(path.dirname(from), url).replace(/[\?#].*$/, '');
this.addDependency('./' + path.relative(path.dirname(this.name), resolved), Object.assign({dynamic: true}, opts));
return this.options.parser.getAsset(resolved, this.package, this.options).generateBundleName();
let resolved = path
.resolve(path.dirname(from), url)
.replace(/[\?#].*$/, '');
this.addDependency(
'./' + path.relative(path.dirname(this.name), resolved),
Object.assign({dynamic: true}, opts)
);
return this.options.parser
.getAsset(resolved, this.package, this.options)
.generateBundleName();
}

mightHaveDependencies() {
Expand Down Expand Up @@ -139,7 +146,10 @@ class Asset {

generateBundleName(isMainAsset) {
// Resolve the main file of the package.json
let main = this.package && this.package.main ? path.resolve(path.dirname(this.package.pkgfile), this.package.main) : null;
let main =
this.package && this.package.main
? path.resolve(path.dirname(this.package.pkgfile), this.package.main)
: null;
let ext = '.' + this.type;

// If this asset is main file of the package, use the package name
Expand Down
21 changes: 14 additions & 7 deletions src/Bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ class Bundle {
this.name = name;
this.parentBundle = parent;
this.entryAsset = null;
this.assets = new Set;
this.childBundles = new Set;
this.siblingBundles = new Map;
this.assets = new Set();
this.childBundles = new Set();
this.siblingBundles = new Map();
}

addAsset(asset) {
Expand All @@ -35,7 +35,13 @@ class Bundle {
}

if (!this.siblingBundles.has(type)) {
let bundle = this.createChildBundle(type, Path.join(Path.dirname(this.name), Path.basename(this.name, Path.extname(this.name)) + '.' + type));
let bundle = this.createChildBundle(
type,
Path.join(
Path.dirname(this.name),
Path.basename(this.name, Path.extname(this.name)) + '.' + type
)
);
this.siblingBundles.set(type, bundle);
}

Expand All @@ -52,7 +58,7 @@ class Bundle {
return this.assets.size === 0;
}

async package(bundler, oldHashes, newHashes = new Map) {
async package(bundler, oldHashes, newHashes = new Map()) {
if (this.isEmpty) {
return newHashes;
}
Expand All @@ -79,7 +85,7 @@ class Bundle {

await packager.start();

let included = new Set;
let included = new Set();
for (let asset of this.assets) {
await this._addDeps(asset, packager, included);
}
Expand Down Expand Up @@ -128,7 +134,8 @@ class Bundle {
b = theirParents.pop();
}

if (a === b) { // One bundle descended from the other
if (a === b) {
// One bundle descended from the other
return a;
}

Expand Down
81 changes: 52 additions & 29 deletions src/Bundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,42 @@ class Bundler extends EventEmitter {

this.resolver = new Resolver(this.options);
this.parser = new Parser(this.options);
this.packagers = new PackagerRegistry;
this.packagers = new PackagerRegistry();
this.cache = this.options.cache ? new FSCache(this.options) : null;
this.logger = new Logger(this.options);
this.delegate = options.delegate || {};

this.pending = false;
this.loadedAssets = new Map;
this.loadedAssets = new Map();
this.farm = null;
this.watcher = null;
this.hmr = null;
this.bundleHashes = null;
this.errored = false;
this.buildQueue = new Set;
this.buildQueue = new Set();
this.rebuildTimeout = null;

this.loadPlugins();
}

normalizeOptions(options) {
const isProduction = options.production || process.env.NODE_ENV === 'production';
const publicURL = options.publicUrl || options.publicURL || '/' + Path.basename(options.outDir || 'dist');
const watch = typeof options.watch === 'boolean' ? options.watch : !isProduction;
const isProduction =
options.production || process.env.NODE_ENV === 'production';
const publicURL =
options.publicUrl ||
options.publicURL ||
'/' + Path.basename(options.outDir || 'dist');
const watch =
typeof options.watch === 'boolean' ? options.watch : !isProduction;
return {
outDir: Path.resolve(options.outDir || 'dist'),
publicURL: publicURL,
watch: watch,
cache: typeof options.cache === 'boolean' ? options.cache : true,
killWorkers: typeof options.killWorkers === 'boolean' ? options.killWorkers : true,
minify: typeof options.minify === 'boolean' ? options.minify : isProduction,
killWorkers:
typeof options.killWorkers === 'boolean' ? options.killWorkers : true,
minify:
typeof options.minify === 'boolean' ? options.minify : isProduction,
hmr: typeof options.hmr === 'boolean' ? options.hmr : watch,
logLevel: typeof options.logLevel === 'number' ? options.logLevel : 3
};
Expand Down Expand Up @@ -126,7 +133,10 @@ class Bundler extends EventEmitter {
let bundle = await this.buildQueuedAssets(isInitialBundle);

let buildTime = Date.now() - startTime;
let time = buildTime < 1000 ? `${buildTime}ms` : `${(buildTime / 1000).toFixed(2)}s`;
let time =
buildTime < 1000
? `${buildTime}ms`
: `${(buildTime / 1000).toFixed(2)}s`;
this.logger.status('✨', `Built in ${time}.`, 'green');

return bundle;
Expand Down Expand Up @@ -166,7 +176,7 @@ class Bundler extends EventEmitter {
}

if (this.options.hmr) {
this.hmr = new HMRServer;
this.hmr = new HMRServer();
this.options.hmrPort = await this.hmr.start();
}
}
Expand All @@ -187,7 +197,7 @@ class Bundler extends EventEmitter {

async buildQueuedAssets(isInitialBundle = false) {
// Consume the rebuild queue until it is empty.
let loadedAssets = new Set;
let loadedAssets = new Set();
while (this.buildQueue.size > 0) {
let promises = [];
for (let asset of this.buildQueue) {
Expand Down Expand Up @@ -280,7 +290,7 @@ class Bundler extends EventEmitter {
asset.processed = true;

// First try the cache, otherwise load and compile in the background
let processed = this.cache && await this.cache.read(asset.name);
let processed = this.cache && (await this.cache.read(asset.name));
if (!processed) {
processed = await this.farm.run(asset.name, asset.package, this.options);
if (this.cache) {
Expand All @@ -301,19 +311,21 @@ class Bundler extends EventEmitter {
}

// Process asset dependencies
await Promise.all(dependencies.map(async dep => {
let assetDep = await this.resolveDep(asset, dep);
if (dep.includedInParent) {
// This dependency is already included in the parent's generated output,
// so no need to load it. We map the name back to the parent asset so
// that changing it triggers a recompile of the parent.
this.loadedAssets.set(dep.name, asset);
} else {
asset.dependencies.set(dep.name, dep);
asset.depAssets.set(dep.name, assetDep);
await this.loadAsset(assetDep);
}
}));
await Promise.all(
dependencies.map(async dep => {
let assetDep = await this.resolveDep(asset, dep);
if (dep.includedInParent) {
// This dependency is already included in the parent's generated output,
// so no need to load it. We map the name back to the parent asset so
// that changing it triggers a recompile of the parent.
this.loadedAssets.set(dep.name, asset);
} else {
asset.dependencies.set(dep.name, dep);
asset.depAssets.set(dep.name, assetDep);
await this.loadAsset(assetDep);
}
})
);

this.buildQueue.delete(asset);
}
Expand All @@ -327,7 +339,10 @@ class Bundler extends EventEmitter {
// If the asset is already in a bundle, it is shared. Move it to the lowest common ancestor.
if (asset.parentBundle !== bundle) {
let commonBundle = bundle.findCommonAncestor(asset.parentBundle);
if (asset.parentBundle !== commonBundle && asset.parentBundle.type === commonBundle.type) {
if (
asset.parentBundle !== commonBundle &&
asset.parentBundle.type === commonBundle.type
) {
this.moveAssetToBundle(asset, commonBundle);
}
}
Expand All @@ -337,13 +352,19 @@ class Bundler extends EventEmitter {

// Create the root bundle if it doesn't exist
if (!bundle) {
bundle = new Bundle(asset.type, Path.join(this.options.outDir, asset.generateBundleName(true)));
bundle = new Bundle(
asset.type,
Path.join(this.options.outDir, asset.generateBundleName(true))
);
bundle.entryAsset = asset;
}

// Create a new bundle for dynamic imports
if (dep && dep.dynamic) {
bundle = bundle.createChildBundle(asset.type, Path.join(this.options.outDir, asset.generateBundleName()));
bundle = bundle.createChildBundle(
asset.type,
Path.join(this.options.outDir, asset.generateBundleName())
);
bundle.entryAsset = asset;
}

Expand Down Expand Up @@ -426,7 +447,9 @@ class Bundler extends EventEmitter {
}

serve(port = 1234) {
this.logger.persistent('Server running at ' + this.logger.chalk.cyan(`http://localhost:${port}`));
this.logger.persistent(
'Server running at ' + this.logger.chalk.cyan(`http://localhost:${port}`)
);
this.bundle();
return Server.serve(this, port);
}
Expand Down
Loading

0 comments on commit 548eef9

Please sign in to comment.