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

Draft: fix: align amd option behavior with webpack #9011

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl DependencyTemplate for AMDRequireDependency {
{
let start_block = promise + ".then(function() {";
let end_block = format!(
";}})['catch']{}",
";}})['catch']({})",
RuntimeGlobals::UNCAUGHT_ERROR_HANDLER.name()
);
code_generatable_context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,6 @@ impl JavascriptParserPlugin for APIPlugin {
not_supported_expr!(is_require_extensions, expr, "require.extensions");
not_supported_expr!(is_require_config, expr, "require.config");
not_supported_expr!(is_require_version, expr, "require.version");
not_supported_expr!(is_require_amd, expr, "require.amd");
not_supported_expr!(is_require_include, expr, "require.include");
not_supported_expr!(is_require_onerror, expr, "require.onError");
not_supported_expr!(is_require_main_require, expr, "require.main.require");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ impl ESMDetectionParserPlugin {
}
}

// nonHarmonyIdentifiers
fn is_non_esm_identifier(name: &str) -> bool {
name == "exports" || name == "define"
}

// Port from https://github.com/webpack/webpack/blob/main/lib/dependencies/HarmonyDetectionParserPlugin.js
impl JavascriptParserPlugin for ESMDetectionParserPlugin {
fn program(&self, parser: &mut JavascriptParser, ast: &Program) -> Option<bool> {
Expand Down Expand Up @@ -97,17 +102,35 @@ impl JavascriptParserPlugin for ESMDetectionParserPlugin {
expr: &UnaryExpr,
for_name: &str,
) -> Option<BasicEvaluatedExpression> {
(parser.is_esm && for_name == "exports")
(parser.is_esm && is_non_esm_identifier(for_name))
.then(|| BasicEvaluatedExpression::with_range(expr.span().real_lo(), expr.span_hi().0))
}

fn r#typeof(
&self,
parser: &mut JavascriptParser,
_expr: &UnaryExpr,
for_name: &str,
) -> Option<bool> {
(parser.is_esm && is_non_esm_identifier(for_name)).then_some(true)
}

fn identifier(
&self,
parser: &mut JavascriptParser,
_ident: &Ident,
for_name: &str,
) -> Option<bool> {
(parser.is_esm && for_name == "exports").then_some(true)
(parser.is_esm && is_non_esm_identifier(for_name)).then_some(true)
}

fn call(
&self,
parser: &mut JavascriptParser,
_expr: &swc_core::ecma::ast::CallExpr,
for_name: &str,
) -> Option<bool> {
(parser.is_esm && is_non_esm_identifier(for_name)).then_some(true)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the module is an esm, define/typeof define/define(...) processing will be skipped by HarmonyDetectionParserPlugin.

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,26 @@ Object {
resolve: Object {
aliasFields: Array [],
byDependency: Object {
amd: Object {
aliasFields: Array [
browser,
],
conditionNames: Array [
require,
module,
...,
],
extensions: Array [
.js,
.json,
.wasm,
],
mainFields: Array [
browser,
module,
...,
],
},
commonjs: Object {
aliasFields: Array [
browser,
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import fn from './fn';

fn(123);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

module.exports = jest.fn();
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
it("require([...]) should work well", async function () {
const p = require(['./dep']);
expect(p).toBeInstanceOf(Promise);

await p;

expect(require('./fn')).toHaveBeenCalledWith(123);
});

it("require([...], function () {}) should work well", function (done) {
require(['./add'], function (add) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
/** @type {import("@rspack/core").Configuration} */
module.exports = {
resolve: {
extensions: ["...", ".js"],
},
amd: { jQuery: true },
};
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ module.exports = {
@@ ... @@
- "browser",
@@ ... @@
- "aliasFields": Array [
- "browser",
- ],
+ "aliasFields": Array [],
@@ ... @@
- "browser",
@@ ... @@
- "browser",
+ "node",
+ "electron",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ module.exports = {
@@ ... @@
- "browser",
@@ ... @@
- "aliasFields": Array [
- "browser",
- ],
+ "aliasFields": Array [],
@@ ... @@
- "browser",
@@ ... @@
+ "node",
@@ ... @@
+ "electron",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ module.exports = {
@@ ... @@
- "browser",
@@ ... @@
- "aliasFields": Array [
- "browser",
- ],
+ "aliasFields": Array [],
@@ ... @@
- "browser",
@@ ... @@
- "browser",
+ "node",
@@ ... @@
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ module.exports = {
@@ ... @@
- "browser",
@@ ... @@
- "aliasFields": Array [
- "browser",
- ],
+ "aliasFields": Array [],
@@ ... @@
- "browser",
@@ ... @@
+ "node",
@@ ... @@
+ "nwjs",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
it("should transform unsupported require api to undefined", function () {
expect(require.extensions).toBeUndefined();
expect(require.config).toBeUndefined();
expect(require.version).toBeUndefined();
expect(require.amd).toBeUndefined();
// expect(require.version).toBeUndefined();
expect(require.include).toBeUndefined();
expect(require.onError).toBeUndefined();
// expect(require.onError).toBeUndefined();
expect(require.main.require).toBeUndefined();
expect(module.parent.require).toBeUndefined();

expect(require.include("a")).toBeUndefined();
expect(require.onError(function () {})).toBeUndefined();
expect(require.onError(function () { })).toBeUndefined();
expect(require.main.require("a")).toBeUndefined();
expect(module.parent.require("a")).toBeUndefined();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
module.exports = [
[/require.extensions is not supported by Rspack/],
[/require.config is not supported by Rspack/],
[/require.version is not supported by Rspack/],
[/require.amd is not supported by Rspack/],
// [/require.version is not supported by Rspack/],
[/require.include is not supported by Rspack/],
[/require.onError is not supported by Rspack/],
// [/require.onError is not supported by Rspack/],
[/require.main.require is not supported by Rspack/],
[/module.parent.require is not supported by Rspack/],
[/require.include\(\) is not supported by Rspack/],
Expand Down
2 changes: 1 addition & 1 deletion packages/rspack/etc/core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -9575,7 +9575,7 @@ export { RspackOptionsApply as WebpackOptionsApply }
// @public (undocumented)
export interface RspackOptionsNormalized {
// (undocumented)
amd?: string;
amd?: Amd;
// (undocumented)
bail?: Bail;
// (undocumented)
Expand Down
2 changes: 1 addition & 1 deletion packages/rspack/src/config/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export const getRawOptions = (
experiments,
node: getRawNode(options.node),
profile: options.profile!,
amd: options.amd,
amd: options.amd !== false ? JSON.stringify(options.amd || {}) : undefined,
bail: options.bail!,
__references: {}
};
Expand Down
2 changes: 1 addition & 1 deletion packages/rspack/src/config/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1078,7 +1078,7 @@ const getResolveDefaults = ({
preferRelative: true
},
commonjs: cjsDeps(),
// amd: cjsDeps(),
amd: cjsDeps(),
// for backward-compat: loadModule
// loader: cjsDeps(),
// for backward-compat: Custom Dependency and getResolve without dependencyType
Expand Down
5 changes: 3 additions & 2 deletions packages/rspack/src/config/normalization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import path from "node:path";
import util from "node:util";
import type { Compilation } from "../Compilation";
import type {
Amd,
AssetModuleFilename,
Bail,
CacheOptions,
Expand Down Expand Up @@ -372,7 +373,7 @@ export const getNormalizedRspackOptions = (
watchOptions: cloneObject(config.watchOptions),
devServer: config.devServer,
profile: config.profile,
amd: config.amd ? JSON.stringify(config.amd) : undefined,
amd: config.amd,
bail: config.bail
};
};
Expand Down Expand Up @@ -648,6 +649,6 @@ export interface RspackOptionsNormalized {
ignoreWarnings?: IgnoreWarningsNormalized;
performance?: Performance;
profile?: Profile;
amd?: string;
amd?: Amd;
bail?: Bail;
}
Loading
Loading