2

I am working on a library that is imported into a React app created through create-react-app. Unfortunately, I am getting a lot of warnings that my d.ts files (from that library) are unable to parse.

./node_modules/my-library/json-schema/json-schema-model.d.ts 1:7
Module parse failed: Unexpected token (1:7)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> export declare class JsonSchema {
|     schema: string | null;
|     id: string | null;

Due to complexity, I don't know which parts of the code may be handy for you. I'm using [email protected]. I haven't ejected the app, so most configuration files are untouched, hidden inside react-scripts.

4
  • Could you please share your webpack.config.js? Commented Dec 28, 2021 at 3:13
  • I don't have one. The app was bootstrapped by create-react-app, so I don't have access to it. Commented Dec 28, 2021 at 3:16
  • 1
    Hi, just reporting on some progress. I think I found your repository with this issue. I think that the issue is created because of the manual linking. Also, for some reason, I am currentlly unable to build the dev branch? Master works tho. Commented Dec 28, 2021 at 3:40
  • Hi, that's the repository. Thank you. I have checked and dev branch should be ok. However the problem is also in the master branch. Regarding the linking, I'm doing npm install from the filesystem. If I'm not mistaken, that should be the same as installing from the npm registry. Commented Dec 28, 2021 at 4:01

1 Answer 1

1

So, the error is cause by webpack being confused about what kind of loader to use for your custom library. You need to teel it what kind of loader to use. I've decided that the best way would be to use react-app-rewired package to do that. This package allows you to make final tweeks to webpack configuration, before it start's to compile everything.

Before you start this tutorial, you should have model-driven-data already linked and dependencies installed.

So the first thing you need to do is install react-app-rewired in your project using this command: npm i -D react-app-rewired

The next thing you need to do is replace "react-scripts" with "react-app-rewired" in your "scripts" commands. This will allow us to inject custom webpack config changes before it executes.

Your package.json should now look like this:

look like this.

You are now ready to create config-overrides.js in your root folder (same folder as package.json).

In the config-overrides.js should be this code:

const path = require("path");
const fs = require("fs");

// Helper functions for folder join
const appDirectory = fs.realpathSync(process.cwd());
const resolveApp = (relativePath) => path.resolve(appDirectory, relativePath);

// I don't know what this does, but it's stole from original webconfig here: https://raw.githubusercontent.com/facebook/create-react-app/9673858a3715287c40aef9e800c431c7d45c05a2/packages/react-scripts/config/paths.js  (raw link, cuz until somebody goes crazy with rebase, it will be valid)
const hasJsxRuntime = (() => {
  if (process.env.DISABLE_NEW_JSX_TRANSFORM === "true") {
    return false;
  }

  try {
    require.resolve("react/jsx-runtime");
    return true;
  } catch (e) {
    return false;
  }
})();

// Inject changes to webpack
module.exports = function override(config, env) {
  config.module.rules[1].oneOf.push({
    test: /\.(js|mjs|jsx|ts|tsx)$/, // Regex for matching file endings
    include: resolveApp("node_modules/model-driven-data"), // Joined path to your custom library
    loader: require.resolve("babel-loader"), // Defautl resolver for js(x)/ts(x) files
    options: {
      customize: require.resolve("babel-preset-react-app/webpack-overrides"),
      presets: [
        [
          require.resolve("babel-preset-react-app"),
          {
            runtime: hasJsxRuntime ? "automatic" : "classic",
          },
        ],
      ],
    },
  });
  // Return newly edited config and execute it
  return config;
};

You can now run npm run build normally and it should work as expected. If you have any issues don't hesitate to write a comment :D

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.