Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minify script in production builds #5

Open
muuvmuuv opened this issue Jun 19, 2020 · 3 comments
Open

Minify script in production builds #5

muuvmuuv opened this issue Jun 19, 2020 · 3 comments

Comments

@muuvmuuv
Copy link

Would it be possible to minify the script in production environments? I think the script just needs to be injected in another way so gatsby can handle the script itself.

<script id="simple-analytics-loader" type="text/javascript" data-loaded="false">!(function(s, i, m, p, l, e) {
    const parent = document.querySelector('#simple-analytics-loader');
    if (!parent) return;
    const dnt = s.doNotTrack || m.doNotTrack || m.msDoNotTrack;
    if (/yes|1/.test(dnt) && parent) {
      parent.setAttribute('data-enabled', false);
      return console.warn('Simple Analytics: Not loading script when doNotTrack is enabled');
    }
    l = i.createElement(p);
    l.addEventListener('load', function() {
      if (parent) {
      parent.setAttribute('data-loaded', true);
        let event;
        if (typeof Event === 'function') {
          event = new Event('script-loaded');
        } else {
          event = document.createEvent('Event');
          event.initEvent('script-loaded', true, true);
        }
        parent.dispatchEvent(event);
      }
    }, false)
    l.async = "true";
    l.src="https://scripts.simpleanalyticscdn.com/latest.js";
    l.type="text/javascript";
    l.setAttribute('id', 'simple-analytics');
    
    e = i.getElementsByTagName(p)[0];
    e.parentNode.insertBefore(l, e);
  })(
    window,
    document,
    navigator,
    'script'
  )</script></html>
@adriaandotcom
Copy link
Collaborator

That would be great, do you know how we could do this in Gatsby?

@muuvmuuv
Copy link
Author

No, sorry. I have asked the Gatsby community (in Discord) but got no response yet. If there is no way I think using terser to minify it first would do the job as well. Since plugins are mostly at build time this would just increase the building time (I don't think significantly).

@muuvmuuv
Copy link
Author

muuvmuuv commented Mar 6, 2021

Coming back to it; I think the only way is to use terser before "loadScript" and minify the script. I would say this is OK, since the script isn't that big it wouldn't increase the build time significantly. Another approach would be to have this script in a script-tmpl.js which gets minified and outputted into src as load-script.js at release time. Then the script would be already minified in the npm release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants