From 4311aca5cb5ccdfc830e7f4f3ef0b62ce305ad4f Mon Sep 17 00:00:00 2001 From: Clark Du Date: Thu, 30 Aug 2018 17:17:41 +0100 Subject: [PATCH] refactor: update nuxt-start dependencies dynamically (#3816) --- scripts/release-edge | 36 +++++++++++++++++++++++++++++------- start/.gitignore | 1 - start/package.json | 22 ---------------------- 3 files changed, 29 insertions(+), 30 deletions(-) diff --git a/scripts/release-edge b/scripts/release-edge index 3d70f3450f..d3e60cf1cd 100755 --- a/scripts/release-edge +++ b/scripts/release-edge @@ -4,18 +4,39 @@ const { resolve, join } = require('path') const { spawnSync } = require('child_process') const { readFileSync, writeFileSync, copySync } = require('fs-extra') +const rootDir = resolve(__dirname, '..') + class NuxtEdgePublisher { static copyFiles(moduleName, src, fieNames) { - const srcDir = resolve(__dirname, '..', src) - const moduleDir = resolve(__dirname, '..', moduleName, src) + const srcDir = resolve(rootDir, src) + const moduleDir = resolve(rootDir, moduleName, src) for (const file of fieNames) { copySync(resolve(srcDir, file), resolve(moduleDir, file)) } } + static updateDeps(moduleName, packageObj, bundleFile) { + const { dependencies: rootDeps } = JSON.parse(readFileSync(resolve(rootDir, 'package.json'), 'utf-8')) + // Required and Excluded packages for start + const requires = {'minimist': true} + const requireRegex = /require\('([-@/\w]+)'\)/g + const rawCore = readFileSync(resolve(rootDir, bundleFile)) + let match = requireRegex.exec(rawCore) + while (match) { + requires[match[1]] = true + match = requireRegex.exec(rawCore) + } + + for (const dep in rootDeps) { + if (requires[dep]) { + packageObj.dependencies[dep] = rootDeps[dep] + } + } + } + static changePackageName(module = '') { // paths - const packagePath = resolve(__dirname, '..', module, 'package.json') + const packagePath = resolve(rootDir, module, 'package.json') // Read original contents of package.json const originalPackage = readFileSync(packagePath, 'utf-8') @@ -40,14 +61,15 @@ class NuxtEdgePublisher { const baseVersion = p.version.split('-')[0] p.version = `${baseVersion}-${date}.${gitCommit}` - // Write package.json - writeFileSync(packagePath, JSON.stringify(p, null, 2) + '\r\n') - if (module === 'start') { + this.updateDeps('start', p, join('dist', 'nuxt-start.js')) this.copyFiles(module, 'dist', ['nuxt-start.js']) this.copyFiles(module, 'bin', ['nuxt-start', join('common', 'utils.js')]) } + // Write package.json + writeFileSync(packagePath, JSON.stringify(p, null, 2) + '\r\n') + return p } @@ -70,7 +92,7 @@ class NuxtEdgePublisher { // Do publish const { status, output } = spawnSync('npm', `publish --tag ${tag}`.split(' '), { - cwd: resolve(__dirname, '..', module) + cwd: resolve(rootDir, module) }) // eslint-disable-next-line no-console diff --git a/start/.gitignore b/start/.gitignore index f0fd204587..1db00d7371 100644 --- a/start/.gitignore +++ b/start/.gitignore @@ -1,3 +1,2 @@ * !README.md -!package.json \ No newline at end of file diff --git a/start/package.json b/start/package.json index 8e505d2205..80cf096eab 100644 --- a/start/package.json +++ b/start/package.json @@ -49,28 +49,6 @@ "npm": ">=5.0.0" }, "dependencies": { - "@nuxtjs/youch": "^4.2.3", - "chalk": "^2.3.2", - "compression": "^1.7.3", - "connect": "^3.6.5", - "consola": "^1.4.3", - "esm": "^3.0.10", - "etag": "^1.8.1", - "fresh": "^0.5.2", - "fs-extra": "^7.0.0", - "hash-sum": "^1.0.2", - "ip": "^1.1.5", - "launch-editor-middleware": "^2.2.1", - "lodash": "^4.17.5", - "lru-cache": "^4.1.3", - "minimist": "^1.2.0", - "serialize-javascript": "^1.4.0", - "serve-static": "^1.13.2", - "server-destroy": "^1.0.1", - "std-env": "^1.3.1", - "vue": "^2.5.17", - "vue-meta": "^1.5.3", - "vue-server-renderer": "^2.5.17" }, "collective": { "type": "opencollective",