Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings
webpack

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Can you set different aliases per entry file?#12933

Answeredbysokra
Jakobud asked this question inQ&A
Discussion options

I have a unique situation where I have 2 different entry files that both use the same "alias" but each of those aliases need to point to different places for each entry file. In my case, it isn't an option to use 2 different alias names. The code uses a cascading/waterfall type of file resolution for the final product..... it's a long explanation but I just can't change the alias names.

Both entry files need to use the same alias "name" but each alias needs to point to different places. However from what I can tell aliases are unique per Webpack configuration.

Is it possible to have unique aliases per entry file?

You must be logged in to vote
Answered by sokraMar 19, 2021

Actually aliases are not unique. They are part of the resolve options which can be different per module.

You can usemodule.rules to assign different aliases per module:

{  test: /abc/,  resolve: { alias: { ... } }}

Replies: 2 comments 9 replies

Comment options

You can create loader and return different content based on your complex logic (look atissuer), alternative you can create plugin for resolverhttps://webpack.js.org/configuration/resolve/#resolveplugins, alternative you can try#12327, but I recommend to avoid this situations

You must be logged in to vote
2 replies
@Jakobud
Comment options

Thanks. So I'm in the process of trying to see if a custom plugin would be doable for this. Never tried writing a plugin for Webpack. Do you know anything about how to use theContextModuleFactory andNormalModuleFactory objects? Every single example I see they are doingcompiler hooks like so:

class MyExampleWebpackPlugin {  apply(compiler) {    compiler.hooks.whatever.tap(.....)...

So withNormalModuleFactory am I supposed to be doing this?

class MyExampleWebpackPlugin {  apply(NormalModuleFactory) {    NormalModuleFactory.hooks.whatever.tap(.....)...

Because that doesn't seem to work. I don't see any examples in the documentation on how to set this up. I would expect some sort of example on the Plugin Patterns doc page but it only showscompiler hook examples.

@alexander-akait
Comment options

Simple examplehttps://github.com/webpack/webpack/blob/master/lib/ExternalModuleFactoryPlugin.js, webpack has a lot of plugins side, so when you need something complex feel free to look at similar plugin logic

Comment options

Actually aliases are not unique. They are part of the resolve options which can be different per module.

You can usemodule.rules to assign different aliases per module:

{  test: /abc/,  resolve: { alias: { ... } }}
You must be logged in to vote
7 replies
@sokra
Comment options

If it's only a single module between the entry and the alias you could usemain.js?cat andmain.js?dog and match onresourceQuery

@Jakobud
Comment options

I'm not familiar with that. Where would that resourceQuery value go? Like this?

{  "websiteDog/main": "js/main.js?dog",  "websiteCat/main": "js/main.js?cat"}

Is that how it's supposed to work?

@Jakobud
Comment options

FYI I got it working perfectly. Thank you for the guidance!

@arthabus
Comment options

FYI I got it working perfectly. Thank you for the guidance!

@Jakobud Can you post your solution?

@shark-legs
Comment options

FYI I got it working perfectly. Thank you for the guidance!

@Jakobud Can you post your solution?

Can you post your solution? same problem

Answer selected byJakobud
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
5 participants
@Jakobud@sokra@alexander-akait@arthabus@shark-legs

[8]ページ先頭

©2009-2025 Movatter.jp