Create-react-app: [Question] Build will contain JS files in src/ folder EVEN if you are not using(not importing them).

Created on 20 Oct 2018  路  2Comments  路  Source: facebook/create-react-app

Hello, thank you for this wonderful tool! I use it every day, it literaly save my life.
I just found out a weird behavior.
I have notice that _Build will contain JS files in src/ folder EVEN if you are not using(not importing them)._
Say if I have a test.js file inside src/ directory

export default {
    str: `I will be include in build even if nobody importing or using me.`
}

I don't know if this is a feature or not, because if you include unused(not imporing) JS in build like above I show you, then you can _dynamic_ require('./unused.js') your JS file, which seems a feature to everybody.

I want to ask if it is good or not, because I can't find anything in the doc(new website) that related to this .
Thank you very much!!!!

needs investigation

Most helpful comment

Ah, yes. This is a known limitation of webpack. If you use a dynamic require webpack has no other option than to bundle everything. The only way to fix this is via proper imports.

All 2 comments

Today, I have found out that how to trigger this situation.

if (true) {
  const a = require(`./${data.long.long.attribute}`);
}

if webpack can determine you are _using require_, and your require is not _statically define_ (even if that's just a long object's attribue name), then webpack will add _ALL JS in src/ directory to build_

Ah, yes. This is a known limitation of webpack. If you use a dynamic require webpack has no other option than to bundle everything. The only way to fix this is via proper imports.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Timer picture Timer  路  144Comments

gabrielmicko picture gabrielmicko  路  70Comments

ericvicenti picture ericvicenti  路  83Comments

gaearon picture gaearon  路  85Comments

benneq picture benneq  路  99Comments