UNPKG

3.23 kBJavaScriptView Raw
1let SizeLimitError = require('size-limit/size-limit-error')
2let { promisify } = require('util')
3let { nanoid } = require('nanoid/non-secure')
4let { tmpdir } = require('os')
5let { join } = require('path')
6let readdir = promisify(require('fs').readdir)
7let rimraf = promisify(require('rimraf'))
8
9let convertConfig = require('./convert-config')
10let runWebpack = require('./run-webpack')
11let getConfig = require('./get-config')
12
13const WEBPACK_EMPTY_PROJECT = 962
14const WEBPACK_EMPTY_PROJECT_GZIP = 461
15const WEBPACK_EMPTY_PROJECT_IMPORT = 965
16const WEBPACK_EMPTY_PROJECT_IMPORT_GZIP = 473
17
18function getFiles(stats, check) {
19 let entries = {}
20 if (check.entry) {
21 for (let i of check.entry) {
22 if (stats.entrypoints[i]) {
23 entries[i] = stats.entrypoints[i]
24 } else {
25 throw new SizeLimitError('unknownEntry', i)
26 }
27 }
28 } else {
29 entries = stats.entrypoints
30 }
31
32 return Object.keys(entries)
33 .reduce((assets, i) => assets.concat(entries[i].assets), [])
34 .map(i => {
35 if (check.webpackConfig.output && check.webpackConfig.output.path) {
36 return join(check.webpackConfig.output.path, i)
37 } else {
38 return join(process.cwd(), 'dist', i)
39 }
40 })
41}
42
43async function isDirNotEmpty(dir) {
44 try {
45 let files = await readdir(dir)
46 return !!files.length
47 } catch (e) {
48 if (e.code === 'ENOENT') return false
49 throw e
50 }
51}
52
53let self = {
54 name: '@size-limit/webpack',
55
56 async before(config) {
57 if (config.saveBundle) {
58 if (config.cleanDir) {
59 await rimraf(config.saveBundle)
60 } else {
61 let notEmpty = await isDirNotEmpty(config.saveBundle)
62 if (notEmpty) {
63 throw new SizeLimitError('bundleDirNotEmpty', config.saveBundle)
64 }
65 }
66 }
67 },
68
69 async step20(config, check) {
70 if (check.webpack === false) return
71 check.webpackOutput = config.saveBundle
72 if (!check.webpackOutput) {
73 check.webpackOutput = join(tmpdir(), `size-limit-${nanoid()}`)
74 }
75 if (check.config) {
76 check.webpackConfig = require(check.config)
77 convertConfig(check.webpackConfig, config.configPath)
78 } else {
79 check.webpackConfig = await getConfig(config, check, check.webpackOutput)
80 if (check.modifyWebpackConfig) {
81 check.webpackConfig = check.modifyWebpackConfig(check.webpackConfig)
82 }
83 }
84 },
85
86 wait40: 'Adding to empty webpack project',
87 async step40(config, check) {
88 if (check.webpackConfig) {
89 check.bundles = getFiles(await runWebpack(check), check)
90 }
91 },
92
93 async step61(config, check) {
94 if (check.bundles) {
95 if (typeof check.size === 'undefined') {
96 throw new SizeLimitError('missedPlugin', 'file')
97 }
98 if (check.import && check.gzip === false) {
99 check.size -= WEBPACK_EMPTY_PROJECT_IMPORT
100 } else if (check.import) {
101 check.size -= WEBPACK_EMPTY_PROJECT_IMPORT_GZIP
102 } else if (check.gzip === false) {
103 check.size -= WEBPACK_EMPTY_PROJECT
104 } else {
105 check.size -= WEBPACK_EMPTY_PROJECT_GZIP
106 }
107 }
108 },
109
110 async finally(config, check) {
111 if (check.webpackOutput && !config.saveBundle) {
112 await rimraf(check.webpackOutput)
113 }
114 }
115}
116
117module.exports = [self]