简体   繁体   中英

Rollup cannot resolve dependencies without an extension (same file extension import)

I have an express server written in Typescript that I'd like to bundle with Rollup. The server might use some code from the client which's why I'm trying to bundle it.
The issue I've encountered is that Rollup can't resolve import of files when the file extension is not specified (the extension is the same as the importing file.

Eg
import { getConfiguration } from "@server/configuration/configuration";

results in the log (under (!) Unresolved dependencies) C:\whatever\server/configuration/configuration (imported by src/server/server.ts)

And the final server.js file attempts to (incorrectly) require the bundle
var configuration = require('./configuration/configuration');

Updating the import to
import { getConfiguration } from "@server/configuration/configuration.ts";
handles this particular case, but I don't want to rewrite all imports for the whole codebase.

The rollup config is as follows

import { resolve } from "path";
import commonjs from "@rollup/plugin-commonjs";
import { nodeResolve } from "@rollup/plugin-node-resolve";
import json from "@rollup/plugin-json";
import { babel } from "@rollup/plugin-babel";

const extensions = [".js", ".jsx", ".es6", ".es", ".mjs", "ts", "tsx"];

export default {
    input: "src/server/server.ts",
    output: {
        dir: "dist/server",
        format: "cjs",
    },
    // the SSR entry is transpiled by Vite
    external: [/entryServer/i],
    plugins: [
        json(),
        nodeResolve({
            preferBuiltins: true,
        }),
        commonjs(),
        babel({
            babelHelpers: "bundled",
            exclude: "node_modules/**",
            configFile: resolve(__dirname, "babel.node.js"),
            extensions,
        }),
    ],
};

Is there a way I can for instance rewrite the imports by adding the extension of the importing file?
Or am I approaching this wrong and should a be using a different tool for the TS + aliases transpilation?

While not an answer how to handle this with rollup I solved the issue by building the server with esbuild.
As a nice bonus it's also a lot faster.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM