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

Tree shaking enabled results in a script that has ReferenceError on line 1 (precompiled purescript source) #2841

Closed
Benjmhart opened this issue Mar 24, 2019 · 4 comments · Fixed by #2875

Comments

@Benjmhart
Copy link
Contributor

🐛 bug report

running the following script

spago build && rm -rf ..//dist/* && parcel build index.html --no-source-maps --public-url ./ --out-dir ../dist

results in a script that has ReferenceError: module is undefined on line 1 of the output.

this is precompiled purescript input -> which just means it's a commonjs module (with it's own commonjs dependancies) being imported into the following script:

import Main from './output/Main';

function main() {
  const body = document.querySelector('body');
  Array.from(body.children).forEach(element => {
    body.removeChild(element);
  });
  const environment = process.env.ENVIRONMENT;
  const apiUrl = process.env.API_URL;
  Main.main(environment)(apiUrl)();
}

main();

the entry point is an html file that uses the js script above.

🎛 Configuration (.babelrc, package.json, cli command)

no babel configuration, cli command is parcel build index.html --no-source-maps --public-url ./ --out-dir ../dist

🤔 Expected Behavior

it should replicate the behavior without tree shaking, which is the code should run without errors introduced by the bundling process

😯 Current Behavior

ReferenceError: module is undefined on line 1 of the output

💁 Possible Solution

🔦 Context

this increases the bundle size of our output by 400kb +

💻 Code Sample

🌍 Your Environment

Linux, 64 bit, xubuntu 18.04

Software Version(s)
Parcel 1.11
Node 10.15.1
npm/Yarn 6.4.1
Operating System xubuntu 18.04
@mischnic
Copy link
Member

This is nearly impossible to fix without some kind of reproduction.

@Benjmhart
Copy link
Contributor Author

I'll put together a reproduction in a gist in the next day or two

@Benjmhart
Copy link
Contributor Author

I've created a repo that reproduces this issue here: https://github.com/Benjmhart/parcelBugReproduction
the scripts are saved within npm and while the main source is written in purescript, npm scripts SHOULD make it pretty approachable without anyone needing to know any purescript or anything about how dependancies are being managed.

steps to reproduce:

  1. clone the repo above
  2. npm install
  3. have parcel globally installed (i left parcel out of the dependancies list because if you are directly using source to test against this repo, you may want to swap out your version of parcel for mine.
  4. run npm run build:treeshake to compile the code with treeshaking (see package.json for the exact arguments passed to parcel
  5. run npm run serve to serve the built code. in your browser go to localhost:8000, you will see a blank page, open the console to see the error ReferenceError: module is not defined

if you want a baseline - you can follow the same directions as above but run npm run build:normal, which runs the same pipeline without tree shaking, running npm run serve and visiting the page thereafter will properly load the page contents.

@Benjmhart
Copy link
Contributor Author

wow I see a PR already! that was quick! thanks @mischnic

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

Successfully merging a pull request may close this issue.

2 participants