@nuxt/vite-builder
Advanced tools
Comparing version 0.3.3 to 0.4.0
@@ -5,7 +5,9 @@ 'use strict'; | ||
const vite = require('vite'); | ||
const consola = require('consola'); | ||
const path = require('path'); | ||
const vitePlugin = require('@vitejs/plugin-vue'); | ||
const upath = require('upath'); | ||
const fsExtra = require('fs-extra'); | ||
const vue = require('@vitejs/plugin-vue'); | ||
const consola = require('consola'); | ||
const vite = require('vite'); | ||
const debounce = require('debounce'); | ||
@@ -26,53 +28,34 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e['default'] : e; } | ||
const vue__default = /*#__PURE__*/_interopDefaultLegacy(vue); | ||
const vite__namespace = /*#__PURE__*/_interopNamespace(vite); | ||
const consola__default = /*#__PURE__*/_interopDefaultLegacy(consola); | ||
const vite__namespace = /*#__PURE__*/_interopNamespace(vite); | ||
const vitePlugin__default = /*#__PURE__*/_interopDefaultLegacy(vitePlugin); | ||
const fsExtra__default = /*#__PURE__*/_interopDefaultLegacy(fsExtra); | ||
const debounce__default = /*#__PURE__*/_interopDefaultLegacy(debounce); | ||
async function bundle(nuxt) { | ||
const ctx = { | ||
nuxt, | ||
config: { | ||
root: nuxt.options.buildDir, | ||
mode: nuxt.options.dev ? "development" : "production", | ||
logLevel: "warn", | ||
resolve: { | ||
alias: { | ||
"nuxt/app": nuxt.options.appDir, | ||
"nuxt/build": nuxt.options.buildDir, | ||
"~": nuxt.options.srcDir, | ||
"@": nuxt.options.srcDir | ||
} | ||
}, | ||
clearScreen: false, | ||
plugins: [ | ||
vue__default({}) | ||
], | ||
build: { | ||
emptyOutDir: false | ||
} | ||
function cacheDirPlugin(rootDir, name) { | ||
const optimizeCacheDir = upath.resolve(rootDir, "node_modules/.cache/vite", name); | ||
return { | ||
name: "nuxt:cache-dir", | ||
configResolved(resolvedConfig) { | ||
resolvedConfig.optimizeCacheDir = optimizeCacheDir; | ||
} | ||
}; | ||
await nuxt.callHook("vite:extend", ctx); | ||
await fsExtra.mkdirp(nuxt.options.buildDir); | ||
await fsExtra.mkdirp(path.resolve(nuxt.options.buildDir, ".vite/temp")); | ||
const callBuild = async (fn, name) => { | ||
try { | ||
const start = Date.now(); | ||
await fn(ctx); | ||
const time = (Date.now() - start) / 1e3; | ||
consola__default.success(`${name} compiled successfully in ${time}s`); | ||
} catch (err) { | ||
consola__default.error(`${name} compiled with errors:`, err); | ||
} | ||
function replace(replacements) { | ||
return { | ||
name: "nuxt:replace", | ||
transform(code) { | ||
Object.entries(replacements).forEach(([key, value]) => { | ||
const escapedKey = key.replace(/\./g, "\\."); | ||
code = code.replace(new RegExp(escapedKey, "g"), value); | ||
}); | ||
return { | ||
code, | ||
map: null | ||
}; | ||
} | ||
}; | ||
if (nuxt.options.dev) { | ||
await Promise.all([ | ||
callBuild(buildClient, "Client"), | ||
callBuild(buildServer, "Server") | ||
]); | ||
} else { | ||
await callBuild(buildClient, "Client"); | ||
await callBuild(buildServer, "Server"); | ||
} | ||
} | ||
async function buildClient(ctx) { | ||
@@ -82,3 +65,5 @@ const clientConfig = vite__namespace.mergeConfig(ctx.config, { | ||
"process.server": false, | ||
"process.client": true | ||
"process.client": true, | ||
"module.hot": false, | ||
global: "globalThis" | ||
}, | ||
@@ -89,5 +74,10 @@ build: { | ||
rollupOptions: { | ||
input: path.resolve(ctx.nuxt.options.buildDir, "./entry.client") | ||
input: path.resolve(ctx.nuxt.options.buildDir, "client.js") | ||
} | ||
}, | ||
plugins: [ | ||
replace({"process.env": "import.meta.env"}), | ||
cacheDirPlugin(ctx.nuxt.options.rootDir, "client"), | ||
vitePlugin__default(ctx.config.vue) | ||
], | ||
server: { | ||
@@ -97,15 +87,41 @@ middlewareMode: true | ||
}); | ||
if (ctx.nuxt.options.dev) { | ||
const viteServer = await vite__namespace.createServer(clientConfig); | ||
await ctx.nuxt.callHook("server:devMiddleware", (req, res, next) => { | ||
const originalURL = req.url; | ||
viteServer.middlewares.handle(req, res, (err) => { | ||
req.url = originalURL; | ||
next(err); | ||
}); | ||
await ctx.nuxt.callHook("vite:extendConfig", clientConfig, {isClient: true, isServer: false}); | ||
const viteServer = await vite__namespace.createServer(clientConfig); | ||
await ctx.nuxt.callHook("vite:serverCreated", viteServer); | ||
const viteMiddleware = (req, res, next) => { | ||
const originalURL = req.url; | ||
if (req.url === "/_nuxt/client.js") { | ||
return res.end(""); | ||
} | ||
viteServer.middlewares.handle(req, res, (err) => { | ||
req.url = originalURL; | ||
next(err); | ||
}); | ||
} else { | ||
await vite__namespace.build(clientConfig); | ||
} | ||
}; | ||
await ctx.nuxt.callHook("server:devMiddleware", viteMiddleware); | ||
ctx.nuxt.hook("close", async () => { | ||
await viteServer.close(); | ||
}); | ||
} | ||
var __defProp$1 = Object.defineProperty; | ||
var __hasOwnProp$1 = Object.prototype.hasOwnProperty; | ||
var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols; | ||
var __propIsEnum$1 = Object.prototype.propertyIsEnumerable; | ||
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, {enumerable: true, configurable: true, writable: true, value}) : obj[key] = value; | ||
var __assign$1 = (a, b) => { | ||
for (var prop in b || (b = {})) | ||
if (__hasOwnProp$1.call(b, prop)) | ||
__defNormalProp$1(a, prop, b[prop]); | ||
if (__getOwnPropSymbols$1) | ||
for (var prop of __getOwnPropSymbols$1(b)) { | ||
if (__propIsEnum$1.call(b, prop)) | ||
__defNormalProp$1(a, prop, b[prop]); | ||
} | ||
return a; | ||
}; | ||
const wpfs = __assign$1(__assign$1({}, fsExtra__default), { | ||
join: upath.join | ||
}); | ||
async function buildServer(ctx) { | ||
@@ -116,4 +132,13 @@ const serverConfig = vite__namespace.mergeConfig(ctx.config, { | ||
"process.client": false, | ||
window: void 0 | ||
"typeof window": '"undefined"', | ||
"typeof document": '"undefined"', | ||
"typeof navigator": '"undefined"', | ||
"typeof location": '"undefined"', | ||
"typeof XMLHttpRequest": '"undefined"' | ||
}, | ||
ssr: { | ||
external: [ | ||
"axios" | ||
] | ||
}, | ||
build: { | ||
@@ -123,3 +148,3 @@ outDir: "dist/server", | ||
rollupOptions: { | ||
input: path.resolve(ctx.nuxt.options.buildDir, "./entry.server"), | ||
input: path.resolve(ctx.nuxt.options.buildDir, "entry.mjs"), | ||
onwarn(warning, rollupWarn) { | ||
@@ -131,16 +156,107 @@ if (!["UNUSED_EXTERNAL_IMPORT"].includes(warning.code)) { | ||
} | ||
} | ||
}, | ||
plugins: [ | ||
cacheDirPlugin(ctx.nuxt.options.rootDir, "server"), | ||
vitePlugin__default() | ||
] | ||
}); | ||
await ctx.nuxt.callHook("vite:extendConfig", serverConfig, {isClient: false, isServer: true}); | ||
const serverDist = path.resolve(ctx.nuxt.options.buildDir, "dist/server"); | ||
await fsExtra.mkdirp(serverDist); | ||
await fsExtra.writeFile(path.resolve(serverDist, "client.manifest.json"), "false"); | ||
await fsExtra.writeFile(path.resolve(serverDist, "server.js"), 'const entry = require("./entry.server"); module.exports = entry.default || entry;'); | ||
await vite__namespace.build(serverConfig); | ||
if (ctx.nuxt.options.dev) { | ||
ctx.nuxt.hook("builder:watch", () => { | ||
vite__namespace.build(serverConfig).catch(consola__default.error); | ||
}); | ||
await fsExtra.writeFile(path.resolve(serverDist, "server.js"), 'module.exports = require("./entry")', "utf8"); | ||
await fsExtra.writeFile(path.resolve(serverDist, "client.manifest.json"), "false", "utf8"); | ||
const onBuild = () => ctx.nuxt.callHook("build:resources", wpfs); | ||
if (!ctx.nuxt.options.ssr) { | ||
await onBuild(); | ||
return; | ||
} | ||
const build = debounce__default(async () => { | ||
const start = Date.now(); | ||
await vite__namespace.build(serverConfig); | ||
await onBuild(); | ||
consola__default.info(`Server built in ${Date.now() - start}ms`); | ||
}, 300); | ||
await build(); | ||
ctx.nuxt.hook("builder:watch", () => build()); | ||
ctx.nuxt.hook("app:templatesGenerated", () => build()); | ||
} | ||
async function warmupViteServer(server, entries) { | ||
const warmedUrls = new Set(); | ||
const warmup = async (url) => { | ||
if (warmedUrls.has(url)) { | ||
return void 0; | ||
} | ||
warmedUrls.add(url); | ||
await server.transformRequest(url); | ||
const deps = Array.from(server.moduleGraph.urlToModuleMap.get(url).importedModules); | ||
await Promise.all(deps.map((m) => warmup(m.url))); | ||
}; | ||
await Promise.all(entries.map((entry) => warmup(entry))); | ||
} | ||
var __defProp = Object.defineProperty; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __getOwnPropSymbols = Object.getOwnPropertySymbols; | ||
var __propIsEnum = Object.prototype.propertyIsEnumerable; | ||
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, {enumerable: true, configurable: true, writable: true, value}) : obj[key] = value; | ||
var __assign = (a, b) => { | ||
for (var prop in b || (b = {})) | ||
if (__hasOwnProp.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
if (__getOwnPropSymbols) | ||
for (var prop of __getOwnPropSymbols(b)) { | ||
if (__propIsEnum.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
} | ||
return a; | ||
}; | ||
async function bundle(nuxt) { | ||
const ctx = { | ||
nuxt, | ||
config: vite__namespace.mergeConfig(nuxt.options.vite || {}, { | ||
root: nuxt.options.buildDir, | ||
mode: nuxt.options.dev ? "development" : "production", | ||
logLevel: "warn", | ||
define: { | ||
"process.dev": nuxt.options.dev | ||
}, | ||
resolve: { | ||
extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json", ".vue"], | ||
alias: __assign(__assign({}, nuxt.options.alias), { | ||
"#build": nuxt.options.buildDir, | ||
"#app": nuxt.options.appDir, | ||
"~": nuxt.options.srcDir, | ||
"@": nuxt.options.srcDir, | ||
"web-streams-polyfill/ponyfill/es2018": "unenv/runtime/mock/empty", | ||
"abort-controller": "unenv/runtime/mock/empty" | ||
}) | ||
}, | ||
vue: {}, | ||
css: {}, | ||
optimizeDeps: { | ||
exclude: [] | ||
}, | ||
esbuild: { | ||
jsxFactory: "h", | ||
jsxFragment: "Fragment" | ||
}, | ||
clearScreen: false, | ||
build: { | ||
emptyOutDir: false | ||
}, | ||
plugins: [] | ||
}) | ||
}; | ||
await nuxt.callHook("vite:extend", ctx); | ||
nuxt.hook("vite:serverCreated", (server) => { | ||
const start = Date.now(); | ||
warmupViteServer(server, ["/entry.mjs"]).then(() => { | ||
consola__default.info(`Vite warmed up in ${Date.now() - start}ms`); | ||
}).catch(consola__default.error); | ||
}); | ||
await buildClient(ctx); | ||
await buildServer(ctx); | ||
} | ||
exports.bundle = bundle; |
{ | ||
"name": "@nuxt/vite-builder", | ||
"version": "0.3.3", | ||
"version": "0.4.0", | ||
"repository": "nuxt/framework", | ||
@@ -14,14 +14,18 @@ "license": "MIT", | ||
"devDependencies": { | ||
"@types/debounce": "^1.2.0", | ||
"unbuild": "^0.2.3" | ||
}, | ||
"dependencies": { | ||
"@nuxt/kit": "^0.5.3", | ||
"@nuxt/kit": "^0.6.0", | ||
"@vitejs/plugin-vue": "^1.2.2", | ||
"@vue/compiler-sfc": "^3.0.11", | ||
"chokidar": "^3.5.1", | ||
"consola": "^2.15.3", | ||
"fs-extra": "^9.1.0", | ||
"vite": "^2.2.3", | ||
"debounce": "^1.2.1", | ||
"fs-extra": "^10.0.0", | ||
"upath": "^2.0.1", | ||
"vite": "^2.3.3", | ||
"vue": "3.0.11" | ||
}, | ||
"gitHead": "bfcd3b0e9c11e353b93fe619e88a3624f2d91959" | ||
"gitHead": "2f2e818e325048a3f619f8eb11de2e9cdd3aea7c" | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15821
463
10
2
3
+ Addedchokidar@^3.5.1
+ Addeddebounce@^1.2.1
+ Addedupath@^2.0.1
+ Added@nodelib/fs.scandir@2.1.5(transitive)
+ Added@nodelib/fs.stat@2.0.5(transitive)
+ Added@nodelib/fs.walk@1.2.8(transitive)
+ Added@nuxt/kit@0.6.4(transitive)
+ Addedanymatch@3.1.3(transitive)
+ Addedarray-union@2.1.0(transitive)
+ Addedbinary-extensions@2.3.0(transitive)
+ Addedbraces@3.0.3(transitive)
+ Addedchokidar@3.6.0(transitive)
+ Addeddebounce@1.2.1(transitive)
+ Addeddefu@5.0.1(transitive)
+ Addeddir-glob@3.0.1(transitive)
+ Addeddotenv@10.0.0(transitive)
+ Addedfast-glob@3.3.2(transitive)
+ Addedfastq@1.17.1(transitive)
+ Addedfill-range@7.1.1(transitive)
+ Addedfs-extra@10.1.0(transitive)
+ Addedglob-parent@5.1.2(transitive)
+ Addedglobby@11.1.0(transitive)
+ Addedignore@5.3.2(transitive)
+ Addedis-binary-path@2.1.0(transitive)
+ Addedis-extglob@2.1.1(transitive)
+ Addedis-glob@4.0.3(transitive)
+ Addedis-number@7.0.0(transitive)
+ Addedmerge2@1.4.1(transitive)
+ Addedmicromatch@4.0.8(transitive)
+ Addednormalize-path@3.0.0(transitive)
+ Addedpath-type@4.0.0(transitive)
+ Addedpicomatch@2.3.1(transitive)
+ Addedqueue-microtask@1.2.3(transitive)
+ Addedreaddirp@3.6.0(transitive)
+ Addedreusify@1.0.4(transitive)
+ Addedrun-parallel@1.2.0(transitive)
+ Addedslash@3.0.0(transitive)
+ Addedto-regex-range@5.0.1(transitive)
- Removed@nuxt/kit@0.5.3(transitive)
- Removedat-least-node@1.0.0(transitive)
- Removeddefu@4.0.1(transitive)
- Removeddotenv@8.6.0(transitive)
- Removedfs-extra@9.1.0(transitive)
Updated@nuxt/kit@^0.6.0
Updatedfs-extra@^10.0.0
Updatedvite@^2.3.3