nrwl / nx

Smart Monorepos · Fast CI
https://nx.dev
MIT License
23.69k stars 2.36k forks source link

Rollup plugin enhancement #8575

Closed linbudu599 closed 1 year ago

linbudu599 commented 2 years ago

Description

There is now room for a number of enhancements to the rollup plugin, including but not limited to:

Motivation

Suggested Implementation

Alternate Implementations

vadistic commented 2 years ago

I'm using fully specified ESM import paths. @nrwl/web can handle those with babel, but not with swc.

It seem to be supported by swc/spack - but not configured for rollups' commonjs & node-resolve plugins. Idk.

https://github.com/swc-project/swc/issues/3043

    "build": {
      "executor": "@nrwl/web:rollup",
      "outputs": ["{options.outputPath}"],
      "options": {
        "outputPath": "dist/libs/my-lib",
        "tsConfig": "libs/my-lib/tsconfig.lib.json",
        "project": "libs/my-lib/package.json",
        "entryFile": "libs/my-lib/src/index.ts",
        "external": ["react/jsx-runtime"],
        "rollupConfig": "@nrwl/react/plugins/bundle-rollup",
        "compiler": "swc"
      }
    },
// index.ts
import { abort } from './two.js';

// two.ts
export function two() {};
[!] Error: unfinished hook action(s) on exit:
(commonjs) resolveId "./two.js" "/home/vadistic/work/my-project/libs/my-lib/src/index.ts"
(node-resolve) resolveId "./two.js" "/home/vadistic/work/my-project/libs/my-lib/src/index.ts"
perjerz commented 2 years ago

There is issue when you try import other libray files to app that use @nrwl/web:rollup (babel) as executor. Ex. // apps/bundler-js/src/main.ts

// apps/bundler-js/src/main.ts
import '../../../libs/shared-assets-backend/generator/main';

The error will be shown as

Error during bundle: /Users/xxx/yyy/apps/bundler-js/src/main.ts(2,8): semantic error TS6059: File '/Users/xxx/yyy/libs/shared-assets-backend/generator/main.ts' is not under 'rootDir' '/Users/xxx/yyy/apps/bundler-js/src'. 'rootDir' is expected to contain all source files.

The rootDir is set by entryFile folder.


 "projectType": "application",
  "root": "apps/bundler-js",
  "sourceRoot": "apps/bundler-js/src",
  "tags": [],
  "targets": {
    "build": {
      "executor": "@nrwl/web:rollup",
      "outputs": ["{options.outputPath}"],
      "defaultConfiguration": "production",
      "options": {
        "project": "apps/bundler-js/package.json",
        "entryFile": "apps/bundler-js/src/main.ts", // <-------------- entry Folder = apps/bundler-js/src
        "tsConfig": "apps/bundler-js/tsconfig.gulp.json",
        "outputPath": "dist/apps/bundler-js",
        "format": ["esm", "cjs"],
        "deleteOutputPath": true,

You can see the source code below.

https://github.com/nrwl/nx/blob/a622ee5f0851e652ee4b6284e4e4b001275a158d/packages/web/src/executors/rollup/lib/normalize.ts#L14-L20

To fix this problem, the entryRoot has to be root folder by default.

The workaround is to move main.ts to root project. and configure project.json and fix entryFile as below.

"options": {
        "project": "apps/bundler-js/package.json",
        "entryFile": "main.ts",
github-actions[bot] commented 1 year ago

This issue has been automatically marked as stale because it hasn't had any recent activity. It will be closed in 14 days if no further activity occurs. If we missed this issue please reply to keep it active. Thanks for being a part of the Nx community! 🙏

github-actions[bot] commented 1 year ago

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.