Skip to content

Commit

Permalink
Merge pull request rotundasoftware#46 from dxuehu/fix/urlTransforms
Browse files Browse the repository at this point in the history
change metaData.assetMap to path from packageId dir
  • Loading branch information
dgbeck committed Nov 15, 2015
2 parents 619015b + b4a6783 commit 289d261
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 25 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Log

### v4.0.0
* BREAKING CHANGE: Change metaData.json assetMap to contain path from asset package Id to asset filename (insted of just asset filename)
* NOTE: hooks written for cartero < v4.0.0 will NOT work with cartero v4.0.0 and later.
* There were no API changes in this version but since a hook is required we bumped the major version number.

### v3.0.0
* Allow `parcelsDirPath` argument to cartero to be an array of explicit entry points, instead of a directory name.
* Combined `parcel_map.json` and `package_map.json` into one `metaData.json` file.
Expand Down Expand Up @@ -50,4 +55,4 @@

### v0.1.0

* Initial release
* Initial release
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ You can also ask the cartero hook to lookup the url of a specific asset. For exa


```javascript
h.getAssetUrl( 'views/page1/carnes.png' ), function( err, url ) {
res.locals.imgUrl = url;
} );
var imageUrl = h.getAssetUrl( 'views/page1/carnes.png' );
```

## It's all in the package.json
Expand Down
34 changes: 18 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

var _ = require( 'underscore' );
var fs = require( 'fs' );
var url = require( 'url' );
Expand Down Expand Up @@ -52,7 +51,7 @@ function Cartero( entryPoints, outputDirPath, options ) {

assetTypes : [ 'style', 'image' ],
assetTypesToConcatenate : [ 'style' ],

appTransforms : [],
appTransformDirs : _.isString( entryPoints ) && fs.existsSync( entryPoints ) && fs.lstatSync( entryPoints ).isDirectory() ? [ entryPoints ] : [],

Expand All @@ -65,7 +64,7 @@ function Cartero( entryPoints, outputDirPath, options ) {

factorThreshold : function( row, group ) {
return this.mainPaths.length > 1 && ( group.length >= this.mainPaths.length || group.length === 0 );
},
},

postProcessors : []
} );
Expand Down Expand Up @@ -181,7 +180,7 @@ Cartero.prototype._getMainPathsFromEntryPointsArgument = function( entryPoints,
if( this.entryPointFilter ) callback( null, _.filter( unfilteredEntryPoints, this.entryPointFilter ) );
else callback( null, unfilteredEntryPoints );
}
}
};

Cartero.prototype.processMains = function( callback ) {
var _this = this;
Expand Down Expand Up @@ -431,7 +430,11 @@ Cartero.prototype.processMains = function( callback ) {
var newFileName = path.basename( fileName, fileExt ) + '_' + fileShasum + fileExt;

// save the old name and new name for later use with the transforms
_this.assetMap[ thisAsset.srcPath ] = newFileName;
var thisAssetDstPath = path.relative( newPackage.path, thisAsset.srcPath );
var relativeAssetDir = path.dirname( thisAssetDstPath );
var relativeAssetPath = path.join( newPackage.id, relativeAssetDir, newFileName ); //ex: fingerprint/images/photo_fingerprint.png--'images' is relativeAssetDir

_this.assetMap[ thisAsset.srcPath ] = relativeAssetPath;
});
});

Expand All @@ -449,7 +452,8 @@ Cartero.prototype.processMains = function( callback ) {

if ( newAssetName ) {
// replace the url with the new name
theUrl = path.join( path.dirname( theUrl ), newAssetName );
theUrl = path.relative( path.dirname( file ), newAssetName );

} else {
// this happen when we have packages that have assets references that are not specified
// in the image tag in package.json. It happens in modules like jqueryui
Expand All @@ -463,7 +467,7 @@ Cartero.prototype.processMains = function( callback ) {
// urls in css files are relative to the css file itself
var absUrl = url.resolve( cssFileDirPathRelativeToPackageDir, theUrl );

absUrl = _this.outputDirUrl + newPackage.id + absUrl;
absUrl = path.join( _this.outputDirUrl, absUrl );

return 'url( \'' + absUrl + '\' )';
}
Expand Down Expand Up @@ -592,7 +596,7 @@ Cartero.prototype.copyTempBundleToParcelDiretory = function( tempBundlePath, ass

if( this.watching ) {
// if there is an old bundle that already exists for this asset type, delete it. this
// happens in watch mode when a new bundle is generated. (note the old bundle
// happens in watch mode when a new bundle is generated. (note the old bundle
// likely does not have the same path as the new bundle due to sha1)
if( oldBundlePath ) {
fs.unlinkSync( oldBundlePath );
Expand Down Expand Up @@ -714,7 +718,6 @@ Cartero.prototype.getTempBundlePath = function( fileExtension ) {
Cartero.prototype.resolvePostProcessors = function( postProcessorNames, callback ) {
async.map( postProcessorNames, function( thisPostProcessorName, nextPostProcessorName ) {
if( _.isFunction( thisPostProcessorName ) ) return nextPostProcessorName( null, thisPostProcessorName );

resolve( thisPostProcessorName, { basedir : process.cwd() }, function( err, modulePath ) {
if( err ) return nextPostProcessorName( err );

Expand All @@ -732,8 +735,8 @@ Cartero.prototype.writeIndividualAssetsToDisk = function( thePackage, assetTypes
async.each( assetTypesToWriteToDisk, function( thisAssetType, nextAssetType ) {
async.each( thePackage.assetsByType[ thisAssetType ], function( thisAsset, nextAsset ) {

var thisAssetDstPath = path.relative( thePackage.path, thisAsset.srcPath );
thisAssetDstPath = path.join( outputDirectoryPath, path.dirname( thisAssetDstPath ), _this.assetMap[ thisAsset.srcPath ] );
var assetsDir = path.dirname( outputDirectoryPath );
var thisAssetDstPath = path.join( assetsDir, _this.assetMap[ thisAsset.srcPath ] ); //assetMap contains path starting from fingerprint folder

if( thisAssetType === 'style' ) thisAssetDstPath = renameFileExtension( thisAssetDstPath, '.css' );

Expand All @@ -744,7 +747,7 @@ Cartero.prototype.writeIndividualAssetsToDisk = function( thePackage, assetTypes
if( err ) return nextAsset( err );

_this.emit( 'fileWritten', thisAssetDstPath, thisAssetType, false, _this.watching );

// why were we doing this? metaData does not contain references to individual assets
// if( _this.watching ) _this.writeMetaDataFile( function() {} );

Expand Down Expand Up @@ -780,7 +783,6 @@ Cartero.prototype.writeMetaDataFile = function( callback ) {
var _this = this;

var metaDataFilePath = path.join( _this.outputDirPath, kMetaDataFileName );

var packageMap = _.reduce( _this.packagePathsToIds, function( memo, thisPackageId, thisPackagePath ) {
var thisPackageKey = _this.getPackageMapKeyFromPath( thisPackagePath );

Expand All @@ -789,7 +791,7 @@ Cartero.prototype.writeMetaDataFile = function( callback ) {
// // note that if we had a situation where there was more than one incarnation as a parcel, we
// // might run into problems. can cross that bridge when we get to it...
// if( _this.parcelMap[ thisPackageKey ] ) thisPackageId = _this.parcelMap[ thisPackageKey ];

memo[ thisPackageKey ] = thisPackageId;
return memo;
}, {} );
Expand All @@ -800,7 +802,7 @@ Cartero.prototype.writeMetaDataFile = function( callback ) {
}, {} );

var metaData = JSON.stringify( {
formatVersion : 2,
formatVersion : 3,
packageMap : packageMap,
entryPointMap : entryPointMap,
assetMap: _this.assetMap
Expand Down Expand Up @@ -844,4 +846,4 @@ function printDependencies( thePackage, level, traversedPackagePaths ) {
printDependencies( thisDependency, level + 1, traversedPackagePaths );
} );
}
}
}
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,4 @@ test( 'example4', function( t ) {
'body {\n\tbackground : blue url( \'/' + [ packageId, imgDir, imgFile ].join( '/' ) + '\' );\n}' );

} );
} );
} );
7 changes: 3 additions & 4 deletions transforms/asset_url.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ module.exports = function( file, options ) {
if( url === assetSrcAbsPath )
return _this.emit( 'error', new Error( 'The file "' + assetSrcAbsPath + '" referenced from ##asset_url( "' + assetSrcPath + '" ) in file "' + file + '" is not an asset.' ) );

var filename = path.basename(assetSrcAbsPath);
var newFilename = options.assetMap[assetSrcAbsPath];
var newFilePath = options.assetMap[assetSrcAbsPath];

url = path.join( path.dirname( url ), newFilename);
url = newFilePath; //ex - fingerprint/image/photo_fingerprint.png, no need for path.dirname(url) + path.basename(assetSrcAbsPath)

if( options.outputDirUrl ) {
var baseUrl = options.outputDirUrl;
Expand All @@ -49,4 +48,4 @@ module.exports = function( file, options ) {
function end() {
this.queue( null );
}
};
};

0 comments on commit 289d261

Please sign in to comment.