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

[bug]resolve.alias 无法正确解析 moment 包 #4187

Closed
Fi2zz opened this issue Sep 13, 2023 · 6 comments
Closed

[bug]resolve.alias 无法正确解析 moment 包 #4187

Fi2zz opened this issue Sep 13, 2023 · 6 comments
Labels
bug Something isn't working

Comments

@Fi2zz
Copy link

Fi2zz commented Sep 13, 2023

System Info

System:
OS: macOS 13.5.1
CPU: (4) x64 Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
Memory: 153.21 MB / 8.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 18.6.0 - /usr/local/bin/node
Yarn: 1.22.19 - /usr/local/bin/yarn
npm: 8.13.2 - /usr/local/bin/npm
pnpm: 8.3.1 - /usr/local/bin/pnpm
Browsers:
Chrome: 116.0.5845.179
Safari: 16.6
npmPackages:
@rspack/cli: latest => 0.3.2

Details

case 1

/**
 * @type {import('@rspack/cli').Configuration}
 */
module.exports = {
	context: __dirname,
	entry: { main: "./case-1/src/main.js" },
	builtins: { html: [{ template: "./case-1/index.html" }] },
	resolve: {
		alias: { moment: require.resolve("moment/min/moment.min.js") },
	},
};
    # install
    yarn
    # run build
    yarn build:case1

    # $ rspack build
    # error[internal]: Failed to resolve ./locale in project root



    # rspack compiled with 1 error in 299 ms
    # error Command failed with exit code 1.
    # info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.


case 2

/**
 * @type {import('@rspack/cli').Configuration}
 */
module.exports = {
	context: __dirname,
	entry: { main: "./case-2/src/main.js" },
	builtins: { html: [{ template: "./case-2/index.html" }] },

};
    # install
    yarn
    # run build
    yarn build:case2

    # $ rspack build
    # error[internal]: Failed to resolve ./locale in project root



    # rspack compiled with 1 error in 299 ms
    # error Command failed with exit code 1.
    # info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.


Reproduce link

https://github.com/Fi2zz/rspack-alias-bug

Reproduce Steps

  1. run yarn
  2. run yarn build:case1 or yarn build:case2
@Fi2zz Fi2zz added bug Something isn't working pending triage The issue/PR is currently untouched. labels Sep 13, 2023
@hyf0 hyf0 removed the pending triage The issue/PR is currently untouched. label Sep 13, 2023
@hyf0 hyf0 added this to the Planned milestone Sep 13, 2023
@h-a-n-a
Copy link
Contributor

h-a-n-a commented Sep 13, 2023

I believe this is an issue related to this: moment/moment#6092. Have you try to use moment with locale package?

@Fi2zz
Copy link
Author

Fi2zz commented Sep 13, 2023

I believe this is an issue related to this: moment/moment#6092. Have you try to use moment with locale package?

no, but when use with esbuild, it did not throw an error

@Fi2zz
Copy link
Author

Fi2zz commented Sep 13, 2023

I believe this is an issue related to this: moment/moment#6092. Have you try to use moment with locale package?

when direct import like below which also can reproduce this issue.

import moment from "./node_modules/moment/min/moment.min.js"
moment(new Date()).format("yyy-mm-dd");

This problem occurs when using rspack, but not webpack and esbuild;
Is it because rspack will execute the code first and then parse it?

@h-a-n-a I have updated cases here https://github.com/Fi2zz/rspack-alias-bug

@h-a-n-a
Copy link
Contributor

h-a-n-a commented Sep 13, 2023

Would you take a look? @underfin

@ahabhgk
Copy link
Contributor

ahabhgk commented Sep 18, 2023

Tried with webpack, webpack also have the problem, and checkout the code in moment, they use require("./locale"+t) but no locale directory under min
Screenshot 2023-09-18 at 13 48 29
so the error is correct, it's expected, caused by moment/moment#6092

actually you can avoid this problem by importing "moment.js" instead of "moment.min.js", and in production rspack will minimize the code anyway

but besides this there is also another blocker:

aliasedRequire = require;
aliasedRequire('./locale/' + name);

aliasedRequire hasn't supported in rspack yet #3955, so you need to patch it to make it works

// aliasedRequire = require;
require('./locale/' + name);

@Fi2zz
Copy link
Author

Fi2zz commented Sep 18, 2023

Tried with webpack, webpack also have the problem, and checkout the code in moment, they use require("./locale"+t) but no locale directory under min Screenshot 2023-09-18 at 13 48 29 so the error is correct, it's expected, caused by moment/moment#6092

actually you can avoid this problem by importing "moment.js" instead of "moment.min.js", and in production rspack will minimize the code anyway

but besides this there is also another blocker:

aliasedRequire = require;
aliasedRequire('./locale/' + name);

aliasedRequire hasn't supported in rspack yet #3955, so you need to patch it to make it works

// aliasedRequire = require;
require('./locale/' + name);

thanks , I tried with webpack in development mode which throw error but production mode don't.

this solution also works for rspack #6092

@Fi2zz Fi2zz closed this as completed Sep 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants