Skip to content

Commit 0dc27ce

Browse files
committed
esm snapshot
1 parent e6680dd commit 0dc27ce

19 files changed

Lines changed: 31 additions & 182 deletions

File tree

crates/rspack_plugin_javascript/src/parser_plugin/api_plugin.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ fn static_require_member_chain(
201201
expr_span: Span,
202202
write: bool,
203203
) -> Option<bool> {
204+
if parser.compiler_options.experiments.runtime_mode != ExperimentRuntimeMode::Rspack {
205+
return None;
206+
}
207+
204208
if for_name == API_REQUIRE
205209
&& let Some(property) = members.first()
206210
{
@@ -210,8 +214,7 @@ fn static_require_member_chain(
210214
let dep_span = if members.len() > 1 {
211215
member_ranges
212216
.and_then(|ranges| ranges.get(1))
213-
.map(|range| Span::new(expr_span.lo, range.hi))
214-
.unwrap_or(expr_span)
217+
.map_or(expr_span, |range| Span::new(expr_span.start, range.end))
215218
} else {
216219
expr_span
217220
};
@@ -476,17 +479,13 @@ impl<'p, 'a> JavascriptParserPlugin<'p, 'a> for APIPlugin {
476479
&self,
477480
parser: &mut JavascriptParser,
478481
_expr: &AssignExpr,
482+
_ident: &Ident,
479483
for_name: &str,
480484
) -> Option<bool> {
481485
if let Some(runtime_global) = runtime_api_from_name(for_name).and_then(|api| api.runtime_global)
486+
&& parser.compiler_options.experiments.runtime_mode == ExperimentRuntimeMode::Rspack
482487
{
483-
if parser.compiler_options.experiments.runtime_mode == ExperimentRuntimeMode::Rspack {
484-
parser.add_presentational_dependency(Box::new(RuntimeRequirementsDependency::write_only(
485-
runtime_global,
486-
)));
487-
return None;
488-
}
489-
parser.add_presentational_dependency(Box::new(RuntimeRequirementsDependency::add_only(
488+
parser.add_presentational_dependency(Box::new(RuntimeRequirementsDependency::write_only(
490489
runtime_global,
491490
)));
492491
}
@@ -541,7 +540,9 @@ impl<'p, 'a> JavascriptParserPlugin<'p, 'a> for APIPlugin {
541540
call_expr: &CallExpr,
542541
for_name: &str,
543542
) -> Option<bool> {
544-
if for_name == API_REQUIRE {
543+
if for_name == API_REQUIRE
544+
&& parser.compiler_options.experiments.runtime_mode == ExperimentRuntimeMode::Rspack
545+
{
545546
parser.add_presentational_dependency(Box::new(RuntimeRequirementsDependency::add_only(
546547
RuntimeGlobals::REQUIRE,
547548
)));

crates/rspack_plugin_runtime/src/runtime_module/create_fake_namespace_object.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ impl RuntimeModule for CreateFakeNamespaceObjectRuntimeModule {
3838
let params = Some(
3939
if context.compilation.options.experiments.runtime_mode == RuntimeMode::Rspack {
4040
serde_json::json!({
41-
"REQUIRE": "(typeof this === \"function\" ? this : this.r)"
41+
"__this": "(typeof this === \"function\" ? this : this.r)"
4242
})
4343
} else {
4444
serde_json::json!({
45-
"REQUIRE": "this"
45+
"__this": "this"
4646
})
4747
},
4848
);

crates/rspack_plugin_runtime/src/runtime_module/runtime/create_fake_namespace_object.ejs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var leafPrototypes;
77
// mode & 16: return value when it's Promise-like
88
// mode & 8|1: behave like require
99
<%- CREATE_FAKE_NAMESPACE_OBJECT %> = function(value, mode) {
10-
if(mode & 1) value = <%- REQUIRE %>(value);
10+
if(mode & 1) value = <%- __this %>(value);
1111
if(mode & 8) return value;
1212
if(typeof value === 'object' && value) {
1313
if((mode & 4) && value.__esModule) return value;
@@ -23,4 +23,4 @@ var leafPrototypes;
2323
def['default'] = <%- returningFunction("value", "") %>;
2424
<%- DEFINE_PROPERTY_GETTERS %>(ns, def);
2525
return ns;
26-
};
26+
};

tests/rspack-test/esmOutputCases/dynamic-import/import-cjs-runtime-chunk-false-async-chunk/__snapshots__/esm.snap.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ __webpack_require__.t = function(value, mode) {
116116
__webpack_require__.d(ns, def);
117117
return ns;
118118
};
119-
120119
})();
121120
// webpack/runtime/define_property_getters
122121
(() => {

tests/rspack-test/esmOutputCases/dynamic-import/import-cjs-runtime-chunk-false-entry-export/__snapshots__/esm.snap.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ __webpack_require__.t = function(value, mode) {
9090
__webpack_require__.d(ns, def);
9191
return ns;
9292
};
93-
9493
})();
9594
// webpack/runtime/define_property_getters
9695
(() => {

tests/rspack-test/esmOutputCases/dynamic-import/import-cjs/__snapshots__/esm.snap.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ __webpack_require__.t = function(value, mode) {
117117
__webpack_require__.d(ns, def);
118118
return ns;
119119
};
120-
121120
})();
122121
// webpack/runtime/define_property_getters
123122
(() => {

tests/rspack-test/esmOutputCases/externals/dynamic-import-commonjs-require-collision/__snapshots__/esm.snap.txt

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,8 @@ export { index_require as userRequire, loadPlatform };
3030
```
3131

3232
```mjs title=runtime.mjs
33-
// The module cache
34-
var __webpack_module_cache__ = {};
35-
// The require function
36-
function __webpack_require__(moduleId) {
37-
// Check if module is in cache
38-
var cachedModule = __webpack_module_cache__[moduleId];
39-
if (cachedModule !== undefined) {
40-
return cachedModule.exports;
41-
}
42-
// Create a new module (and put it into the cache)
43-
var module = (__webpack_module_cache__[moduleId] = {
44-
exports: {}
45-
});
46-
// Execute the module function
47-
__webpack_modules__[moduleId](module, module.exports, __webpack_require__);
48-
49-
// Return the exports of the module
50-
return module.exports;
51-
}
33+
// The require scope
34+
var __webpack_require__ = {};
5235

5336
// webpack/runtime/create_fake_namespace_object
5437
(() => {
@@ -78,7 +61,6 @@ __webpack_require__.t = function(value, mode) {
7861
__webpack_require__.d(ns, def);
7962
return ns;
8063
};
81-
8264
})();
8365
// webpack/runtime/define_property_getters
8466
(() => {

tests/rspack-test/esmOutputCases/externals/dynamic-import-node-commonjs-helper-collision/__snapshots__/esm.snap.txt

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,8 @@ export { loadFs };
2929
```
3030

3131
```mjs title=runtime.mjs
32-
// The module cache
33-
var __webpack_module_cache__ = {};
34-
// The require function
35-
function __webpack_require__(moduleId) {
36-
// Check if module is in cache
37-
var cachedModule = __webpack_module_cache__[moduleId];
38-
if (cachedModule !== undefined) {
39-
return cachedModule.exports;
40-
}
41-
// Create a new module (and put it into the cache)
42-
var module = (__webpack_module_cache__[moduleId] = {
43-
exports: {}
44-
});
45-
// Execute the module function
46-
__webpack_modules__[moduleId](module, module.exports, __webpack_require__);
47-
48-
// Return the exports of the module
49-
return module.exports;
50-
}
32+
// The require scope
33+
var __webpack_require__ = {};
5134

5235
// webpack/runtime/create_fake_namespace_object
5336
(() => {
@@ -77,7 +60,6 @@ __webpack_require__.t = function(value, mode) {
7760
__webpack_require__.d(ns, def);
7861
return ns;
7962
};
80-
8163
})();
8264
// webpack/runtime/define_property_getters
8365
(() => {

tests/rspack-test/esmOutputCases/externals/esm-node-target/__snapshots__/esm.snap.txt

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,8 @@ export { readFileSync } from "fs";
3030
```
3131

3232
```mjs title=runtime.mjs
33-
// The module cache
34-
var __webpack_module_cache__ = {};
35-
// The require function
36-
function __webpack_require__(moduleId) {
37-
// Check if module is in cache
38-
var cachedModule = __webpack_module_cache__[moduleId];
39-
if (cachedModule !== undefined) {
40-
return cachedModule.exports;
41-
}
42-
// Create a new module (and put it into the cache)
43-
var module = (__webpack_module_cache__[moduleId] = {
44-
exports: {}
45-
});
46-
// Execute the module function
47-
__webpack_modules__[moduleId](module, module.exports, __webpack_require__);
48-
49-
// Return the exports of the module
50-
return module.exports;
51-
}
33+
// The require scope
34+
var __webpack_require__ = {};
5235

5336
// webpack/runtime/create_fake_namespace_object
5437
(() => {
@@ -78,7 +61,6 @@ __webpack_require__.t = function(value, mode) {
7861
__webpack_require__.d(ns, def);
7962
return ns;
8063
};
81-
8264
})();
8365
// webpack/runtime/define_property_getters
8466
(() => {

tests/rspack-test/esmOutputCases/externals/node-commonjs-export-namespace-as-with-named/__snapshots__/esm.snap.txt

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,8 @@ export { external_fs_namespaceObject as fsNs, readFile, readFileSync };
2828
```
2929

3030
```mjs title=runtime.mjs
31-
// The module cache
32-
var __webpack_module_cache__ = {};
33-
// The require function
34-
function __webpack_require__(moduleId) {
35-
// Check if module is in cache
36-
var cachedModule = __webpack_module_cache__[moduleId];
37-
if (cachedModule !== undefined) {
38-
return cachedModule.exports;
39-
}
40-
// Create a new module (and put it into the cache)
41-
var module = (__webpack_module_cache__[moduleId] = {
42-
exports: {}
43-
});
44-
// Execute the module function
45-
__webpack_modules__[moduleId](module, module.exports, __webpack_require__);
46-
47-
// Return the exports of the module
48-
return module.exports;
49-
}
31+
// The require scope
32+
var __webpack_require__ = {};
5033

5134
// webpack/runtime/create_fake_namespace_object
5235
(() => {
@@ -76,7 +59,6 @@ __webpack_require__.t = function(value, mode) {
7659
__webpack_require__.d(ns, def);
7760
return ns;
7861
};
79-
8062
})();
8163
// webpack/runtime/define_property_getters
8264
(() => {

0 commit comments

Comments
 (0)