brunch / deppack

Extract node modules to browser.
MIT License
4 stars 13 forks source link

When using TypeScript dependencies from node_modules are not bundled. #49

Open danthedaniel opened 6 years ago

danthedaniel commented 6 years ago

None of my required files make it into the bundle if they're from node_modules. I've tracked this down to the function isJs in lib/explore.js:

const isJs = (fileList, path) => {
  const file = fileList.files.get(path);
  return file && file.type === 'javascript';
};

Because my paths are being transformed (eg: js/views/not_found.tsx to js/views/not_found.js) this function does not work correctly. I've been able to fix the issue by changing it to:

const isJs = (fileList, path) => {
  return /\.(js|ts|tsx|jsx)/.test(path);
};

Oddly, this issue only emerged after I switched to a fork of typescript-brunch (specifically this) and when I try to switch back to the official package the problem persists. I've even gone as far as wiping my node_modules folder and redownloading all of my dependencies.

danthedaniel commented 6 years ago

Perhaps a better solution is to modify the compile() function in lib/fs_utils/source_file.js from brunch

From this -

  compile() {
    const path = this.path = this._realPath;
    // ...
      .then(processFile)
      .then(file => {
        this.type = this.type || file.type || '';
        file.compiled = file.data;
        return file;
      })
      // ...
  }

to this -

  compile() {
    const path = this.path = this._realPath;
    // ...
      .then(processFile)
      .then(file => {
        this.type = this.type || file.type || '';
        file.compiled = file.data;
        file.path = path; // Makes sure that the internally stored path remains unchanged
        return file;
      })
      // ...
  }
Thorn1089 commented 6 years ago

I noticed what I think is this issue when making changes to the typescript-brunch plugin myself. Weirdly, node_modules is included fine with Typescript@2.2.2 or lower, but 2.3.0 and above cause the symptoms described here, where it seems like deppack just doesn't find my imports. Not sure why this would be version-dependent.

Thorn1089 commented 6 years ago

@teaearlgraycold diving further, I think the "real" error is this:

return Object.assign({}, compiled, {
        dependencies,
        type: compiler.type,
        path: compiler.targetExtension ?
          path.replace(extRe, compiler.targetExtension) :
          path,
      });

in fs_utils/pipeline.js in Brunch. It's replacing the path based on the extension (".js") specified by the typescript-brunch plugin, which causes the issue you noted with isJs.

Thorn1089 commented 6 years ago

Aha - it's due to commit 7e99449 on typescript-brunch, which adds the target extension property. That might be why it started happening when you switched to a fork, since the fork you mention seems to be up-to-date with master. It's why my own fork also has this issue. I didn't notice that latest commit hasn't been released, which is why the version from npm install still works fine. I'm opening a corresponding issue on the typescript-brunch repo.

Thorn1089 commented 6 years ago

See also https://github.com/brunch/brunch/issues/1722 and https://github.com/brunch/typescript-brunch/issues/46

viviansteller commented 4 years ago

Any news on this? This issue still persists?

paulmillr commented 4 years ago

You can see the news in this issue. Since noone worked on it, it still persists.