|
1 | 1 | /* eslint-disable no-restricted-globals */ |
2 | 2 | import fs from 'node:fs' |
3 | 3 | import path from 'node:path' |
| 4 | +import url from 'node:url' |
4 | 5 | import nodeResolve from '@rollup/plugin-node-resolve' |
5 | 6 | import typescript from '@rollup/plugin-typescript' |
6 | 7 | import commonjs from '@rollup/plugin-commonjs' |
@@ -134,15 +135,13 @@ function createNodePlugins( |
134 | 135 | }, |
135 | 136 | // postcss-load-config calls require after register ts-node |
136 | 137 | 'postcss-load-config/src/index.js': { |
137 | | - src: `require(configFile)`, |
138 | | - replacement: `__require(configFile)` |
139 | | - }, |
140 | | - // @rollup/plugin-commonjs uses incorrect esm |
141 | | - '@rollup/plugin-commonjs/dist/index.es.js': { |
142 | | - src: `import { sync } from 'resolve';`, |
143 | | - replacement: `import __resolve from 'resolve';const sync = __resolve.sync;` |
| 138 | + pattern: /require(?=\((configFile|'ts-node')\))/g, |
| 139 | + replacement: `eval('require')` |
144 | 140 | } |
145 | 141 | }), |
| 142 | + |
| 143 | + buildTimeImportMetaUrl(), |
| 144 | + |
146 | 145 | commonjs({ |
147 | 146 | extensions: ['.js'], |
148 | 147 | // Optional peer deps of ws. Native deps that are mostly for performance. |
@@ -287,6 +286,21 @@ function shimDepsPlugin(deps: Record<string, ShimOptions>): Plugin { |
287 | 286 | } |
288 | 287 | } |
289 | 288 |
|
| 289 | +// The use of `import.meta.url` in source code is not reliable after bundling. |
| 290 | +// For example, it is affected by the `isEntry` bug brought in by the Rollup CJS plugin |
| 291 | +// https://github.com/rollup/plugins/pull/1180 |
| 292 | +// The better way is to resolve it at build time. |
| 293 | +function buildTimeImportMetaUrl(): Plugin { |
| 294 | + return { |
| 295 | + name: 'buildTimeImportMetaUrl', |
| 296 | + resolveImportMeta: (property, chunk) => { |
| 297 | + if (property === 'url') { |
| 298 | + return `'${url.pathToFileURL(chunk.moduleId).href}'` |
| 299 | + } |
| 300 | + } |
| 301 | + } |
| 302 | +} |
| 303 | + |
290 | 304 | function licensePlugin() { |
291 | 305 | return license({ |
292 | 306 | thirdParty(dependencies) { |
|
0 commit comments