Skip to content
This repository has been archived by the owner on Jan 13, 2022. It is now read-only.

Better compatibility with browserify modules #46

Open
wants to merge 2 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
96 changes: 62 additions & 34 deletions src/Package.js
@@ -1,16 +1,16 @@
'use strict';

const isAbsolutePath = require('absolute-path');
const path = require('./fastpath');

class Package {

constructor({ file, fastfs, cache }) {
constructor({ file, fastfs, cache, replacements = {} }) {
this.path = file;
this.root = path.dirname(this.path);
this._fastfs = fastfs;
this.type = 'Package';
this._cache = cache;
this.replacements = replacements;
}

getMain() {
Expand All @@ -19,17 +19,14 @@ class Package {
if (typeof replacements === 'string') {
return path.join(this.root, replacements);
}

let main = json.main || 'index';

if (replacements && typeof replacements === 'object') {
main = replacements[main] ||
replacements[main + '.js'] ||
replacements[main + '.json'] ||
replacements[main.replace(/(\.js|\.json)$/, '')] ||
main;
// cut of the extension, if any
main = main.replace(/(\.js|\.json)$/, '');
// find a possible replacement
var replacement = this.getReplacement(main, replacements);
if (replacement !== undefined) {
main = replacement;
}

return path.join(this.root, main);
});
}
Expand All @@ -50,34 +47,65 @@ class Package {
this._cache.invalidate(this.path);
}

getReplacement(name, replacements) {
if (typeof replacements !== 'object') {
return undefined;
}
const relPath = './' + path.relative(this.root, name);
const relName = './' + name;
const checks = [
replacements[name],
replacements[name + '.js'],
replacements[name + '.json'],
replacements[relName],
replacements[relName + '.js'],
replacements[relName + '.json'],
replacements[relPath],
replacements[relPath + '.js'],
replacements[relPath + '.json'],
];
const matches = checks.filter(check => check !== undefined);
if (matches[0] === false) {
return false;
}
return matches[0] || undefined;
}

redirectRequire(name) {
return this.read().then(json => {
var replacements = getReplacements(json);

if (!replacements || typeof replacements !== 'object') {
return name;
let replacements = getReplacements(json);
if (typeof replacements === 'string') {
replacements = {
[json.main || 'index']: replacements,
};
}

if (name[0] !== '/') {
return replacements[name] || name;
const replacement = this.getReplacement(name, replacements);
// no replacement
if (replacement === undefined) {
// could stil be requiring a builtin
if (this.replacements[name]) {
var redirect = path.relative(this.root, this.replacements[name]);
// cut off node_modules if the builtin is required
// from the "index" of the react-native project
if (redirect.slice(0, 13) === 'node_modules/') {
redirect = redirect.slice(13);
}
return redirect;
}
return name;
}

if (!isAbsolutePath(name)) {
throw new Error(`Expected ${name} to be absolute path`);
// replacement is false boolean
if (replacement === false) {
// find path to _empty.js
const emptyPath = require.resolve('./_empty.js');
return './' + path.relative(this.root, emptyPath);
}

const relPath = './' + path.relative(this.root, name);
const redirect = replacements[relPath] ||
replacements[relPath + '.js'] ||
replacements[relPath + '.json'];
if (redirect) {
return path.join(
this.root,
redirect
);
// replacement is other module (or absolute path?)
if (replacement[0] !== '.') {
return replacement;
}

return name;
// replacement is relative path
return path.join(this.root, replacement);
});
}

Expand All @@ -93,7 +121,7 @@ class Package {

function getReplacements(pkg) {
return pkg['react-native'] == null
? pkg.browser
? (pkg.browser == null ? pkg.browserify : pkg.browser)
: pkg['react-native'];
}

Expand Down
8 changes: 8 additions & 0 deletions src/_empty.js
@@ -0,0 +1,8 @@
/**
* Copyright 2013-2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/