保誠-保戶業務員媒合平台
Tomas
2022-05-19 957a1f10a06fdbb76f1a0ba94fe44126c613fee3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*!
 * @nuxt/cli v2.15.8 (c) 2016-2021
 * Released under the MIT License
 * Repository: https://github.com/nuxt/nuxt.js
 * Website: https://nuxtjs.org
*/
'use strict';
 
const utils = require('@nuxt/utils');
const consola = require('consola');
const index = require('./cli-index.js');
const path = require('path');
const upath = require('upath');
const fs = require('fs-extra');
const crc32 = require('crc/lib/crc32');
const globby = require('globby');
const destr = require('destr');
require('@nuxt/config');
require('exit');
require('chalk');
require('std-env');
require('wrap-ansi');
require('boxen');
require('minimist');
require('hable');
require('defu');
require('semver');
require('fs');
require('execa');
 
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
 
const consola__default = /*#__PURE__*/_interopDefaultLegacy(consola);
const path__default = /*#__PURE__*/_interopDefaultLegacy(path);
const upath__default = /*#__PURE__*/_interopDefaultLegacy(upath);
const fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
const crc32__default = /*#__PURE__*/_interopDefaultLegacy(crc32);
const globby__default = /*#__PURE__*/_interopDefaultLegacy(globby);
const destr__default = /*#__PURE__*/_interopDefaultLegacy(destr);
 
async function generate$1 (cmd) {
  const nuxt = await getNuxt({ server: true }, cmd);
  const generator = await cmd.getGenerator(nuxt);
 
  await nuxt.server.listen(0);
  const { errors } = await generator.generate({ build: false });
  await nuxt.close();
  if (cmd.argv['fail-on-error'] && errors.length > 0) {
    throw new Error('Error generating pages, exiting with non-zero code')
  }
}
 
async function ensureBuild (cmd) {
  const nuxt = await getNuxt({ _build: true, server: false }, cmd);
  const { options } = nuxt;
 
  if (options.generate.cache === false || destr__default['default'](process.env.NUXT_BUILD) || cmd.argv['force-build']) {
    const builder = await cmd.getBuilder(nuxt);
    await builder.build();
    await nuxt.close();
    return
  }
 
  // Default build ignore files
  const ignore = [
    options.buildDir,
    options.dir.static,
    options.generate.dir,
    'node_modules',
    '.**/*',
    '.*',
    'README.md'
  ];
 
  // Extend ignore
  const { generate } = options;
  if (typeof generate.cache.ignore === 'function') {
    generate.cache.ignore = generate.cache.ignore(ignore);
  } else if (Array.isArray(generate.cache.ignore)) {
    generate.cache.ignore = generate.cache.ignore.concat(ignore);
  }
  await nuxt.callHook('generate:cache:ignore', generate.cache.ignore);
 
  // Take a snapshot of current project
  const snapshotOptions = {
    rootDir: options.rootDir,
    ignore: generate.cache.ignore.map(upath__default['default'].normalize),
    globbyOptions: generate.cache.globbyOptions
  };
 
  const currentBuildSnapshot = await snapshot(snapshotOptions);
 
  // Detect process.env usage in nuxt.config
  const processEnv = {};
  if (nuxt.options._nuxtConfigFile) {
    const configSrc = await fs__default['default'].readFile(nuxt.options._nuxtConfigFile);
    const envRegex = /process.env.(\w+)/g;
    let match;
    // eslint-disable-next-line no-cond-assign
    while (match = envRegex.exec(configSrc)) {
      processEnv[match[1]] = process.env[match[1]];
    }
  }
 
  // Current build meta
  const currentBuild = {
    // @ts-ignore
    nuxtVersion: nuxt.constructor.version,
    ssr: nuxt.options.ssr,
    target: nuxt.options.target,
    snapshot: currentBuildSnapshot,
    env: nuxt.options.env,
    'process.env': processEnv
  };
 
  // Check if build can be skipped
  const nuxtBuildFile = path__default['default'].resolve(nuxt.options.buildDir, 'build.json');
  if (fs__default['default'].existsSync(nuxtBuildFile)) {
    const previousBuild = destr__default['default'](fs__default['default'].readFileSync(nuxtBuildFile, 'utf-8')) || {};
 
    // Quick diff
    let needBuild = false;
    for (const field of ['nuxtVersion', 'ssr', 'target', 'env', 'process.env']) {
      if (JSON.stringify(previousBuild[field]) !== JSON.stringify(currentBuild[field])) {
        needBuild = true;
        consola__default['default'].info(`Doing webpack rebuild because ${field} changed`);
        break
      }
    }
 
    // Full snapshot diff
    if (!needBuild) {
      const changed = compareSnapshots(previousBuild.snapshot, currentBuild.snapshot);
      if (!changed) {
        consola__default['default'].success('Skipping webpack build as no changes detected');
        return
      } else {
        consola__default['default'].info(`Doing webpack rebuild because ${changed} modified`);
      }
    }
  }
 
  // Webpack build
  const builder = await cmd.getBuilder(nuxt);
  await builder.build();
 
  // Write build.json
  fs__default['default'].writeFileSync(nuxtBuildFile, JSON.stringify(currentBuild, null, 2), 'utf-8');
 
  await nuxt.close();
}
 
async function getNuxt (args, cmd) {
  const config = await cmd.getNuxtConfig({ dev: false, ...args });
 
  if (config.target === utils.TARGETS.static) {
    config._export = true;
  } else {
    config._legacyGenerate = true;
  }
  config.buildDir = (config.static && config.static.cacheDir) || path__default['default'].resolve(config.rootDir, 'node_modules/.cache/nuxt');
  config.build = config.build || {};
  // https://github.com/nuxt/nuxt.js/issues/7390
  config.build.parallel = false;
  config.build.transpile = config.build.transpile || [];
  if (!config.static || !config.static.cacheDir) {
    config.build.transpile.push('.cache/nuxt');
  }
 
  const nuxt = await cmd.getNuxt(config);
 
  return nuxt
}
 
function compareSnapshots (from, to) {
  const allKeys = Array.from(new Set([
    ...Object.keys(from).sort(),
    ...Object.keys(to).sort()
  ]));
 
  for (const key of allKeys) {
    if (JSON.stringify(from[key]) !== JSON.stringify(to[key])) {
      return key
    }
  }
 
  return false
}
 
async function snapshot ({ globbyOptions, ignore, rootDir }) {
  const snapshot = {};
 
  const files = await globby__default['default']('**/*.*', {
    ...globbyOptions,
    ignore,
    cwd: rootDir,
    absolute: true
  });
 
  await Promise.all(files.map(async (p) => {
    const key = path.relative(rootDir, p);
    try {
      const fileContent = await fs__default['default'].readFile(p);
      snapshot[key] = {
        checksum: await crc32__default['default'](fileContent).toString(16)
      };
    } catch (e) {
      // TODO: Check for other errors like permission denied
      snapshot[key] = {
        exists: false
      };
    }
  }));
 
  return snapshot
}
 
const generate = {
  name: 'generate',
  description: 'Generate a static web application (server-rendered)',
  usage: 'generate <dir>',
  options: {
    ...index.common,
    ...index.locking,
    build: {
      type: 'boolean',
      default: true,
      description: 'Only generate pages for dynamic routes, used for incremental builds. Generate has to be run once without this option before using it'
    },
    devtools: {
      type: 'boolean',
      default: false,
      description: 'Enable Vue devtools',
      prepare (cmd, options, argv) {
        options.vue = options.vue || {};
        options.vue.config = options.vue.config || {};
        if (argv.devtools) {
          options.vue.config.devtools = true;
        }
      }
    },
    quiet: {
      alias: 'q',
      type: 'boolean',
      description: 'Disable output except for errors',
      prepare (cmd, options, argv) {
        // Silence output when using --quiet
        options.build = options.build || {};
        if (argv.quiet) {
          options.build.quiet = true;
        }
      }
    },
    modern: {
      ...index.common.modern,
      description: 'Generate app in modern build (modern mode can be only client)',
      prepare (cmd, options, argv) {
        if (index.normalizeArg(argv.modern)) {
          options.modern = 'client';
        }
      }
    },
    'force-build': {
      type: 'boolean',
      default: false,
      description: 'Force to build the application with webpack'
    },
    'fail-on-error': {
      type: 'boolean',
      default: false,
      description: 'Exit with non-zero status code if there are errors when generating pages'
    }
  },
  async run (cmd) {
    const config = await cmd.getNuxtConfig({ dev: false });
 
    // Disable analyze if set by the nuxt config
    config.build = config.build || {};
    config.build.analyze = false;
 
    // Full static
    if (config.target === utils.TARGETS.static) {
      await ensureBuild(cmd);
      await generate$1(cmd);
      return
    }
 
    // Forcing static target anyway
    config.target = utils.TARGETS.static;
    consola__default['default'].warn(`When using \`nuxt generate\`, you should set \`target: 'static'\` in your \`nuxt.config\`\n       👉 Learn more about it on https://go.nuxtjs.dev/static-target`);
 
    // Set flag to keep the prerendering behaviour
    config._legacyGenerate = true;
    if (config.build) {
      // https://github.com/nuxt/nuxt.js/issues/7390
      config.build.parallel = false;
    }
 
    const nuxt = await cmd.getNuxt(config);
 
    if (cmd.argv.lock) {
      await cmd.setLock(await index.createLock({
        id: 'build',
        dir: nuxt.options.buildDir,
        root: config.rootDir
      }));
 
      nuxt.hook('build:done', async () => {
        await cmd.releaseLock();
 
        await cmd.setLock(await index.createLock({
          id: 'generate',
          dir: nuxt.options.generate.dir,
          root: config.rootDir
        }));
      });
    }
 
    const generator = await cmd.getGenerator(nuxt);
    await nuxt.server.listen(0);
 
    const { errors } = await generator.generate({
      init: true,
      build: cmd.argv.build
    });
 
    await nuxt.close();
    if (cmd.argv['fail-on-error'] && errors.length > 0) {
      throw new Error('Error generating pages, exiting with non-zero code')
    }
  }
};
 
exports.default = generate;