UNPKG

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