Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/configurable logical path mappings #7

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test/integration/scenarios/*/src/*
test/integration/scenarios/*/webpack.config.js

3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
node_modules
*.log
*.swp
examples/*/build/*
/tmp/*/build/*
!/tmp/*/build/.keep
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
language: node_js

node_js:
- 0.12
- 4
- 5
- 6
Expand All @@ -24,6 +25,4 @@ cache:
directories:
- node_modules
- example/node_modules

script:
- npm run test:ci
- tmp

This file was deleted.

This file was deleted.

31 changes: 0 additions & 31 deletions example/build/sprockets-manifest.json

This file was deleted.

31 changes: 0 additions & 31 deletions example/test/fixtures/sprockets-manifest.json

This file was deleted.

4 changes: 2 additions & 2 deletions example/package.json → examples/01_simple/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "",
"main": "index.js",
"scripts": {
"build": "NODE_ENV=production webpack --config webpack.config.js --progress --profile --colors",
"build": "npm run reinstall && NODE_ENV=production webpack --config webpack.config.js --progress --profile --colors",
"reinstall": "npm uninstall sprockets-stats-webpack-plugin && npm install"
},
"author": "",
Expand All @@ -14,7 +14,7 @@
"css-loader": "^0.23.1",
"extract-text-webpack-plugin": "^0.9.1",
"file-loader": "^0.8.5",
"sprockets-stats-webpack-plugin": "file:../",
"sprockets-stats-webpack-plugin": "file:../../",
"sri-stats-webpack-plugin": "^0.7.0",
"style-loader": "^0.13.0",
"url-loader": "^0.5.7",
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ test('generated sprockets manifest matches expected', function(t) {

t.truthy(Date.parse(file.mtime), 'not a valid date');

file.mtime = '2016-06-21T07:42:09.623Z';
file.mtime = '2016-06-27T20:53:11.389Z';
});

actualManifest.hash = 'test';

t.deepEqual(actualManifest, expectedManifest, 'manifests not equal');
});
31 changes: 31 additions & 0 deletions examples/01_simple/test/fixtures/sprockets-manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"assets": {
"images/maru.jpg": "images/maru-01-test.jpg",
"main.js": "main-01-test.js",
"main.css": "main-01-test.css"
},
"files": {
"images/maru-01-test.jpg": {
"logical_path": "images/maru.jpg",
"size": 48416,
"digest": "6d18eff577039c3cd85aa19b82571845",
"mtime": "2016-06-27T20:53:11.389Z"
},
"main-01-test.js": {
"integrity": "sha384-9WCBflot2kBNysprVPr2Cspm01ivh4eg4OOLQPBOFPA/V96H3eWOFreCuQedp892",
"size": 386,
"digest": "bfe15df886795b3cfdba39725fe0761a",
"logical_path": "main.js",
"mtime": "2016-06-27T20:53:11.389Z"
},
"main-01-test.css": {
"integrity": "sha384-lkGaxwmX5qUkXJGM8wX4uMLH+AO5QZKlhbZdtQS0niV3w5EeOnJ0HsNDSEcvFFu1",
"size": 21,
"digest": "f170406bbf80db11a0f7afdee504fa52",
"logical_path": "main.css",
"mtime": "2016-06-27T20:53:11.389Z"
}
},
"hash": "test",
"publicPath": "/assets/01-test/"
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ module.exports = {
},

"output": {
"path": path.join(PROJECT_DIR, "build", "assets", "[hash]"),
"publicPath": "/assets/[hash]/",
"filename": "[name]-[hash].js",
"chunkFilename": "chunk-[id].[name]-[hash].js",
"sourceMapFilename": "debug/[file]-[hash].id-[id].map",
"path": path.join(PROJECT_DIR, "build", "assets", "01-test"),
"publicPath": "/assets/01-test/",
"filename": "[name]-01-test.js",
"chunkFilename": "chunk-[id].[name]-01-test.js",
"sourceMapFilename": "debug/[file]-01-test.map",
"pathInfo": "false"
},

Expand All @@ -51,14 +51,16 @@ module.exports = {
},
{
"test": /\.jpg$/,
"loader": "url?limit=10&name=images/[name]-[hash].[ext]"
"loader": "url?limit=10&name=images/[name]-01-test.[ext]"
}
]
},

"plugins": [
new webpack.NoErrorsPlugin(),
new CleanPlugin(["build"]),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.AggressiveMergingPlugin(),
new webpack.optimize.DedupePlugin(),
new webpack.optimize.UglifyJsPlugin({
output: {
Expand All @@ -68,9 +70,7 @@ module.exports = {
warnings: false
}
}),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.AggressiveMergingPlugin(),
new ExtractTextPlugin("[name]-[contenthash].css"),
new ExtractTextPlugin("[name]-01-test.css"),
new SriStatsPlugin({
customStatsKey: "rails"
}),
Expand Down
33 changes: 29 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,25 @@ SprocketsStatsWebpackPlugin.prototype.apply = function(compiler) {
});

compilation.plugin('module-asset', function(mod, filename) {
var logicalPath = "";
var logicalPath = '';
var filenameKey = Object.keys(mod.assets).slice(-1)[0];

if (mappings.length > 0) {
/* eslint-disable vars-on-top */
// loaderUtils.interpolatePath takes a loader context but only uses the
// resourcePath property, so let's create a stub
var loaderContextStub = {resourcePath: mod.userRequest};
var loaderContextStub = { resourcePath: mod.userRequest };
var i;
/* eslint-enable vars-on-top */

for (let mapping of mappings) {
for (i = 0; i < mappings.length; i++) {
/* eslint-disable vars-on-top */
var mapping = mappings[i];
var re = new RegExp(mapping.test);
var match = mod.userRequest.match(re);
/* eslint-enable vars-on-top */

if(match) {
if (match) {
logicalPath = loaderUtils.interpolateName(
loaderContextStub,
mapping.logicalPath,
Expand Down Expand Up @@ -111,6 +117,24 @@ SprocketsStatsWebpackPlugin.prototype.apply = function(compiler) {
var walker = walk.walk(outputAssetsPath);
var assets = stats.toJson().assets;

assets.forEach(function(asset) {
var hashedAssetName = asset.name;
var assetName;
var assetExt;
var filename;

if ((asset.chunks && asset.chunks.length > 0) &&
(asset.chunkNames && asset.chunkNames.length > 0)
) {
assetName = asset.chunkNames.slice(-1)[0];
assetExt = hashedAssetName.split('.').pop();

filename = assetName + '.' + assetExt;

sprockets[hashedAssetName].logical_path = filename;
}
});

walker.on('file', function(rootPath, fileStat, next) {
var fullPath = path.join(rootPath, fileStat.name);
var filename = (path.relative(outputPath, fullPath));
Expand Down Expand Up @@ -140,6 +164,7 @@ SprocketsStatsWebpackPlugin.prototype.apply = function(compiler) {

Object.keys(output.files).forEach(function(filename) {
var asset = output.files[filename];

output.assets[asset.logical_path] = filename;
});

Expand Down
14 changes: 10 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
],
"scripts": {
"test": "ava",
"build:integration": "cd example && npm run reinstall && npm run build",
"test:all": "npm run test && npm run build:integration && ava ./example/test/SprocketsManifest.test.js",
"test:ci": "npm run test && ava ./example/test/SprocketsManifest.test.js",
"lint": "eslint ./test ./lib index.js",
"check": "npm run lint && npm run test"
},
Expand All @@ -35,10 +32,18 @@
"homepage": "https://github.com/mikechau/sprockets-stats-webpack-plugin#readme",
"devDependencies": {
"ava": "^0.15.2",
"css-loader": "^0.23.1",
"eslint": "^2.13.1",
"eslint-config-mc": "^0.6.5",
"eslint-plugin-ava": "^2.5.0",
"eslint-plugin-import": "^1.8.1",
"extract-text-webpack-plugin": "^1.0.1",
"file-loader": "^0.9.0",
"memory-fs": "^0.3.0",
"remove-webpack-plugin": "^1.1.1",
"sri-stats-webpack-plugin": "^0.7.3",
"style-loader": "^0.13.1",
"url-loader": "^0.5.7",
"webpack": "^1.13.1"
},
"dependencies": {
Expand All @@ -49,7 +54,8 @@
},
"ava": {
"files": [
"test/**/*.js"
"test/**/*.test.js",
"!test/integration/scenarios/**/*"
],
"failFast": true,
"verbose": true
Expand Down
3 changes: 2 additions & 1 deletion test/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "eslint-config-mc/ava",
"rules": {
"func-names": 0
"func-names": 0,
"consistent-return": 0
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('hi2u');
Binary file not shown.
3 changes: 3 additions & 0 deletions test/integration/scenarios/rails/client/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var font = require('./src/assets/fonts');

console.log(font);
Loading