Skip to content

Commit 7e09c2f

Browse files
authored
Fix: Detecting version relays on the node_modules from process.cwd() (#27)
* Build: Editorconfig sync [skip-ci] * Build: Editorconfig sync [skip-ci] * Fix: Detecting version relays on the node_modules from process.cwd() call * Fix: Npm audit fix
1 parent 44d952d commit 7e09c2f

File tree

3 files changed

+1762
-250
lines changed

3 files changed

+1762
-250
lines changed

lib/index.js

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,53 @@
1-
const { join } = require('path');
21
const { RawSource } = require('webpack-sources');
2+
const npmCheck = require('npm-check');
3+
34

45
class MicroservicesWebpackPlugin {
56
constructor(config = {}) {
67
this.modules = config.modules || [];
78
this.url = config.url || 'https://unpkg.com/:name@:version/:path';
89
this.paramsRegex = /:([a-z]+)/gi;
9-
this.node_modules = join(process.cwd(), 'node_modules');
10-
11-
this.modules = this.modules
12-
.map(({ name, ...other }) => ({
13-
name,
14-
...other,
15-
version: require(join(this.node_modules, name, 'package.json')).version
16-
}))
17-
.map(({ name, version, path }) => {
18-
if (!path.includes('.js')) {
19-
throw new Error(`Unsupported extension in ${path}`);
20-
}
10+
}
2111

22-
const cdn = this.url.replace(this.paramsRegex, (_, type) => ({
23-
name,
24-
version,
25-
path
26-
})[type]);
12+
async createUrlsToModules(modules) {
13+
const currentState = await npmCheck();
2714

28-
return {
29-
name,
30-
cdn
31-
}
32-
})
33-
}
15+
const packages = currentState
16+
.get('packages')
17+
.reduce((list, { moduleName, ...others }) => list.set(moduleName, others), new Map());
18+
19+
return modules
20+
.map(({ name, ...other }) => ({
21+
name,
22+
...other,
23+
version: packages.get(name).installed
24+
}))
25+
.map(({ name, version, path }) => {
26+
if (!path.includes('.js')) {
27+
throw new Error(`Unsupported extension in ${path}`);
28+
}
29+
30+
const cdn = this.url.replace(this.paramsRegex, (_, type) => ({
31+
name,
32+
version,
33+
path
34+
})[type]);
35+
36+
return {
37+
name,
38+
cdn
39+
}
40+
});
41+
}
3442

3543
apply(compiler) {
3644
compiler.hooks.emit.tapAsync(MicroservicesWebpackPlugin.name, this.tapAsync.bind(this))
3745
compiler.options.externals = this.modules.map(({ name }) => name)
3846
}
3947

40-
tapAsync(compilation, callback) {
48+
async tapAsync(compilation, callback) {
49+
this.modules = await this.createUrlsToModules(this.modules);
50+
4151
compilation.assets = Object.keys(compilation.assets)
4252
.map((fileName) => {
4353
if (!fileName.includes('.js')) {

0 commit comments

Comments
 (0)