microsoft / microsoft/TypeScript

Bundling typescript using webpack: the request of a dependency is an expression (+possible fix)

Open
#39,436 22 comments 35 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Experience Enhancement Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 3.9.6 and 4.0.0-dev.20200706

Search Terms:
webpack request of a dependency expression

Code

import ts from 'typescript';
console.log(ts)

Expected behavior:
Bundle successfully, and without warnings, using webpack.

Actual behavior:
Bundles successfully, but a warning is shown:

WARNING in ./node_modules/typescript/lib/typescript.js 5710:41-60
Critical dependency: the request of a dependency is an expression

Playground Link:
N/A

Related Issues:
Couldn't find any that talked about this issue

Suggested fix:

The warning is printed due to the following dynamic require call:

require: function (baseDir, moduleName) {
    try {
        var modulePath = ts.resolveJSModule(moduleName, baseDir, nodeSystem);
        return { module: require(modulePath), modulePath: modulePath, error: undefined };
    }
    catch (error) {
        return { module: undefined, modulePath: undefined, error: error };
    }
}

Rather than calling require directly, create a helper function:

function requireModule(requestingModule, specifier) {
    return requestingModule.require(specifier)
}

And call it the following way:

return { module: requireModule(module, modulePath), modulePath: modulePath, error: undefined };

The resulting code will behave 1:1 in Node, while not triggering any warnings in bundlers trying to resolve these dynamic calls.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the warning by bundling the shown TypeScript import with webpack, then inspect typescript/lib/typescript.js around 5710:41-60. Evaluate the proposed requireModule approach and verify that the bundle completes without the critical-dependency warning while preserving the Node behavior described in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript, webpack
Domain
build-system
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.