Skip to content

Commit

Permalink
Replace rollup-plugin-alias with our own plugin
Browse files Browse the repository at this point in the history
This does exactly what we need and doesn't suffer from rollup/rollup-plugin-alias#34.
  • Loading branch information
gaearon committed Nov 30, 2017
1 parent 6167ae4 commit ff183e2
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 35 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@
"prop-types": "^15.6.0",
"rimraf": "^2.6.1",
"rollup": "^0.51.7",
"rollup-plugin-alias": "^1.2.1",
"rollup-plugin-babel": "^2.7.1",
"rollup-plugin-closure-compiler-js": "^1.0.4",
"rollup-plugin-commonjs": "^8.2.6",
Expand Down
68 changes: 52 additions & 16 deletions scripts/rollup/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ const rollup = require('rollup').rollup;
const babel = require('rollup-plugin-babel');
const closure = require('rollup-plugin-closure-compiler-js');
const commonjs = require('rollup-plugin-commonjs');
const alias = require('rollup-plugin-alias');
const prettier = require('rollup-plugin-prettier');
const replace = require('rollup-plugin-replace');
const stripBanner = require('rollup-plugin-strip-banner');
const chalk = require('chalk');
const join = require('path').join;
const resolve = require('path').resolve;
const resolvePlugin = require('rollup-plugin-node-resolve');
const path = require('path');
const resolve = require('rollup-plugin-node-resolve');
const fs = require('fs');
const rimraf = require('rimraf');
const argv = require('minimist')(process.argv.slice(2));
Expand Down Expand Up @@ -88,7 +86,7 @@ function getBabelConfig(updateBabelOptions, bundleType, filename) {
return Object.assign({}, options, {
plugins: options.plugins.concat([
// Use object-assign polyfill in open source
resolve('./scripts/babel/transform-object-assign-require'),
path.resolve('./scripts/babel/transform-object-assign-require'),
// Minify invariant messages
require('../error-codes/replace-invariant-error-codes'),
// Wrap warning() calls in a __DEV__ check so they are stripped from production.
Expand All @@ -113,11 +111,11 @@ function handleRollupWarnings(warning) {
);
}
const importSideEffects = Modules.getImportSideEffects();
const path = match[1];
if (typeof importSideEffects[path] !== 'boolean') {
const externalModule = match[1];
if (typeof importSideEffects[externalModule] !== 'boolean') {
throw new Error(
'An external module "' +
path +
externalModule +
'" is used in a DEV-only code path ' +
'but we do not know if it is safe to omit an unused require() to it in production. ' +
'Please add it to the `importSideEffects` list in `scripts/rollup/modules.js`.'
Expand Down Expand Up @@ -210,6 +208,44 @@ function isProductionBundleType(bundleType) {
}
}

let resolveCache = new Map();
function useForks(forks) {
let resolvedForks = {};
Object.keys(forks).forEach(srcModule => {
const targetModule = forks[srcModule];
resolvedForks[require.resolve(srcModule)] = require.resolve(targetModule);
});
return {
resolveId(importee, importer) {
if (!importer || !importee) {
return null;
}
let resolvedImportee = null;
let cacheKey = `${importer}:::${importee}`;
if (resolveCache.has(cacheKey)) {
// Avoid hitting file system if possible.
resolvedImportee = resolveCache.get(cacheKey);
} else {
try {
resolvedImportee = require.resolve(importee, {
paths: [path.dirname(importer)],
});
} catch (err) {
// Not our fault, let Rollup fail later.
}
if (resolvedImportee) {
resolveCache.set(cacheKey, resolvedImportee);
}
}
if (resolvedImportee && resolvedForks.hasOwnProperty(resolvedImportee)) {
// We found a fork!
return resolvedForks[resolvedImportee];
}
return null;
},
};
}

function getPlugins(
entry,
externals,
Expand All @@ -236,9 +272,9 @@ function getPlugins(
},
},
// Shim any modules that need forking in this environment.
alias(forks),
useForks(forks),
// Use Node resolution mechanism.
resolvePlugin({
resolve({
skip: externals,
}),
// Remove license headers from individual modules
Expand Down Expand Up @@ -437,9 +473,9 @@ rimraf('build', async () => {
// create a new build directory
fs.mkdirSync('build');
// create the packages folder for NODE+UMD bundles
fs.mkdirSync(join('build', 'packages'));
fs.mkdirSync(path.join('build', 'packages'));
// create the dist folder for UMD bundles
fs.mkdirSync(join('build', 'dist'));
fs.mkdirSync(path.join('build', 'dist'));

await Packaging.createFacebookWWWBuild();
await Packaging.createReactNativeBuild();
Expand All @@ -460,11 +496,11 @@ rimraf('build', async () => {
}

if (syncFbsource) {
await syncReactNative(join('build', 'react-native'), syncFbsource);
await syncReactNativeRT(join('build', 'react-rt'), syncFbsource);
await syncReactNativeCS(join('build', 'react-cs'), syncFbsource);
await syncReactNative(path.join('build', 'react-native'), syncFbsource);
await syncReactNativeRT(path.join('build', 'react-rt'), syncFbsource);
await syncReactNativeCS(path.join('build', 'react-cs'), syncFbsource);
} else if (syncWww) {
await syncReactDom(join('build', 'facebook-www'), syncWww);
await syncReactDom(path.join('build', 'facebook-www'), syncWww);
}

console.log(Stats.printResults());
Expand Down
13 changes: 1 addition & 12 deletions scripts/rollup/modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,7 @@ function getForks(bundleType, entry) {
if (targetModule === null) {
return;
}
const targetPath = require.resolve(targetModule);
shims[srcModule] = targetPath;
// <hack>
// Unfortunately the above doesn't work for relative imports,
// and Rollup isn't smart enough to understand they refer to the same file.
// We should come up with a real fix for this, but for now this will do.
// FIXME: this is gross.
const fileName = path.parse(srcModule).name;
shims['./' + fileName] = targetPath;
shims['../' + fileName] = targetPath;
// We don't have deeper relative requires between source files.
// </hack>
shims[srcModule] = targetModule;
});
return shims;
}
Expand Down
6 changes: 0 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4122,12 +4122,6 @@ rimraf@^2.5.4:
dependencies:
glob "^7.0.5"

rollup-plugin-alias@^1.2.1:
version "1.3.1"
resolved "https://registry.yarnpkg.com/rollup-plugin-alias/-/rollup-plugin-alias-1.3.1.tgz#a9152fec4b6a6510dae93989517ca7853c32a6fa"
dependencies:
slash "^1.0.0"

rollup-plugin-babel@^2.7.1:
version "2.7.1"
resolved "https://registry.yarnpkg.com/rollup-plugin-babel/-/rollup-plugin-babel-2.7.1.tgz#16528197b0f938a1536f44683c7a93d573182f57"
Expand Down

0 comments on commit ff183e2

Please sign in to comment.