Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[help]bundle exec error which should output a dependency tree built by rollup, ansiStyles.color.close #4861

Open
fionaInBits opened this issue Feb 16, 2023 · 3 comments

Comments

@fionaInBits
Copy link

I'm developing a tool that can output a dependency tree of program with @babel/core, in development mode, it runs well

 "dev": "node -r ts-node/register src/index.ts"

but after building by rollup, the output runs with error.

/Projects/dependency-analysis/dist/node/index.cjs:28410
ansiStyles.color.closeRe = new RegExp(escapeStringRegexp(ansiStyles.color.close), 'g');
^
TypeError: Cannot read properties of undefined (reading 'close')
at /Users/liangfangfang/Projects/dependency-analysis/dist/node/index.cjs:28410:77
at requireChalk (/Users/liangfangfang/Projects/dependency-analysis/dist/node/index.cjs:28567:3)
at requireLib$b (/Users/liangfangfang/Projects/dependency-analysis/dist/node/index.cjs:28588:15)
at requireLib$a (/Users/liangfangfang/Projects/dependency-analysis/dist/node/index.cjs:28706:19)
at requireParse$2 (/Users/liangfangfang/Projects/dependency-analysis/dist/node/index.cjs:43979:19)
at requireString (/Users/liangfangfang/Projects/dependency-analysis/dist/node/index.cjs:44285:15)
at requireBuilder (/Users/liangfangfang/Projects/dependency-analysis/dist/node/index.cjs:44391:16)
at requireLib$8 (/Users/liangfangfang/Projects/dependency-analysis/dist/node/index.cjs:44467:17)
at Object. (/Users/liangfangfang/Projects/dependency-analysis/dist/node/index.cjs:44497:19)
at Module._compile (node:internal/modules/cjs/loader:1246:14)

I figured out that the code is in ansi-styles v3.2.1 which is referred by chalk v2.0.0, and chalk v2.0.0. is referred by @babel/highlight -> @babel/code-frame -> @babel/core。I guess the problem occurred because Rollup did not treat commonjs as well as modulejs.
chalk v2.0.0 does not support esmodule mode.

Notification: in my package.json, type is set to "module".
Is there a workaround ?

source code

import traverse, { NodePath } from '@babel/traverse';
import { transformFileSync } from '@babel/core';

...

  routeEntry?.forEach(routePath => {
    const fullPath = resolvePath({ filePath: routePath });
    const res = transformFileSync(fullPath, TRANSFORM_OPTIONS);
    log('路由AST:', res?.ast);

    // 变量字典, 用于根据组件名获取importPath
    const cmpPathDict = getCmpPathDict(res?.ast);

    // 遍历ast
    traverse(res?.ast, {
      // <Component />
      JSXElement($path) {
        attachNode($path, cmpPathDict, routePath);
      },

      // <></>
      JSXFragment($path) {
        attachNode($path, cmpPathDict, routePath);
      },
    });
  });

rollup.config.js

import { babel } from '@rollup/plugin-babel'
import commonjs from '@rollup/plugin-commonjs'
import json from "@rollup/plugin-json"
import resolve, { nodeResolve } from '@rollup/plugin-node-resolve'
// import replace from '@rollup/plugin-replace'
// import uglify from 'rollup-plugin-uglify'
// import shebang from 'rollup-plugin-shebang-bin'

const extensions = ['.js', '.jsx', '.ts', '.tsx', '.vue']
export default {
  input: './src/index.ts', // 入口文件
  output: {
    file: './dist/node/index.cjs', // 出口文件
    name: 'index', // 出口文件
    format: 'cjs' // 输出的模块语法格式
  },
  plugins: [
    babel({ 
      babelHelpers: 'bundled',
      exclude: 'node_modules/**',
      extensions,
      presets: [
      '@babel/preset-env',
      '@babel/preset-typescript'
      ],
  }),
    commonjs(),
    resolve({
        mainFields: ['module', 'main', 'jsnext:main', 'browser'],
        extensions,
        customResolveOptions: {
          moduleDirectories: ['node_modules']
        }
    }),
    nodeResolve({
        exclude: 'node_modules/**',
    }),
    json(),
    // replace({
    //   "const shebang = '#!/usr/bin/env node'": JSON.stringify('#!/usr/bin/env node')
    // })
    // uglify.uglify()
    // shebang(),
]
}
@bpetii
Copy link

bpetii commented Feb 16, 2023

Having the same issue with jsondiffpatch package using vite.
First, I thought it's vite issue, but then I saw they updated the commonjs dependecy which breaks it.
You can check the two reproduction links: vitejs/vite#11986

@lukastaegert
Copy link
Member

Probably an issue for @rollup/plugin-commonjs instead.

@fionaInBits
Copy link
Author

Probably an issue for @rollup/plugin-commonjs instead.

Right. I installed @rollup/plugin-commonjs@23.0.4 and it works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants