Skip to content

Commit

Permalink
feat: support scoped bundle names
Browse files Browse the repository at this point in the history
e.g. "@lennyburdette/some-engine".
  • Loading branch information
Lenny Burdette committed Mar 13, 2018
1 parent 1d4ada7 commit 24be7a5
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 23 deletions.
83 changes: 60 additions & 23 deletions lib/asset-manifest-generator.js
Expand Up @@ -74,15 +74,16 @@ AssetManifestGenerator.prototype.build = function() {
}

var manifest = walk(inputPath).reduce(function(manifest, entry) {
var pathParts = entry.split('/');
var assetName = pathParts.pop();
var bundleName = pathParts.shift();

// If there is no assetName, then we have a directory per the rules of
// walk-sync. And, if there are no pathParts left we have a root directory.
// https://github.com/joliss/node-walk-sync/blob/master/README.md#usage
var isNewBundle = !assetName && pathParts.length === 0;
if (isNewBundle) {
var bundleInfo = bundleInfoFromAssetEntry(entry);
var bundleName = bundleInfo.bundleName;
var assetName = bundleInfo.assetName;
var assetType = bundleInfo.assetType;

if (!bundleInfo.isValid) {
return manifest;
}

if (bundleInfo.isNewBundle) {
if (manifest.bundles[bundleName]) {
throw new Error('Attempting to add bundle "' + bundleName + '" to manifest but a bundle with that name already exists.');
}
Expand All @@ -94,28 +95,24 @@ AssetManifestGenerator.prototype.build = function() {

// If the asset is named "dependencies.manifest.json" then we should read
// the json in and set it as "dependencies" on the corresponding bundle.
else if (bundleName && assetName && assetName === 'dependencies.manifest.json') {
else if (assetName === 'dependencies.manifest.json') {
var dependencies = fs.readJsonSync(path.join(inputPath, entry));
manifest.bundles[bundleName].dependencies = dependencies;
}

// If the asset is in a bundle, then attempt to add it to the manifest by
// checking if it is a supported type.
else if (assetName && bundleName) {
var assetType = assetName.split('.').pop();
else if (supportedTypes.indexOf(assetType) !== -1 && !stringOrRegexMatch(filesToIgnore, entry)) {

if (supportedTypes.indexOf(assetType) !== -1 && !stringOrRegexMatch(filesToIgnore, entry)) {
// only add non-empty assets
let fullPath = path.join(inputPath, entry);
let contents = fs.readFileSync(fullPath, 'utf-8');

// only add non-empty assets
let fullPath = path.join(inputPath, entry);
let contents = fs.readFileSync(fullPath, 'utf-8');

if (contents.trim() !== '') {
manifest.bundles[bundleName].assets.push({
uri: generateURI(path.join(prepend, entry)),
type: assetType
});
}
if (contents.trim() !== '') {
manifest.bundles[bundleName].assets.push({
uri: generateURI(path.join(prepend, entry)),
type: assetType
});
}
}

Expand All @@ -141,4 +138,44 @@ function stringOrRegexMatch(matchers, value) {
return false;
}

/**
* @typedef {Object} BundleInfo
* @property {boolean} isValid - true if we have a valid new bundle name or a bundle and asset name
* @property {boolean} isNewBundle - true if this entry is just a bundle name
* @property {string} bundleName
* @property {string=} assetName
* @property {string=} assetType - asset extension
*
* @param {string} entry
* @returns {BundleInfo}
*/
function bundleInfoFromAssetEntry(entry) {
var pathParts = entry.split('/').filter(Boolean);

var bundleName = pathParts.shift();
// If the bundle name starts with a "@", then we have a scoped package[1]
// so we want to grab the next path part to complete the bundle name.
// [1]:https://docs.npmjs.com/misc/scope
if (bundleName && bundleName[0] === '@') {
if (!pathParts.length) {
return { isValid: false };
}
bundleName = [bundleName, pathParts.shift()].join('/');
}
var assetName = pathParts.pop();

// If there is no assetName, then we have a directory per the rules of
// walk-sync. And, if there are no pathParts left we have a root directory.
// https://github.com/joliss/node-walk-sync/blob/master/README.md#usage
var isNewBundle = !assetName && pathParts.length === 0;

return {
isValid: isNewBundle || Boolean(bundleName && assetName),
isNewBundle,
bundleName,
assetName,
assetType: assetName && assetName.split('.').pop()
};
}

module.exports = AssetManifestGenerator;
@@ -0,0 +1 @@
/* empty on purpose */
@@ -0,0 +1 @@
/* empty on purpose */
@@ -0,0 +1 @@
/* empty on purpose */
@@ -0,0 +1 @@
/* empty on purpose */
@@ -0,0 +1 @@
/* empty on purpose */
@@ -0,0 +1 @@
/* empty on purpose */
@@ -0,0 +1 @@
/* empty on purpose */
@@ -1,5 +1,25 @@
{
"bundles": {
"@scope/pkg": {
"assets": [
{
"type": "css",
"uri": "/bundles/@scope/pkg/assets/engine.css"
},
{
"type": "js",
"uri": "/bundles/@scope/pkg/assets/engine.js"
},
{
"type": "css",
"uri": "/bundles/@scope/pkg/assets/vendor.css"
},
{
"type": "js",
"uri": "/bundles/@scope/pkg/assets/vendor.js"
}
]
},
"blog": {
"assets": [
{
Expand Down
@@ -1,5 +1,25 @@
{
"bundles": {
"@scope/pkg": {
"assets": [
{
"type": "css",
"uri": "http://cdn.io/bundles/@scope/pkg/assets/engine.css"
},
{
"type": "js",
"uri": "http://cdn.io/bundles/@scope/pkg/assets/engine.js"
},
{
"type": "css",
"uri": "http://cdn.io/bundles/@scope/pkg/assets/vendor.css"
},
{
"type": "js",
"uri": "http://cdn.io/bundles/@scope/pkg/assets/vendor.js"
}
]
},
"blog": {
"assets": [
{
Expand Down

0 comments on commit 24be7a5

Please sign in to comment.