forked from facebook/create-react-app
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a staging environment to create webpack staging builds.
- Loading branch information
Edo
committed
Oct 4, 2016
1 parent
38a1f27
commit f46a890
Showing
8 changed files
with
132 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
node_modules/ | ||
build | ||
staging | ||
.DS_Store | ||
*.tgz | ||
my-app* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// @remove-on-eject-begin | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
// @remove-on-eject-end | ||
|
||
var webpack = require('webpack'); | ||
var HtmlWebpackPlugin = require('html-webpack-plugin'); | ||
var paths = require('./paths'); | ||
var config = require('./webpack.config.prod'); | ||
|
||
module.exports = Object.assign(config, { | ||
output: Object.assign(config.output, { | ||
path: paths.appStaging, | ||
}), | ||
plugins: [new HtmlWebpackPlugin({ | ||
inject: true, | ||
template: paths.appHtml | ||
})].concat(config.plugins.filter(plugin => | ||
!(plugin instanceof webpack.optimize.UglifyJsPlugin) && | ||
!(plugin instanceof HtmlWebpackPlugin) | ||
)) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// @remove-on-eject-begin | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
// @remove-on-eject-end | ||
|
||
// Do this as the first thing so that any code reading it knows the right env. | ||
process.env.NODE_ENV = 'staging'; | ||
|
||
// Load environment variables from .env file. Suppress warnings using silent | ||
// if this file is missing. dotenv will never modify any environment variables | ||
// that have already been set. | ||
// https://github.com/motdotla/dotenv | ||
require('dotenv').config({silent: true}); | ||
|
||
var chalk = require('chalk'); | ||
var fs = require('fs-extra'); | ||
var rimrafSync = require('rimraf').sync; | ||
var webpack = require('webpack'); | ||
var config = require('../config/webpack.config.staging'); | ||
var paths = require('../config/paths'); | ||
var checkRequiredFiles = require('react-dev-utils/checkRequiredFiles'); | ||
var recursive = require('recursive-readdir'); | ||
|
||
// Warn and crash if required files are missing | ||
if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) { | ||
process.exit(1); | ||
} | ||
|
||
recursive(paths.appStaging, (err, fileNames) => { | ||
// Remove all content but keep the directory so that | ||
// if you're in it, you don't end up in Trash | ||
rimrafSync(paths.appStaging + '/*'); | ||
|
||
// Start the webpack build | ||
build(); | ||
|
||
// Merge with the public folder | ||
copyPublicFolder(); | ||
}); | ||
|
||
|
||
// Create the staging build and print the deployment instructions. | ||
function build() { | ||
console.log('Creating a staging build...'); | ||
webpack(config).run((err, stats) => { | ||
if (err) { | ||
console.error('Failed to create a staging build. Reason:'); | ||
console.error(err.message || err); | ||
process.exit(1); | ||
} | ||
|
||
console.log(chalk.green('Compiled successfully.')); | ||
console.log(); | ||
|
||
var openCommand = process.platform === 'win32' ? 'start' : 'open'; | ||
var homepagePath = require(paths.appPackageJson).homepage; | ||
var publicPath = config.output.publicPath; | ||
if (publicPath !== '/') { | ||
// "homepage": "http://mywebsite.com/project" | ||
console.log('The project was built assuming it is hosted at ' + chalk.green(publicPath) + '.'); | ||
console.log('You can control this with the ' + chalk.green('homepage') + ' field in your ' + chalk.cyan('package.json') + '.'); | ||
console.log(); | ||
console.log('The ' + chalk.cyan('staging') + ' folder is ready to be deployed.'); | ||
console.log(); | ||
} else { | ||
// no homepage or "homepage": "http://mywebsite.com" | ||
console.log('The project was built assuming it is hosted at the server root.'); | ||
if (homepagePath) { | ||
// "homepage": "http://mywebsite.com" | ||
console.log('You can control this with the ' + chalk.green('homepage') + ' field in your ' + chalk.cyan('package.json') + '.'); | ||
console.log(); | ||
} | ||
console.log('The ' + chalk.cyan('staging') + ' folder is ready to be deployed.'); | ||
console.log('You may also serve it locally with a static server:') | ||
console.log(); | ||
console.log(' ' + chalk.cyan('npm') + ' install -g pushstate-server'); | ||
console.log(' ' + chalk.cyan('pushstate-server') + ' staging'); | ||
console.log(' ' + chalk.cyan(openCommand) + ' http://localhost:9000'); | ||
console.log(); | ||
} | ||
}); | ||
} | ||
|
||
function copyPublicFolder() { | ||
fs.copySync(paths.appPublic, paths.appStaging, { | ||
dereference: true, | ||
filter: file => file !== paths.appHtml | ||
}); | ||
} |