1 An `import` statement with a sole label will import the modules default 2 export and bind it to a local variable named after the label. 3 4 -- Testcase -- 5 import defVal from "./files/test1.uc"; 6 7 print(defVal, "\n"); 8 -- End -- 9 10 -- File test1.uc -- 11 export default "This is the default export"; 12 -- End -- 13 14 -- Args -- 15 -R 16 -- End -- 17 18 -- Expect stdout -- 19 This is the default export 20 -- End -- 21 22 23 Attemping to import a default export from a module without default 24 export will raise an error. 25 26 -- Testcase -- 27 import defVal from "./files/test2.uc"; 28 29 print(defVal, "\n"); 30 -- End -- 31 32 -- File test2.uc -- 33 export const x = "This is a non-default export"; 34 -- End -- 35 36 -- Args -- 37 -R 38 -- End -- 39 40 -- Expect stderr -- 41 Syntax error: Module ./files/test2.uc has no default export 42 In [stdin], line 1, byte 20: 43 44 `import defVal from "./files/test2.uc";` 45 Near here ---------^ 46 47 48 -- End -- 49 50 51 In import statements usign the list syntax, the `default` keyword can be 52 used to refer to default exports. 53 54 -- Testcase -- 55 import { default as defVal } from "./files/test3.uc"; 56 57 print(defVal, "\n"); 58 -- End -- 59 60 -- File test3.uc -- 61 export default "This is the default export"; 62 -- End -- 63 64 -- Args -- 65 -R 66 -- End -- 67 68 -- Expect stdout -- 69 This is the default export 70 -- End -- 71 72 73 When using the default keyword within the list syntax, the `as` keyword is 74 mandatory to assign a non-reserved keyword as name. 75 76 -- Testcase -- 77 import { default } from "./files/test4.uc"; 78 79 print(defVal, "\n"); 80 -- End -- 81 82 -- File test4.uc -- 83 export default "This is the default export"; 84 -- End -- 85 86 -- Args -- 87 -R 88 -- End -- 89 90 -- Expect stderr -- 91 Syntax error: Unexpected token 92 Expecting 'as' 93 In line 1, byte 18: 94 95 `import { default } from "./files/test4.uc";` 96 Near here -------^ 97 98 99 -- End --
This page was automatically generated by LXR 0.3.1. • OpenWrt