From cb0ada7a00af269d196f122ebd346c83f3b0a4a1 Mon Sep 17 00:00:00 2001 From: Dan Abramov <dan.abramov@gmail.com> Date: Sun, 17 Jul 2016 16:44:43 +0100 Subject: [PATCH] Make it work outside its own directory --- .babelrc | 12 ------------ global-cli/index.js | 2 +- package.json | 1 - scripts/graduate.js | 3 +-- scripts/init.js | 44 ++++++++++++++++++++++++++++-------------- webpack.config.dev.js | 18 ++++++++++++----- webpack.config.prod.js | 18 +++++++++++++---- 7 files changed, 58 insertions(+), 40 deletions(-) delete mode 100644 .babelrc diff --git a/.babelrc b/.babelrc deleted file mode 100644 index 37133a891..000000000 --- a/.babelrc +++ /dev/null @@ -1,12 +0,0 @@ -{ - "presets": ["es2015", "es2016", "react"], - "plugins": ["transform-object-rest-spread"], - "env": { - "production": { - "plugins": [ - "transform-react-constant-elements", - "transform-react-inline-elements" - ] - } - } -} diff --git a/global-cli/index.js b/global-cli/index.js index 09d720716..9a1ddfec8 100644 --- a/global-cli/index.js +++ b/global-cli/index.js @@ -121,7 +121,7 @@ function run(root, appName, version, verbose) { 'init.js' ); var init = require(scriptsPath); - init(root, appName); + init(root, appName, verbose); }); } diff --git a/package.json b/package.json index fe3a5692d..1ecdaf7b4 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,6 @@ "babel-loader": "^6.2.4", "babel-plugin-transform-object-rest-spread": "^6.8.0", "babel-plugin-transform-react-constant-elements": "^6.9.1", - "babel-plugin-transform-react-inline-elements": "^6.8.0", "babel-preset-es2015": "^6.9.0", "babel-preset-es2016": "^6.11.3", "babel-preset-react": "^6.11.1", diff --git a/scripts/graduate.js b/scripts/graduate.js index 58d9719c4..3e8e058e8 100644 --- a/scripts/graduate.js +++ b/scripts/graduate.js @@ -20,8 +20,7 @@ var files = [ 'scripts', 'webpack.config.dev.js', 'webpack.config.prod.js', - '.babelrc', - '.eslintrc', + '.eslintrc' ]; // Ensure that the host folder is clean and we won't override any files diff --git a/scripts/init.js b/scripts/init.js index 6c3dc72bc..15e86a5e5 100644 --- a/scripts/init.js +++ b/scripts/init.js @@ -9,8 +9,9 @@ var fs = require('fs'); var path = require('path'); +var spawn = require('child_process').spawn; -module.exports = function(hostPath, appName) { +module.exports = function(hostPath, appName, verbose) { var selfPath = path.join(hostPath, 'node_modules', 'create-react-app-scripts'); var hostPackage = require(path.join(hostPath, 'package.json')); @@ -34,21 +35,34 @@ module.exports = function(hostPath, appName) { JSON.stringify(hostPackage, null, 2) ); - // TODO: run npm install in hostPath, (not needed for npm 3 if we accept some hackery) - // Move the src folder + // TODO: copying might be more correct? fs.renameSync(path.join(selfPath, 'src'), path.join(hostPath, 'src')); - console.log('Success! Created ' + appName + ' at ' + hostPath + '.'); - console.log(); - console.log('Inside that directory, you can run several commands:'); - console.log(' * npm start: Starts the development server.'); - console.log(' * npm run build: Builds the app for production.'); - console.log(' * npm run graduate: Removes this tool. If you do this, you can’t go back!'); - console.log(); - console.log('We suggest that you begin by typing:'); - console.log(' cd', appName); - console.log(' npm start'); - console.log(); - console.log('Happy hacking!'); + // Run another npm install for react and react-dom + // TODO: having to do two npm installs is bad, can we avoid it? + var args = [ + 'install', + verbose && '--verbose' + ].filter(function(e) { return e; }); + var proc = spawn('npm', args, {stdio: 'inherit'}); + proc.on('close', function (code) { + if (code !== 0) { + console.error('`npm ' + args.join(' ') + '` failed'); + return; + } + + console.log('Success! Created ' + appName + ' at ' + hostPath + '.'); + console.log(); + console.log('Inside that directory, you can run several commands:'); + console.log(' * npm start: Starts the development server.'); + console.log(' * npm run build: Builds the app for production.'); + console.log(' * npm run graduate: Removes this tool. If you do this, you can’t go back!'); + console.log(); + console.log('We suggest that you begin by typing:'); + console.log(' cd', appName); + console.log(' npm start'); + console.log(); + console.log('Happy hacking!'); + }); }; diff --git a/webpack.config.dev.js b/webpack.config.dev.js index 5b8847233..4d68cd843 100644 --- a/webpack.config.dev.js +++ b/webpack.config.dev.js @@ -32,15 +32,20 @@ module.exports = { preLoaders: [ { test: /\.js$/, - loader: 'eslint-loader', - include: path.resolve(__dirname, relative, 'src') + loader: 'eslint', + include: path.resolve(__dirname, relative, 'src'), } ], loaders: [ { test: /\.js$/, include: path.resolve(__dirname, relative, 'src'), - loader: 'babel' + loader: 'babel', + query: { + cacheDirectory: true, + presets: ['es2015', 'es2016', 'react'], + plugins: ['transform-object-rest-spread'] + } }, { test: /\.css$/, @@ -61,8 +66,11 @@ module.exports = { } ] }, - postcss: function () { - return [ autoprefixer ]; + eslint: { + configFile: path.join(__dirname, '.eslintrc') + }, + postcss: function() { + return [autoprefixer]; }, plugins: [ // TODO: infer from package.json? diff --git a/webpack.config.prod.js b/webpack.config.prod.js index 76a445057..2505a3e6a 100644 --- a/webpack.config.prod.js +++ b/webpack.config.prod.js @@ -30,7 +30,7 @@ module.exports = { preLoaders: [ { test: /\.js$/, - loader: 'eslint-loader', + loader: 'eslint', include: path.resolve(__dirname, relative, 'src') } ], @@ -38,7 +38,14 @@ module.exports = { { test: /\.js$/, include: path.resolve(__dirname, relative, 'src'), - loader: 'babel' + loader: 'babel', + query: { + presets: ['es2015', 'es2016', 'react'], + plugins: [ + 'transform-object-rest-spread', + 'transform-react-constant-elements' + ] + } }, { test: /\.css$/, @@ -59,8 +66,11 @@ module.exports = { } ] }, - postcss: function () { - return [ autoprefixer ]; + eslint: { + configFile: path.join(__dirname, '.eslintrc') + }, + postcss: function() { + return [autoprefixer]; }, plugins: [ // TODO: infer from package.json? -- GitLab