Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

we can use it without ejecting cra #48

Open
ylji1990 opened this issue Dec 23, 2020 · 2 comments
Open

we can use it without ejecting cra #48

ylji1990 opened this issue Dec 23, 2020 · 2 comments

Comments

@ylji1990
Copy link

I haven't found any way of integrating this into create-react-app scaffold project without ejecting the scripts and modifying them


we can use @craco/craco and add craco.config.js like this

const tsxControlStatements = require("tsx-control-statements");
const path = require("path");
const fs = require("fs");

const appDirectory = fs.realpathSync(process.cwd());
const resolveApp = (relativePath) => path.resolve(appDirectory, relativePath);
const appSrc = resolveApp("src");

// 动态修改配置
const overrideWebpackConfigPlugin = {
  plugin: {
    overrideWebpackConfig: ({ webpackConfig }) => {
      for (let _rule of webpackConfig.module.rules) {
        if (_rule.oneOf) {
          _rule.oneOf.unshift({
            test: /\.(ts|tsx)$/,
            include: appSrc,
            use: [
              {
                loader: require.resolve("ts-loader"),
                options: {
                  // disable type checker - we will use it in fork plugin
                  transpileOnly: true,
                  getCustomTransformers: () => ({
                    before: [tsxControlStatements.default()],
                  }),
                },
              },
            ],
          });
          break;
        }
      }
      //   console.log(JSON.stringify(webpackConfig.module.rules, null, 4));
      //   throw 1
      return webpackConfig;
    },
  },
};

module.exports = {
  plugins: [overrideWebpackConfigPlugin],
};

image
image

@KonstantinSimeonov
Copy link
Owner

@ylji1990, would you like to make a PR with the info added to the readme or maybe a new demo included? This is awesome! If you don't, I'll work on adding it when I have the time!

@kbzowski
Copy link

Does not work:

./src/index.tsx
TypeError: loaderContext.getOptions is not a function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants