Skip to content
This repository has been archived by the owner on Apr 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #25 from Nargonath/develop
Browse files Browse the repository at this point in the history
v1.4.0
  • Loading branch information
Nargonath authored Jan 13, 2019
2 parents a1d0e24 + 53ef9d9 commit cb78244
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ If those defaults do not work for you, the script accepts some arguments:

* `-b|--build-path`: expects either an absolute or relative path. If a relative path is given it will be prefixed by your project root path.
* default: `yourProjectRoot/build`.
* `--react-scripts-version`: expects the `react-scripts` version you are using in your project i.e `2.0.3`. If not given it will be implied from your package.json and if it cannot be implied the version `2.0.4` will be the default. Consider setting it if you ejected.
* `--react-scripts-version`: expects the `react-scripts` version you are using in your project i.e `2.0.3`. If not given it will be implied from your `node_modules` and if it cannot be implied the version `2.1.2` will be the default. Consider setting it if you **ejected** and are not using the latest `react-scripts` version.
* `-p|--public-path`: expects a relative URL where `/` is the root. If you serve your files using an external webserver this argument is to match with your web server configuration. More information can be found in [webpack configuration guide](https://webpack.js.org/configuration/output/#output-publicpath).
* default: "".
* `-v|--verbose`: display webpack build output.
29 changes: 23 additions & 6 deletions scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,21 @@ const {
flags: { buildPath, publicPath, reactScriptsVersion, verbose },
} = require('../utils/cliHandler');
const { getReactScriptsVersion, isEjected } = require('../utils');

const { major, minor, patch } = getReactScriptsVersion(reactScriptsVersion);

const paths = isEjected ? importCwd('./config/paths') : importCwd('react-scripts/config/paths');
const webpack = importCwd('webpack');
const config = isEjected
? importCwd('./config/webpack.config.dev')
: importCwd('react-scripts/config/webpack.config.dev');

const config =
major >= 2 && minor >= 1 && patch >= 2
? (isEjected
? importCwd('./config/webpack.config')
: importCwd('react-scripts/config/webpack.config'))('development')
: isEjected
? importCwd('./config/webpack.config.dev')
: importCwd('react-scripts/config/webpack.config.dev');

const HtmlWebpackPlugin = importCwd('html-webpack-plugin');
const InterpolateHtmlPlugin = importCwd('react-dev-utils/InterpolateHtmlPlugin');
const getClientEnvironment = isEjected
Expand Down Expand Up @@ -50,10 +60,17 @@ config.output.filename = `js/bundle.js`;
config.output.chunkFilename = `js/[name].chunk.js`;

// update media path destination
const { major, minor, patch } = getReactScriptsVersion(reactScriptsVersion);

if (major >= 2) {
const oneOfIndex = minor >= 1 || patch >= 4 ? 2 : 3;
// 2.0.0 => 2
// 2.0.1 => 3
// 2.0.2 => 3
// 2.0.3 => 3
// 2.0.4 => 2
// 2.1.0 => 2
// 2.1.1 => 2
// 2.1.2 => 2
// 2.1.3 => 2
const oneOfIndex = minor === 0 && patch < 4 && patch >= 1 ? 3 : 2;
config.module.rules[oneOfIndex].oneOf[0].options.name = `media/[name].[hash:8].[ext]`;
config.module.rules[oneOfIndex].oneOf[7].options.name = `media/[name].[hash:8].[ext]`;
} else {
Expand Down
18 changes: 9 additions & 9 deletions utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ const path = require('path');

const DEFAULT_VERSION = {
major: 2,
minor: 0,
patch: 4,
minor: 1,
patch: 2,
};

exports.isEjected = fs.pathExistsSync(path.join(process.cwd(), 'config/webpack.config.dev.js'));
exports.isEjected = fs.pathExistsSync(path.join(process.cwd(), 'config/paths.js'));

exports.getReactScriptsVersion = function getReactScriptsVersion(cliVersion) {
if (cliVersion) {
Expand All @@ -23,16 +23,16 @@ exports.getReactScriptsVersion = function getReactScriptsVersion(cliVersion) {
return versions;
}

const packageJson = importCwd.silent('./package.json');
if (!packageJson || !packageJson.dependencies['react-scripts']) {
const reactScriptsPkg = importCwd.silent('react-scripts/package.json');
if (!reactScriptsPkg || !reactScriptsPkg.version) {
return DEFAULT_VERSION;
}

const { dependencies } = packageJson;
const { version } = reactScriptsPkg;
const versions = {
major: Number(semver.major(dependencies['react-scripts'])),
minor: Number(semver.minor(dependencies['react-scripts'])),
patch: Number(semver.patch(dependencies['react-scripts'])),
major: Number(semver.major(version)),
minor: Number(semver.minor(version)),
patch: Number(semver.patch(version)),
};
return versions;
};

0 comments on commit cb78244

Please sign in to comment.