Skip to content

Commit 530687a

Browse files
authored
refactor: use import in worker threads (#20641)
1 parent a1be1bf commit 530687a

File tree

3 files changed

+17
-26
lines changed

3 files changed

+17
-26
lines changed

packages/vite/rolldown.config.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,6 @@ const nodeConfig = defineConfig({
8383
debug: 'debug/src/node.js',
8484
},
8585
},
86-
output: {
87-
...sharedNodeOptions.output,
88-
// When polyfillRequire is enabled, `require` gets renamed by rolldown.
89-
// But the current usage of require() inside inlined workers expects `require`
90-
// to not be renamed. To workaround, polyfillRequire is disabled and
91-
// the banner is used instead.
92-
// Ideally we should move workers to ESM
93-
polyfillRequire: false,
94-
banner:
95-
"import { createRequire as ___createRequire } from 'module'; const require = ___createRequire(import.meta.url);",
96-
},
9786
external: [
9887
/^vite\//,
9988
'fsevents',

packages/vite/src/node/plugins/css.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2676,11 +2676,11 @@ const makeLessWorker = (
26762676
}
26772677

26782678
const worker = new WorkerWithFallback(
2679-
() => {
2680-
// eslint-disable-next-line no-restricted-globals -- this function runs inside a cjs worker
2681-
const fsp = require('node:fs/promises')
2682-
// eslint-disable-next-line no-restricted-globals
2683-
const path = require('node:path')
2679+
async () => {
2680+
const [fsp, path] = await Promise.all([
2681+
import('node:fs/promises'),
2682+
import('node:path'),
2683+
])
26842684

26852685
let ViteLessManager: any
26862686
const createViteLessPlugin = (
@@ -2741,8 +2741,7 @@ const makeLessWorker = (
27412741
additionalData: undefined
27422742
},
27432743
) => {
2744-
// eslint-disable-next-line no-restricted-globals -- this function runs inside a cjs worker
2745-
const nodeLess: typeof Less = require(lessPath)
2744+
const nodeLess: typeof Less = (await import(lessPath)).default
27462745
const viteResolverPlugin = createViteLessPlugin(
27472746
nodeLess,
27482747
options.filename,
@@ -2787,7 +2786,9 @@ const lessProcessor = (
27872786
worker?.stop()
27882787
},
27892788
async process(environment, source, root, options, resolvers) {
2790-
const lessPath = loadPreprocessorPath(PreprocessLang.less, root)
2789+
const lessPath = pathToFileURL(
2790+
loadPreprocessorPath(PreprocessLang.less, root),
2791+
).href
27912792
worker ??= makeLessWorker(environment, resolvers, maxWorkers)
27922793

27932794
const { content, map: additionalMap } = await getSource(
@@ -2853,8 +2854,7 @@ const makeStylWorker = (maxWorkers: number | undefined) => {
28532854
additionalData: undefined
28542855
},
28552856
) => {
2856-
// eslint-disable-next-line no-restricted-globals -- this function runs inside a cjs worker
2857-
const nodeStylus: typeof Stylus = require(stylusPath)
2857+
const nodeStylus: typeof Stylus = (await import(stylusPath)).default
28582858

28592859
const ref = nodeStylus(content, {
28602860
// support @import from node dependencies by default
@@ -2907,7 +2907,9 @@ const stylProcessor = (
29072907
worker?.stop()
29082908
},
29092909
async process(_environment, source, root, options, _resolvers) {
2910-
const stylusPath = loadPreprocessorPath(PreprocessLang.stylus, root)
2910+
const stylusPath = pathToFileURL(
2911+
loadPreprocessorPath(PreprocessLang.stylus, root),
2912+
).href
29112913
worker ??= makeStylWorker(maxWorkers)
29122914

29132915
// Get source with preprocessor options.additionalData. Make sure a new line separator

packages/vite/src/node/plugins/terser.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { pathToFileURL } from 'node:url'
12
import type {
23
TerserMinifyOptions,
34
TerserMinifyOutput,
@@ -47,9 +48,8 @@ export function terserPlugin(config: ResolvedConfig): Plugin {
4748
code: string,
4849
options: TerserMinifyOptions,
4950
) => {
50-
// test fails when using `import`. maybe related: https://github.com/nodejs/node/issues/43205
51-
// eslint-disable-next-line no-restricted-globals -- this function runs inside cjs
52-
const terser = require(terserPath)
51+
const terser: typeof import('terser') = (await import(terserPath))
52+
.default
5353
return terser.minify(code, options) as TerserMinifyOutput
5454
},
5555
{
@@ -99,7 +99,7 @@ export function terserPlugin(config: ResolvedConfig): Plugin {
9999
// Lazy load worker.
100100
worker ||= makeWorker()
101101

102-
const terserPath = loadTerserPath(config.root)
102+
const terserPath = pathToFileURL(loadTerserPath(config.root)).href
103103
const res = await worker.run(terserPath, code, {
104104
safari10: true,
105105
...terserOptions,

0 commit comments

Comments
 (0)