From 0e2f67168bf4cc53734fd6aa0bc8cbd8af8e0cec Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Tue, 21 Mar 2017 18:57:51 +0000 Subject: [PATCH] added NPM package creation and copying into build chain --- package.json | 1 + packages/react-dom/index.js | 6 ++- packages/react-native-renderer/index.js | 2 +- packages/react/index.js | 7 +++ packages/react/package.json | 2 +- packages/react/react.js | 3 -- scripts/rollup/build.js | 55 ++++++++++++++++++++++ scripts/rollup/bundles.js | 18 ++++--- src/renderers/native/NativeMethodsMixin.js | 9 +++- src/renderers/native/ReactNative.js | 5 +- 10 files changed, 93 insertions(+), 15 deletions(-) create mode 100644 packages/react/index.js delete mode 100644 packages/react/react.js diff --git a/package.json b/package.json index 498c5f9412..ab8a495dad 100644 --- a/package.json +++ b/package.json @@ -72,6 +72,7 @@ "loose-envify": "^1.1.0", "merge-stream": "^1.0.0", "minimist": "^1.2.0", + "ncp": "^2.0.0", "object-assign": "^4.1.1", "platform": "^1.1.0", "prettier": "^0.22.0", diff --git a/packages/react-dom/index.js b/packages/react-dom/index.js index 44ae595f87..555fc5b74e 100644 --- a/packages/react-dom/index.js +++ b/packages/react-dom/index.js @@ -1,3 +1,7 @@ 'use strict'; -module.exports = require('./lib/ReactDOMFiber'); +if (process.env.NODE_ENV === 'production') { + module.exports = require('./react-dom.node-prod.js'); +} else { + module.exports = require('./react-dom.node-dev.js'); +} diff --git a/packages/react-native-renderer/index.js b/packages/react-native-renderer/index.js index e9fbbc91a5..087ea9759d 100644 --- a/packages/react-native-renderer/index.js +++ b/packages/react-native-renderer/index.js @@ -1,3 +1,3 @@ 'use strict'; -module.exports = require('./lib/ReactNative'); +module.exports = require('./ReactNative'); diff --git a/packages/react/index.js b/packages/react/index.js new file mode 100644 index 0000000000..cb00ad61ba --- /dev/null +++ b/packages/react/index.js @@ -0,0 +1,7 @@ +'use strict'; + +if (process.env.NODE_ENV === 'production') { + module.exports = require('./react.node-prod.js'); +} else { + module.exports = require('./react.node-dev.js'); +} diff --git a/packages/react/package.json b/packages/react/package.json index 730d0b98ad..1b3a06b4d1 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -17,7 +17,7 @@ "dist/", "lib/" ], - "main": "react.js", + "main": "index.js", "repository": "facebook/react", "engines": { "node": ">=0.10.0" diff --git a/packages/react/react.js b/packages/react/react.js deleted file mode 100644 index 53abd6f036..0000000000 --- a/packages/react/react.js +++ /dev/null @@ -1,3 +0,0 @@ -'use strict'; - -module.exports = require('./lib/React'); diff --git a/scripts/rollup/build.js b/scripts/rollup/build.js index f7aa9a5a27..124d6fe975 100644 --- a/scripts/rollup/build.js +++ b/scripts/rollup/build.js @@ -7,9 +7,11 @@ const alias = require('rollup-plugin-alias'); const filesize = require('rollup-plugin-filesize'); const uglify = require('rollup-plugin-uglify'); const replace = require('rollup-plugin-replace'); +const ncp = require('ncp').ncp; const chalk = require('chalk'); const boxen = require('boxen'); const { resolve, join } = require('path'); +const { mkdirSync, unlinkSync, existsSync } = require('fs'); const rimraf = require('rimraf'); const argv = require('minimist')(process.argv.slice(2)); const { @@ -173,6 +175,53 @@ function getCommonJsConfig(bundleType) { } } +function copyNodePackageTemplate(packageName) { + const from = resolve(`./packages/${packageName}`); + const to = resolve(`./build/rollup/packages/${packageName}`); + + // if the package directory already exists, we skip copying to it + if (!existsSync(to)) { + return new Promise((res, rej) => { + ncp(from, to, error => { + if (error) { + rej(error); + } + res(); + }); + }); + } else { + return Promise.resolve(); + } +} + +function copyBundleIntoNodePackage(packageName, filename) { + const from = resolve(`./build/rollup/${filename}`); + const to = resolve(`./build/rollup/packages/${packageName}/${filename}`); + + return new Promise((res, rej) => { + ncp(from, to, error => { + if (error) { + rej(error); + } + // delete the old file + unlinkSync(from); + res(); + }); + }); +} + +function createNodePackage(bundleType, packageName, filename) { + const { NODE_DEV, NODE_PROD, RN } = bundleTypes; + + // we only copy packages for NODE_DEV, NODE_PROD and FB bundle types + if (bundleType === NODE_DEV || bundleType === NODE_PROD || bundleType === RN) { + return copyNodePackageTemplate(packageName).then( + () => copyBundleIntoNodePackage(packageName, filename) + ); + } + return Promise.resolve(); +} + function getPlugins(entry, babelOpts, paths, filename, bundleType, isRenderer) { const plugins = [ replace( @@ -243,6 +292,8 @@ function createBundle({ plugins: getPlugins(entry, babelOpts, paths, filename, bundleType, isRenderer), }).then(({write}) => write( updateBundleConfig(config, filename, format, bundleType, hasteName) + )).then(() => ( + createNodePackage(bundleType, name, filename) )).catch(error => { console.error(error); process.exit(1); @@ -251,6 +302,10 @@ function createBundle({ // clear the build folder rimraf(join('build', 'rollup'), () => { + // TODO: this line can go away once we remove rollup folder + mkdirSync(resolve(`./build/rollup`)); + // create the packages folder + mkdirSync(resolve(`./build/rollup/packages/`)); bundles.forEach(bundle => Promise.resolve() .then(() => createBundle(bundle, bundleTypes.DEV)) diff --git a/scripts/rollup/bundles.js b/scripts/rollup/bundles.js index 33619f4be3..6b38eb55ff 100644 --- a/scripts/rollup/bundles.js +++ b/scripts/rollup/bundles.js @@ -57,7 +57,7 @@ const bundles = [ /******* React DOM *******/ { babelOpts: babelOptsReact, - bundleTypes: [DEV, PROD, NODE_DEV, NODE_PROD, FB], + bundleTypes: [DEV, PROD, FB], config: { destDir: 'build/rollup/', globals: { @@ -71,7 +71,7 @@ const bundles = [ fbEntry: 'src/fb/ReactDOMFBEntry.js', hasteName: 'ReactDOMStack-fb', isRenderer: true, - name: 'react-dom', + name: 'react-dom-stack', paths: [ 'src/umd/ReactDOMUMDEntry.js', @@ -99,7 +99,7 @@ const bundles = [ fbEntry: 'src/fb/ReactDOMFiberFBEntry.js', hasteName: 'ReactDOMFiber-fb', isRenderer: true, - name: 'react-dom-fiber', + name: 'react-dom', paths: [ 'src/renderers/dom/**/*.js', 'src/renderers/shared/**/*.js', @@ -113,7 +113,8 @@ const bundles = [ /******* React DOM Server *******/ { babelOpts: babelOptsReact, - bundleTypes: [DEV, PROD, NODE_DEV, NODE_PROD, FB], + // TODO: deal with the Node version of react-dom-server package + bundleTypes: [DEV, PROD, FB], config: { destDir: 'build/rollup/', globals: { @@ -145,7 +146,9 @@ const bundles = [ /******* React ART *******/ { babelOpts: babelOptsReactART, - bundleTypes: [DEV, PROD, NODE_DEV, NODE_PROD, FB], + // TODO: we merge react-art repo into this repo so the NODE_DEV and NODE_PROD + // builds sync up to the building of the package directories + bundleTypes: [DEV, PROD, FB], config: { destDir: 'build/rollup/', globals: { @@ -164,6 +167,7 @@ const bundles = [ hasteName: 'ReactARTStack', isRenderer: true, name: 'react-art', + nodePackageName: 'react-art', paths: [ // TODO: it relies on ReactDOMFrameScheduling. Need to move to shared/? 'src/renderers/dom/**/*.js', @@ -176,7 +180,9 @@ const bundles = [ }, { babelOpts: babelOptsReactART, - bundleTypes: [DEV, PROD, NODE_DEV, NODE_PROD, FB], + // TODO: we merge react-art repo into this repo so the NODE_DEV and NODE_PROD + // builds sync up to the building of the package directories + bundleTypes: [DEV, PROD, FB], config: { destDir: 'build/rollup/', globals: { diff --git a/src/renderers/native/NativeMethodsMixin.js b/src/renderers/native/NativeMethodsMixin.js index 1584fa745b..7d5da83f5a 100644 --- a/src/renderers/native/NativeMethodsMixin.js +++ b/src/renderers/native/NativeMethodsMixin.js @@ -11,7 +11,6 @@ */ 'use strict'; -var ReactNative = require('ReactNative'); var ReactNativeFeatureFlags = require('ReactNativeFeatureFlags'); var ReactNativeAttributePayload = require('ReactNativeAttributePayload'); var TextInputState = require('TextInputState'); @@ -20,6 +19,12 @@ var UIManager = require('UIManager'); var invariant = require('fbjs/lib/invariant'); var findNodeHandle = require('findNodeHandle'); +var ReactNative; + +function injectReactNative(RN) { + ReactNative = RN; +} + var { mountSafeCallback, throwOnStylesProp, @@ -133,7 +138,7 @@ var NativeMethodsMixin = { // Without having executed ReactNative. // Defer the factory function until now to avoid a cycle with UIManager. // TODO (bvaughn) Remove this once ReactNativeStack is dropped. - require('ReactNative'); + // require('ReactNative'); injectedSetNativeProps(this, nativeProps); }, diff --git a/src/renderers/native/ReactNative.js b/src/renderers/native/ReactNative.js index ce3a6dd12b..b7de25ae8b 100644 --- a/src/renderers/native/ReactNative.js +++ b/src/renderers/native/ReactNative.js @@ -10,12 +10,15 @@ */ 'use strict'; -const ReactNativeFeatureFlags = require('ReactNativeFeatureFlags'); +var ReactNativeFeatureFlags = require('ReactNativeFeatureFlags'); +var NativeMethodsMixin = require('NativeMethodsMixin'); var ReactNative = ReactNativeFeatureFlags.useFiber ? require('ReactNativeFiber') : require('ReactNativeStack'); + NativeMethodsMixin.__injectReactNative(ReactNative); + ReactNative.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED = { createReactNativeComponentClass: require('createReactNativeComponentClass'), findNodeHandle: require('findNodeHandle'),