Skip to content

Commit a08ca62

Browse files
authored
Merge pull request #12 from vite-plugin/v1.5.0
V1.5.0
2 parents 4fea4ab + e0ee2a0 commit a08ca62

File tree

6 files changed

+87
-41
lines changed

6 files changed

+87
-41
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## [2023-05-13] v1.5.0
2+
3+
- b0df1c0 feat: support custom `webpack` version
4+
15
## [2022-10-19] v1.4.4
26

37
- 952da00 v1.4.4

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ When using ES modules(e.g.`node-fetch`) in Node.js/Electron projects, we may nee
1414
## Install
1515

1616
```sh
17-
npm i vite-plugin-esmodule -D
17+
npm i -D vite-plugin-esmodule webpack
1818
```
1919

2020
## Usage

build.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const fs = require('fs');
22
const path = require('path');
3-
const libEsm = require('lib-esm');
3+
const libEsm = require('lib-esm').default;
44

55
// const iswatch = process.argv.slice(2).includes('--watch');
66

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,

package.json

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vite-plugin-esmodule",
3-
"version": "1.4.4",
3+
"version": "1.5.0",
44
"description": "Build ES module to CommonJs module for Node.js/Electron",
55
"main": "index.js",
66
"exports": {
@@ -34,13 +34,21 @@
3434
},
3535
"homepage": "https://github.com/vite-plugin/vite-plugin-esmodule#readme",
3636
"dependencies": {
37-
"lib-esm": "~0.3.0",
38-
"vite-plugin-optimizer": "~1.4.2",
39-
"webpack": "^5.74.0"
37+
"lib-esm": "~0.4.0",
38+
"vite-plugin-optimizer": "~1.4.2"
39+
},
40+
"peerDependencies": {
41+
"webpack": "*"
42+
},
43+
"peerDependenciesMeta": {
44+
"webpack": {
45+
"optional": true
46+
}
4047
},
4148
"devDependencies": {
4249
"file-type": "^18.0.0",
43-
"vite": "^3.2.0-beta.2"
50+
"vite": "^4.3.5",
51+
"webpack": "^5.82.1"
4452
},
4553
"files": [
4654
"index.js",

test/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const vite = require('vite');
88
const esmodule = require('vite-plugin-esmodule');
99
const test = esmodule.__test__;
1010
const moduleName = 'file-type';
11+
12+
const TAG = '[test]';
1113
const node_modules = path.join(__dirname, '../node_modules');
1214
const __snapshots__ = path.join(__dirname, '__snapshots__');
1315

@@ -63,6 +65,6 @@ function runTest() {
6365
child.stdout.on('data', chunk => {
6466
const str = chunk.toString().trim();
6567
assert.equal(str, '[AsyncFunction: fileTypeFromFile]');
66-
console.log('test success');
68+
console.log(TAG, 'index.js passed ✅');
6769
});
6870
}

0 commit comments

Comments
 (0)