From 1ac58c789e3f1aba49983078e17ffdd6b7005fc9 Mon Sep 17 00:00:00 2001 From: Konstantin Tarkus Date: Mon, 12 Mar 2018 21:20:14 +0300 Subject: [PATCH] Clean up react-app-tools before mergning CRA --- packages/react-app-tools/.gitignore | 9 - packages/react-app-tools/config/paths.js | 25 --- .../config/webpack.config.server.js | 77 --------- .../config/webpackDevServer.config.js | 38 ----- packages/react-app-tools/customize.js | 69 -------- packages/react-app-tools/package.json | 48 ------ packages/react-app-tools/scripts/patch.js | 161 ------------------ 7 files changed, 427 deletions(-) delete mode 100644 packages/react-app-tools/.gitignore delete mode 100644 packages/react-app-tools/config/paths.js delete mode 100644 packages/react-app-tools/config/webpack.config.server.js delete mode 100644 packages/react-app-tools/config/webpackDevServer.config.js delete mode 100644 packages/react-app-tools/customize.js delete mode 100644 packages/react-app-tools/package.json delete mode 100644 packages/react-app-tools/scripts/patch.js diff --git a/packages/react-app-tools/.gitignore b/packages/react-app-tools/.gitignore deleted file mode 100644 index 6434d9e2..00000000 --- a/packages/react-app-tools/.gitignore +++ /dev/null @@ -1,9 +0,0 @@ -bin/react-app.js -config/env.js -config/webpack.config.dev.js -config/webpack.config.prod.js -scripts/build.js -scripts/start.js -scripts/test.js -README.md -WebpackDevServerUtils.js diff --git a/packages/react-app-tools/config/paths.js b/packages/react-app-tools/config/paths.js deleted file mode 100644 index 73454c5f..00000000 --- a/packages/react-app-tools/config/paths.js +++ /dev/null @@ -1,25 +0,0 @@ -/** - * Copyright © 2016-present Kriasoft. All rights reserved. - * - * This source code is licensed under the MIT license found in the - * LICENSE.txt file in the root directory of this source tree. - */ - -'use strict'; - -const path = require('path'); -const paths = require('react-scripts/config/paths'); - -const { appPath, appSrc } = paths; -const serverBuild = paths.serverBuild || paths.appBuild; - -module.exports = Object.assign(paths, { - serverBuild, - serverBuildAppJs: path.join(serverBuild, 'app.js'), - appBuild: path.join(serverBuild, 'public'), - appIndexJs: path.join(appSrc, 'app.browser.js'), - override: path.join(appPath, 'override.js'), - appEntry: path.join(appSrc, 'app.node.js'), - serverEntry: path.join(appSrc, 'server.js'), - assets: path.join(serverBuild, 'assets.json'), -}); diff --git a/packages/react-app-tools/config/webpack.config.server.js b/packages/react-app-tools/config/webpack.config.server.js deleted file mode 100644 index a5671fdd..00000000 --- a/packages/react-app-tools/config/webpack.config.server.js +++ /dev/null @@ -1,77 +0,0 @@ -/** - * Copyright © 2016-present Kriasoft. All rights reserved. - * - * This source code is licensed under the MIT license found in the - * LICENSE.txt file in the root directory of this source tree. - */ - -'use strict'; - -const nodeExternals = require('webpack-node-externals'); -const InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin'); -const HtmlWebpackPlugin = require('html-webpack-plugin'); -const UglifyJsPlugin = require('uglifyjs-webpack-plugin'); -const SWPrecacheWebpackPlugin = require('sw-precache-webpack-plugin'); -const WriteFilePlugin = require('write-file-webpack-plugin'); -const customize = require('../customize'); -const paths = require('./paths'); - -module.exports = config => - customize( - 'webpack', - Object.assign({}, config, { - name: 'server', - target: 'node', - node: false, - - entry: { - app: paths.appEntry, - server: paths.serverEntry, - }, - - output: Object.assign({}, config.output, { - path: paths.serverBuild, - filename: '[name].js', - libraryTarget: 'commonjs2', - }), - - module: Object.assign({}, config.module, { - rules: config.module.rules.map(x => { - if (x.oneOf) { - return Object.assign({}, x, { - oneOf: x.oneOf.map(y => { - if (y.use && y.use[0] === require.resolve('style-loader')) { - return Object.assign({}, y, { - use: y.use.slice(1), - }); - } - return y; - }), - }); - } - return x; - }), - }), - - // Remove plugins that are not needed in the server-side bundle - plugins: config.plugins - .filter( - x => - !( - x instanceof InterpolateHtmlPlugin || - x instanceof HtmlWebpackPlugin || - x instanceof UglifyJsPlugin || - x instanceof SWPrecacheWebpackPlugin - ) - ) - .concat([ - new WriteFilePlugin({ - output: paths.serverBuild, - test: /^(.(?!hot-update))*$/, - }), - ]), - - externals: ['./assets.json', nodeExternals()], - }), - { target: 'node' } - ); diff --git a/packages/react-app-tools/config/webpackDevServer.config.js b/packages/react-app-tools/config/webpackDevServer.config.js deleted file mode 100644 index 6846e930..00000000 --- a/packages/react-app-tools/config/webpackDevServer.config.js +++ /dev/null @@ -1,38 +0,0 @@ -/** - * Copyright © 2016-present Kriasoft. All rights reserved. - * - * This source code is licensed under the MIT license found in the - * LICENSE.txt file in the root directory of this source tree. - */ - -'use strict'; - -const createConfig = require('react-scripts/config/webpackDevServer.config'); -const paths = require('./paths'); - -module.exports = function(proxy, allowedHost) { - const config = createConfig(proxy, allowedHost); - return Object.assign(config, { - historyApiFallback: false, - setup(app) { - app.get('/', (req, res, next) => { - global.appPromise - .then(() => { - const app = require(paths.serverBuildAppJs).default; - app.handle(req, res); - }) - .catch(next); - }); - }, - after(app) { - app.use((req, res, next) => { - global.appPromise - .then(() => { - const app = require(paths.serverBuildAppJs).default; - app.handle(req, res); - }) - .catch(next); - }); - }, - }); -}; diff --git a/packages/react-app-tools/customize.js b/packages/react-app-tools/customize.js deleted file mode 100644 index 0cf72273..00000000 --- a/packages/react-app-tools/customize.js +++ /dev/null @@ -1,69 +0,0 @@ -/** - * Copyright © 2016-present Kriasoft. All rights reserved. - * - * This source code is licensed under the MIT license found in the - * LICENSE.txt file in the root directory of this source tree. - */ - -'use strict'; - -const fs = require('fs'); -const HtmlWebpackPlugin = require('html-webpack-plugin'); -const paths = require('./config/paths'); - -// Make sure that including paths.js after env.js will read .env variables. -delete require.cache[require.resolve('react-scripts/config/paths')]; -delete require.cache[require.resolve('./config/paths')]; - -const override = fs.existsSync(paths.override) ? require(paths.override) : null; - -function overrideBabel(config, options) { - const newConfig = Object.assign({}, config); - - if (options.target === 'node') { - newConfig.compact = false; - } - - return override && typeof override.babel === 'function' - ? override.babel(newConfig, options) - : newConfig; -} - -module.exports = function customize(name, config, options) { - const newConfig = Object.assign({}, config, { - // Remove HtmlWebpackPlugin - plugins: config.plugins.filter(x => !(x instanceof HtmlWebpackPlugin)), - - // Find Babel config - module: Object.assign({}, config.module, { - rules: config.module.rules.map( - x => - x.oneOf - ? Object.assign({}, x, { - oneOf: x.oneOf.map( - y => - y.use - ? Object.assign({}, y, { - use: y.use.map( - z => - z.loader === require.resolve('babel-loader') && - z.options.presets[0] === - require.resolve('babel-preset-react-app') - ? Object.assign({}, z, { - options: overrideBabel(z.options, options), - }) - : z - ), - }) - : y - ), - }) - : x - ), - }), - }); - - return override && typeof override[name] === 'function' - ? override[name](newConfig, options) - : newConfig; -}; diff --git a/packages/react-app-tools/package.json b/packages/react-app-tools/package.json deleted file mode 100644 index e9677e79..00000000 --- a/packages/react-app-tools/package.json +++ /dev/null @@ -1,48 +0,0 @@ -{ - "name": "react-app-tools", - "version": "2.0.0-beta.10", - "description": "Build and test tooling for front-end application development with Node.js and React.", - "repository": "kriasoft/react-app", - "bugs": { - "url": "https://github.com/kriasoft/react-app/issues" - }, - "author": "Kriasoft (https://www.kriasoft.com)", - "contributors": [ - "Konstantin Tarkus (https://tarkus.me)" - ], - "license": "MIT", - "keywords": [ - "react", - "reactjs", - "react.js", - "server rendering", - "server-rendering", - "server-side rendering", - "server side rendering", - "pre-render", - "prerender", - "ssr" - ], - "engines": { - "node": ">=6" - }, - "files": [ - "bin", - "config", - "scripts", - "customize.js", - "README.md", - "WebpackDevServerUtils.js" - ], - "bin": { - "react-app": "./bin/react-app.js" - }, - "dependencies": { - "react-scripts": "2.0.0-next.47d2d941", - "webpack-node-externals": "^1.6.0", - "write-file-webpack-plugin": "^4.2.0" - }, - "scripts": { - "prepublishOnly": "cp ../../README.md . && yarn install && node ./scripts/patch" - } -} diff --git a/packages/react-app-tools/scripts/patch.js b/packages/react-app-tools/scripts/patch.js deleted file mode 100644 index ea2d3646..00000000 --- a/packages/react-app-tools/scripts/patch.js +++ /dev/null @@ -1,161 +0,0 @@ -/** - * Copyright © 2016-present Kriasoft. All rights reserved. - * - * This source code is licensed under the MIT license found in the - * LICENSE.txt file in the root directory of this source tree. - */ - -'use strict'; - -const fs = require('fs'); -const path = require('path'); - -try { - fs.mkdirSync(path.join(__dirname, '../bin')); -} catch (err) { - if (err.code !== 'EEXIST') { - throw err; - } -} - -function copy(from, to, fixes = []) { - let source = fs.readFileSync(require.resolve(from), 'utf8'); - - fixes.forEach(([find, replace]) => { - if ( - (typeof find === 'string' && source.indexOf(find) === -1) || - (typeof find === 'object' && !source.match(find)) - ) { - throw new Error(`Cannot find "${find}" in ${from}.`); - } - source = source.replace(find, replace); - }); - - fs.writeFileSync(path.resolve(__dirname, `../${to}`), source, 'utf8'); -} - -copy('react-scripts/bin/react-scripts.js', 'bin/react-app.js'); - -copy('react-scripts/config/env.js', 'config/env.js', [ - [ - /const stringified =[\s\S]*?\};/m, - `const stringified = Object.keys(raw).reduce((env, key) => { - env[\`process.env.$\{key}\`] = JSON.stringify(raw[key]); - return env; - }, {});`, - ], - [ - "delete require.cache[require.resolve('./paths')];", - "delete require.cache[require.resolve('react-scripts/config/paths')];\ndelete require.cache[require.resolve('./paths')];", - ], -]); - -copy( - 'react-scripts/config/webpack.config.dev.js', - 'config/webpack.config.dev.js', - [ - ['./polyfills', 'react-scripts/config/polyfills'], - [ - "const paths = require('./paths');", - "const customize = require('../customize');\nconst paths = require('./paths');", - ], - ['module.exports = {', "module.exports = customize('webpack', {"], - [ - 'hints: false,\n },\n};', - "hints: false,\n },\n}, { target: 'browser' });", - ], - ] -); - -copy( - 'react-scripts/config/webpack.config.prod.js', - 'config/webpack.config.prod.js', - [ - ['./polyfills', 'react-scripts/config/polyfills'], - ["['process.env'].NODE_ENV", "['process.env.NODE_ENV']"], - [ - "const paths = require('./paths');", - "const customize = require('../customize');\nconst paths = require('./paths');", - ], - ['module.exports = {', "module.exports = customize('webpack', {"], - [ - "child_process: 'empty',\n },\n};", - "child_process: 'empty',\n },\n}, { target: 'browser' });", - ], - ] -); - -copy('react-scripts/scripts/build.js', 'scripts/build.js', [ - ['./utils/', 'react-scripts/scripts/utils/'], - ["const paths = require('../config/paths');\n", ''], - [ - "const config = require('../config/webpack.config.prod');", - `const paths = require('../config/paths'); -const config = require('../config/webpack.config.prod'); -const configServer = require('../config/webpack.config.server')(config);`, - ], - ['webpack(config)', 'webpack([config, configServer])'], - ['(stats.toJson', '(stats.stats[0].toJson'], - [/(resolve\({\n\s+)stats,/, '$1stats: stats.stats[0],'], - [ - '[paths.appHtml, paths.appIndexJs]', - '[paths.appIndexJs, paths.appEntry, paths.serverEntry]', - ], - [ - 'printBrowsers(paths.appPath);', - 'printBrowsers(paths.appPath);\n fs.outputJsonSync(paths.assets, stats.toJson({}, true).assetsByChunkName, { spaces: 2 });', - ], -]); - -copy('react-scripts/scripts/start.js', 'scripts/start.js', [ - ['./utils/', 'react-scripts/scripts/utils/'], - ['react-dev-utils/WebpackDevServerUtils', '../WebpackDevServerUtils'], - [ - "const config = require('../config/webpack.config.dev');", - `const config = require('../config/webpack.config.dev'); -const configServer = require('../config/webpack.config.server')(config);`, - ], - ['webpack, config, appName', 'webpack, [config, configServer], appName'], - [ - '[paths.appHtml, paths.appIndexJs]', - '[paths.appIndexJs, paths.appEntry, paths.serverEntry]', - ], -]); - -copy('react-scripts/scripts/test.js', 'scripts/test.js', [ - ['./utils/', 'react-scripts/scripts/utils/'], -]); - -copy('react-dev-utils/WebpackDevServerUtils.js', 'WebpackDevServerUtils.js', [ - [/'\.\//g, "'react-dev-utils/"], - [ - "require('react-dev-utils/getProcessForPort');", - "require('react-dev-utils/getProcessForPort');\nconst paths = require('./config/paths');", - ], - [ - "console.log('Compiling...');", - `console.log('Compiling...'); - - global.appPromise = new Promise(resolve => { - global.appPromiseResolve = resolve; - });`, - ], - [ - ' });\n return compiler;', - ` - const assets = JSON.stringify(stats.stats[0].toJson({}, true).assetsByChunkName, null, ' '); - fs.writeFileSync(paths.assets, assets, 'utf8'); - delete require.cache[paths.assets]; - delete require.cache[paths.serverBuildAppJs]; - global.appPromiseResolve(); - });\n return compiler;`, - ], - [ - 'let isFirstCompile = true;', - `let isFirstCompile = true; - - global.appPromise = new Promise(resolve => { - global.appPromiseResolve = resolve; - });`, - ], -]);