From f6550db098b631530e26ab17731867610adec61a Mon Sep 17 00:00:00 2001 From: Jonny Buchanan Date: Thu, 30 Jul 2015 16:24:37 +0100 Subject: [PATCH] Add React 0.14 support --- bin/heatpack.js | 6 ++++-- dummy.js | 8 +++++++- webpack.config.js | 12 +++++++++--- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/bin/heatpack.js b/bin/heatpack.js index ecbe1f3..ee624f0 100755 --- a/bin/heatpack.js +++ b/bin/heatpack.js @@ -31,7 +31,7 @@ if (args.help || args._.length === 0) { console.log('Options:') console.log(" -v, --version print heatpack's version") console.log(' -p, --port port to run the webpack dev server on [default: 3000]') - console.log(' -f, --force force heatpack to run the given script (disable React.render check)') + console.log(' -f, --force force heatpack to run the given script (disables .render() check)') process.exit(0) } @@ -43,7 +43,9 @@ var options = { if (!args.force) { var code = fs.readFileSync(options.entry).toString() - if (code.indexOf('React.render') === -1) { + if (!/React(DOM)?\.render/.test(code)) { + // We'll alias the given file to a unique module name. If someone creates + // this on npm and the user installs it, they're both trolling ¯\_(ツ)_/¯ options.alias['theydoitonpurposelynn'] = options.entry options.entry = path.join(__dirname, '../dummy.js') } diff --git a/dummy.js b/dummy.js index 62dda36..64c17b4 100644 --- a/dummy.js +++ b/dummy.js @@ -1,11 +1,17 @@ var React = require('react') +try { + var ReactDOM = require('react-dom') +} +catch (e) { + // pass +} var Component = require('theydoitonpurposelynn') function render(Component) { if (!(Component.type && Component.props)) { Component = } - React.render(Component, document.querySelector('#app')) + (ReactDOM || React).render(Component, document.querySelector('#app')) } render(Component) diff --git a/webpack.config.js b/webpack.config.js index 174c487..9b1e845 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -23,6 +23,8 @@ function findWorkingDirNodeModules() { } } +var workingDirNodeModules = findWorkingDirNodeModules() + /** * We need to special-case exclusion to allow the heatpack dummy entry module to * be processed by loaders, as it will be under global node_modules. @@ -58,15 +60,19 @@ module.exports = function config(options) { // If there's a node_modules in scope from where the user ran heatpack, we // want to pick up React from it first, so prepend it to the list of // directories to resolve modules from. - root: findWorkingDirNodeModules(), + root: workingDirNodeModules, extensions: ['', '.js', '.jsx', '.cjsx', '.coffee'], // Resolve webpack dev server entry modules from heatpack's dependencies fallback: HEATPACK_MODULES, + // A unique alias will be set up for the module passed by the user if + // we're using dummy.js for rendering. alias: options.alias }, resolveLoader: { - // Always resolve loaders from heatpack's own dependencies - root: HEATPACK_MODULES + // We always want to pick up whichever version of React the user has... + root: workingDirNodeModules, + // ...but fall back to heatpack's dependencies for everything else + fallback: HEATPACK_MODULES }, module: { loaders: [