javascript - How can I change in Rollup.js an import module of a dependency package to a local file replacing that module? - Sta

I have a JavaScript project that must be bundled using Rollup.js which depends on a package A which in

I have a JavaScript project that must be bundled using Rollup.js which depends on a package A which in turn depends on a package B:

"mypackage" ---import--> "A" ----import----> "B"

Let's say that my package import a function "connect" from package A, which in turn import a "connect" function exported by the module B. Something like:

//mypackage index.js
import { connect } from 'A'

//A index.js
import { connect } from 'B'

//B index.js
export function connect() {}

Since my package requires a bundled version of the package B (let's say "B.bundle.js"), how can i configure Rollup.js in order to replace for each dependency of my project requiring B (A in this case) to use my local bundled version (i.e. B.bundle.js, which of course exports the "connect" function too)?

When Rollup.js creates the bundled version of my project i would like to achieve something like the following:

//A index.js after being processed by Rollup
import { connect } from './src/B.bundle.js'

Is something like this possible with Rollup or with a plugin? Sorry for the question, but I'm new to rollup and bundling in general.

I have a JavaScript project that must be bundled using Rollup.js which depends on a package A which in turn depends on a package B:

"mypackage" ---import--> "A" ----import----> "B"

Let's say that my package import a function "connect" from package A, which in turn import a "connect" function exported by the module B. Something like:

//mypackage index.js
import { connect } from 'A'

//A index.js
import { connect } from 'B'

//B index.js
export function connect() {}

Since my package requires a bundled version of the package B (let's say "B.bundle.js"), how can i configure Rollup.js in order to replace for each dependency of my project requiring B (A in this case) to use my local bundled version (i.e. B.bundle.js, which of course exports the "connect" function too)?

When Rollup.js creates the bundled version of my project i would like to achieve something like the following:

//A index.js after being processed by Rollup
import { connect } from './src/B.bundle.js'

Is something like this possible with Rollup or with a plugin? Sorry for the question, but I'm new to rollup and bundling in general.

Share Improve this question edited Apr 16, 2022 at 20:35 Jason Aller 3,65228 gold badges41 silver badges39 bronze badges asked Apr 16, 2022 at 18:06 msalafiamsalafia 2,7435 gold badges24 silver badges34 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

I solved this issue using some configuration of my package package.json and the rollup plugin @rollup/plugin-node-resolve.

In the package.json of my package I inserted the browser option that specifies how modules should be resolved when my package is used in the browser context. From the npm doc on the browser option of the package.json:

If your module is meant to be used client-side the browser field should be used instead of the main field. This is helpful to hint users that it might rely on primitives that aren't available in Node.js modules. (e.g. window)

So considering the example provided in the original question the npm package contains something like this:

{
  "name": "mypackage",
  "version": "1.5.1",
  "description": "A brand new package",
  "main": "index.js",
  "browser": {
    "B": "./B.bundle.js"
  },
}

This means that when mypackage is used in the context of the browser the module B import will load from the file located in "./B.bundle.js".

Now, with rollup i need to specify that the bundle i am creating is intended for browser context. The plugin that handle imports of node modules is @rollup/plugin-node-resolve. There is an option is this plugin that specify that the context is browser. From the plugin documentation about the option browser:

If true, instructs the plugin to use the browser module resolutions in package.json and adds 'browser' to exportConditions if it is not present so browser conditionals in exports are applied. If false, any browser properties in package files will be ignored. Alternatively, a value of 'browser' can be added to both the mainFields and exportConditions options, however this option takes precedence over mainFields.

So that in my rollup config file i have something like:

// rollup.config.js

import monjs from "@rollup/plugin-monjs";
import resolve from "@rollup/plugin-node-resolve";
import nodePolyfills from "rollup-plugin-node-polyfills";

   export default {
    input: "index.js",
    output: {
      file: "dist/mypackage.bundle.js",
      format: "es",
    },
    plugins: [
      nodePolyfills(),
      resolve({
        browser: true, //<- tells to rollup to use browser module resolution
      }),
      monjs(),
    ],
  },

The answer of @vrajpaljhala seems feasable for the purpose but imho @rollup/plugin-replace its kind of too tricky and raw approach to the problem because it involves direct replacmeent of strings surrounded by "". We may face some very "hard to discover" errors if the package name we are going to replace is a mon word that can be present as a string in the code but not in the context of an import statement.

We had the exact same requirement and we resolved it using @rollup/plugin-replace package.

Basically, our project uses a monorepo structure but we are not using any tools like learna or workspace for managing it. So we have two packages which are interdependent. Our ui-kit uses icons package for different icons and icons package uses the Icon ponent from ui-kit package. We just replaced the imports for ui-kit with local path with the following:

import replace from '@rollup/plugin-replace';

...
...

export default {
  ...
  ...
  plugins: [
    replace({
      'ui-kit': JSON.stringify('../../../../src/ponents/Icon'),
      delimiters: ['"', '"'],
    }),
  ]
  ...
  ...
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744201604a4562904.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信