Skip to content
This repository has been archived by the owner on Jun 24, 2024. It is now read-only.

Commit

Permalink
fix: remove absolute paths from file paths
Browse files Browse the repository at this point in the history
This was breaking determinism because absolute paths could be
different on different machines.
  • Loading branch information
digitalsadhu committed Mar 20, 2018
1 parent 6287d58 commit 668c960
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 26 deletions.
2 changes: 1 addition & 1 deletion lib/browserify-plugins/id-hasher/id-hash-transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = function idHashTransform(idToHashMap) {
transform(obj, enc, next) {
// include id and source in hash to avoid situation where
// multiple modules have the same source code but different
// depedendencies somewhere down the line.
// dependencies somewhere down the line.
idToHashMap[obj.id] = common.hasher(
relative(process.cwd(), obj.id) + obj.source
);
Expand Down
7 changes: 7 additions & 0 deletions lib/browserify-plugins/relative-paths/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

const relativePathTransform = require('./relative-path-transform');

module.exports = function outputFeed(browserify) {
browserify.pipeline.get('deps').push(relativePathTransform());
};
15 changes: 15 additions & 0 deletions lib/browserify-plugins/relative-paths/relative-path-transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

const { Transform } = require('readable-stream');
const { relative } = require('path');

module.exports = function idHashTransform() {
return new Transform({
objectMode: true,
transform(obj, enc, next) {
obj.file = relative(process.cwd(), obj.file);
this.push(obj);
next();
},
});
};
6 changes: 5 additions & 1 deletion lib/writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const Browserify = require('browserify');
const outputFeed = require('./browserify-plugins/output-feed');
const idHasher = require('./browserify-plugins/id-hasher');
const relativePaths = require('./browserify-plugins/relative-paths');
const assert = require('assert');
const { name } = require('../package.json');

Expand All @@ -24,6 +25,9 @@ module.exports = class Writer {
const opts = { ...options, dedupe: false };
const browserify = new Browserify(files, opts);
if (bundle) return browserify;
return browserify.plugin(idHasher).plugin(outputFeed);
return browserify
.plugin(idHasher)
.plugin(relativePaths)
.plugin(outputFeed);
}
};
34 changes: 17 additions & 17 deletions test/__snapshots__/writer.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ exports[`feed is not deduped 1`] = `
\\"asset-pipe-test-es5b\\":
\\"ecd571b921d0e82abe77bd7cbe5c97c9186504c86f840fb8bdc0a372310231c3\\"
},
file: \\"asset-pipe-js-writer/node_modules/asset-pipe-test-es5a/lib/main.js\\",
file: \\"node_modules/asset-pipe-test-es5a/lib/main.js\\",
index: 1,
indexDeps: {
\\"./util.js\\": 2,
Expand All @@ -123,7 +123,7 @@ exports[`feed is not deduped 1`] = `
\\"asset-pipe-test-es5c\\":
\\"a8307dbbd8da4b0752187d0f5aaf7184da3b85743e87ec77110ed88e36370047\\"
},
file: \\"asset-pipe-js-writer/node_modules/asset-pipe-test-es5a/lib/util.js\\",
file: \\"node_modules/asset-pipe-test-es5a/lib/util.js\\",
index: 2,
indexDeps: {
\\"asset-pipe-test-es5c\\": 4
Expand All @@ -137,7 +137,7 @@ exports[`feed is not deduped 1`] = `
\\"asset-pipe-test-es5c\\":
\\"a8307dbbd8da4b0752187d0f5aaf7184da3b85743e87ec77110ed88e36370047\\"
},
file: \\"asset-pipe-js-writer/node_modules/asset-pipe-test-es5b/main.js\\",
file: \\"node_modules/asset-pipe-test-es5b/main.js\\",
index: 3,
indexDeps: {
\\"asset-pipe-test-es5c\\": 4
Expand All @@ -148,7 +148,7 @@ exports[`feed is not deduped 1`] = `
source:
\\"'use strict';\\\\n\\\\n function prime (num) {\\\\n if (num < 4) {\\\\n return true;\\\\n }\\\\n var max = Math.ceil(Math.sqrt(num));\\\\n for (var i = 2; i <= max; i++) {\\\\n if (num % i === 0) {\\\\n return false;\\\\n }\\\\n }\\\\n return true;\\\\n};\\\\n\\\\nmodule.exports = prime;\\\\n\\",
deps: {},
file: \\"asset-pipe-js-writer/node_modules/asset-pipe-test-es5c/main.js\\",
file: \\"node_modules/asset-pipe-test-es5c/main.js\\",
index: 4,
indexDeps: {}
},
Expand All @@ -159,7 +159,7 @@ exports[`feed is not deduped 1`] = `
\\"./main\\":
\\"6bc16f4686fe6cf0bb03134e009a29a452568f28f6b0e1174f8d75d47366a6ec\\"
},
file: \\"asset-pipe-js-writer/test/mock/common.js\\",
file: \\"test/mock/common.js\\",
index: 5,
indexDeps: {
\\"./main\\": 6
Expand All @@ -173,7 +173,7 @@ exports[`feed is not deduped 1`] = `
\\"asset-pipe-test-es5a\\":
\\"0e0c593f5f89dd62af488699574c678b4726ad1e3de2e0a81141f3dfc02b6037\\"
},
file: \\"asset-pipe-js-writer/test/mock/main.js\\",
file: \\"test/mock/main.js\\",
index: 6,
indexDeps: {
\\"asset-pipe-test-es5a\\": 1
Expand All @@ -182,7 +182,7 @@ exports[`feed is not deduped 1`] = `
{
entry: true,
expose: false,
file: \\"asset-pipe-js-writer/test/mock/no-dedupe.js\\",
file: \\"test/mock/no-dedupe.js\\",
id: \\"c98cd71bd49ced468ea7011b8733d1a4f940f28f9336797fe7c24687d2d82dc6\\",
order: 0,
source: \\"'use strict';\\\\n\\\\nrequire('./common');\\\\nrequire('./main');\\\\n\\",
Expand Down Expand Up @@ -216,7 +216,7 @@ exports[`options object passes options on to browserify 1`] = `
\\"asset-pipe-test-es5b\\":
\\"ecd571b921d0e82abe77bd7cbe5c97c9186504c86f840fb8bdc0a372310231c3\\"
},
file: \\"asset-pipe-js-writer/node_modules/asset-pipe-test-es5a/lib/main.js\\",
file: \\"node_modules/asset-pipe-test-es5a/lib/main.js\\",
index: 1,
indexDeps: {
\\"./util.js\\": 2,
Expand All @@ -233,7 +233,7 @@ exports[`options object passes options on to browserify 1`] = `
\\"asset-pipe-test-es5c\\":
\\"a8307dbbd8da4b0752187d0f5aaf7184da3b85743e87ec77110ed88e36370047\\"
},
file: \\"asset-pipe-js-writer/node_modules/asset-pipe-test-es5a/lib/util.js\\",
file: \\"node_modules/asset-pipe-test-es5a/lib/util.js\\",
index: 2,
indexDeps: {
\\"asset-pipe-test-es5c\\": 4
Expand All @@ -249,7 +249,7 @@ exports[`options object passes options on to browserify 1`] = `
\\"asset-pipe-test-es5c\\":
\\"a8307dbbd8da4b0752187d0f5aaf7184da3b85743e87ec77110ed88e36370047\\"
},
file: \\"asset-pipe-js-writer/node_modules/asset-pipe-test-es5b/main.js\\",
file: \\"node_modules/asset-pipe-test-es5b/main.js\\",
index: 3,
indexDeps: {
\\"asset-pipe-test-es5c\\": 4
Expand All @@ -262,7 +262,7 @@ exports[`options object passes options on to browserify 1`] = `
source:
\\"'use strict';\\\\n\\\\n function prime (num) {\\\\n if (num < 4) {\\\\n return true;\\\\n }\\\\n var max = Math.ceil(Math.sqrt(num));\\\\n for (var i = 2; i <= max; i++) {\\\\n if (num % i === 0) {\\\\n return false;\\\\n }\\\\n }\\\\n return true;\\\\n};\\\\n\\\\nmodule.exports = prime;\\\\n\\",
deps: {},
file: \\"asset-pipe-js-writer/node_modules/asset-pipe-test-es5c/main.js\\",
file: \\"node_modules/asset-pipe-test-es5c/main.js\\",
index: 4,
indexDeps: {},
sourceRoot: \\"file://localhost\\",
Expand All @@ -271,7 +271,7 @@ exports[`options object passes options on to browserify 1`] = `
{
entry: true,
expose: false,
file: \\"asset-pipe-js-writer/test/mock/main.js\\",
file: \\"test/mock/main.js\\",
id: \\"6bc16f4686fe6cf0bb03134e009a29a452568f28f6b0e1174f8d75d47366a6ec\\",
order: 0,
source:
Expand Down Expand Up @@ -303,7 +303,7 @@ exports[`writer produces a feed of dependencies with ids hashed 1`] = `
\\"asset-pipe-test-es5b\\":
\\"ecd571b921d0e82abe77bd7cbe5c97c9186504c86f840fb8bdc0a372310231c3\\"
},
file: \\"asset-pipe-js-writer/node_modules/asset-pipe-test-es5a/lib/main.js\\",
file: \\"node_modules/asset-pipe-test-es5a/lib/main.js\\",
index: 1,
indexDeps: {
\\"./util.js\\": 2,
Expand All @@ -318,7 +318,7 @@ exports[`writer produces a feed of dependencies with ids hashed 1`] = `
\\"asset-pipe-test-es5c\\":
\\"a8307dbbd8da4b0752187d0f5aaf7184da3b85743e87ec77110ed88e36370047\\"
},
file: \\"asset-pipe-js-writer/node_modules/asset-pipe-test-es5a/lib/util.js\\",
file: \\"node_modules/asset-pipe-test-es5a/lib/util.js\\",
index: 2,
indexDeps: {
\\"asset-pipe-test-es5c\\": 4
Expand All @@ -332,7 +332,7 @@ exports[`writer produces a feed of dependencies with ids hashed 1`] = `
\\"asset-pipe-test-es5c\\":
\\"a8307dbbd8da4b0752187d0f5aaf7184da3b85743e87ec77110ed88e36370047\\"
},
file: \\"asset-pipe-js-writer/node_modules/asset-pipe-test-es5b/main.js\\",
file: \\"node_modules/asset-pipe-test-es5b/main.js\\",
index: 3,
indexDeps: {
\\"asset-pipe-test-es5c\\": 4
Expand All @@ -343,14 +343,14 @@ exports[`writer produces a feed of dependencies with ids hashed 1`] = `
source:
\\"'use strict';\\\\n\\\\n function prime (num) {\\\\n if (num < 4) {\\\\n return true;\\\\n }\\\\n var max = Math.ceil(Math.sqrt(num));\\\\n for (var i = 2; i <= max; i++) {\\\\n if (num % i === 0) {\\\\n return false;\\\\n }\\\\n }\\\\n return true;\\\\n};\\\\n\\\\nmodule.exports = prime;\\\\n\\",
deps: {},
file: \\"asset-pipe-js-writer/node_modules/asset-pipe-test-es5c/main.js\\",
file: \\"node_modules/asset-pipe-test-es5c/main.js\\",
index: 4,
indexDeps: {}
},
{
entry: true,
expose: false,
file: \\"asset-pipe-js-writer/test/mock/main.js\\",
file: \\"test/mock/main.js\\",
id: \\"6bc16f4686fe6cf0bb03134e009a29a452568f28f6b0e1174f8d75d47366a6ec\\",
order: 0,
source:
Expand Down
8 changes: 1 addition & 7 deletions test/writer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@ const getStream = require('./helpers/get-stream');
const Writer = require('../');
const prettier = require('prettier');

const clean = body =>
prettier.format(
JSON.stringify(body, null, 2).replace(
/"file":\s".*\/asset-pipe\//g,
'"file": "'
)
);
const clean = body => prettier.format(JSON.stringify(body, null, 2));

test('writer produces a feed of dependencies with ids hashed', async () => {
expect.hasAssertions();
Expand Down

0 comments on commit 668c960

Please sign in to comment.