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

Include non ts file (like json) #548

Open
StErMi opened this issue May 31, 2020 · 7 comments
Open

Include non ts file (like json) #548

StErMi opened this issue May 31, 2020 · 7 comments

Comments

@StErMi
Copy link

StErMi commented May 31, 2020

Hi all,

the underlying framework that I'm using assumes that I have some JSON and TS file in a specific path.

Would it be possible to force include some files or some files in a specific platform in the final build?

@gomes42
Copy link

gomes42 commented Jun 10, 2020

I don't think it would be a good practice to include files that in some types of applications, need to be read at different times. For example, an application that reads a particular file, then makes an edit and saves it again. It would be confusing to specify files that should or should not be included in the final .js file, unless ncc wants to stop being a simple CLI in the future as it says.

@StErMi
Copy link
Author

StErMi commented Jun 11, 2020

In my example, those JSON files are config files and translations files. At the moment I don't have a solution in mind because I cannot modify the framework and I'm not allowed by NCC to include them in the build process. Do you have any suggestions? Those files would be read-only.

@ofhouse
Copy link
Member

ofhouse commented Jun 15, 2020

When the files are referenced somewhere in your code, e.g.

fs.readFileSync(__dirname + 'path/to/your.json')

The underlying webpack-asset-relocator-loader should be able to find it & add it into the bundle.

@ofhouse
Copy link
Member

ofhouse commented Jun 15, 2020

If want to read a specific path, that should not be modified by ncc, you can use __non_webpack_require__:

const translations = __non_webpack_require__('static/path/not/altered/by/ncc/translations.json')

But keep in mind that static/path/not/altered/by/ncc/translations.json is not included in the final bundle (Has to be added manually to the bundle) because webpack/ncc does not resolve the content of this require.

@StErMi
Copy link
Author

StErMi commented Jun 18, 2020

Hi @ofhouse the problem is that those files are not directly required in that way by the framework. They're going to load all those files automatically from a specific path. So there's no way to bundle them if the framework does not change how it works?

@ofhouse
Copy link
Member

ofhouse commented Jun 19, 2020

@StErMi Is the framework public available so that you can post a link?
It's hard to guess what's going wrong in your case without an example.

ncc covers a lot of cases but after all it's all based on static code analyzing so there is always a chance that something is missed by the algorithm.
To address this issue it would be necessary to share at least the part of the code where the framework loads the missing files.

Then we can find out whether it is something that could be covered by static code analyzing from ncc or if changes to the framework are needed.

@StephenHodgson
Copy link

StephenHodgson commented Aug 12, 2024

When the files are referenced somewhere in your code, e.g.

fs.readFileSync(__dirname + 'path/to/your.json')

The underlying webpack-asset-relocator-loader should be able to find it & add it into the bundle.

Hate to dig up an old topic, but I also ran into this recently when upgrading one of my projects from JavaScript to TypeScript.

The JavaScript version had copied some .sh and .ps1 scripts into the bundle, but the TypeScript version no longer does.
In the final index.js bundle I can also see that the required calls had also been replaced with just a simple path.join which lead me to suspect that the asset relocator somehow lost the reference.

image

[Edit]
I was able to fix this.
I realized that I didn't need to compile separately then run the ncc build on that. Instead run ncc build directly on index.ts.

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

4 participants