Skip to content

Commit

Permalink
Revert "chore: use require inside worker"
Browse files Browse the repository at this point in the history
This reverts commit 925fd58.
  • Loading branch information
sapphi-red committed May 24, 2022
1 parent 593df66 commit eb0cc61
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions packages/vite/src/node/plugins/terser.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { pathToFileURL } from 'url'
import { Worker } from 'okie'
import type { Terser } from 'types/terser'
import type { Plugin } from '../plugin'
import type { ResolvedConfig } from '..'
import { requireResolveFromRootWithFallback } from '../utils'

let terserPath: string | undefined
const loadTerserPath = (root: string) => {
if (terserPath) return terserPath
let terserFileUrl: URL | undefined
const loadTerserFileUrl = (root: string) => {
if (terserFileUrl) return terserFileUrl
try {
terserPath = requireResolveFromRootWithFallback(root, 'terser')
terserFileUrl = pathToFileURL(
requireResolveFromRootWithFallback(root, 'terser')
)
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
throw new Error(
Expand All @@ -20,19 +23,18 @@ const loadTerserPath = (root: string) => {
throw message
}
}
return terserPath
return terserFileUrl
}

export function terserPlugin(config: ResolvedConfig): Plugin {
const makeWorker = () =>
new Worker(
async (
terserPath: string,
terserFileUrl: string,
code: string,
options: Terser.MinifyOptions
) => {
// eslint-disable-next-line no-restricted-globals -- this function runs inside cjs
const terser = require(terserPath)
const terser = await import(terserFileUrl)
return terser.minify(code, options) as Terser.MinifyOutput
}
)
Expand Down Expand Up @@ -63,8 +65,8 @@ export function terserPlugin(config: ResolvedConfig): Plugin {
// Lazy load worker.
worker ||= makeWorker()

const terserPath = loadTerserPath(config.root)
const res = await worker.run(terserPath, code, {
const terserFileUrl = loadTerserFileUrl(config.root)
const res = await worker.run(terserFileUrl.href, code, {
safari10: true,
...config.build.terserOptions,
sourceMap: !!outputOptions.sourcemap,
Expand Down

0 comments on commit eb0cc61

Please sign in to comment.