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

Equivalent of 'expose-loader' #5479

Closed
ilx opened this issue Jan 25, 2024 · 9 comments
Closed

Equivalent of 'expose-loader' #5479

ilx opened this issue Jan 25, 2024 · 9 comments
Labels
feat New feature or request

Comments

@ilx
Copy link

ilx commented Jan 25, 2024

What problem does this feature solve?

Hi, is there an equivalent of webpack 'expose-loader'?

The expose-loader loader allows to expose a module (in whole or in part) to global object (self, window and global).

What does the proposed API of configuration look like?

{ test: /lodash\.js/, loader: 'expose-loader', options: { exposes: [{ globalName: ['_', 'lodash'], moduleLocalName: 'lodash' }] } },
{ test: /moment\.js$/, loader: 'expose-loader', options: { exposes: { globalName: 'moment', override: true } } },
{ test: /chroma\.js$/, loader: 'expose-loader', options: { exposes: 'chroma' } },
{ test: /echarts.common.min\.js$/, loader: 'expose-loader', options: { exposes: 'echarts' } },
@ilx ilx added feat New feature or request pending triage The issue/PR is currently untouched. labels Jan 25, 2024
@hardfist
Copy link
Contributor

have you checked whether it is working in Rspack?

@ilx
Copy link
Author

ilx commented Jan 26, 2024

have you checked whether it is working in Rspack?

It does not work with Rspack. For example:

    { test: /lodash\.js/, loader: 'expose-loader', options: { exposes: [{ globalName: ['_', 'lodash'], moduleLocalName: 'lodash' }] } },
    { test: /moment\.js$/, loader: 'expose-loader', options: { exposes: { globalName: 'moment', override: true } } },

results in errors:

ERROR in ./node_modules/lodash/lodash.js
  × Module build failed:
  ╰─▶   × TypeError: Cannot read properties of undefined (reading 'userRequest')
        │     at Object.loader (/Users/ilx/work/xebialabs/xlr-only/xl-release/node_modules/expose-loader/dist/index.js:33:73)
        │     at LOADER_EXECUTION (/Users/ilx/work/xebialabs/xlr-only/xl-release/node_modules/@rspack/core/dist/loader-runner/index.js:617:23)
        │     at runSyncOrAsync (/Users/ilx/work/xebialabs/xlr-only/xl-release/node_modules/@rspack/core/dist/loader-runner/index.js:618:11)
        │     at /Users/ilx/work/xebialabs/xlr-only/xl-release/node_modules/@rspack/core/dist/loader-runner/index.js:722:9
        │     at handleResult (/Users/ilx/work/xebialabs/xlr-only/xl-release/node_modules/@rspack/core/dist/loader-runner/loadLoader.js:128:5)
        │     at loadLoader (/Users/ilx/work/xebialabs/xlr-only/xl-release/node_modules/@rspack/core/dist/loader-runner/loadLoader.js:109:16)
        │     at iterateNormalLoaders (/Users/ilx/work/xebialabs/xlr-only/xl-release/node_modules/@rspack/core/dist/loader-runner/index.js:711:5)
        │     at /Users/ilx/work/xebialabs/xlr-only/xl-release/node_modules/@rspack/core/dist/loader-runner/index.js:538:13
        │     at new Promise (<anonymous>)
        │     at runLoaders (/Users/ilx/work/xebialabs/xlr-only/xl-release/node_modules/@rspack/core/dist/loader-runner/index.js:513:12)
        │

  help: File was processed with this loader: '/Users/ilx/work/xebialabs/xlr-only/xl-release/node_modules/expose-loader/dist/cjs.js??ruleSet[1].rules[3].use[0]'


ERROR in ./node_modules/moment/moment.js
  × Module build failed:
  ╰─▶   × TypeError: Cannot read properties of undefined (reading 'userRequest')
        │     at Object.loader (/Users/ilx/work/xebialabs/xlr-only/xl-release/node_modules/expose-loader/dist/index.js:33:73)
        │     at LOADER_EXECUTION (/Users/ilx/work/xebialabs/xlr-only/xl-release/node_modules/@rspack/core/dist/loader-runner/index.js:617:23)
        │     at runSyncOrAsync (/Users/ilx/work/xebialabs/xlr-only/xl-release/node_modules/@rspack/core/dist/loader-runner/index.js:618:11)
        │     at /Users/ilx/work/xebialabs/xlr-only/xl-release/node_modules/@rspack/core/dist/loader-runner/index.js:722:9
        │     at handleResult (/Users/ilx/work/xebialabs/xlr-only/xl-release/node_modules/@rspack/core/dist/loader-runner/loadLoader.js:128:5)
        │     at loadLoader (/Users/ilx/work/xebialabs/xlr-only/xl-release/node_modules/@rspack/core/dist/loader-runner/loadLoader.js:109:16)
        │     at iterateNormalLoaders (/Users/ilx/work/xebialabs/xlr-only/xl-release/node_modules/@rspack/core/dist/loader-runner/index.js:711:5)
        │     at /Users/ilx/work/xebialabs/xlr-only/xl-release/node_modules/@rspack/core/dist/loader-runner/index.js:538:13
        │     at new Promise (<anonymous>)
        │     at runLoaders (/Users/ilx/work/xebialabs/xlr-only/xl-release/node_modules/@rspack/core/dist/loader-runner/index.js:513:12)
        │

  help: File was processed with this loader: '/Users/ilx/work/xebialabs/xlr-only/xl-release/node_modules/expose-loader/dist/cjs.js??ruleSet[1].rules[4].use[0]'

There are 2 additional loaders that might be "connected": imports-loader and exports-loader
(I don't have standalone repo to test this - it's an app with a lot of "old" code that requires expose-loader 1st)

@ilx
Copy link
Author

ilx commented Jan 26, 2024

I was searching github for issues related to userRequest and found https://github.com/webpack-contrib/closure-webpack-plugin/blob/60cd06c4c0bab620fb78356070546bf926b6ad23/src/chunk-sources.js#L54-L55

it says:

// Multi entry modules have no userRequest or id, but they do have multiple
// nested dependencies. Traverse all of them.

This project has multiple entries, not sure if it is related.

@h-a-n-a
Copy link
Contributor

h-a-n-a commented Jan 31, 2024

Rspack does not support loaderContext._module right now and not intended to be supported as it exposed a whole API that a webpack module provides: https://github.com/webpack/webpack/blob/6be4065ade1e252c1d8dcba4af0f43e32af1bdc1/lib/NormalModule.js#L676.
You can still use fields provided by loaderContext to see if this can be rewritten.

@h-a-n-a h-a-n-a removed the pending triage The issue/PR is currently untouched. label Jan 31, 2024
Copy link

stale bot commented Mar 31, 2024

This issue has been automatically marked as stale because it has not had recent activity. If this issue is still affecting you, please leave any comment (for example, "bump"). We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!

@stale stale bot added the stale label Mar 31, 2024
@instagibb
Copy link

So is there no alternative for expose-loader? If there is, can the documentation be updated to show how to migrate?

@stale stale bot removed the stale label Apr 16, 2024
Copy link

stale bot commented Jun 15, 2024

This issue has been automatically marked as stale because it has not had recent activity. If this issue is still affecting you, please leave any comment (for example, "bump"). We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!

@stale stale bot added the stale label Jun 15, 2024
@instagibb
Copy link

bump

@stale stale bot removed the stale label Jun 16, 2024
@h-a-n-a
Copy link
Contributor

h-a-n-a commented Jun 26, 2024

I believe it's fixed.
The case below is working:

import $ from 'expose-loader?exposes=$,jQuery!jquery';

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

No branches or pull requests

4 participants