septh / rollup-plugin-node-externals Goto Github PK
View Code? Open in Web Editor NEWA Rollup/Vite plugin that automatically declares NodeJS built-in modules and npm dependencies as 'external'
A Rollup/Vite plugin that automatically declares NodeJS built-in modules and npm dependencies as 'external'
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.
Looks like 3.1.1 ESM build is broken: find-up
moved to be a devDep and therefore, not found in runtime.
https://unpkg.com/browse/[email protected]/dist/esm/index.js
import { findUp } from 'find-up';
It could either move back to deps or not be an external
in the ESM build
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
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.
import Foo from 'foo'; import 'foo/index.css'
postcss({extract: true})
, expecting all imported css to end up in dist/index.css
foo
and foo/index.css
end up as externalsexclude: /\.css$/
doesn't workIdeal solution is to be able to have exclude
also match against import path in resolveId
.
externals({ deps: true, exclude: [/\.css$/] }),
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
Looks like rollup
is under engines
in version 4.0.0
, is that expected?
I'm getting warnings in a few projects
yarn install v1.22.17
[1/5] ๐ Validating package.json...
[2/5] ๐ Resolving packages...
[3/5] ๐ Fetching packages...
warning [email protected]: The engine "rollup" appears to be invalid.
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.
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.
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 ofdevDeps
is true.
My question is that what is the scenario that someone wants to ship their library with dev dependencies bundled?
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!
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'.
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.
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()],
}),
],
};
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?
https://unpkg.com/browse/[email protected]/dist/index.d.ts
doesn't contain packagePath
option.
code is up to date though
https://unpkg.com/browse/[email protected]/dist/es/index.js
Suppose we are making a package that we want to publish to npm.
My understanding of dependencies
, devDependencies
, and peerDependencies
is as follows:
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.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.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?
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.
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
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
})
https://github.com/jiaozitang/react-masonry-component2
how can i do something to resolve this question
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?
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.
Getting this warning when using with vite
with vite.config.ts
:
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
this is still old code:
https://unpkg.com/browse/[email protected]/dist/es/index.js
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.