Skip to content

Instantly share code, notes, and snippets.

@J2D2Development
Created August 10, 2017 16:38
Show Gist options
  • Save J2D2Development/6f520dd991a6a33c1152aabcdf346790 to your computer and use it in GitHub Desktop.
Save J2D2Development/6f520dd991a6a33c1152aabcdf346790 to your computer and use it in GitHub Desktop.
Replace es6 with umd bundle
'use strict';
const { readFile, writeFile } = require('fs');
const { join } = require('path');
readFile(join(__dirname, '../dist-server/main.bundle.js'), 'utf-8', (err, data) => {
if(err) { return console.log('Error reading file:', err); }
const ssrPageScrollBundle = 'ng2-page-scroll/bundles/ng2-page-scroll.umd.js'
const newCode = data.replace('ng2-page-scroll', ssrPageScrollBundle)
.replace('ng2-page-scroll/src/ng2-page-scroll.module', ssrPageScrollBundle)
.replace('ng2-page-scroll/src/ng2-page-scroll.service', ssrPageScrollBundle)
.replace('ng2-page-scroll/src/ng2-page-scroll.directive', ssrPageScrollBundle);
writeFile('./dist-server/main.bundle.js', newCode, 'utf-8', err => {
if(err) { return console.log('Error writing file:', err); }
console.log('Done editing main.bundle.js to use amd bundles for external libraries');
});
});
@MartinNuc
Copy link

Thanks for this. It worked for ng2-page-scroll but I got stucked on the next library :) I guess I will wait with SSR until such issues are resolved.

@SpeedoPasanen
Copy link

SpeedoPasanen commented Sep 3, 2017

Holee cow, this solved it for me with mobx-angular and angular-tree-component! Some RegExp can save you from the pain of finding all the module names one lib may have:

.replace(/require\(\"angular\-tree\-component[^\"]*\"/g, 'require("angular-tree-component/dist/angular-tree-component.umd.js"')

@StephanMullis
Copy link

I have the same Issue with ng2-page-scroll and SSR, Where do i add this script?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment