Skip to content

Commit b0df1c0

Browse files
committed
feat: support custom webpack version
1 parent 4fea4ab commit b0df1c0

File tree

1 file changed

+65
-33
lines changed

1 file changed

+65
-33
lines changed

index.js

Lines changed: 65 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
const fs = require('fs');
22
const path = require('path');
3-
const webpack = require('webpack');
43
const optimizer = require('vite-plugin-optimizer');
5-
const libEsm = require('lib-esm');
4+
const libEsm = require('lib-esm').default;
65

76
const PLUGIN_NAME = 'vite-plugin-esmodule';
7+
const TAG = `[${PLUGIN_NAME}]`;
88
const CACHE_DIR = `.${PLUGIN_NAME}`;
99

1010
/**
11-
* @type {import('.')}
11+
* @type {import('vite-plugin-esmodule')}
1212
*/
1313
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+
1421
/**
1522
* @type {import('vite').ConfigEnv}
1623
*/
@@ -93,38 +100,21 @@ function esmodule(modules, webpackFn) {
93100
};
94101

95102
/**
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>}
97104
*/
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+
);
126117

127-
await new Promise(resolve => {
128118
fs.rmSync(args.dir, { recursive: true, force: true });
129119

130120
// Whey use Webpack?
@@ -204,6 +194,48 @@ function ensureDir(filename) {
204194
return filename;
205195
}
206196

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+
207239
esmodule.__test__ = {
208240
CACHE_DIR,
209241
ensureDir,

0 commit comments

Comments
 (0)