Giter Club home page Giter Club logo

rollup-plugin-node-externals's People

Contributors

arnesfield avatar septh avatar tomeraberbach avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

rollup-plugin-node-externals's Issues

feature: externalize imports from the same package

Overall, really liking this library. The one feature that I wish it had was that it automatically externalized imports from the same package I'm building. A common pattern for packages with secondary entrypoints is to use file-relative paths for imports you want in the same bundle, but package-relative paths for imports from other entry points (which should be externalized).

E.g.

// Should not be externalized
import Button from './Button';

// Should be externalized (since my package.name is '@my/components')
import Select from '@my/components/Select';

I would have expected that to be the default behavior, but if it makes more sense to have it as an opt-in thing, whatever you think is best.

Doesn't work on Node 12

After the v3 changes, this library no longer works on Node 12 since ?? isn't down-leveled.

     /home/runner/work/packemon/packemon/node_modules/rollup-plugin-node-externals/dist/cjs/index.js:315
                const pkg = JSON.parse((await fs.promises.readFile(packagePath)).toString()) ?? {};
                                                                                              ^

    SyntaxError: Unexpected token '?'

`exclude` only works on module names, not import paths

Issue

exclude option only filters out modules by their name, not by an import path.
So while it's possible to exclude something like /^lodash/ (covers lodash, lodash-es etc) it's not possible to exclude specifically sub paths or files by extensions.

Use case:

  • I have a package with some imports, e.g. import Foo from 'foo'; import 'foo/index.css'
  • I use postcss({extract: true}), expecting all imported css to end up in dist/index.css
  • With this plugin, both foo and foo/index.css end up as externals
  • Using exclude: /\.css$/ doesn't work

Solution

Ideal solution is to be able to have exclude also match against import path in resolveId.

externals({ deps: true, exclude: [/\.css$/] }),

TypeError: Cannot read properties of undefined (reading 'length'), error when using with vitest

Hi,
Thanks for this plugin.
I have been using this in one of my projects without any issuess untill recent past with vitest/vite.

However I have upgraded the plguin version from 7.0.1 to 7.1.1 and since then when I run the vitest run or vitest, I am getting the error TypeError: Cannot read properties of undefined (reading 'length'), (thrown in the resolveId call) and the process is exiting.

The same code is working fine with v 7.0.1 of this plugin.
I think the introduction of the 'order': 'pre' of the resolveId function could be triggering this. https://github.com/Septh/rollup-plugin-node-externals/blob/main/source/index.ts#L231

Stackblitz with v7.0.1 works fine: https://stackblitz.com/edit/vitest-dev-vitest-ydhtdg?file=package.json

Stackblitz with v7.1.1 fails: https://stackblitz.com/edit/vitest-dev-vitest-jlcazj?file=package.json

Must use `rollup.config.mjs` in a `type: module` package

This is unrelated specifically to this package; due to Rollup always porting ESM -> CJS in rollup.config files, the underlying import of find-up used in this package doesn't work (it's ESM only). The error message given by Rollup is not very helpful pointing out the actual culprit. The solution is to use rollup.config.mjs instead of rollup.config.js.

[!] Error: While loading the Rollup configuration from "rollup.config.js", Node tried to require an ES module from a CommonJS file, which is not supported. A common cause is if there is a package.json file with "type": "module" in the same folder. You can try to fix this by changing the extension of your configuration file to ".cjs" or ".mjs" depending on the content, which will prevent Rollup from trying to preprocess the file but rather hand it to Node directly.
https://rollupjs.org/guide/en/#using-untranspiled-config-files
Error: While loading the Rollup configuration from "rollup.config.js", Node tried to require an ES module from a CommonJS file, which is not supported. A common cause is if there is a package.json file with "type": "module" in the same folder. You can try to fix this by changing the extension of your configuration file to ".cjs" or ".mjs" depending on the content, which will prevent Rollup from trying to preprocess the file but rather hand it to Node directly.

Maybe deserves updating the docs.

Missing "types" in package.json

Recently updated to v6.1.0 and got an error (missing type declarations) for importing rollup-plugin-node-externals in rollup.config.ts.

Adding types to package.json should fix the issue:

"types": "./dist/index.d.ts"

Looks like types was removed in 8866dcf.

Upgrade to Rollup 4.0

package.json

    "rollup": "^4.0.0",
    "rollup-plugin-node-externals": "^6.1.1",
npm ERR! Could not resolve dependency:
npm ERR! peer rollup@"^3.0.0" from [email protected]
npm ERR! node_modules/rollup-plugin-node-externals
npm ERR!   dev rollup-plugin-node-externals@"^6.1.1" from the root project
npm ERR! 
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.

Question about dev dependencies.

Nice Work!
To be clear, this is a question no a bug.
In the readme,
you said

This will bundle your devDependencies in while leaving your dependencies, peerDependencies and optionalDependencies external.
So the default value of devDeps is true.
My question is that what is the scenario that someone wants to ship their library with dev dependencies bundled?

change of type crashes my electron app

Ever since v3 my electron app won't run because rollup complains about a mixup between commonjs and esm. I had to revert to v2 and that works just fine for me.
Anyway, thank you very much for your plugin!

RollupError: Entry module "./src/node/index.ts" cannot be external.

Since latest update 5.1.0 -> 5.1.1, I have this error when building

./src/node/index.ts โ†’ ./dist/esm, ./dist/cjs...
[!] RollupError: Entry module "./src/node/index.ts" cannot be external.

My config

  {
    input: "./src/node/index.ts",
    output: [{
      dir: "./dist/esm",
      format: "esm",
      exports: "named",
      preserveModules: true,
      sourcemap: true,
      entryFileNames: "[name].mjs",
    }, {
      dir: "./dist/cjs",
      format: "cjs",
      exports: "named",
      preserveModules: true,
      sourcemap: true,
      entryFileNames: "[name].cjs",
    }],
    plugins: [externals(), ts()]
  },

Maybe it's related to the patch Consider an empty include option means 'includes all'.

Allow disabling node builtins handling entirely

Currently there is no way to disable the handling of node builtins it will always either strip or add the node: prefix, its useful to be able to disable it so it can be handled by other plugins or configuration instead.

Error: No "exports" main defined

rollup-plugin-node-externals v6
rollup.config.js import externals from "rollup-plugin-node-externals"; has Error:

[!] Error: No "exports" main defined in D:\workspace\js\react-library-demo\node_modules\rollup-plugin-node-externals\package.json
Error: No "exports" main defined in D:\workspace\js\react-library-demo\node_modules\rollup-plugin-node-externals\package.json
    at new NodeError (node:internal/errors:387:5)
    at throwExportsNotFound (node:internal/modules/esm/resolve:464:9)
    at packageExportsResolve (node:internal/modules/esm/resolve:688:7)
    at resolveExports (node:internal/modules/cjs/loader:493:36)
    at Function.Module._findPath (node:internal/modules/cjs/loader:533:31)
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:942:27)
    at Function.Module._load (node:internal/modules/cjs/loader:804:27)
    at Module.require (node:internal/modules/cjs/loader:1028:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:942:27)
    at Function.Module._load (node:internal/modules/cjs/loader:804:27)
    at Module.require (node:internal/modules/cjs/loader:1028:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (D:\workspace\js\react-library-demo\rollup.config.js:9:17)

rollup.config.js

import resolve from "@rollup/plugin-node-resolve";
import postcss from "rollup-plugin-postcss";
import autoprefixer from "autoprefixer";
import { babel } from "@rollup/plugin-babel";
import commonjs from "@rollup/plugin-commonjs";
import externals from "rollup-plugin-node-externals";  // not use ,but has error

// eslint-disable-next-line import/no-anonymous-default-export
export default {
  input: "src/index.js",
  output: {
    file: "lib/index.js",
    format: "cjs",
  },
  plugins: [
    // externals({ deps: true }),
    resolve({
      extensions: [".js", ".jsx"], // Default: [ '.mjs', '.js', '.json', '.node'
    }),
    commonjs(),
    // babel({ babelHelpers: "bundled" }),
    babel({
      babelHelpers: "bundled",
      exclude: "**/node_modules/**",
      extensions: [".js", ".jsx", ".ts", ".tsx"],
    }),
    postcss({
      plugins: [autoprefixer()],
    }),
  ],
};

some question about 'include' vs 'exclude'

Hi, i have a trouble in 'include' and 'exclude'.Now there are two external packages('axios' and 'core-js'), i only want extern axios. So i have below config and it can meet the needs.

externals({
            deps: false,
            include: ["axios"],       
        }),

But i use another config and it don't work.

externals({
            deps: true,
            exclude: ["core-js"],  
        }),

I don't know why, can you help me?

Question about defaults

Suppose we are making a package that we want to publish to npm.

My understanding of dependencies, devDependencies, and peerDependencies is as follows:

  1. dependencies are packages that we do not want the people who download our package to ever really have to think about, so we bundle them into our dist file. We do not put them into our bundler's "externals" config.
  2. devDependencies are things like unit test runners, eslint, and rollup itself. We should never be importing them into our source code, and therefore it's irrelevant whether we include them into "externals" or not.
  3. peerDependencies are packages which we want the people who download our package to ALSO install. Therefore we put them into the "externals" config to prevent duplication between our bundle and the downloaders' installations.

This understanding does not seem to match with the defaults of this plugin. My understanding is that the defaults should be:

deps: false
devDeps: false
peerDeps: true

Can someone explain what is different between my understanding and this plugin's philosophy?

Not working with vite?

Hi. Thanks for the plugin.
I'm trying to make use of it, but it does not seem to work for my project -- resulting file is still very large, containing the dependencies. To the contrary, another similar (yet less powerful) plugin rollup-plugin-peer-deps-external seems to work.

I'm using Vite 4.4.6, and configured the plugin like this:

export default defineConfig({
  build: {
    ...
    rollupOptions: {
      ...
      plugins: [
        nodeExternals(),
      ]
    },
  },
});

I saw #17, which has a different location to set this up. Also #17 appeared not to be resolved in the end. So I'm not sure how much I can learn from that -- I tried putting nodeExternals() to there but that did not seem to make any difference.

if use rollup-plugin-postcss some pkg not bundled in

description

I use rollup-plugin-postcss pkg in rollup.config.js
when i run rollup --config rollup.config.js

in index.scss.js

import styleInject from '../../node_modules/style-inject/dist/style-inject.es.js';

var css_248z = ".masonry-absolute-wrap {\n  position: relative;\n}\n\n.masonry-absolute-item {\n  position: absolute;\n}";
styleInject(css_248z);

export { css_248z as default };

style-inject is not bundled in

try

i set externals, but it;s still not bundled in

externals({
  deps: true,                 // Deps are external
  exclude: 'style-inject'  // Yet we want `electron-reload` bundled in
})

example

https://github.com/jiaozitang/react-masonry-component2

help

how can i do something to resolve this question

Support rollup v3

Rollup v3 has just been released, with a few breaking changes for plugins. This plugin is currently (5.0.0) not compatible with rollup v3. I'm happy to pr in changes if needed, but is a maintainer able to do that?

Support multiple package.json files

Hi thanks for this package!

I was about to post this issue in rollup-plugin-auto-external but this one seems to be more active!

I might be working on an edge case but in a monorepo a package can share dependencies and have some of its own.

Could it be possible that the packagePath support an array of paths?
If you're ok, I can make a PR for it.

... is not assignable to type 'PluginOption'

Getting this warning when using with vite with vite.config.ts:

image

Type '{ enforce: "pre"; api?: any; cacheKey?: string | undefined; name: string; augmentChunkHash?: ObjectHook<(this: PluginContext, chunk: RenderedChunk) => string | void, {}> | undefined; ... 24 more ...; watchChange?: ObjectHook<...> | undefined; }' is not assignable to type 'PluginOption'.
vite: 3.2.5
rollup-plugin-node-externals: 5.0.2
typescript: 4.9.3

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.