|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const get = prop => value => value[prop]; |
| 4 | +const flatten = (others, next) => others.concat(next); |
| 5 | +const getLoadersFromRules = (rules, path, loaderName) => |
| 6 | + rules |
| 7 | + .filter(get(path)) |
| 8 | + .map(get(path)) |
| 9 | + .reduce(flatten, []) |
| 10 | + .filter(get("loader")) |
| 11 | + .filter(({ loader }) => loader.includes(loaderName)); |
| 12 | + |
| 13 | +const script = process.argv[2] || "start"; |
| 14 | +process.env.NODE_ENV = script === "build" ? "production" : "development"; |
| 15 | + |
| 16 | +const webpackConfigPath = `react-scripts/config/webpack.config.${ |
| 17 | + script === "build" ? "prod" : "dev" |
| 18 | +}`; |
| 19 | + |
| 20 | +// load original configs |
| 21 | +const webpackConfig = require(webpackConfigPath); |
| 22 | +if (!webpackConfig) { |
| 23 | + throw new Error(`no Webpack config found for: ${webpackConfigPath}`); |
| 24 | +} |
| 25 | +const { module: { rules = [] } = {} } = webpackConfig; |
| 26 | + |
| 27 | +const eslintLoaders = getLoadersFromRules(rules, "use", "eslint"); |
| 28 | +if (!eslintLoaders.length) { |
| 29 | + throw new Error( |
| 30 | + `missing ESLint config in webpack config: ${webpackConfigPath}` |
| 31 | + ); |
| 32 | +} |
| 33 | +const eslintConfig = eslintLoaders[0].options.baseConfig; |
| 34 | + |
| 35 | +const babelLoaders = getLoadersFromRules(rules, "oneOf", "babel"); |
| 36 | +if (!babelLoaders.length) { |
| 37 | + throw new Error( |
| 38 | + `missing Babel config in webpack config: ${webpackConfigPath}` |
| 39 | + ); |
| 40 | +} |
| 41 | +const babelOptions = babelLoaders[0].options; |
| 42 | + |
| 43 | +// override ESLint rules to allow using JSX with Hyperapp |
| 44 | +eslintConfig.rules = Object.assign(eslintConfig.rules || {}, { |
| 45 | + "react/react-in-jsx-scope": "off", |
| 46 | + "no-unused-vars": [ |
| 47 | + "warn", |
| 48 | + { |
| 49 | + varsIgnorePattern: "^h$" |
| 50 | + } |
| 51 | + ] |
| 52 | +}); |
| 53 | + |
| 54 | +// configure babel to allow using JSX with Hyperapp |
| 55 | +babelOptions.plugins = (babelOptions.plugins || []).concat([ |
| 56 | + ["transform-react-jsx", { pragma: "h", useBuiltIns: true }] |
| 57 | +]); |
| 58 | + |
| 59 | +// override config in cache |
| 60 | +require.cache[require.resolve(webpackConfigPath)].exports = webpackConfig; |
| 61 | + |
| 62 | +// call original react script |
| 63 | +require(`react-scripts/scripts/${script}.js`); |
0 commit comments