Comparing version 1.2.2 to 1.2.3
@@ -1,4 +0,2 @@ | ||
/// <reference types="node" /> | ||
import type { Plugin } from "vite"; | ||
import type { FlagSet } from "./utils/flags"; | ||
import { type Options } from "./utils/options"; | ||
@@ -18,10 +16,1 @@ export { createFlagSets, hasFlags } from "./utils/flags"; | ||
} | ||
declare module "rollup" { | ||
interface CustomPluginOptions { | ||
arcSourceId?: string; | ||
arcSourceCode?: string; | ||
arcScanIds?: string[]; | ||
arcFlagSet?: FlagSet; | ||
arcFS?: typeof import("fs"); | ||
} | ||
} |
// src/plugins/build-ssr.ts | ||
import path5 from "path"; | ||
import path4 from "path"; | ||
// src/utils/arc-fs.ts | ||
import fs2 from "fs"; | ||
// src/utils/file-types.ts | ||
var assetFileReg = /\.(?:a?png|jpe?g|jfif|pipeg|pjp|gif|svg|ico|web[pm]|avif|mp4|ogg|mp3|wav|flac|aac|opus|woff2?|eot|[ot]tf|webmanifest|pdf|txt)(\?|$)/; | ||
var globalCSSFileReg = /(?<!\.module)\.(css|less|sass|scss|styl|stylus|pcss|postcss|sss)(?:$|\?)/; | ||
function isAssetFile(id) { | ||
return assetFileReg.test(id); | ||
} | ||
function isGlobalCSSFile(id) { | ||
return globalCSSFileReg.test(id); | ||
} | ||
// src/utils/index-to-id.ts | ||
var ID_START_CHARS = "hjkmoquxzABCDEFGHIJKLNPQRTUVWXYZ$_"; | ||
var ID_START_CHARS_LEN = ID_START_CHARS.length; | ||
var ID_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789$_"; | ||
var ID_CHARS_LEN = ID_CHARS.length; | ||
function indexToId(i) { | ||
let mod = i % ID_START_CHARS_LEN; | ||
let id = ID_START_CHARS[mod]; | ||
i = (i - mod) / ID_START_CHARS_LEN; | ||
while (i > 0) { | ||
mod = i % ID_CHARS_LEN; | ||
id += ID_CHARS[mod]; | ||
i = (i - mod) / ID_CHARS_LEN; | ||
} | ||
return id; | ||
} | ||
// src/utils/matches.ts | ||
@@ -148,176 +172,4 @@ import fs from "fs"; | ||
// src/utils/arc-fs.ts | ||
var arcFlagReg = /\[/; | ||
var fsByFlagSet = /* @__PURE__ */ new Map(); | ||
function getArcFS(flagSet) { | ||
const flags = flagSet.join("."); | ||
let fileSystem = fsByFlagSet.get(flags); | ||
if (!fileSystem) { | ||
fileSystem = createAdaptiveFS(flagSet); | ||
fsByFlagSet.set(flags, fileSystem); | ||
} | ||
return fileSystem; | ||
} | ||
function patchFS(flagSet, afs) { | ||
const flagSets = [flagSet]; | ||
const { readFileSync, statSync, readlinkSync, accessSync, readdirSync } = fs2; | ||
afs.readFileSync = (id, ...args) => readFileSync(getMatch(id), ...args); | ||
afs.statSync = (id, ...args) => statSync(getMatch(id), ...args); | ||
afs.readlinkSync = (id, ...args) => readlinkSync(getMatch(id), ...args); | ||
afs.accessSync = (id, ...args) => accessSync(getMatch(id), ...args); | ||
afs.readdirSync = (id, ...args) => { | ||
const match = getMatch(id); | ||
const entries = readdirSync(id, ...args); | ||
return match === id ? entries : ignoreAdaptiveEntries(entries); | ||
}; | ||
const { readFile, stat, readlink, access, readdir } = fs2; | ||
afs.readFile = (id, ...args) => readFile(getMatch(id), ...args); | ||
afs.stat = (id, ...args) => stat(getMatch(id), ...args); | ||
afs.readlink = (id, ...args) => readlink(getMatch(id), ...args); | ||
afs.access = (id, ...args) => access(getMatch(id), ...args); | ||
afs.readdir = (id, ...args) => { | ||
const match = getMatch(id); | ||
if (match === id) | ||
return readdir(id, ...args); | ||
const cb = args.pop(); | ||
readdir(id, ...args, (err, entries) => { | ||
if (err) | ||
return cb(err); | ||
cb(null, ignoreAdaptiveEntries(entries)); | ||
}); | ||
}; | ||
const { | ||
promises: { | ||
readFile: readFilePromise, | ||
stat: statPromise, | ||
readlink: readlinkPromise, | ||
access: accessPromise, | ||
readdir: readdirPromise | ||
} | ||
} = fs2; | ||
afs.promises.readFile = (id, ...args) => readFilePromise(getMatch(id), ...args); | ||
afs.promises.stat = (id, ...args) => statPromise(getMatch(id), ...args); | ||
afs.promises.readlink = (id, ...args) => readlinkPromise(getMatch(id), ...args); | ||
afs.promises.access = (id, ...args) => accessPromise(getMatch(id), ...args); | ||
afs.promises.readdir = (id, ...args) => { | ||
const match = getMatch(id); | ||
const entriesPromise = readdirPromise(id, ...args); | ||
return match === id ? entriesPromise : entriesPromise.then(ignoreAdaptiveEntries); | ||
}; | ||
return () => { | ||
afs.readFileSync = readFileSync; | ||
afs.statSync = statSync; | ||
afs.readlinkSync = readlinkSync; | ||
afs.accessSync = accessSync; | ||
afs.readdirSync = readdirSync; | ||
afs.readFile = readFile; | ||
afs.stat = stat; | ||
afs.readlink = readlink; | ||
afs.access = access; | ||
afs.readdir = readdir; | ||
afs.promises.readFile = readFilePromise; | ||
afs.promises.stat = statPromise; | ||
afs.promises.readlink = readlinkPromise; | ||
afs.promises.access = accessPromise; | ||
afs.promises.readdir = readdirPromise; | ||
}; | ||
function getMatch(id) { | ||
if (typeof id !== "string") | ||
return id; | ||
const match = getMatches(id, flagSets); | ||
return match ? match.alternates[0].value : id; | ||
} | ||
} | ||
function createAdaptiveFS(flagSet) { | ||
const afs = { ...fs2, promises: { ...fs2.promises } }; | ||
patchFS(flagSet, afs); | ||
return afs; | ||
} | ||
function ignoreAdaptiveEntries(entries) { | ||
for (let i = 0; i < entries.length; i++) { | ||
const entry = entries[i]; | ||
if (arcFlagReg.test(entry)) { | ||
const uniqueEntries = entries.slice(0, i); | ||
for (; i < entries.length; i++) { | ||
const entry2 = entries[i]; | ||
if (!arcFlagReg.test(entry2)) { | ||
uniqueEntries.push(entry2); | ||
} | ||
} | ||
return uniqueEntries; | ||
} | ||
} | ||
return entries; | ||
} | ||
// src/utils/ensure-arc-plugin-is-first.ts | ||
var arcVitePluginNameReg = /^arc-vite/; | ||
function ensureArcPluginIsFirst(plugins) { | ||
for (let i = plugins.length; i--; ) { | ||
const plugin = plugins[i]; | ||
if (!Array.isArray(plugin)) | ||
continue; | ||
const [firstPlugin] = plugin; | ||
if (firstPlugin && typeof firstPlugin === "object" && "name" in firstPlugin && arcVitePluginNameReg.test(firstPlugin.name)) { | ||
plugins[i] = false; | ||
plugins.unshift(plugin); | ||
} | ||
} | ||
} | ||
// src/utils/filename-encoding.ts | ||
import { Buffer } from "buffer"; | ||
var cwd = process.cwd(); | ||
var encodeCwdReg = new RegExp(`~|${escapeReg(cwd)}`, "g"); | ||
var decodeCwdReg = /_?~/g; | ||
function encodeFileName(str) { | ||
return Buffer.from( | ||
str.replace(encodeCwdReg, encodeReplace), | ||
"utf-8" | ||
).toString("base64url"); | ||
} | ||
function decodeFileName(str) { | ||
return Buffer.from(str, "base64").toString("utf-8").replace(decodeCwdReg, decodeReplace); | ||
} | ||
function encodeReplace(match) { | ||
if (match === "~") { | ||
return "_~"; | ||
} | ||
return "~"; | ||
} | ||
function decodeReplace(match) { | ||
if (match === "~") { | ||
return cwd; | ||
} | ||
return "~"; | ||
} | ||
function escapeReg(value) { | ||
return value.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&").replace(/-/g, "\\x2d"); | ||
} | ||
// src/utils/index-to-id.ts | ||
var ID_START_CHARS = "hjkmoquxzABCDEFGHIJKLNPQRTUVWXYZ$_"; | ||
var ID_START_CHARS_LEN = ID_START_CHARS.length; | ||
var ID_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789$_"; | ||
var ID_CHARS_LEN = ID_CHARS.length; | ||
function indexToId(i) { | ||
let mod = i % ID_START_CHARS_LEN; | ||
let id = ID_START_CHARS[mod]; | ||
i = (i - mod) / ID_START_CHARS_LEN; | ||
while (i > 0) { | ||
mod = i % ID_CHARS_LEN; | ||
id += ID_CHARS[mod]; | ||
i = (i - mod) / ID_CHARS_LEN; | ||
} | ||
return id; | ||
} | ||
// src/utils/is-css-file.ts | ||
var cssFileReg = /(?<!\.module)\.(css|less|sass|scss|styl|stylus|pcss|postcss|sss)(?:$|\?)/; | ||
function isCssFile(id) { | ||
return cssFileReg.test(id); | ||
} | ||
// src/utils/read-once-persisted-store.ts | ||
import { promises as fs3 } from "fs"; | ||
import { promises as fs2 } from "fs"; | ||
import os from "os"; | ||
@@ -347,3 +199,3 @@ import path2 from "path"; | ||
} | ||
await (loadedFromDisk ||= fs3.readFile(tmpFile, "utf-8").then(syncDataFromDisk).catch(finishLoadFromDisk)); | ||
await (loadedFromDisk ||= fs2.readFile(tmpFile, "utf-8").then(syncDataFromDisk).catch(finishLoadFromDisk)); | ||
return this.read(); | ||
@@ -354,3 +206,3 @@ } | ||
finishLoadFromDisk(); | ||
fs3.unlink(tmpFile).catch(noop); | ||
fs2.unlink(tmpFile).catch(noop); | ||
for (const [k, v] of JSON.parse(data)) { | ||
@@ -365,3 +217,3 @@ values.set(k, v); | ||
if (code === 0 && values.size) { | ||
fs3.writeFile(tmpFile, JSON.stringify([...values])).catch(noop); | ||
fs2.writeFile(tmpFile, JSON.stringify([...values])).catch(noop); | ||
} | ||
@@ -388,79 +240,7 @@ }); | ||
// src/utils/virtual-matches.ts | ||
import path4 from "path"; | ||
var arcPrefix = "\0arc-"; | ||
var arcVirtualMatchPrefix = `${arcPrefix}match:`; | ||
var isBuiltinModuleType = /\.(?:[mc]?[tj]s|json|css|less|sass|scss|styl|stylus|pcss|postcss|sss)(\?|$)/; | ||
async function getVirtualMatches(ctx, flagSets, resolved) { | ||
const { id } = resolved; | ||
if (path4.isAbsolute(id)) { | ||
const matches = getMatches(id, flagSets); | ||
if (matches) | ||
return matches; | ||
} | ||
if (isBuiltinModuleType.test(id)) | ||
return; | ||
let info = ctx.getModuleInfo(id); | ||
if (!info?.ast) | ||
info = await ctx.load(resolved); | ||
const { meta } = info; | ||
if (Array.isArray(meta.arcScanIds)) { | ||
if (typeof meta.arcSourceCode !== "string") { | ||
ctx.error( | ||
"arc-vite: when providing 'arcScanIds' you must also provide the original source code as 'arcSourceCode'." | ||
); | ||
} | ||
const matchesFlagSets = []; | ||
await Promise.all( | ||
meta.arcScanIds.map(async (id2) => { | ||
const matches = await getVirtualMatches(ctx, flagSets, { id: id2 }); | ||
if (matches) { | ||
for (const alternate of matches.alternates) { | ||
matchesFlagSets.push(alternate.flags); | ||
} | ||
} | ||
}) | ||
); | ||
let alternates; | ||
for (const flagSet of normalizeFlagSets(matchesFlagSets)) { | ||
if (!flagSet.length) | ||
continue; | ||
const alternate = { | ||
flags: flagSet, | ||
value: encodeArcVirtualMatch(id, flagSet) | ||
}; | ||
if (alternates) { | ||
alternates.push(alternate); | ||
} else { | ||
alternates = [alternate]; | ||
} | ||
} | ||
if (alternates) { | ||
return { | ||
default: id, | ||
alternates | ||
}; | ||
} | ||
} | ||
} | ||
function isArcVirtualMatch(id) { | ||
return id.startsWith(arcVirtualMatchPrefix); | ||
} | ||
function decodeArcVirtualMatch(id) { | ||
const prefixEnd = id.indexOf(":", arcVirtualMatchPrefix.length + 1) + 1; | ||
const adaptiveImport = decodeFileName( | ||
id.slice(arcVirtualMatchPrefix.length, prefixEnd - 1) | ||
); | ||
const flagSet = decodeFileName(id.slice(prefixEnd)).split("."); | ||
return [adaptiveImport, flagSet]; | ||
} | ||
function encodeArcVirtualMatch(id, flagSet) { | ||
return arcVirtualMatchPrefix + encodeFileName(id) + ":" + encodeFileName(flagSet.join(".")); | ||
} | ||
// src/plugins/build-ssr.ts | ||
var virtualArcServerModuleId = "\0arc-server-virtual"; | ||
var arcPrefix2 = "\0arc-"; | ||
var arcSuffix = ".mjs"; | ||
var arcProxyPrefix = `${arcPrefix2}proxy:`; | ||
var arcPrefix = "\0arc-"; | ||
var arcJsSuffix = ".mjs"; | ||
var arcProxyPrefix = `${arcPrefix}proxy:`; | ||
function pluginBuildSSR({ | ||
@@ -471,4 +251,5 @@ store, | ||
}) { | ||
const adaptiveMatchesForId = /* @__PURE__ */ new Map(); | ||
let root; | ||
let proxyModuleId = 0; | ||
let metaForProxy = /* @__PURE__ */ new Map(); | ||
return { | ||
@@ -480,3 +261,3 @@ name: "arc-vite:build-ssr", | ||
return `__ARC_ASSETS__(${JSON.stringify( | ||
toPosix(path5.relative(root, id)) | ||
toPosix(path4.relative(root, id)) | ||
)})`; | ||
@@ -488,5 +269,2 @@ } | ||
}, | ||
config(config) { | ||
ensureArcPluginIsFirst(config.plugins); | ||
}, | ||
configResolved(config) { | ||
@@ -507,9 +285,2 @@ root = config.root; | ||
if (isArcId(importer)) { | ||
if (isArcVirtualMatch(importer)) { | ||
return this.resolve( | ||
source, | ||
this.getModuleInfo(importer)?.meta.arcSourceId, | ||
options | ||
); | ||
} | ||
return source; | ||
@@ -522,10 +293,8 @@ } | ||
if (resolved && !resolved.external) { | ||
const matches = await getVirtualMatches(this, flagSets, resolved); | ||
const { id } = resolved; | ||
const matches = getMatches(id, flagSets); | ||
if (matches) { | ||
const { id } = resolved; | ||
if (!this.getModuleInfo(id)?.ast) { | ||
await this.load(resolved); | ||
} | ||
adaptiveMatchesForId.set(id, matches); | ||
return { id: encodeArcProxyId(id) }; | ||
const proxyId = nextProxyId(); | ||
metaForProxy.set(proxyId, { resolved, matches }); | ||
return proxyId; | ||
} | ||
@@ -571,83 +340,71 @@ } | ||
if (isArcProxyId(id)) { | ||
id = decodeArcProxyId(id); | ||
const adaptiveMatches = adaptiveMatchesForId.get(id); | ||
if (adaptiveMatches) { | ||
if (isCssFile(id)) { | ||
let code = ""; | ||
for (const { value } of adaptiveMatches.alternates) { | ||
code += `import ${JSON.stringify(value)}; | ||
const { resolved, matches } = metaForProxy.get(id); | ||
let code = ""; | ||
if (isGlobalCSSFile(resolved.id)) { | ||
for (const { value } of matches.alternates) { | ||
code += `import ${JSON.stringify(value)}; | ||
`; | ||
} | ||
code += `import ${JSON.stringify(adaptiveMatches.default)}; | ||
`; | ||
return { | ||
code, | ||
moduleSideEffects: "no-treeshake" | ||
}; | ||
} | ||
const info = this.getModuleInfo(id); | ||
if (info) { | ||
let code = ""; | ||
let matchCode = ""; | ||
let matchCodeSep = ""; | ||
let i = 0; | ||
for (const { flags, value } of adaptiveMatches.alternates) { | ||
const adaptedImportId = `_${indexToId(i++)}`; | ||
code += `import * as ${adaptedImportId} from ${JSON.stringify( | ||
value | ||
)}; | ||
code += `import ${JSON.stringify(matches.default)}; | ||
`; | ||
matchCode += matchCodeSep + flags.map((flag) => `f.${flag}`).join("&&") + "?" + adaptedImportId; | ||
matchCodeSep = ":"; | ||
} | ||
const defaultId = `_${indexToId(i)}`; | ||
code += `import * as ${defaultId} from ${JSON.stringify( | ||
adaptiveMatches.default | ||
)}; | ||
return { | ||
code, | ||
moduleSideEffects: "no-treeshake" | ||
}; | ||
} | ||
let matchCode = ""; | ||
let matchCodeSep = ""; | ||
let i = 0; | ||
for (const { flags, value } of matches.alternates) { | ||
const adaptedImportId = `_${indexToId(i++)}`; | ||
code += `import * as ${adaptedImportId} from ${JSON.stringify( | ||
value | ||
)}; | ||
`; | ||
matchCode += `:${defaultId}`; | ||
let syntheticNamedExports = false; | ||
if (info.exports?.length) { | ||
const hasNamedExports = info.exports.length >= (info.hasDefaultExport ? 2 : 1); | ||
if (hasNamedExports || info.hasDefaultExport) { | ||
const proxyCode = `/*@__PURE__*/createAdaptiveProxy({default:${defaultId},match(f){return ${matchCode}}})`; | ||
code += `import createAdaptiveProxy from "arc-server/proxy"; | ||
matchCode += matchCodeSep + flags.map((flag) => `f.${flag}`).join("&&") + "?" + adaptedImportId; | ||
matchCodeSep = ":"; | ||
} | ||
const defaultId = `_${indexToId(i)}`; | ||
code += `import * as ${defaultId} from ${JSON.stringify( | ||
matches.default | ||
)}; | ||
`; | ||
if (hasNamedExports) { | ||
syntheticNamedExports = "_"; | ||
code += `export const _ = ${proxyCode}; | ||
matchCode += `:${defaultId}`; | ||
let syntheticNamedExports = false; | ||
let hasNamedExports = false; | ||
let hasDefaultExport = false; | ||
if (isAssetFile(resolved.id)) { | ||
hasDefaultExport = true; | ||
} else { | ||
let info = this.getModuleInfo(resolved.id); | ||
if (!info?.ast) { | ||
info = await this.load(resolved); | ||
} | ||
if (info.exports) { | ||
hasDefaultExport = info.hasDefaultExport === true; | ||
hasNamedExports = info.exports.length >= (hasDefaultExport ? 2 : 1); | ||
} | ||
} | ||
if (hasNamedExports || hasDefaultExport) { | ||
const proxyCode = `/*@__PURE__*/createAdaptiveProxy({default:${defaultId},match(f){return ${matchCode}}})`; | ||
code += `import createAdaptiveProxy from "arc-server/proxy"; | ||
`; | ||
if (info.hasDefaultExport) { | ||
code += "export default _.default;\n"; | ||
} | ||
} else { | ||
code += `export default ${proxyCode}.default; | ||
if (hasNamedExports) { | ||
syntheticNamedExports = "_"; | ||
code += `export const _ = ${proxyCode}; | ||
`; | ||
} | ||
} | ||
} else { | ||
code += "export {};\n"; | ||
if (hasDefaultExport) { | ||
code += "export default _.default;\n"; | ||
} | ||
return { | ||
code, | ||
syntheticNamedExports, | ||
moduleSideEffects: "no-treeshake" | ||
}; | ||
} else { | ||
code += `export default ${proxyCode}.default; | ||
`; | ||
} | ||
} else { | ||
code += "export {};\n"; | ||
} | ||
} else if (isArcVirtualMatch(id)) { | ||
const [arcSourceId, arcFlagSet] = decodeArcVirtualMatch(id); | ||
const { meta, moduleSideEffects, syntheticNamedExports } = this.getModuleInfo(arcSourceId); | ||
const code = meta.arcSourceCode; | ||
const arcFS = getArcFS(arcFlagSet); | ||
return { | ||
code, | ||
moduleSideEffects, | ||
syntheticNamedExports, | ||
meta: { | ||
...meta, | ||
arcSourceId, | ||
arcFlagSet, | ||
arcFS | ||
} | ||
moduleSideEffects: "no-treeshake" | ||
}; | ||
@@ -657,5 +414,6 @@ } | ||
}, | ||
buildEnd() { | ||
closeBundle() { | ||
clearCache(); | ||
adaptiveMatchesForId.clear(); | ||
proxyModuleId = 0; | ||
metaForProxy = /* @__PURE__ */ new Map(); | ||
}, | ||
@@ -668,3 +426,3 @@ generateBundle(outputOptions, bundle, isWrite) { | ||
const cwd2 = process.cwd(); | ||
const dir = outputOptions.dir ? path5.resolve(outputOptions.dir) : path5.resolve(outputOptions.file, ".."); | ||
const dir = outputOptions.dir ? path4.resolve(outputOptions.dir) : path4.resolve(outputOptions.file, ".."); | ||
for (const fileName in bundle) { | ||
@@ -674,3 +432,3 @@ const chunk = bundle[fileName]; | ||
serverEntryFiles.push( | ||
path5.relative(cwd2, path5.resolve(dir, fileName)) | ||
path4.relative(cwd2, path4.resolve(dir, fileName)) | ||
); | ||
@@ -685,5 +443,8 @@ } | ||
}; | ||
function nextProxyId() { | ||
return arcProxyPrefix + (proxyModuleId++).toString(36) + arcJsSuffix; | ||
} | ||
} | ||
function isArcId(id) { | ||
return id.startsWith(arcPrefix2); | ||
return id.startsWith(arcPrefix); | ||
} | ||
@@ -693,13 +454,37 @@ function isArcProxyId(id) { | ||
} | ||
function encodeArcProxyId(id) { | ||
return arcProxyPrefix + encodeFileName(id) + arcSuffix; | ||
// src/plugins/build-web.ts | ||
import { promises as fs3 } from "fs"; | ||
import path5 from "path"; | ||
// src/utils/filename-encoding.ts | ||
import { Buffer } from "buffer"; | ||
var cwd = process.cwd(); | ||
var encodeCwdReg = new RegExp(`~|${escapeReg(cwd)}`, "g"); | ||
var decodeCwdReg = /_?~/g; | ||
function encodeFileName(str) { | ||
return Buffer.from( | ||
str.replace(encodeCwdReg, encodeReplace), | ||
"utf-8" | ||
).toString("base64url"); | ||
} | ||
function decodeArcProxyId(id) { | ||
return decodeFileName(id.slice(arcProxyPrefix.length, -arcSuffix.length)); | ||
function decodeFileName(str) { | ||
return Buffer.from(str, "base64").toString("utf-8").replace(decodeCwdReg, decodeReplace); | ||
} | ||
function encodeReplace(match) { | ||
if (match === "~") { | ||
return "_~"; | ||
} | ||
return "~"; | ||
} | ||
function decodeReplace(match) { | ||
if (match === "~") { | ||
return cwd; | ||
} | ||
return "~"; | ||
} | ||
function escapeReg(value) { | ||
return value.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&").replace(/-/g, "\\x2d"); | ||
} | ||
// src/plugins/build-web.ts | ||
import { promises as fs4 } from "fs"; | ||
import path6 from "path"; | ||
// src/utils/manifest.ts | ||
@@ -850,6 +635,6 @@ import { parseDocument, ElementType, DomUtils } from "htmlparser2"; | ||
} | ||
function stripBasePath(basePath, path8) { | ||
if (path8.startsWith(basePath)) | ||
return path8.slice(basePath.length); | ||
return path8; | ||
function stripBasePath(basePath, path7) { | ||
if (path7.startsWith(basePath)) | ||
return path7.slice(basePath.length); | ||
return path7; | ||
} | ||
@@ -861,16 +646,26 @@ | ||
import { parseDocument as parseDocument2, DomUtils as DomUtils2, ElementType as ElementType2 } from "htmlparser2"; | ||
var { isTag: isTag2, filter, appendChild, prepend, removeElement } = DomUtils2; | ||
var { isTag: isTag2, filter, appendChild, prepend } = DomUtils2; | ||
var parserOptions = { decodeEntities: false, encodeEntities: false }; | ||
var emptyScriptReg = /^[\s;]+$/; | ||
var emptyScriptReg = /^(?:[\s;]+|\/\/[^\n]*|\/\*[\s\S]*?\*\/)*$/; | ||
function prepareArcEntryHTML(runtimeId, html, renderAssetURL, originalChunk, adaptedChunk) { | ||
const originalChunkURL = renderAssetURL(originalChunk.fileName); | ||
if (emptyScriptReg.test(adaptedChunk.code)) { | ||
return [ | ||
{ | ||
tag: "script", | ||
attrs: { | ||
type: "module", | ||
async: true, | ||
crossorigin: true, | ||
src: originalChunkURL | ||
} | ||
} | ||
]; | ||
} | ||
const dom = parseDocument2(html, parserOptions); | ||
const originalChunkIsEmpty = emptyScriptReg.test(originalChunk.code); | ||
const adaptedChunkIsEmpty = emptyScriptReg.test(adaptedChunk.code); | ||
const originalChunkURL = renderAssetURL(originalChunk.fileName); | ||
const adaptedChunkURL = renderAssetURL(adaptedChunk.fileName); | ||
for (const script of filter(isModule, dom)) { | ||
if (script.attribs.src === adaptedChunkURL) { | ||
if (originalChunkIsEmpty && adaptedChunkIsEmpty) { | ||
removeElement(script); | ||
} else if (originalChunkIsEmpty) { | ||
if (originalChunkIsEmpty) { | ||
prepend( | ||
@@ -885,4 +680,2 @@ script, | ||
); | ||
} else if (adaptedChunkIsEmpty) { | ||
script.attribs.src = originalChunkURL; | ||
} else { | ||
@@ -920,3 +713,3 @@ delete script.attribs.src; | ||
import { parseDocument as parseDocument3, DomUtils as DomUtils3 } from "htmlparser2"; | ||
var { isTag: isTag3, removeElement: removeElement2, filter: filter2 } = DomUtils3; | ||
var { isTag: isTag3, removeElement, filter: filter2 } = DomUtils3; | ||
function stripEntryScript(entryScriptURL, html) { | ||
@@ -926,3 +719,3 @@ const dom = parseDocument3(html); | ||
if (script.attribs.src === entryScriptURL) { | ||
removeElement2(script); | ||
removeElement(script); | ||
} | ||
@@ -937,6 +730,6 @@ } | ||
// src/plugins/build-web.ts | ||
var arcPrefix3 = "\0arc-"; | ||
var arcJsSuffix = ".mjs"; | ||
var arcInitPrefix = `${arcPrefix3}init:`; | ||
var arcProxyPrefix2 = `${arcPrefix3}proxy:`; | ||
var arcPrefix2 = "\0arc-"; | ||
var arcJsSuffix2 = ".mjs"; | ||
var arcInitPrefix = `${arcPrefix2}init:`; | ||
var arcProxyPrefix2 = `${arcPrefix2}proxy:`; | ||
var arcChunkFileNameReg = /(.+)\.arc(?:\.(.+))?\.html$/; | ||
@@ -950,4 +743,4 @@ function pluginBuildWeb({ | ||
let globalIds = /* @__PURE__ */ new Map(); | ||
let metaForProxy = /* @__PURE__ */ new Map(); | ||
let adaptiveImporters = /* @__PURE__ */ new Map(); | ||
let adaptiveMatchesForId = /* @__PURE__ */ new Map(); | ||
let bindingsByAdaptiveId = /* @__PURE__ */ new Map(); | ||
@@ -964,5 +757,2 @@ let metaForAdaptiveChunk = /* @__PURE__ */ new Map(); | ||
apply, | ||
config(config) { | ||
ensureArcPluginIsFirst(config.plugins); | ||
}, | ||
configResolved(config) { | ||
@@ -992,5 +782,5 @@ basePath = config.base; | ||
adaptiveImporters = /* @__PURE__ */ new Map(); | ||
adaptiveMatchesForId = /* @__PURE__ */ new Map(); | ||
bindingsByAdaptiveId = /* @__PURE__ */ new Map(); | ||
metaForAdaptiveChunk = /* @__PURE__ */ new Map(); | ||
metaForProxy = /* @__PURE__ */ new Map(); | ||
}, | ||
@@ -1003,9 +793,2 @@ async resolveId(source, importer, options) { | ||
if (isArcId2(importer)) { | ||
if (isArcVirtualMatch(importer)) { | ||
return this.resolve( | ||
source, | ||
this.getModuleInfo(importer)?.meta.arcSourceId, | ||
options | ||
); | ||
} | ||
return source; | ||
@@ -1018,9 +801,5 @@ } | ||
if (resolved && !resolved.external) { | ||
const matches = await getVirtualMatches(this, flagSets, resolved); | ||
const { id } = resolved; | ||
const matches = getMatches(id, flagSets); | ||
if (matches) { | ||
const { id } = resolved; | ||
if (!this.getModuleInfo(id)?.ast) { | ||
await this.load(resolved); | ||
} | ||
adaptiveMatchesForId.set(id, matches); | ||
const adaptiveImportsForImporter = adaptiveImporters.get(importer); | ||
@@ -1032,3 +811,5 @@ if (adaptiveImportsForImporter) { | ||
} | ||
return { id: encodeArcProxyId2(id) }; | ||
const proxyId = nextProxyId(); | ||
metaForProxy.set(proxyId, { resolved, matches }); | ||
return proxyId; | ||
} | ||
@@ -1052,3 +833,3 @@ } | ||
if (isArcProxyId2(childId)) { | ||
adaptiveImports.push(decodeArcProxyId2(childId)); | ||
adaptiveImports.push(childId); | ||
} else { | ||
@@ -1087,25 +868,20 @@ const info = await this.load({ | ||
pending.map(async (adaptiveImport) => { | ||
const adaptiveMatches = adaptiveMatchesForId.get(adaptiveImport); | ||
if (adaptiveMatches) { | ||
let adaptedImport; | ||
for (const { | ||
flags, | ||
value | ||
} of adaptiveMatches.alternates) { | ||
if (hasFlags(flagSet, flags)) { | ||
adaptedImport = value; | ||
break; | ||
} | ||
const { matches } = metaForProxy.get(adaptiveImport); | ||
let adaptedImport; | ||
for (const { flags, value } of matches.alternates) { | ||
if (hasFlags(flagSet, flags)) { | ||
adaptedImport = value; | ||
break; | ||
} | ||
adaptedImport ||= adaptiveMatches.default; | ||
resolvedAdaptiveImports.set( | ||
adaptiveImport, | ||
adaptedImport | ||
); | ||
await scanImports( | ||
adaptedImport, | ||
importsForFlagSet, | ||
pendingAdaptiveImports | ||
); | ||
} | ||
adaptedImport ||= matches.default; | ||
resolvedAdaptiveImports.set( | ||
adaptiveImport, | ||
adaptedImport | ||
); | ||
await scanImports( | ||
adaptedImport, | ||
importsForFlagSet, | ||
pendingAdaptiveImports | ||
); | ||
}) | ||
@@ -1192,40 +968,48 @@ ); | ||
} else if (isArcProxyId2(id)) { | ||
id = decodeArcProxyId2(id); | ||
if (isCssFile(id)) { | ||
const { resolved } = metaForProxy.get(id); | ||
let code = ""; | ||
if (isGlobalCSSFile(resolved.id)) { | ||
return { code: "" }; | ||
} | ||
const info = this.getModuleInfo(id); | ||
if (info) { | ||
let code = ""; | ||
let syntheticNamedExports = false; | ||
if (info.exports?.length) { | ||
const hasNamedExports = info.exports.length >= (info.hasDefaultExport ? 2 : 1); | ||
if (hasNamedExports || info.hasDefaultExport) { | ||
const arcId = getArcId(id); | ||
if (hasNamedExports) { | ||
code += `export const {${arcId}} = ${runtimeId}; | ||
let hasNamedExports = false; | ||
let hasDefaultExport = false; | ||
let syntheticNamedExports = false; | ||
if (isAssetFile(resolved.id)) { | ||
hasDefaultExport = true; | ||
} else { | ||
let info = this.getModuleInfo(resolved.id); | ||
if (!info?.ast) { | ||
info = await this.load(resolved); | ||
} | ||
if (info.exports) { | ||
hasDefaultExport = info.hasDefaultExport === true; | ||
hasNamedExports = info.exports.length >= (hasDefaultExport ? 2 : 1); | ||
} | ||
} | ||
if (hasNamedExports || hasDefaultExport) { | ||
const arcId = getArcId(id); | ||
if (hasNamedExports) { | ||
code += `export const {${arcId}} = ${runtimeId}; | ||
`; | ||
syntheticNamedExports = arcId; | ||
if (info.hasDefaultExport) { | ||
code += `export default ${arcId}.default; | ||
syntheticNamedExports = arcId; | ||
if (hasDefaultExport) { | ||
code += `export default ${arcId}.default; | ||
`; | ||
} | ||
} else { | ||
code += `export default ${runtimeId}.${arcId}.default; | ||
`; | ||
} | ||
} | ||
} else { | ||
code = "export {};\n"; | ||
code += `export default ${runtimeId}.${arcId}.default; | ||
`; | ||
} | ||
return { | ||
code, | ||
syntheticNamedExports, | ||
moduleSideEffects: false | ||
}; | ||
} else { | ||
code = "export {};\n"; | ||
} | ||
return { | ||
code, | ||
syntheticNamedExports, | ||
moduleSideEffects: false | ||
}; | ||
} else if (isArcInitId(id)) { | ||
const [adaptiveImport, adaptedImport] = decodeArcInitId(id); | ||
const bindings = bindingsByAdaptiveId.get(adaptiveImport); | ||
if (!bindings || isCssFile(adaptiveImport)) { | ||
if (!bindings || isGlobalCSSFile(adaptiveImport)) { | ||
return { | ||
@@ -1251,18 +1035,2 @@ code: `import ${JSON.stringify(adaptedImport)}; | ||
}; | ||
} else if (isArcVirtualMatch(id)) { | ||
const [arcSourceId, arcFlagSet] = decodeArcVirtualMatch(id); | ||
const { meta, moduleSideEffects, syntheticNamedExports } = this.getModuleInfo(arcSourceId); | ||
const code = meta.arcSourceCode; | ||
const arcFS = getArcFS(arcFlagSet); | ||
return { | ||
code, | ||
moduleSideEffects, | ||
syntheticNamedExports, | ||
meta: { | ||
...meta, | ||
arcSourceId, | ||
arcFlagSet, | ||
arcFS | ||
} | ||
}; | ||
} | ||
@@ -1354,3 +1122,3 @@ return null; | ||
await Promise.all( | ||
serverEntryFiles.map((file) => fs4.appendFile(file, manifestCode)) | ||
serverEntryFiles.map((file) => fs3.appendFile(file, manifestCode)) | ||
); | ||
@@ -1360,9 +1128,9 @@ } | ||
]; | ||
function encodeArcProxyId2(id) { | ||
return `${arcProxyPrefix2 + (proxyModuleId++).toString(36)}:${encodeFileName(id) + arcJsSuffix}`; | ||
function nextProxyId() { | ||
return arcProxyPrefix2 + (proxyModuleId++).toString(36) + arcJsSuffix2; | ||
} | ||
function encodeArcInitId(adaptiveImport, adaptedImport) { | ||
return `${arcInitPrefix + (initModuleId++).toString(36)}:${(adaptiveImport === adaptedImport ? encodeFileName(adaptiveImport) : `${encodeFileName(adaptiveImport)},${encodeFileName( | ||
adaptedImport[0] === "\0" ? adaptedImport : path6.relative(path6.dirname(adaptiveImport), adaptedImport) | ||
)}`) + arcJsSuffix}`; | ||
adaptedImport[0] === "\0" ? adaptedImport : path5.relative(path5.dirname(adaptiveImport), adaptedImport) | ||
)}`) + arcJsSuffix2}`; | ||
} | ||
@@ -1379,3 +1147,3 @@ function getArcId(source) { | ||
function isArcId2(id) { | ||
return id.startsWith(arcPrefix3); | ||
return id.startsWith(arcPrefix2); | ||
} | ||
@@ -1388,10 +1156,2 @@ function isArcProxyId2(id) { | ||
} | ||
function decodeArcProxyId2(id) { | ||
return decodeFileName( | ||
id.slice( | ||
id.indexOf(":", arcProxyPrefix2.length + 1) + 1, | ||
-arcJsSuffix.length | ||
) | ||
); | ||
} | ||
function decodeArcInitId(id) { | ||
@@ -1402,3 +1162,3 @@ const prefixEnd = id.indexOf(":", arcInitPrefix.length + 1) + 1; | ||
const adaptedImport2 = decodeFileName( | ||
id.slice(prefixEnd, -arcJsSuffix.length) | ||
id.slice(prefixEnd, -arcJsSuffix2.length) | ||
); | ||
@@ -1409,5 +1169,5 @@ return [adaptedImport2, adaptedImport2]; | ||
const relativeAdaptedImport = decodeFileName( | ||
id.slice(sepStart + 1, -arcJsSuffix.length) | ||
id.slice(sepStart + 1, -arcJsSuffix2.length) | ||
); | ||
const adaptedImport = relativeAdaptedImport[0] === "\0" ? relativeAdaptedImport : path6.join(adaptiveImport, "..", relativeAdaptedImport); | ||
const adaptedImport = relativeAdaptedImport[0] === "\0" ? relativeAdaptedImport : path5.join(adaptiveImport, "..", relativeAdaptedImport); | ||
return [adaptiveImport, adaptedImport]; | ||
@@ -1417,5 +1177,4 @@ } | ||
// src/plugins/serve.ts | ||
import fs5 from "fs"; | ||
import { createRequire, syncBuiltinESMExports } from "module"; | ||
import path7 from "path"; | ||
import { createRequire } from "module"; | ||
import path6 from "path"; | ||
function pluginServe({ | ||
@@ -1426,3 +1185,2 @@ flagSets, | ||
const flagSet = forceFlagSet?.length ? forceFlagSet : void 0; | ||
let restoreFS; | ||
return { | ||
@@ -1442,6 +1200,3 @@ name: "arc-vite:serve", | ||
return; | ||
syncBuiltinESMExports(); | ||
restoreFS = patchFS(flagSet, fs5); | ||
ensureArcPluginIsFirst(config.plugins); | ||
config.cacheDir = path7.resolve( | ||
config.cacheDir = path6.resolve( | ||
`node_modules/.vite/arc/${flagSet.join(".")}` | ||
@@ -1465,7 +1220,3 @@ ); | ||
build.onLoad({ filter: /./ }, (args) => { | ||
const adaptedImport = getAdaptedMatch( | ||
args.path, | ||
flagSets, | ||
flagSet | ||
); | ||
const adaptedImport = getAdaptedMatch(args.path, flagSets, flagSet); | ||
if (adaptedImport) { | ||
@@ -1492,17 +1243,6 @@ const proxiedImportCode = JSON.stringify( | ||
if (resolved) { | ||
const adaptedImport = getAdaptedMatch(resolved.id, flagSets, flagSet); | ||
if (adaptedImport) { | ||
return { id: adaptedImport }; | ||
} | ||
return getAdaptedMatch(resolved.id, flagSets, flagSet) || resolved; | ||
} | ||
return resolved; | ||
} | ||
return null; | ||
}, | ||
buildEnd() { | ||
if (restoreFS) { | ||
restoreFS(); | ||
syncBuiltinESMExports(); | ||
restoreFS = void 0; | ||
} | ||
} | ||
@@ -1512,3 +1252,3 @@ }; | ||
function getAdaptedMatch(id, flagSets, flagSet) { | ||
if (path7.isAbsolute(id)) { | ||
if (path6.isAbsolute(id)) { | ||
const matches = getMatches(id, flagSets); | ||
@@ -1515,0 +1255,0 @@ if (matches) { |
import type { Rollup } from "vite"; | ||
export declare function prepareArcEntryHTML(runtimeId: string, html: string, renderAssetURL: (fileName: string) => string, originalChunk: Rollup.OutputChunk, adaptedChunk: Rollup.OutputChunk): string; | ||
export declare function prepareArcEntryHTML(runtimeId: string, html: string, renderAssetURL: (fileName: string) => string, originalChunk: Rollup.OutputChunk, adaptedChunk: Rollup.OutputChunk): string | { | ||
tag: string; | ||
attrs: { | ||
type: string; | ||
async: boolean; | ||
crossorigin: boolean; | ||
src: string; | ||
}; | ||
}[]; |
{ | ||
"name": "arc-vite", | ||
"version": "1.2.2", | ||
"version": "1.2.3", | ||
"description": "Declaratively bundle and execute code specific to your users ARC and Vite.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
5
53216
19
1333