-
Notifications
You must be signed in to change notification settings - Fork 492
Is using react *.tsx components from an npm package something that should work ? #348
Comments
To achieve this, you will need to change the create-react-app-typescript/packages/react-scripts/config/webpack.config.dev.js Lines 173 to 176 in c27879b
Note that this change has to be done for both dev and prod config. |
@DorianGrey thanks for this, I will look into it asap and see how I go. |
@DorianGrey I tried that in both files, it made no difference, I then added back the mode to file-loader i mention in first as well and went back to error in first. I tried removing the tsconfig.json |
I was pointing at an include, not an exclude - that's a major difference.
in the |
My apologies I miss-typed, I also didn't make my context clear enough always a risk in written form. Let me try again. 1. My test process and what I am observing with unmodified react-scripts-ts v2.16.0My test npm package called In my testapp file I then run The build works without any errors and outputs files to build folder inside testapp. The TestButton.tsx file source file is output to the folder The TestButton.tsx component does not appear in the output file I check the bundle and can find function(e,t,n){e.exports=n.p+"static/media/TestButton.983518d3.tsx"} Referencing this static asset file. 2. I tried using paths.appNodeModules as follows.I now modify react-create-ts v2.16.0 in webpack.config.prod.js In the rule for I run 3. I tried using as you suggested
|
Ok - if the pattern for I'm not that familiar with how local installations affect the resolution mechanism, esp. the path it returns. |
Ok your suggestions have let me make a bit more progress, if our lib folder physically exists instead of linked under node_modules things can work with your original suggested change. I got rid of the default link behaviour of installing a local module by just copying the lib module into the node_modules folder as a test. Using default react-scripts-ts v2.16.0 I still get that the TestButton is treated as an asset. But now when I modify Now Similary Part of the reason for this asset behavior of imported typscript is the rule you pointed out, another part seems to be that the behaviour for installed local packages is to use links - on windows these links are Junctions, if the module is a link in node_modules it gets treated as an asset. For a test I linked the module to another folder under src not under node_modules and imported it from that new location under src and it also got treated as an asset so not even involving the node_module folder in that case. I have no idea if Windows link behaviour is different from linux I may have to run up a linux vm to try it, our dev platforms are all windows at the moment so even if it worked we cant just switch over either having other dependencies on windows. Still not a great answer, of having a separated code module actually have as source path for editing be inside node_modules of another package I can see causing some bad side effects down the path. I don't believe I would learned this much this quickly with out your help so far, thank you. |
Some further information. In the webpack config if I set Documentation for the It is not clear to me yet if this may have other negative side effects so will explore further. |
Got to testing by adding the external lib to the include path for // Compile .tsx?
{
test: /\.(ts|tsx)$/,
include: [ paths.appSrc, path.resolve('..', 'lib') ],
use: [
{
loader: require.resolve('ts-loader'),
// the rest omitted. No other modifications are required for build and start to work as desired. Thanks for your input, I have learned a lot. |
Is this a bug report? No.
Running npm 5.10 node 8.94.
Totally clean start with one linked modules.
I am trying this out with dev mode
npm start
Is putting react component typescript components into a npm package something that should work ?
I created a trivial local module with a single react component button TestButton.tsx it fails the solution compiles for the wrong reason.
My import
import TestButton from 'lib/components/TestButton'
from my local module lib is treated as a file-loader dependency. As a test I ejected to see if adding/\.(ts|tsx)$/,
to exclude helped and it gets past being treated as a file asset.After modify exclude to.
exclude: [/\.(ts|tsx)$/,/\.(js|jsx|mjs)$/, /\.html$/, /\.json$/],
I then get another error
The text was updated successfully, but these errors were encountered: