@devloco/react-scripts-test
Version:
Configuration and scripts for Create React WP Theme.
296 lines (257 loc) • 8.82 kB
JavaScript
// @remove-on-eject-begin
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
// @remove-on-eject-end
;
// Do this as the first thing so that any code reading it knows the right env.
process.env.BABEL_ENV = 'development';
process.env.NODE_ENV = 'development';
// Makes the script crash on unhandled rejections instead of silently
// ignoring them. In the future, promise rejections that are not handled will
// terminate the Node.js process with a non-zero exit code.
process.on('unhandledRejection', err => {
throw err;
});
// Ensure environment variables are read.
require('../config/env');
// @remove-on-eject-begin
// Do the preflight check (only happens before eject).
const verifyPackageTree = require('./utils/verifyPackageTree');
if (process.env.SKIP_PREFLIGHT_CHECK !== 'true') {
verifyPackageTree();
}
const verifyTypeScriptSetup = require('./utils/verifyTypeScriptSetup');
verifyTypeScriptSetup();
// @remove-on-eject-end
const fs = require('fs-extra'); // wptheme - touched
const chalk = require('react-dev-utils/chalk');
const webpack = require('webpack');
//const WebpackDevServer = require('webpack-dev-server'); // wptheme - remarked out
const clearConsole = require('react-dev-utils/clearConsole');
const checkRequiredFiles = require('react-dev-utils/checkRequiredFiles');
const {
choosePort,
createCompiler,
prepareProxy,
prepareUrls,
} = require('react-dev-utils/WebpackDevServerUtils');
const openBrowser = require('react-dev-utils/openBrowser');
//const paths = require('../config/paths'); // wptheme - remarked out
//const configFactory = require('../config/webpack.config'); // wptheme - remarked out
//const createDevServerConfig = require('../config/webpackDevServer.config'); // wptheme - remarked out
// wptheme - added section - start
const paths = require('../config/paths-wptheme'); // wptheme - touched
const configFactory = require('../config/webpack.config.wptheme');
const config = configFactory('development');const appPackage = require(paths.appPackageJson);
const wpThemeUserConfig = require('@devloco/react-scripts-wptheme-utils/getUserConfig')(
paths,
process.env.NODE_ENV
);
const wpThemePostInstallerInfo = require('@devloco/react-scripts-wptheme-utils/postInstallerInfo');
const wpThemeFileFunctions = require('@devloco/react-scripts-wptheme-utils/fileFunctions');
const copyPublicFolder = wpThemeFileFunctions.copyPublicFolder;
const copyToThemeFolder = wpThemeFileFunctions.copyToThemeFolder;
const setupCopyToThemeFolder = wpThemeFileFunctions.setupCopyToThemeFolder;
const writeDoNotEditFile = wpThemeFileFunctions.writeDoNotEditFile;
const _wpThemeServer =
wpThemeUserConfig &&
wpThemeUserConfig.wpThemeServer &&
wpThemeUserConfig.wpThemeServer.enable === true
? require('@devloco/react-scripts-wptheme-utils/wpThemeServer')
: null;
// wptheme - added section - end
const useYarn = fs.existsSync(paths.yarnLockFile);
const isInteractive = process.stdout.isTTY;
// Warn and crash if required files are missing
if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {
process.exit(1);
}
// Tools like Cloud9 rely on this.
// const DEFAULT_PORT = parseInt(process.env.PORT, 10) || 3000;
// const HOST = process.env.HOST || '0.0.0.0';
// if (process.env.HOST) {
// console.log(
// chalk.cyan(
// `Attempting to bind to HOST environment variable: ${chalk.yellow(
// chalk.bold(process.env.HOST)
// )}`
// )
// );
// console.log(
// `If this was unintentional, check that you haven't mistakenly set it in your shell.`
// );
// console.log(
// `Learn more here: ${chalk.yellow('https://bit.ly/CRA-advanced-config')}`
// );
// console.log();
// }
// We require that you explictly set browsers and do not fall back to
// browserslist defaults.
const { checkBrowsers } = require('react-dev-utils/browsersHelper');
checkBrowsers(paths.appPath, isInteractive).then(() => {
// Using setTimeout just to put the call to startWatch() at the top of the file.
setTimeout(() => {
startWatch();
}, 0);
});
function startWatch() {
// Remove all content but keep the directory so that
// if you're in it, you don't end up in Trash
fs.emptyDirSync(paths.appBuild);
const injectWpThemeClient = function(wpThemeServer) {
if (!wpThemeUserConfig) {
return;
}
let clientConfig = wpThemeUserConfig.injectWpThemeClient;
if (
!clientConfig ||
typeof clientConfig.mode !== 'string' ||
clientConfig.mode === 'disable'
) {
return;
}
if (typeof clientConfig.override === 'function') {
clientConfig.override.call();
return;
}
let toInject = wpThemeServer.getClientInjectString(
clientConfig.mode,
clientConfig.token
);
if (fs.existsSync(clientConfig.file)) {
let fileContents = fs.readFileSync(clientConfig.file, 'utf8');
if (clientConfig.mode === 'endOfFile') {
fileContents += toInject;
} else {
fileContents = fileContents.replace(clientConfig.token, toInject);
}
fs.writeFileSync(clientConfig.file, fileContents);
} else {
console.log(
chalk.red(
`wpstart::injectWpThemeClient: ${clientConfig.file} was not found.`
)
);
process.exit();
}
};
let doLaunchBrowser = true;
const launchBrowser = function() {
openBrowser(appPackage.browserLaunchTo);
};
const webpackOutputFormat = {
chunks: false, // Makes the build much quieter
children: false,
colors: true,
modules: false,
};
return new Promise((resolve, reject) => {
const watchOptions = {
aggregateTimeout: 300,
poll: undefined,
};
// print the post init instructions
// doLaunchBrowser is only true on startup
if (
doLaunchBrowser &&
isInteractive &&
wpThemePostInstallerInfo.postInstallerExists(paths)
) {
clearConsole();
copyToThemeFolder(paths);
setupCopyToThemeFolder(paths);
console.log('Nodejs watcher is exiting...');
console.log(`${chalk.red('Your theme is not quite ready!')}`);
console.log(
"Now go to your WP site's admin area and set the site's theme to this new theme."
);
console.log(
`Then click "${chalk.cyan(
'Visit Site'
)}" to complete the PHP portion of the setup.`
);
console.log(
chalk.green('Once that is done, restart the Nodejs watcher.')
);
process.exit();
}
// Launch the refresh websocket server
// doLaunchBrowser is only true on startup
if (doLaunchBrowser && _wpThemeServer) {
_wpThemeServer.startServer(paths);
}
const appName = require(paths.appPackageJson).name;
const useTypeScript = fs.existsSync(paths.appTsConfig);
const urls = {
localUrlForTerminal: appPackage.browserLaunchTo,
};
const devSocket = {
warnings: warnings => {
if (_wpThemeServer) {
_wpThemeServer.update(warnings, "warnings");
}
},
errors: errors => {
if (_wpThemeServer) {
_wpThemeServer.update(errors, "errors");
}
}
};
// Create a webpack compiler that is configured with custom messages.
const compiler = createCompiler({
appName,
config,
devSocket,
urls,
useYarn,
useTypeScript,
webpack,
});
const watcher = compiler.watch(watchOptions, (err, stats) => {
if (err) {
// starting the watcher failed.
return console.log(err);
}
if (isInteractive) {
let buildCommand = useYarn ? 'yarn build' : 'npm run build';
clearConsole();
console.log(stats.toString(webpackOutputFormat));
console.log();
console.log('Note that the development build is not optimized.');
console.log(
`To create a production build, use ${chalk.cyan(buildCommand)}.`
);
console.log();
}
if (typeof stats.hasErrors === "function" && !stats.hasErrors()) {
// Merge with the public folder
copyPublicFolder(paths);
}
injectWpThemeClient(_wpThemeServer);
copyToThemeFolder(paths);
writeDoNotEditFile();
if (_wpThemeServer) {
_wpThemeServer.update(stats);
}
if (doLaunchBrowser) {
launchBrowser();
}
doLaunchBrowser = false;
});
['SIGINT', 'SIGTERM'].forEach(function(sig) {
process.on(sig, function() {
watcher.close();
process.exit();
});
});
}).catch(err => {
if (err && err.message) {
console.log(err.message);
}
process.exit(1);
});
}