From ecf19b161a8e16ca904c4b709737543ed9292818 Mon Sep 17 00:00:00 2001 From: "Mohamed H. Hegazy" Date: Sat, 30 Sep 2017 02:39:06 +0000 Subject: [PATCH 1/2] fix build on window --- scripts/rollup/build.js | 20 +++- scripts/rollup/plugins/rollup-plugin-alias.js | 107 ++++++++++++++++++ 2 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 scripts/rollup/plugins/rollup-plugin-alias.js diff --git a/scripts/rollup/build.js b/scripts/rollup/build.js index 3ef229f94345..a88d32e51a52 100644 --- a/scripts/rollup/build.js +++ b/scripts/rollup/build.js @@ -3,7 +3,10 @@ const rollup = require('rollup').rollup; const babel = require('rollup-plugin-babel'); const commonjs = require('rollup-plugin-commonjs'); -const alias = require('rollup-plugin-alias'); +// temporary load edited version of rollup-plugin-alias until issue +// https://github.com/rollup/rollup-plugin-alias/issues/11 got resolved +// TODO watch rollup-plugin-alias and revert to it when issue got resolved +const alias = require('./plugins/rollup-plugin-alias'); const uglify = require('rollup-plugin-uglify'); const replace = require('rollup-plugin-replace'); const chalk = require('chalk'); @@ -296,6 +299,17 @@ function getCommonJsConfig(bundleType) { } } +// for use with rollup repalce plugin: +// windows paths breaks the string if we dosn't replace all "\" with "/" +// it only make effect on windows paths +function normalizeWindowsPathsInReplaceModules(mapToReplace) { + const newMap = {}; + Object.keys(mapToReplace).forEach(key => { + newMap[key] = mapToReplace[key].replace(/\\/g, '/'); + }); + return newMap; +} + function getPlugins( entry, babelOpts, @@ -323,7 +337,9 @@ function getPlugins( // We have to do this check because Rollup breaks on empty object. // TODO: file an issue with rollup-plugin-replace. if (Object.keys(replaceModules).length > 0) { - plugins.unshift(replace(replaceModules)); + plugins.unshift( + replace(normalizeWindowsPathsInReplaceModules(replaceModules)) + ); } const headerSanityCheck = getHeaderSanityCheck(bundleType, hasteName); diff --git a/scripts/rollup/plugins/rollup-plugin-alias.js b/scripts/rollup/plugins/rollup-plugin-alias.js new file mode 100644 index 000000000000..33606378a575 --- /dev/null +++ b/scripts/rollup/plugins/rollup-plugin-alias.js @@ -0,0 +1,107 @@ +// TODO resolve rollup-plugin-alias issue on its repo +// https://github.com/rollup/rollup-plugin-alias/issues/11 +const path = require('path'); +const {posix} = path; +const {platform} = require('os'); +const fs = require('fs'); + +const slash = require('slash'); + +const VOLUME = /^([A-Z]:)/; +const IS_WINDOWS = platform() === 'win32'; + +// Helper functions +const noop = () => null; +const matches = (key, importee) => { + if (importee.length < key.length) { + return false; + } + if (importee === key) { + return true; + } + const importeeStartsWithKey = importee.indexOf(key) === 0; + const importeeHasSlashAfterKey = importee.substring(key.length)[0] === '/'; + return importeeStartsWithKey && importeeHasSlashAfterKey; +}; +const endsWith = (needle, haystack) => + haystack.slice(-needle.length) === needle; +const isFilePath = id => /^\.?\//.test(id); +const exists = uri => { + try { + return fs.statSync(uri).isFile(); + } catch (e) { + return false; + } +}; + +const normalizeId = id => { + if ((IS_WINDOWS && typeof id === 'string') || VOLUME.test(id)) { + return slash(id.replace(VOLUME, '')); + } + + return id; +}; + +module.exports = function alias(options = {}) { + const hasResolve = Array.isArray(options.resolve); + const resolve = hasResolve ? options.resolve : ['.js']; + const aliasKeys = hasResolve + ? Object.keys(options).filter(k => k !== 'resolve') + : Object.keys(options); + + // No aliases? + if (!aliasKeys.length) { + return { + resolveId: noop, + }; + } + + return { + resolveId(importee, importer) { + const importeeId = normalizeId(importee); + const importerId = normalizeId(importer); + + // First match is supposed to be the correct one + const toReplace = aliasKeys.find(key => matches(key, importeeId)); + + if (!toReplace) { + return null; + } + + const entry = options[toReplace]; + + let updatedId = normalizeId(importeeId.replace(toReplace, entry)); + + if (isFilePath(updatedId)) { + const directory = posix.dirname(importerId); + + // Resolve file names + const filePath = posix.resolve(directory, updatedId); + const match = resolve + .map( + ext => (endsWith(ext, filePath) ? filePath : `${filePath}${ext}`) + ) + .find(exists); + + if (match) { + updatedId = match; + // To keep the previous behaviour we simply return the file path + // with extension + } else if (endsWith('.js', filePath)) { + updatedId = filePath; + } else { + updatedId = filePath + '.js'; + } + } + + // if alias is windows absoulate path return platform + // resolved path or rollup on windows will throw: + // [TypeError: Cannot read property 'specifier' of undefined] + if (VOLUME.test(entry)) { + return path.resolve(updatedId); + } + + return updatedId; + }, + }; +}; From 9c5932d1fe11a6e892a45159edc0850d5facbc76 Mon Sep 17 00:00:00 2001 From: "Mohamed H. Hegazy" Date: Fri, 20 Oct 2017 03:46:49 +0200 Subject: [PATCH 2/2] revert back to rollup-plugin-alias and update it to v1.4.0 --- package.json | 2 +- scripts/rollup/build.js | 5 +- scripts/rollup/plugins/rollup-plugin-alias.js | 107 ------------------ yarn.lock | 6 +- 4 files changed, 5 insertions(+), 115 deletions(-) delete mode 100644 scripts/rollup/plugins/rollup-plugin-alias.js diff --git a/package.json b/package.json index 1eac640c001e..088919cc63b4 100644 --- a/package.json +++ b/package.json @@ -77,7 +77,7 @@ "prop-types": "^15.6.0", "rimraf": "^2.6.1", "rollup": "^0.41.6", - "rollup-plugin-alias": "^1.2.1", + "rollup-plugin-alias": "^1.4.0", "rollup-plugin-babel": "^2.7.1", "rollup-plugin-closure-compiler-js": "^1.0.4", "rollup-plugin-commonjs": "^8.0.2", diff --git a/scripts/rollup/build.js b/scripts/rollup/build.js index 425e0191e436..7124280c3e42 100644 --- a/scripts/rollup/build.js +++ b/scripts/rollup/build.js @@ -3,10 +3,7 @@ const rollup = require('rollup').rollup; const babel = require('rollup-plugin-babel'); const commonjs = require('rollup-plugin-commonjs'); -// temporary load edited version of rollup-plugin-alias until issue -// https://github.com/rollup/rollup-plugin-alias/issues/11 got resolved -// TODO watch rollup-plugin-alias and revert to it when issue got resolved -const alias = require('./plugins/rollup-plugin-alias'); +const alias = require('rollup-plugin-alias'); const uglify = require('rollup-plugin-uglify'); const replace = require('rollup-plugin-replace'); const chalk = require('chalk'); diff --git a/scripts/rollup/plugins/rollup-plugin-alias.js b/scripts/rollup/plugins/rollup-plugin-alias.js deleted file mode 100644 index 33606378a575..000000000000 --- a/scripts/rollup/plugins/rollup-plugin-alias.js +++ /dev/null @@ -1,107 +0,0 @@ -// TODO resolve rollup-plugin-alias issue on its repo -// https://github.com/rollup/rollup-plugin-alias/issues/11 -const path = require('path'); -const {posix} = path; -const {platform} = require('os'); -const fs = require('fs'); - -const slash = require('slash'); - -const VOLUME = /^([A-Z]:)/; -const IS_WINDOWS = platform() === 'win32'; - -// Helper functions -const noop = () => null; -const matches = (key, importee) => { - if (importee.length < key.length) { - return false; - } - if (importee === key) { - return true; - } - const importeeStartsWithKey = importee.indexOf(key) === 0; - const importeeHasSlashAfterKey = importee.substring(key.length)[0] === '/'; - return importeeStartsWithKey && importeeHasSlashAfterKey; -}; -const endsWith = (needle, haystack) => - haystack.slice(-needle.length) === needle; -const isFilePath = id => /^\.?\//.test(id); -const exists = uri => { - try { - return fs.statSync(uri).isFile(); - } catch (e) { - return false; - } -}; - -const normalizeId = id => { - if ((IS_WINDOWS && typeof id === 'string') || VOLUME.test(id)) { - return slash(id.replace(VOLUME, '')); - } - - return id; -}; - -module.exports = function alias(options = {}) { - const hasResolve = Array.isArray(options.resolve); - const resolve = hasResolve ? options.resolve : ['.js']; - const aliasKeys = hasResolve - ? Object.keys(options).filter(k => k !== 'resolve') - : Object.keys(options); - - // No aliases? - if (!aliasKeys.length) { - return { - resolveId: noop, - }; - } - - return { - resolveId(importee, importer) { - const importeeId = normalizeId(importee); - const importerId = normalizeId(importer); - - // First match is supposed to be the correct one - const toReplace = aliasKeys.find(key => matches(key, importeeId)); - - if (!toReplace) { - return null; - } - - const entry = options[toReplace]; - - let updatedId = normalizeId(importeeId.replace(toReplace, entry)); - - if (isFilePath(updatedId)) { - const directory = posix.dirname(importerId); - - // Resolve file names - const filePath = posix.resolve(directory, updatedId); - const match = resolve - .map( - ext => (endsWith(ext, filePath) ? filePath : `${filePath}${ext}`) - ) - .find(exists); - - if (match) { - updatedId = match; - // To keep the previous behaviour we simply return the file path - // with extension - } else if (endsWith('.js', filePath)) { - updatedId = filePath; - } else { - updatedId = filePath + '.js'; - } - } - - // if alias is windows absoulate path return platform - // resolved path or rollup on windows will throw: - // [TypeError: Cannot read property 'specifier' of undefined] - if (VOLUME.test(entry)) { - return path.resolve(updatedId); - } - - return updatedId; - }, - }; -}; diff --git a/yarn.lock b/yarn.lock index 4ce6d57113b0..0e6b55c431b8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4131,9 +4131,9 @@ rimraf@2, rimraf@^2.2.8, rimraf@^2.5.1, rimraf@^2.6.1: 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" +rollup-plugin-alias@^1.4.0: + version "1.4.0" + resolved "https://registry.npmjs.org/rollup-plugin-alias/-/rollup-plugin-alias-1.4.0.tgz#120cba7c46621c03138f0ca6fd5dd2ade9872db9" dependencies: slash "^1.0.0"