Skip to content

Commit

Permalink
Update: Replace Object.keys with Object.entries. Update dependencies. (
Browse files Browse the repository at this point in the history
…#30)

* Update: -

* Update: -
  • Loading branch information
morriq authored Dec 18, 2019
1 parent fc02b74 commit 942ce3c
Show file tree
Hide file tree
Showing 8 changed files with 1,729 additions and 966 deletions.
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
save-exact=true
1 change: 1 addition & 0 deletions examples/umd/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
save-exact=true
1,067 changes: 650 additions & 417 deletions examples/umd/package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions examples/umd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
"license": "ISC",
"description": "",
"devDependencies": {
"webpack": "^4.30.0",
"webpack-cli": "^3.3.1"
"webpack": "4.41.3",
"webpack-cli": "3.3.10"
},
"dependencies": {
"react": "^16.8.6"
"react": "16.12.0"
}
}
3 changes: 2 additions & 1 deletion examples/umd/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ const MicroservicesWebpackPlugin = require('./../../lib/index.js');


module.exports = {
mode: 'production',
entry: resolve(__dirname, 'src', 'index.js'),
output: {
path: resolve(__dirname, 'dist'),
libraryTarget: 'amd'
},
plugins: [
plugins: [
new MicroservicesWebpackPlugin({
modules: [
{ name: 'react', path: `umd/react.production.min.js` },
Expand Down
34 changes: 17 additions & 17 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { RawSource } = require('webpack-sources');
const npmCheck = require('npm-check');
const fromEntries = require('object.fromentries');


class MicroservicesWebpackPlugin {
Expand Down Expand Up @@ -48,29 +49,28 @@ class MicroservicesWebpackPlugin {
async tapAsync(compilation, callback) {
this.modules = await this.createUrlsToModules(this.modules);

compilation.assets = Object.keys(compilation.assets)
.map((fileName) => {
if (!fileName.includes('.js')) {
return {[fileName]: compilation.assets[fileName]};
}
compilation.assets = fromEntries(Object.entries(compilation.assets)
.map(([fileName, asset]) => {
if (!fileName.includes('.js')) {
return [fileName, asset];
}

const source = compilation.assets[fileName].source();
const supportAmdOnly = 'define(';
const source = asset.source();
const supportAmdOnly = 'define(';

if (!source.startsWith(supportAmdOnly)) {
return {[fileName]: compilation.assets[fileName]};
if (!source.startsWith(supportAmdOnly)) {
return [fileName, asset];
}

const value = [
this.modules
.map(({ cdn, name }) => `define('${name}', ['${cdn}'], function(v) {return v;})`)
.join('\n'),
const value = [
this.modules
.map(({ cdn, name }) => `define('${name}', ['${cdn}'], function(v) {return v;})`)
.join('\n'),
source
].join('\n');
].join('\n');

return {[fileName]: new RawSource(value)};
})
.reduce((previous, current) => ({ ...previous, ...current }), {});
return [fileName, new RawSource(value)];
}));

callback()
}
Expand Down
Loading

0 comments on commit 942ce3c

Please sign in to comment.