Search code examples
swc-compiler

Import JSON file in .ts and compile it with SWC


I have a typescript file that import a json file like this:

import DefaultMapping from "./default.json";

It compile with SWC like this in the js file:

var _defaultJson = require("./default.json");

However, SWC does not copy the JSON file in the build folder. Therefore, i have an error when I run the js file:

Error: Cannot find module './default.json'

Can someone help me please?

EDIT: I solve it partially by using require instead of import in the ts file and also using the --copy-files swc option. However, I still would like to NOT use require and use import instead if someone has an idea


Solution

  • Ok, I solved it by using the --copy-files options.

    Here is my command: swc ./src --out-dir dist --copy-files