Skip to content

Commit 06c07eb

Browse files
author
benholloway
committed
fix require transform for json files, upgrade external transforms with similar fixes
1 parent 4c0be82 commit 06c07eb

File tree

2 files changed

+67
-63
lines changed

2 files changed

+67
-63
lines changed

lib/build/browserify.js

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -286,44 +286,47 @@ module.exports = create;
286286
*/
287287
function requireTransform(allowBowerRelative, allowProjectRelative) {
288288
var BOWER = path.relative(process.cwd(), bowerDir.sync());
289-
return transformTools.makeRequireTransform('requireTransform', null, function transform(args, opts, done) {
289+
var transformOpts = {
290+
excludeExtensions: ['json'] // must exclude any extension that is not javascript once all transforms have run
291+
};
292+
return transformTools.makeRequireTransform('requireTransform', transformOpts, function transform(args, opts, done) {
290293

291294
// find the original path and transform it so log as it is not relative
292295
var original = args[0];
293-
var split = original.split(/[\\\/]/g); // keep delimiters
294-
var firstTerm = split.splice(0, 1).shift(); // remove the first term from the split
295-
var isTransform = (firstTerm.length) && !(/^\.{1,2}$/.test(firstTerm));
296+
var split = original.split(/[\\\/]/g); // keep delimiters
297+
var firstTerm = split.splice(0, 1).shift(); // remove the first term from the split
298+
var isTransform = (firstTerm.length) && !(/^\.{1,2}$/.test(firstTerm)); // must not be relative
296299

297300
// first valid result wins
298301
// if we are not transforming then we fall back to the original value
299302
var transformed = [
300303

301-
// current project
302-
isTransform && allowProjectRelative && function resolveProjectRelative() {
303-
var packageJson = require(path.resolve('package.json'));
304-
if ((typeof packageJson.name === 'string') && (packageJson.name === firstTerm)) {
305-
return slash(path.resolve(path.join.apply(path, split))); // full path to second term onwards
306-
}
307-
},
308-
309-
// bower project
310-
isTransform && allowBowerRelative && function resolveBowerRelative() {
311-
var partial = path.dirname(original);
312-
var directory = (partial !== '.') && path.resolve(path.join(BOWER, partial));
313-
var isFound = directory && fs.existsSync(directory) && fs.statSync(directory).isDirectory();
314-
if (isFound) {
315-
return slash(path.resolve(path.join(BOWER, original))); // path is within the bower directory
316-
}
317-
},
318-
319-
// fall back value
320-
original
321-
]
322-
.map(function (candidate) {
323-
return (typeof candidate === 'function') ? candidate() : candidate;
324-
})
325-
.filter(Boolean)
326-
.shift();
304+
// current project
305+
isTransform && allowProjectRelative && function resolveProjectRelative() {
306+
var packageJson = require(path.resolve('package.json'));
307+
if ((typeof packageJson.name === 'string') && (packageJson.name === firstTerm)) {
308+
return slash(path.resolve(path.join.apply(path, split))); // full path to second term onwards
309+
}
310+
},
311+
312+
// bower project
313+
isTransform && allowBowerRelative && function resolveBowerRelative() {
314+
var partial = path.dirname(original);
315+
var directory = (partial !== '.') && path.resolve(path.join(BOWER, partial));
316+
var isFound = directory && fs.existsSync(directory) && fs.statSync(directory).isDirectory();
317+
if (isFound) {
318+
return slash(path.resolve(path.join(BOWER, original))); // path is within the bower directory
319+
}
320+
},
321+
322+
// fallback value
323+
original
324+
]
325+
.map(function (candidate) {
326+
return (typeof candidate === 'function') ? candidate() : candidate;
327+
})
328+
.filter(Boolean)
329+
.shift();
327330

328331
// update
329332
done(null, 'require("' + transformed + '")');

npm-shrinkwrap.json

Lines changed: 34 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)