question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Fails with complex usage scenario with `commonjs` and POSIX paths

See original GitHub issue

Apologies for not breaking this down to a minimal reproduction case, but I don’t have time right now and I thought you might be interested in the issue even without the full repro, and that you might understand what the fundamental issue is.

I have a pretty complex Rollup configuration:

module.exports = {
    entry: 'src/host/main.ts',
    dest: 'int/js/mainout.js',
    exports: "none",
    external: [
        "bluebird",
        "power-assert"
    ],
    globals: {
        "bluebird": "Promise",
        "power-assert": "assert"
    },
    useStrict: true,
    sourceMap: true,
    format: "iife",
    plugins: [
        json(),
        typescript({
            typescript: require('typescript'),
        }),
        nodeResolve({
            // use "jsnext:main" if possible
            // – see https://github.com/rollup/rollup/wiki/jsnext:main
            jsnext: true, // Default: false

            // use "main" field or index.js, even if it's not an ES6 module
            // (needs to be converted from CommonJS to ES6
            // – see https://github.com/rollup/rollup-plugin-commonjs
            main: true, // Default: true

            // if there's something your bundle requires that you DON'T
            // want to include, add it to 'skip'
            skip: [],  // Default: []

            // some package.json files have a `browser` field which
            // specifies alternative files to load for people bundling
            // for the browser. If that's you, use this option, otherwise
            // pkg.browser will be ignored
            browser: true, // Default: false

            // not all files you want to resolve are .js files
            extensions: [ '.js', '.json', '.ts' ], // Default: ['.js']
            sourceMap: true,

            // whether to prefer built-in modules (e.g. `fs`, `path`) or
            // local ones with the same names
            preferBuiltins: false // Default: true
        }),
        commonjs({
            extensions: [ '.js', '.ts' ],
            namedExports: {
                "javascript-astar": [ "Graph", "astar" ],
                "screenfull": [ "request" ],
                "marked": ["Renderer","setOptions","parse"],
            }
        }),
        buble({transforms: { dangerousForOf: true, dangerousTaggedTemplateString: true }})
    ]
};

This (mostly) works with the rollup-plugin-typescript plugin, but I thought I’d try this variant of the plugin instead. In part I wanted to try this because the last step above – buble – is not processing the TypeScript helpers injected by rollup-plugin-typescript. Since you’re doing something different, I thought that issue might be solved by switching to this plugin.

But when I switch my include to rollup-plugin-typescript2, I get an error:

Failed to build client-js: Error: Module C:/Users/tim/projects/games/steel/node_modules/javascript-astar/astar.js does not export astar (imported by C:/Users/tim/projects/games/steel/src/common/pathhelper.ts)
    at Module.trace (C:\Users\tim\projects\games\steel\node_modules\rollup\dist\rollup.js:6432:29)
    at C:\Users\tim\projects\games\steel\node_modules\rollup\dist\rollup.js:6006:32
...

This doesn’t even look like it’s in your code, but it doesn’t happen when I run the code through the older plugin, and I don’t have the time to dig down to the root cause here, so I need to fall back on the older plugin. Looking at the TypeScript options you’re setting:

noEmitHelpers: true,
importHelpers: true

… I thought one of these might be causing the problem, but no, when I set those it works using the old plugin. Not only that, but it fixes the problem I was having (because TypeScript is now using the imported helpers which buble correctly).

I have a TypeScript declaration file for javascript-astar, but honestly what it looks like is happening above is not a TypeScript bug but some kind of interaction problem between this plugin and the commonjs plugin (where I specify explicit exports for javascript-astar).

So if I were you and wanted to test this, I’d get an example together that imports some raw JavaScript using the node-resolve and commonjs plugins and see how rollup-plugin-typescript2 might be breaking commonjs.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:16 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
ezolenkocommented, May 7, 2022

That warning means there are cycles in import tree, not necessarily dependency cycles (not sure how useful the warning is, since modules can import and export multiple things and not all at once). So it is probably unrelated. I have it in my own code too for what its worth.

This looks like it could best be fixed in commonjs itself. I opened https://github.com/rollup/rollup-plugin-commonjs/issues/177 and left the hack turned off by default for now. In 0.2.2 now.

Thanks for figuring it out. 😃

0reactions
TimMenschcommented, Mar 9, 2017

OK, grabbed master and now I get this:

$ gulp client-js
[12:26:27] Using gulpfile ~\projects\games\Steel\gulpfile.js
[12:26:27] Starting 'client-js'...
rpt2: import tree has cycles
[12:26:28] Finished 'client-js' after 1.26 s

But at least it seems to work. 🤷. Maybe that warning is a hint as to why things are different?

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js can resolve commonjs file path after typescript ...
My typescript watcher/engine is broken and I thought it would be better to compile my .ts files myself and run them with node/node-watcher....
Read more >
Some notes on using esbuild - Hacker News
I got onboard reluctently because that's the only way to use the libraries I cared about. > problem 2: I don't understand frontend...
Read more >
Node.js v19.3.0 Documentation
In legacy assertion mode, error messages for objects display the objects, often truncated. To use strict assertion mode: import { strict as assert...
Read more >
Why the Hell Would I Use Node.js? A Case-by-case Tutorial
Learn why and when to use Node.js in this case-by-case tutorial. ... keeps your Node.js process up in production in the face of...
Read more >
node-ipc - npm
A great solution for complex multiprocess Neural Networking in Node.JS ... //es6 import ipc from 'node-ipc' //commonjs const ipc ...
Read more >

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found