rollup/rollup-plugin-babel

Unresolved dependencies core-js

bosens-China opened this issue · 11 comments

Output information

https://rollupjs.org/guide/en/#warning-treating-module-as-external-dependency
core-js/modules/es.array.from (imported by src\i.js)
core-js/modules/es.string.iterator (imported by src\i.js)

expect

Packing Dependencies Correctly into Output Files

configure

babel.config.js

module.exports = {
  presets: [
    [
      '@babel/preset-env',
      {
        useBuiltIns: 'usage',
        // useBuiltIns: 'entry',
        corejs: 3,
        modules: false,
      },
    ],
    ['@babel/preset-typescript'],
  ],
  plugins: [
    '@babel/plugin-transform-typescript',
    '@babel/plugin-syntax-dynamic-import',
    '@babel/plugin-proposal-class-properties',
  ],
};

rollup.config.js

import babel from 'rollup-plugin-babel';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import { terser } from 'rollup-plugin-terser';
export default {
  input: './src/index.ts',
  output: [
    {
      file: 'dist/index.esm.js',
      format: 'cjs',
      sourcemap: true,
    },
    {
      file: 'dist/index.js',
      format: 'umd',
      sourcemap: true,
      name: 'lazyLoad',
    },
    {
      file: 'dist/index.esm.browser.js',
      format: 'es',
      sourcemap: true,
    },
  ],
  plugins: [
    resolve(),
    commonjs(),
    // terser(),
    babel({
      extensions: ['js', 'ts'],
    })
  ],
};

I think it's the Babel 7 useBuiltIns problem, but what should I do?

You need to add core-js as a dependency if you are using useBuiltIns: 'usage'. https://babeljs.io/docs/en/babel-preset-env#usebuiltins

This option adds direct references to the core-js module as bare imports. Thus core-js will be resolved relative to the file itself and needs to be accessible. You may need to specify core-js@2 as a top level dependency in your application if there isn't a core-js dependency or there are multiple versions.

@loganfsmyth
You're right. I looked up the dependencies and found that corejs was installed.

@loganfsmyth: What do you mean exactly by add core-js as a dependency?

@drzraf Add it in your package.json's dependencies.

adding as a dependency still doesnt fix the unresolved dependencies for all the runtime helpers

Is there any followup to this? I'm also experiencing this issue.

Which file is requiring core-js?

I'd solved this problem by adding 'exclude' option for babel-plugin.
exclude: 'node_modules/core-js/**',
Attention: for yarn or pnpm could be need another path.
Babel shouldn't transpilling core-js code.

i am also getting the circular dependencies error even though i've excluded core-js from babel. my babel config inside rollup.config.js:

		babel({
			extensions: [ '.js', '.mjs', '.html', '.svelte' ],
			runtimeHelpers: true,
			exclude: [ 'node_modules/@babel/**', 'node_modules/core-js/**' ],
			presets: [
			  [
				'@babel/preset-env',
				{
					targets: '> 0.25%, not dead',
					useBuiltIns: 'usage',
					corejs: 3
				}
			  ]
			],
			plugins: [
			  '@babel/plugin-syntax-dynamic-import',
			  [
				'@babel/plugin-transform-runtime',
				{
				  useESModules: true
				}
			  ]
			]
		  }),	

any more hints/ideas?

ah sorry fixed, forgot to add "core-js": "^3.6.4" as a dependency inside package.json