UNPKG

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