File tree Expand file tree Collapse file tree
configCases/spec-namespace-objects/absent-export Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ ---
2+ " webpack " : patch
3+ ---
4+
5+ Answer a name a module does not export from the spec namespace.
Original file line number Diff line number Diff line change @@ -2571,6 +2571,18 @@ class RuntimeTemplate {
25712571
25722572 if ( exportName . length > 0 ) {
25732573 const exportsInfo = moduleGraph . getExportsInfo ( module ) ;
2574+ // A name the module does not export is the namespace's to answer — reading
2575+ // it is undefined and writing it fails — not the exports object's.
2576+ if (
2577+ ! isModuleDeferred &&
2578+ usesSpecNamespaceObject ( module ) &&
2579+ exportsInfo . isExportProvided ( exportName ) === false
2580+ ) {
2581+ runtimeRequirements . add ( RuntimeGlobals . specNamespaceObject ) ;
2582+ const ns = `${ RuntimeGlobals . specNamespaceObject } (${ importVar } )` ;
2583+
2584+ return `${ ns } ${ propertyAccess ( exportName ) } ` ;
2585+ }
25742586 // in some case the exported item is renamed (get this by getUsedName). for example,
25752587 // x.default might be emitted as x.Z (default is renamed to Z)
25762588 const used = exportsInfo . getUsedName ( exportName , runtime ) ;
Original file line number Diff line number Diff line change 1+ import * as ns from "./m.js" ;
2+
3+ it ( "should read a name the module does not export as undefined" , ( ) => {
4+ // A name that is not an export is answered by the namespace, so nothing
5+ // inherited from `Object.prototype` can show through it.
6+ expect ( ns . __proto__ ) . toBe ( undefined ) ;
7+ expect ( ns . toString ) . toBe ( undefined ) ;
8+ expect ( ns . constructor ) . toBe ( undefined ) ;
9+ expect ( ns . hasOwnProperty ) . toBe ( undefined ) ;
10+ expect ( ns . valueOf ) . toBe ( undefined ) ;
11+ expect ( ns . notAnExport ) . toBe ( undefined ) ;
12+ expect ( ns . renamedLocal ) . toBe ( undefined ) ;
13+ } ) ;
14+
15+ it ( "should refuse a write to a name the module does not export" , ( ) => {
16+ expect ( ( ) => {
17+ ns . notAnExport = 1 ;
18+ } ) . toThrow ( TypeError ) ;
19+ expect ( ( ) => {
20+ ns . renamedLocal = 1 ;
21+ } ) . toThrow ( TypeError ) ;
22+ expect ( ns . notAnExport ) . toBe ( undefined ) ;
23+ } ) ;
24+
25+ it ( "should let a delete of a name the module does not export succeed" , ( ) => {
26+ expect ( delete ns . notAnExport ) . toBe ( true ) ;
27+ } ) ;
28+
29+ it ( "should still hand out the names the module does export" , ( ) => {
30+ expect ( ns . present ) . toBe ( 1 ) ;
31+ expect ( ns . renamed ) . toBe ( 2 ) ;
32+ expect ( Object . getOwnPropertyNames ( ns ) ) . toEqual ( [ "present" , "renamed" ] ) ;
33+ expect ( ( ) => {
34+ ns . present = 9 ;
35+ } ) . toThrow ( TypeError ) ;
36+ } ) ;
Original file line number Diff line number Diff line change 1+ var renamedLocal = 2 ;
2+
3+ export var present = 1 ;
4+ export { renamedLocal as renamed } ;
Original file line number Diff line number Diff line change 1+ "use strict" ;
2+
3+ /** @type {import("../../../../").Configuration } */
4+ module . exports = {
5+ module : {
6+ rules : [
7+ { test : / m \. j s $ / , parser : { specNamespaceObject : true } } ,
8+ // Reading a name the module does not export is the subject here, and
9+ // webpack warns about each one, so leave that reporting to its own case.
10+ { test : / i n d e x \. j s $ / , parser : { exportsPresence : false } }
11+ ]
12+ }
13+ } ;
Original file line number Diff line number Diff line change @@ -915,13 +915,6 @@ const linkErrorsAtBuildTime = new Set([
915915] ) ;
916916
917917const knownBugs = [
918- // A static export reference is read directly rather than through the
919- // namespace, so the proxy never sees the delete or the strict assignment.
920- "module-code/export-expname-binding-index.js" ,
921- "module-code/namespace/internals/set.js" ,
922- // `ns.__proto__` reaches the exports object instead of reporting undefined
923- // for a name the module does not export.
924- "module-code/namespace/internals/get-str-not-found.js" ,
925918 // Expected error because we use `Promise` to load modules, but this test overrides global `Promise`
926919 "expressions/dynamic-import/returns-promise.js" ,
927920
You can’t perform that action at this time.
0 commit comments