Skip to content

Commit

Permalink
refactor(mc-scripts): no need for webpack config files anymore, as th…
Browse files Browse the repository at this point in the history
…e default behavior (#1949)
  • Loading branch information
emmenko authored Jan 7, 2021
1 parent 3198d2c commit bb65fa1
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 62 deletions.
13 changes: 13 additions & 0 deletions .changeset/hip-needles-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
'merchant-center-application-template-starter': minor
'@commercetools-frontend/mc-scripts': minor
'playground': minor
---

The `webpack.config.dev.js` and `webpack.config.prod.js` files are not required anymore to be defined in the application folder and can be removed. The default behavior is now implicitly implemented in case the config file is not found. The default behavior requires the following paths to exist:

- `<application_folder>/dist`
- `<application_folder>/src`
- `<application_folder>/src/index.js`

> You can still use the config files if you need to configure more specific behaviors.
14 changes: 0 additions & 14 deletions application-templates/starter/webpack.config.dev.js

This file was deleted.

14 changes: 0 additions & 14 deletions application-templates/starter/webpack.config.prod.js

This file was deleted.

17 changes: 15 additions & 2 deletions packages/mc-scripts/src/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const checkRequiredFiles = require('react-dev-utils/checkRequiredFiles');
const formatWebpackMessages = require('react-dev-utils/formatWebpackMessages');
const FileSizeReporter = require('react-dev-utils/FileSizeReporter');
const printBuildError = require('react-dev-utils/printBuildError');
const createWebpackConfigForProduction = require('./config/create-webpack-config-for-production');

const measureFileSizesBeforeBuild =
FileSizeReporter.measureFileSizesBeforeBuild;
Expand All @@ -38,10 +39,15 @@ const resolveApp = (relativePath) => path.resolve(appDirectory, relativePath);
const paths = {
appBuild: resolveApp('dist/assets'),
appWebpackConfig: resolveApp('webpack.config.prod.js'),
distPath: resolveApp('dist'),
entryPoint: resolveApp('src/index.js'),
sourceFolders: [resolveApp('src')],
};

const hasWebpackConfig = fs.existsSync(paths.appWebpackConfig);

// Warn and crash if required files are missing
if (!checkRequiredFiles([paths.appWebpackConfig])) {
if (!checkRequiredFiles([])) {
process.exit(1);
}

Expand Down Expand Up @@ -101,7 +107,14 @@ measureFileSizesBeforeBuild(paths.appBuild)
function build(previousFileSizes) {
console.log('Creating an optimized production build...');

const compiler = webpack(require(paths.appWebpackConfig));
const config = hasWebpackConfig
? require(paths.appWebpackConfig)
: createWebpackConfigForProduction({
distPath: paths.distPath,
entryPoint: paths.entryPoint,
sourceFolders: paths.sourceFolders,
});
const compiler = webpack(config);
return new Promise((resolve, reject) => {
compiler.run((err, stats) => {
let messages;
Expand Down
16 changes: 14 additions & 2 deletions packages/mc-scripts/src/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const {
} = require('react-dev-utils/WebpackDevServerUtils');
const openBrowser = require('react-dev-utils/openBrowser');
const createDevServerConfig = require('./config/webpack-dev-server.config');
const createWebpackConfigForDevelopment = require('./config/create-webpack-config-for-development');

// Make sure any symlinks in the project folder are resolved:
// https://github.com/facebook/create-react-app/issues/637
Expand All @@ -41,6 +42,9 @@ const paths = {
appPublic: resolveApp('public'),
appWebpackConfig: resolveApp('webpack.config.dev.js'),
yarnLockFile: resolveApp('yarn.lock'),
distPath: resolveApp('dist'),
entryPoint: resolveApp('src/index.js'),
sourceFolders: [resolveApp('src')],
};

const useYarn = fs.existsSync(paths.yarnLockFile);
Expand All @@ -50,8 +54,10 @@ const isInteractive = process.stdout.isTTY;
// which is why it's disabled by default.
const hasReactRefresh = process.env.FAST_REFRESH === 'true';

const hasWebpackConfig = fs.existsSync(paths.appWebpackConfig);

// Warn and crash if required files are missing
if (!checkRequiredFiles([paths.appWebpackConfig])) {
if (!checkRequiredFiles([])) {
process.exit(1);
}

Expand All @@ -71,7 +77,13 @@ choosePort(HOST, DEFAULT_PORT)
const appName = require(paths.appPackageJson).name;
const urls = prepareUrls(protocol, HOST, port);
// Get webpack config
const config = require(paths.appWebpackConfig);
const config = hasWebpackConfig
? require(paths.appWebpackConfig)
: createWebpackConfigForDevelopment({
distPath: paths.distPath,
entryPoint: paths.entryPoint,
sourceFolders: paths.sourceFolders,
});
const devSocket = {
warnings: (warnings) =>
// eslint-disable-next-line @typescript-eslint/no-use-before-define
Expand Down
14 changes: 0 additions & 14 deletions playground/webpack.config.dev.js

This file was deleted.

14 changes: 0 additions & 14 deletions playground/webpack.config.prod.js

This file was deleted.

2 changes: 0 additions & 2 deletions website/src/content/development/folder-structure.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ After bootstrapping a new application, your project should look like this:
│   ├── index.js
│   ├── load-messages.js
│   └── routes.js
├── webpack.config.dev.js
├── webpack.config.prod.js
└── yarn.lock
```

Expand Down

0 comments on commit bb65fa1

Please sign in to comment.