|
1 | | -# lib.shims |
2 | | - |
3 | | -- **Type:** |
4 | | - |
5 | | - ```ts |
6 | | - type Shims = { |
7 | | - cjs?: { |
8 | | - 'import.meta.url'?: boolean; |
9 | | - }; |
10 | | - esm?: { |
11 | | - __filename?: boolean; |
12 | | - __dirname?: boolean; |
13 | | - require?: boolean; |
14 | | - }; |
15 | | - }; |
16 | | - ``` |
17 | | - |
18 | | -- **Default:** |
19 | | - |
20 | | - ```js |
21 | | - { |
22 | | - cjs: { |
23 | | - 'import.meta.url': true, |
24 | | - }, |
25 | | - esm: { |
26 | | - __filename: false, |
27 | | - __dirname: false, |
28 | | - require: false, |
29 | | - }, |
30 | | - } |
31 | | - ``` |
32 | | - |
33 | | -Used to configure the shims for CommonJS and ESM output. |
34 | | - |
35 | | -## shims.cjs |
36 | | - |
37 | | -Set the fields to `true` to enable the corresponding shims for CommonJS output. |
38 | | - |
39 | | -### shims.cjs['import.meta.url'] |
40 | | - |
41 | | -- **Default:** `true` |
42 | | - |
43 | | -Options: |
44 | | - |
45 | | -- `true`: when `format` is `cjs`, the `import.meta.url` in source code will be replaced with the URL of the current module. |
46 | | - |
47 | | - For example, given the following source code: |
48 | | - |
49 | | - ```js |
50 | | - import { readFileSync } from 'fs'; |
51 | | - const buffer = readFileSync(new URL('./data.proto', import.meta.url)); |
52 | | - ``` |
53 | | -
|
54 | | - the CJS output will be transformed to: |
55 | | -
|
56 | | - ```js |
57 | | - const { readFileSync } = require('fs'); |
58 | | - const buffer = readFileSync( |
59 | | - new URL( |
60 | | - './data.proto', |
61 | | - /*#__PURE__*/ (function () { |
62 | | - return typeof document === 'undefined' |
63 | | - ? new (module.require('url'.replace('', '')).URL)( |
64 | | - 'file:' + __filename, |
65 | | - ).href |
66 | | - : (document.currentScript && document.currentScript.src) || |
67 | | - new URL('main.js', document.baseURI).href; |
68 | | - })(), |
69 | | - ), |
70 | | - ); |
71 | | - ``` |
72 | | -
|
73 | | -- `false`: the `import.meta.url` will be leave as is, which will cause a runtime error when running the output. |
74 | | -
|
75 | | -## shims.esm |
76 | | -
|
77 | | -Set the fields to `true` to enable the corresponding shims for ESM output. |
78 | | -
|
79 | | -### shims.esm.\_\_filename |
80 | | -
|
81 | | -Whether to shim the global `__filename` of CommonJS in ESM output. |
82 | | -
|
83 | | -- **Default:** `false` |
84 | | -
|
85 | | -Options: |
86 | | -
|
87 | | -- `true`: when `format` is `esm`, the `__filename` in source code will be replaced with the filename of the current module. |
88 | | -
|
89 | | - For example, given the following source code: |
| 1 | +--- |
| 2 | +overviewHeaders: [2, 3] |
| 3 | +--- |
90 | 4 |
|
91 | | - ```js |
92 | | - console.log(__filename); |
93 | | - ``` |
94 | | -
|
95 | | - the ESM output will be transformed to: |
96 | | -
|
97 | | - ```js |
98 | | - import { fileURLToPath as __webpack_fileURLToPath__ } from 'url'; |
99 | | - import { dirname as __webpack_dirname__ } from 'path'; |
100 | | - var src_dirname = __webpack_dirname__( |
101 | | - __webpack_fileURLToPath__(import.meta.url), |
102 | | - ); |
103 | | - var src_filename = __webpack_fileURLToPath__(import.meta.url); |
104 | | - console.log(src_filename); |
105 | | - ``` |
106 | | -
|
107 | | -- `false`: the `__filename` will be leave as is, which will cause a runtime error when running the output. |
108 | | -
|
109 | | -### shims.esm.\_\_dirname |
110 | | -
|
111 | | -Whether to shim the global `__dirname` of CommonJS in ESM output. |
112 | | -
|
113 | | -- **Default:** `false` |
114 | | -
|
115 | | -Options: |
116 | | -
|
117 | | -- `true`: when `format` is `esm`, the `__dirname` in source code will be replaced with the directory name of the current module. |
118 | | -
|
119 | | - For example, given the following source code: |
120 | | -
|
121 | | - ```js |
122 | | - console.log(__dirname); |
123 | | - ``` |
124 | | -
|
125 | | - the ESM output will be transformed to: |
126 | | -
|
127 | | - ```js |
128 | | - import { fileURLToPath as __webpack_fileURLToPath__ } from 'url'; |
129 | | - import { dirname as __webpack_dirname__ } from 'path'; |
130 | | - var src_dirname = __webpack_dirname__( |
131 | | - __webpack_fileURLToPath__(import.meta.url), |
132 | | - ); |
133 | | - console.log(src_dirname); |
134 | | - ``` |
135 | | -
|
136 | | -- `false`: the `__dirname` will be leave as is, which will cause a runtime error when running the output. |
137 | | -
|
138 | | -### shims.esm.require |
139 | | -
|
140 | | -Whether to shim the global `require` of CommonJS in ESM output. |
141 | | -
|
142 | | -- **Default:** `false` |
143 | | -
|
144 | | -Options: |
145 | | -
|
146 | | -- `true`: when `format` is `esm`, there will be a `require` that created by `createRequire` at the beginning of the output which can be accessed in source code like the global `require` like CommonJS. |
147 | | -
|
148 | | - For example, given the following source code: |
149 | | -
|
150 | | - ```js |
151 | | - const someModule = require('./someModule'); |
152 | | - |
153 | | - // dynamic require |
154 | | - const dynamicRequiredModule = require(SOME_VALUE_IN_RUNTIME); |
155 | | - // require.resolve |
156 | | - const someModulePath = require.resolve('./someModule'); |
157 | | - // use require as a expression |
158 | | - const lazyFn = (module, requireFn) => {}; |
159 | | - lazyFn('./other.js', require); |
160 | | - ``` |
161 | | -
|
162 | | - the ESM output will be transformed to: |
163 | | -
|
164 | | - ```js |
165 | | - import __rslib_shim_module__ from 'module'; |
166 | | - const require = /*#__PURE__*/ __rslib_shim_module__.createRequire( |
167 | | - import.meta.url, |
168 | | - ); |
169 | | - // dynamic require |
170 | | - require(SOME_VALUE_IN_RUNTIME); |
171 | | - // require.resolve |
172 | | - require.resolve('./someModule'); |
173 | | - // use require as a expression |
174 | | - const lazyFn = (module, requireFn) => {}; |
175 | | - lazyFn('./other.js', require); |
176 | | - ``` |
177 | | -
|
178 | | -- `false`: the `require` will be leave as is, which will cause a runtime error when running the output. |
| 5 | +# lib.shims |
0 commit comments