Skip to content

Commit

Permalink
#221 - Switch to async/await and major Lasso refactors (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
austinkelleher authored Sep 22, 2017
1 parent 9c66d2a commit 7a2df9a
Show file tree
Hide file tree
Showing 208 changed files with 3,113 additions and 3,996 deletions.
11 changes: 11 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "standard",
"rules": {
"indent": ["error", 4],
"semi": ["error", "always"],
"space-before-function-paren": ["off"],
"no-path-concat": ["off"],
"no-useless-escape": ["off"],
"no-eval": ["off"]
}
}
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
.DS_Store
coverage
/test/build
/test/static
*.actual.js
*.actual.html
.lasso-cache
.cache
package-lock.json
~*
~*
.nyc_output/
29 changes: 0 additions & 29 deletions .jshintrc

This file was deleted.

3 changes: 0 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
node_js:
- "4"
- "5"
- "6"
- "8"
sudo: false
language: node_js
Expand Down
68 changes: 26 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1753,6 +1753,10 @@ Each of these dependencies is described in the next few sections. However, it is
If you would like to introduce your own custom dependency types then you will need to have your plugin register a dependency handler. This is illustrated in the following sample code:

```javascript
const { promisify } = require('util');
const fs = require('fs');
const readFileAsync = promisify(fs.readFile);

module.exports = function myPlugin(lasso, config) {
lasso.dependencies.registerJavaScriptType(
'my-custom-type',
Expand All @@ -1763,28 +1767,19 @@ module.exports = function myPlugin(lasso, config) {
},

// Validation checks and initialization based on properties:
init: function(context, callback) {
async init (context) {
if (!this.path) {
return callback(new Error('"path" is required'));
throw new Error('"path" is required');
}

// NOTE: resolvePath can be used to resolve a provided relative path to a full path
this.path = this.resolvePath(this.path);
callback();
},

// Read the resource:
read: function(context, callback) {
var path = this.path;

fs.readFile(path, {encoding: 'utf8'}, function(err, src) {
if (err) {
return callback(err);
}

myCompiler.compile(src, callback);
});

async read (context) {
const src = await readFileAsync(this.path, {encoding: 'utf8'});
return myCompiler.compile(src);
// NOTE: A stream can also be returned
},

Expand Down Expand Up @@ -1842,51 +1837,40 @@ The `handler` argument for a CSS dependency has the exact same interface as a ha
A custom package dependency can be used to dynamically resolve additional dependencies at optimization time. The sample package dependency handler below illustrates how a package dependency can be used to automatically include every file in a directory as a dependency:

```javascript
var fs = require('fs');
var path = require('path');
const { promisify } = require('util');
const fs = promisify(require('fs'));
const path = promisify(require('path'));

lasso.dependencies.registerPackageType('dir', {
properties: {
'path': 'string'
},

init: function(context, callback) {
var path = this.path;
async init (context) {
let path = this.path;

if (!path) {
callback(new Error('"path" is required'));
}

this.path = path = this.resolvePath(path); // Convert the relative path to an absolute path

fs.stat(path, function(err, stat) {
if (err) {
return callback(err);
}

if (!stat.isDirectory()) {
return callback(new Error('Directory expected: ' + path));
}

callback();
});
const stat = await fs.stat(path);
if (!stat.isDirectory()) {
throw new Error('Directory expected: ' + path);
}
},

getDependencies: function(context, callback) {
var dir = this.path;
async getDependencies (context) {
const dir = this.path;
const filenames = await fs.readdir(dir);

fs.readdir(dir, function(err, filenames) {
if (err) {
return callback(err);
}

// Convert the filenames to full paths
var dependencies = filenames.map(function(filename) {
return path.join(dir, filename);
});

callback(null, dependencies);
// Convert the filenames to full paths
var dependencies = filenames.map(function(filename) {
return path.join(dir, filename);
});

return dependencies;
},

getDir: function() {
Expand Down
4 changes: 2 additions & 2 deletions lib/AsyncPackage.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ AsyncPackage.prototype = {

var bundles = this.bundles;

for (var i=0, len=bundles.length; i<len; i++) {
for (var i = 0, len = bundles.length; i < len; i++) {
var bundle = bundles[i];
var url;
if (!bundle.hasContent() || !(url = bundle.getUrl(context))) {
Expand Down Expand Up @@ -56,4 +56,4 @@ AsyncPackage.prototype = {
}
};

module.exports = AsyncPackage;
module.exports = AsyncPackage;
9 changes: 3 additions & 6 deletions lib/Bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Bundle.prototype = {
getHtmlAttributes: function() {
var attributes = {};
this.dependencies.forEach(function(dependency) {
if(typeof dependency.attributes === 'object') {
if (typeof dependency.attributes === 'object') {
Object.keys(dependency.attributes).forEach(function(key) {
attributes[key] = dependency.attributes[key];
});
Expand All @@ -93,11 +93,9 @@ Bundle.prototype = {

if (this.isJavaScript()) {
contentType = contentTypes.JS;
}
else if (this.isStyleSheet()) {
} else if (this.isStyleSheet()) {
contentType = contentTypes.CSS;
}
else {
} else {
contentType = this.getContentType();
}
return '"' + this.getName() + '" (' + contentType + ', ' + this.slot + (this.inlinePos ? ', inlinePos=' + this.inlinePos : '') + ')';
Expand All @@ -108,7 +106,6 @@ Bundle.prototype = {
this.key = Bundle.getKey(this.slot, this.contentType, this.inline, this.name);
}
return this.key;

},

getSlot: function() {
Expand Down
4 changes: 2 additions & 2 deletions lib/BundleConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var DependencyList = require('./DependencyList');
var BundleConfig = function(dirname, filename) {
ok(dirname, '"dirname" is required');
ok(typeof dirname === 'string', '"dirname" is required');

this.name = null;
this.fingerprintsEnabled = undefined;
this.dependencies = [];
Expand Down Expand Up @@ -33,4 +33,4 @@ BundleConfig.prototype = {
}
};

module.exports = BundleConfig;
module.exports = BundleConfig;
12 changes: 3 additions & 9 deletions lib/BundleMappings.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ function safeRelativePath(path) {
return path.replace(/[^A-Za-z0-9_.\-\/\\$@]/g, '_');
}


var BundleMappings = function(config, pageName) {

BundleMappings.$super.call(this);

ok(pageName == null || typeof pageName === 'string', 'pageName should be a String');
Expand Down Expand Up @@ -43,7 +41,7 @@ BundleMappings.prototype = {

getBundleMappingForDependency: function(dependency) {
var key = dependency.getKey();
var bundleMapping = this.dependencyToBundleMapping[key];
var bundleMapping = this.dependencyToBundleMapping[key];
if (bundleMapping) {
return bundleMapping;
} else if (this.parentBundleMappings) {
Expand All @@ -55,7 +53,7 @@ BundleMappings.prototype = {

getBundleForDependency: function(dependency) {
var key = dependency.getKey();
var bundleMapping = this.dependencyToBundleMapping[key];
var bundleMapping = this.dependencyToBundleMapping[key];
if (bundleMapping) {
return bundleMapping.bundle;
} else if (this.parentBundleMappings) {
Expand Down Expand Up @@ -102,7 +100,6 @@ BundleMappings.prototype = {
targetBundle = this.bundlesByKey[bundleKey];

if (!targetBundle) {

targetBundle = new Bundle(targetBundleName);
targetBundle.key = bundleKey;
targetBundle.setInlinePos(inlinePos);
Expand All @@ -113,7 +110,6 @@ BundleMappings.prototype = {
targetBundle.setConfig(bundleConfig);
}


this.bundlesByKey[bundleKey] = targetBundle;
}

Expand Down Expand Up @@ -193,7 +189,6 @@ BundleMappings.prototype = {
// `${dependencyType}-${pageBundleName}` as the
// bundle name.


var targetBundle;

if (dependency.getUnbundledTarget) {
Expand All @@ -211,7 +206,6 @@ BundleMappings.prototype = {
if (targetBundle) {
targetBundle = safeRelativePath(targetBundle);


var prefix = pageBundleName.replace(/[\\\/]/g, '-');

if (flags && !flags.isEmpty()) {
Expand Down Expand Up @@ -252,7 +246,7 @@ BundleMappings.prototype = {
}
} else {
// Bundling is enabled
//Make sure the dependency is part of a bundle. If it not part of a preconfigured bundle then put it in a page-specific bundle
// Make sure the dependency is part of a bundle. If it not part of a preconfigured bundle then put it in a page-specific bundle
bundle = this.addDependencyToBundle(
dependency,
defaultBundleName || pageBundleName,
Expand Down
4 changes: 2 additions & 2 deletions lib/BundleSetConfig.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var nextId = 0;

function BundleSetConfig(name) {
this._id = nextId++;
this.name = name;
Expand All @@ -15,4 +15,4 @@ BundleSetConfig.prototype = {
}
};

module.exports = BundleSetConfig;
module.exports = BundleSetConfig;
Loading

0 comments on commit 7a2df9a

Please sign in to comment.