Skip to content

Commit ff183e2

Browse files
committed
Replace rollup-plugin-alias with our own plugin
This does exactly what we need and doesn't suffer from rollup/rollup-plugin-alias#34.
1 parent 6167ae4 commit ff183e2

File tree

4 files changed

+53
-35
lines changed

4 files changed

+53
-35
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@
8080
"prop-types": "^15.6.0",
8181
"rimraf": "^2.6.1",
8282
"rollup": "^0.51.7",
83-
"rollup-plugin-alias": "^1.2.1",
8483
"rollup-plugin-babel": "^2.7.1",
8584
"rollup-plugin-closure-compiler-js": "^1.0.4",
8685
"rollup-plugin-commonjs": "^8.2.6",

scripts/rollup/build.js

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@ const rollup = require('rollup').rollup;
44
const babel = require('rollup-plugin-babel');
55
const closure = require('rollup-plugin-closure-compiler-js');
66
const commonjs = require('rollup-plugin-commonjs');
7-
const alias = require('rollup-plugin-alias');
87
const prettier = require('rollup-plugin-prettier');
98
const replace = require('rollup-plugin-replace');
109
const stripBanner = require('rollup-plugin-strip-banner');
1110
const chalk = require('chalk');
12-
const join = require('path').join;
13-
const resolve = require('path').resolve;
14-
const resolvePlugin = require('rollup-plugin-node-resolve');
11+
const path = require('path');
12+
const resolve = require('rollup-plugin-node-resolve');
1513
const fs = require('fs');
1614
const rimraf = require('rimraf');
1715
const argv = require('minimist')(process.argv.slice(2));
@@ -88,7 +86,7 @@ function getBabelConfig(updateBabelOptions, bundleType, filename) {
8886
return Object.assign({}, options, {
8987
plugins: options.plugins.concat([
9088
// Use object-assign polyfill in open source
91-
resolve('./scripts/babel/transform-object-assign-require'),
89+
path.resolve('./scripts/babel/transform-object-assign-require'),
9290
// Minify invariant messages
9391
require('../error-codes/replace-invariant-error-codes'),
9492
// Wrap warning() calls in a __DEV__ check so they are stripped from production.
@@ -113,11 +111,11 @@ function handleRollupWarnings(warning) {
113111
);
114112
}
115113
const importSideEffects = Modules.getImportSideEffects();
116-
const path = match[1];
117-
if (typeof importSideEffects[path] !== 'boolean') {
114+
const externalModule = match[1];
115+
if (typeof importSideEffects[externalModule] !== 'boolean') {
118116
throw new Error(
119117
'An external module "' +
120-
path +
118+
externalModule +
121119
'" is used in a DEV-only code path ' +
122120
'but we do not know if it is safe to omit an unused require() to it in production. ' +
123121
'Please add it to the `importSideEffects` list in `scripts/rollup/modules.js`.'
@@ -210,6 +208,44 @@ function isProductionBundleType(bundleType) {
210208
}
211209
}
212210

211+
let resolveCache = new Map();
212+
function useForks(forks) {
213+
let resolvedForks = {};
214+
Object.keys(forks).forEach(srcModule => {
215+
const targetModule = forks[srcModule];
216+
resolvedForks[require.resolve(srcModule)] = require.resolve(targetModule);
217+
});
218+
return {
219+
resolveId(importee, importer) {
220+
if (!importer || !importee) {
221+
return null;
222+
}
223+
let resolvedImportee = null;
224+
let cacheKey = `${importer}:::${importee}`;
225+
if (resolveCache.has(cacheKey)) {
226+
// Avoid hitting file system if possible.
227+
resolvedImportee = resolveCache.get(cacheKey);
228+
} else {
229+
try {
230+
resolvedImportee = require.resolve(importee, {
231+
paths: [path.dirname(importer)],
232+
});
233+
} catch (err) {
234+
// Not our fault, let Rollup fail later.
235+
}
236+
if (resolvedImportee) {
237+
resolveCache.set(cacheKey, resolvedImportee);
238+
}
239+
}
240+
if (resolvedImportee && resolvedForks.hasOwnProperty(resolvedImportee)) {
241+
// We found a fork!
242+
return resolvedForks[resolvedImportee];
243+
}
244+
return null;
245+
},
246+
};
247+
}
248+
213249
function getPlugins(
214250
entry,
215251
externals,
@@ -236,9 +272,9 @@ function getPlugins(
236272
},
237273
},
238274
// Shim any modules that need forking in this environment.
239-
alias(forks),
275+
useForks(forks),
240276
// Use Node resolution mechanism.
241-
resolvePlugin({
277+
resolve({
242278
skip: externals,
243279
}),
244280
// Remove license headers from individual modules
@@ -437,9 +473,9 @@ rimraf('build', async () => {
437473
// create a new build directory
438474
fs.mkdirSync('build');
439475
// create the packages folder for NODE+UMD bundles
440-
fs.mkdirSync(join('build', 'packages'));
476+
fs.mkdirSync(path.join('build', 'packages'));
441477
// create the dist folder for UMD bundles
442-
fs.mkdirSync(join('build', 'dist'));
478+
fs.mkdirSync(path.join('build', 'dist'));
443479

444480
await Packaging.createFacebookWWWBuild();
445481
await Packaging.createReactNativeBuild();
@@ -460,11 +496,11 @@ rimraf('build', async () => {
460496
}
461497

462498
if (syncFbsource) {
463-
await syncReactNative(join('build', 'react-native'), syncFbsource);
464-
await syncReactNativeRT(join('build', 'react-rt'), syncFbsource);
465-
await syncReactNativeCS(join('build', 'react-cs'), syncFbsource);
499+
await syncReactNative(path.join('build', 'react-native'), syncFbsource);
500+
await syncReactNativeRT(path.join('build', 'react-rt'), syncFbsource);
501+
await syncReactNativeCS(path.join('build', 'react-cs'), syncFbsource);
466502
} else if (syncWww) {
467-
await syncReactDom(join('build', 'facebook-www'), syncWww);
503+
await syncReactDom(path.join('build', 'facebook-www'), syncWww);
468504
}
469505

470506
console.log(Stats.printResults());

scripts/rollup/modules.js

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -126,18 +126,7 @@ function getForks(bundleType, entry) {
126126
if (targetModule === null) {
127127
return;
128128
}
129-
const targetPath = require.resolve(targetModule);
130-
shims[srcModule] = targetPath;
131-
// <hack>
132-
// Unfortunately the above doesn't work for relative imports,
133-
// and Rollup isn't smart enough to understand they refer to the same file.
134-
// We should come up with a real fix for this, but for now this will do.
135-
// FIXME: this is gross.
136-
const fileName = path.parse(srcModule).name;
137-
shims['./' + fileName] = targetPath;
138-
shims['../' + fileName] = targetPath;
139-
// We don't have deeper relative requires between source files.
140-
// </hack>
129+
shims[srcModule] = targetModule;
141130
});
142131
return shims;
143132
}

yarn.lock

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4122,12 +4122,6 @@ rimraf@^2.5.4:
41224122
dependencies:
41234123
glob "^7.0.5"
41244124

4125-
rollup-plugin-alias@^1.2.1:
4126-
version "1.3.1"
4127-
resolved "https://registry.yarnpkg.com/rollup-plugin-alias/-/rollup-plugin-alias-1.3.1.tgz#a9152fec4b6a6510dae93989517ca7853c32a6fa"
4128-
dependencies:
4129-
slash "^1.0.0"
4130-
41314125
rollup-plugin-babel@^2.7.1:
41324126
version "2.7.1"
41334127
resolved "https://registry.yarnpkg.com/rollup-plugin-babel/-/rollup-plugin-babel-2.7.1.tgz#16528197b0f938a1536f44683c7a93d573182f57"

0 commit comments

Comments
 (0)