|
1 | 1 | const fs = require('fs'); |
2 | 2 | const path = require('path'); |
3 | | -const webpack = require('webpack'); |
4 | 3 | const optimizer = require('vite-plugin-optimizer'); |
5 | | -const libEsm = require('lib-esm'); |
| 4 | +const libEsm = require('lib-esm').default; |
6 | 5 |
|
7 | 6 | const PLUGIN_NAME = 'vite-plugin-esmodule'; |
| 7 | +const TAG = `[${PLUGIN_NAME}]`; |
8 | 8 | const CACHE_DIR = `.${PLUGIN_NAME}`; |
9 | 9 |
|
10 | 10 | /** |
11 | | - * @type {import('.')} |
| 11 | + * @type {import('vite-plugin-esmodule')} |
12 | 12 | */ |
13 | 13 | function esmodule(modules, webpackFn) { |
| 14 | + try { |
| 15 | + require.resolve('webpack'); |
| 16 | + } catch (error) { |
| 17 | + console.log(TAG, '"webpack" is a required dependency, make sure you have it installed?'); |
| 18 | + return; |
| 19 | + } |
| 20 | + |
14 | 21 | /** |
15 | 22 | * @type {import('vite').ConfigEnv} |
16 | 23 | */ |
@@ -93,38 +100,21 @@ function esmodule(modules, webpackFn) { |
93 | 100 | }; |
94 | 101 |
|
95 | 102 | /** |
96 | | - * @type {(args: import('vite-plugin-optimizer').OptimizerArgs, ...args: Parameters<import('.').Esmodule>) => Promise<void>} |
| 103 | + * @type {(args: import('vite-plugin-optimizer').OptimizerArgs, ...args: Parameters<import('vite-plugin-esmodule')>) => Promise<void>} |
97 | 104 | */ |
98 | | -async function buildESModules(args, modules, webpackFn) { |
99 | | - const entries = modules.reduce((memo, mod) => { |
100 | | - const [key, val] = typeof mod === 'object' ? Object.entries(mod)[0] : [mod, mod]; |
101 | | - return Object.assign(memo, { |
102 | | - // This is essentially an alias |
103 | | - // e.g. { esm-pkg: 'node_modules/.vite-plugin-esmodule/esm-pkg.js' } |
104 | | - [key]: require.resolve(val), |
105 | | - }); |
106 | | - }, {}); |
107 | | - |
108 | | - /** |
109 | | - * @type {import('webpack').Configuration} |
110 | | - */ |
111 | | - let config = { |
112 | | - mode: 'none', |
113 | | - target: 'node14', |
114 | | - entry: entries, |
115 | | - output: { |
116 | | - library: { |
117 | | - type: 'commonjs2', |
118 | | - }, |
119 | | - path: args.dir, |
120 | | - filename: '[name]/index.js', |
121 | | - }, |
122 | | - }; |
123 | | - if (typeof webpackFn === 'function') { |
124 | | - config = webpackFn(config) || config; |
125 | | - } |
| 105 | +async function buildESModules( |
| 106 | + args, |
| 107 | + modules, |
| 108 | + webpackFn, |
| 109 | +) { |
| 110 | + await new Promise(async resolve => { |
| 111 | + const webpack = require('webpack'); |
| 112 | + const config = await resolveWebpackConfig( |
| 113 | + modules, |
| 114 | + args.dir, |
| 115 | + webpackFn, |
| 116 | + ); |
126 | 117 |
|
127 | | - await new Promise(resolve => { |
128 | 118 | fs.rmSync(args.dir, { recursive: true, force: true }); |
129 | 119 |
|
130 | 120 | // Whey use Webpack? |
@@ -204,6 +194,48 @@ function ensureDir(filename) { |
204 | 194 | return filename; |
205 | 195 | } |
206 | 196 |
|
| 197 | +function modules2entries(modules) { |
| 198 | + return modules.reduce((memo, mod) => { |
| 199 | + const [key, val] = typeof mod === 'object' ? Object.entries(mod)[0] : [mod, mod]; |
| 200 | + return Object.assign(memo, { |
| 201 | + // This is essentially an alias |
| 202 | + // e.g. { esm-pkg: 'node_modules/.vite-plugin-esmodule/esm-pkg.js' } |
| 203 | + [key]: require.resolve(val), |
| 204 | + }); |
| 205 | + }, {}); |
| 206 | +} |
| 207 | + |
| 208 | +/** |
| 209 | + * @type {( |
| 210 | + * modules: Record<string, string>, |
| 211 | + * outputPath: string, |
| 212 | + * webpackFn: Parameters<import('vite-plugin-esmodule')>[1] |
| 213 | + * ) => Promise<import('webpack').Configuration>} |
| 214 | + */ |
| 215 | +function resolveWebpackConfig( |
| 216 | + modules, |
| 217 | + outputPath, |
| 218 | + webpackFn, |
| 219 | +) { |
| 220 | + /** |
| 221 | + * @type {import('webpack').Configuration} |
| 222 | + */ |
| 223 | + const config = { |
| 224 | + mode: 'none', |
| 225 | + target: 'node14', |
| 226 | + entry: modules2entries(modules), |
| 227 | + output: { |
| 228 | + library: { |
| 229 | + type: 'commonjs2', |
| 230 | + }, |
| 231 | + path: outputPath, |
| 232 | + filename: '[name]/index.js', |
| 233 | + }, |
| 234 | + }; |
| 235 | + |
| 236 | + return typeof webpackFn === 'function' ? webpackFn(config) : config; |
| 237 | +} |
| 238 | + |
207 | 239 | esmodule.__test__ = { |
208 | 240 | CACHE_DIR, |
209 | 241 | ensureDir, |
|
0 commit comments