Closed peachest closed 4 months ago
I will give it a try, but can I ask if the project compiles without immutable
?
I will reproduce the bug and see what can it be done. I did test 9.2.0 with TS and it worked. Still, your issue helped me to find that Rambdax has issues regarding latest changes, so in any case, I want to thank you for opening it.
I will give it a try, but can I ask if the project compiles without
immutable
?I will reproduce the bug and see what can it be done. I did test 9.2.0 with TS and it worked. Still, your issue helped me to find that Rambdax has issues regarding latest changes, so in any case, I want to thank you for opening it.
Oh,I want to build my project as a lib but not app, so I use rollup-plugin-node-externals
to exclude all dependencies.
The full rollup.config.js
import path from "node:path";
import url from "node:url";
import commonjs from "@rollup/plugin-commonjs";
import json from "@rollup/plugin-json";
import nodeResolve from "@rollup/plugin-node-resolve";
import fs from "fs-extra";
import json5 from "json5";
import clear from "rollup-plugin-clear";
import externals from "rollup-plugin-node-externals";
import typescript from "rollup-plugin-typescript2";
const __filename = url.fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const resolve = p => path.resolve(__dirname, p);
const packageJSONPath = resolve("package.json");
const tsconfigJSONPath = resolve("tsconfig.json");
const packageJson = json5.parse(fs.readFileSync(packageJSONPath).toString());
const tsconfigJson = json5.parse(fs.readFileSync(tsconfigJSONPath).toString());
const {
outDir: outputDirectory
} = tsconfigJson["compilerOptions"];
export default {
input: "src/index.ts",
output: {
dir: "./dist",
format: "es",
exports: "named",
preserveModules: true,
},
plugins: [
clear({
targets: ["./dist"],
}),
json(),
nodeResolve({
preferBuiltins: false,
}),
commonjs(),
typescript({
tsconfigJson,
}),
externals(),
],
};
The output structure is following
dist
`-- index.js
0 directories, 1 file
and the output index.js
only container my project code without Rambda
The above issue is reproduce in this way.
extra problem
I also test not using rollup-plugin-node-externals
as rollup plugins, and the output dir structure is
dist
|-- _virtual
| `-- rambda.js
|-- node_modules
| `-- rambda
| |-- dist
| | `-- rambda.js
| `-- immutable.js
`-- src
`-- index.js
5 directories, 4 files
However,in this cas,execute node ./dist/src/index.js
will occur
import { i as immutable } from '../node_modules/rambda/immutable.js';
^
SyntaxError: Named export 'i' not found. The requested module '../node_modules/rambda/immutable.js' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:
import pkg from '../node_modules/rambda/immutable.js';
const { i: immutable } = pkg;
at ModuleJob._instantiate (node:internal/modules/esm/module_job:132:21)
at async ModuleJob.run (node:internal/modules/esm/module_job:214:5)
at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)
at async loadESM (node:internal/process/esm_loader:28:7)
at async handleMainPromise (node:internal/modules/run_main:113:12)
Node.js v20.11.0
But I check the bundled rambda/immutable.js file, which is
import './dist/rambda.js';
import { __exports as rambda } from '../../_virtual/rambda.js';
var immutable = rambda;
export { immutable as i };
And obviously it has exported i
。
I spent some time exploring options of using exports
object in package.json
vs using main
property and I found that using main
is causing less trouble, so I don't think that I will change that. Still, when I have time, I will check what is the root cause for your case.
Just one question - what happens if you don't use immutable
as I think this is important to know?
I import rambda by rambda/immutable
to use readonly
type declaration for typescript.
Or do you have some way to use readonly
type declaration when coding and compiling without immutable
I mean, is the issue happens if you simply import rambda
not rambda/immutable
? This will be helpful when debugging.
I am closing this as I needed the last feedback. Feel free to reopen it with answer to it.
Describe the bug
The following code and configuration is a small demo to reproduce the real problem I met.
I am wrting TypeScript and import Rambda by path
rambda/immutable
.After bundling with Rollup, Node cannot resolve and execute the bundled file.
Context(which version of library)
version description for key package:
My package.json:
My tsconfig.json
My rollup.config.js (only key config)
My ts code
Bundled js file of Rollup
Advice
Instead of using in your package.json:
Use
exports
field:Without changing anything of code in my project, Node.js now can resolve
rambda/immutable
package correctly:warning: I havn't test umd and
require
.