Skip to content

Commit

Permalink
feat: auto-config for classic react runtime for internal code
Browse files Browse the repository at this point in the history
  • Loading branch information
Diablohu committed Feb 9, 2021
1 parent 8d3d34b commit 6b441aa
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 11 deletions.
32 changes: 22 additions & 10 deletions packages/koot-webpack/loaders/babel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ const testPluginName = (pluginObject, regExp) => {
);
};

/** 检查是否存在某个特定的 preset */
const hasPreset = (presets, regex) =>
presets.some((preset) => regex.test(preset.file.request));

const reactHotLoaderClientBlacklist = [
'koot/React/component-extender.jsx',
'koot/React/hoc/dev-hot.jsx',
'koot/ReactApp/client/index.jsx',
'koot/ReactApp/server/ssr.jsx',
'koot/ReactSPA/client/run.jsx',
];

// ============================================================================

/** _Koot.js_ 所在路径 */
Expand Down Expand Up @@ -108,7 +120,7 @@ module.exports = require('babel-loader').custom((babel) => {
// return cfg.options;
// }

let { __typescript = false } = customOptions;
const { __typescript: __typescript__ = false } = customOptions;
const {
__createDll,
__react,
Expand All @@ -117,13 +129,8 @@ module.exports = require('babel-loader').custom((babel) => {
__routes,
__i18n,
} = customOptions;
const {
presets,
plugins,
filename,
root,
...options
} = cfg.options;
const { presets, plugins, ...options } = cfg.options;
const { filename } = options;
const isServer =
__server || process.env.WEBPACK_BUILD_STAGE === 'server';
const isKootModule = !/^\.\./.test(
Expand All @@ -134,11 +141,15 @@ module.exports = require('babel-loader').custom((babel) => {

// make sure some settings correct ================================
// if (!__react) __react = /\.(jsx|tsx)$/.test(filename);
if (!__typescript) __typescript = /\.(ts|tsx)$/.test(filename);
// const __typescript = __typescript__ || /\.(ts|tsx)$/.test(filename);
const __typescript = __typescript__ || /\.(ts|tsx)$/.test(filename);

// presets ========================================================
const newPresets = [...presets];
if (__typescript) {
if (
__typescript &&
!hasPreset(newPresets, /@babel\/preset-typescript/)
) {
newPresets.unshift([
require('@babel/preset-typescript').default,
__react
Expand Down Expand Up @@ -259,6 +270,7 @@ module.exports = require('babel-loader').custom((babel) => {
if (
!__createDll &&
__react &&
!isKootModule &&
process.env.WEBPACK_BUILD_ENV === 'dev'
) {
// newPlugins.push(require('extract-hoc/babel'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
* Here's a hacky fix
*/
module.exports = function(source) {
module.exports = function (source) {
const expExportDefault = `export default _default`;
source = source.replace(
/(var _default = .+?;\n*)(;\n\n\(function \(\) \{\n[ ]*var reactHotLoader = )/gm,
Expand Down
2 changes: 2 additions & 0 deletions packages/koot-webpack/loaders/react-hot/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ function AddReactHotLoader(source) {
if (!__react) return source;
if (!source || !/^\s*export\s+default/m.exec(source)) return source;
if (/(\/|\\|^)root\.jsx$/.test(this.resourcePath)) return source;
if (/\/\*\s+__KOOT_DEV_NO_REACT_HOT_INJECT__\s+\*\//g.test(source))
return source;

let theSource = getImportLine() + source;

Expand Down
1 change: 1 addition & 0 deletions packages/koot/React/component-extender.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* __KOOT_DEV_NO_REACT_HOT_INJECT__ */
import { Component, forwardRef } from 'react';
import { connect } from 'react-redux';
// import { hot } from 'react-hot-loader'
Expand Down
1 change: 1 addition & 0 deletions packages/koot/React/hoc/dev-hot.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* __KOOT_DEV_NO_REACT_HOT_INJECT__ */
import { hot } from 'react-hot-loader';
import { forwardRef } from 'react';

Expand Down
1 change: 1 addition & 0 deletions packages/koot/ReactApp/client/index.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* __KOOT_DEV_NO_REACT_HOT_INJECT__ */
import * as fullConfig from '__KOOT_PROJECT_CONFIG_PORTION_OTHER_CLIENT_PATHNAME__';

import { hydrate } from 'react-dom';
Expand Down
1 change: 1 addition & 0 deletions packages/koot/ReactApp/server/ssr.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* __KOOT_DEV_NO_REACT_HOT_INJECT__ */
// import 'regenerator-runtime/runtime';
import { renderToString } from 'react-dom/server';
import { match, useRouterHistory as doUseRouterHistory } from 'react-router';
Expand Down
1 change: 1 addition & 0 deletions packages/koot/ReactSPA/client/run.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* __KOOT_DEV_NO_REACT_HOT_INJECT__ */
import ReactDOM from 'react-dom';
import history from '../../React/history';

Expand Down

0 comments on commit 6b441aa

Please sign in to comment.