-
-
Notifications
You must be signed in to change notification settings - Fork 40
dynamic import issue with webpack #989
Comments
Hi @Serge-SDL, As far as I know, the ES imports are not fully supported in the NativeScript runtimes and in order to get this working with the Bundle (Webpack) workflow, you need a common-js In order to do that, you could use the __non_webpack_require__. In this way, Webpack will put a common-js In other words, I suggest you try the following code: const file =knownFolders.documents().getFile('test.js');
const jsCode = `
module.exports = { test: function(str) { return 'x' + str; } }
`;
file.writeTextSync(jsCode);
const dynamicFile = __non_webpack_require__(file.path);
console.log(dynamicFile.test("---- This is working ----")); I suppose that in the Legacy workflow you've tested this in a TypeScript app where the TypeScript compiler ( ** UPDATE ** Another possible workaround without using Webpack features is the const file =knownFolders.documents().getFile('test.js');
const jsCode = `
module.exports = { test: function(str) { return 'x' + str; } }
`;
file.writeTextSync(jsCode);
const dynamicFile = global.require(file.path);
console.log(dynamicFile.test("---- This is working ----")); |
Hi @DimitarTachev, if I import a simple module everything woks perfectly, but if there are some require() in the code I import this doesn't work anymode :'( for example something like:
to fix this I'am looking for a way to pass dependecies to require, as in SystemJS: to you have any idea?? |
@Serge-SDL, the When the CLI prepares your app, it starts a Webpack process generating a few bundled files based on the specified entry points and split chunks (e.g. For this reason, even if you find a way to tell your dynamic files to search for The easiest way of sharing entries["tns_modules/nativescript-angular/router"] = "nativescript-angular/router"; In this way, the Webpack compilation will produce an additional file named
The above-mentioned approach has one important limitation, you have to know and list as entry points all of your dynamically required modules. If this approach is not applicable for you and you need all |
Angular 8 now uses dynamic imports for lazy-loaded routes. Ex: old
new
Does this issue prevent one from using this syntax? I have to admit I have not tried it on NS v6.0 yet. I tried on v5.4 and it didn't work. |
IIRC, you have to change your tsconfig.tns.json and set |
Hi @rynop, @edusperoni. This latest comments in this issue are about using the ES dynamic imports in plain JavaScript files that are directly executed Runtime. The dynamic If you have any issues with the Angular 8 lazy routes with dynamic imports, please log them in the nativescript-angular repository. |
@DimitarTachev a huge thank you for all your informations! that was wery usefull and I can't see how I could find this without you! some further details:
NS 6.0: I got an issue I can't understand when I perform
what is this module?? "./.... ? Why there is no double quote at the end ?? |
I tried to restart from scratch to avoir app complexity:
-> got the same error as in my real project when I run tns run android: test source can be downloaded here: -> look like it's a nativescript 6 bug (same code work fine on ns5.4) thanks! |
I've never seen such The attached application is running as expected on my side but its loading an empty We will need the P.S. I suppose I'm missing something but why do you need the |
@DimitarTachev with the debug tool (tns debug android), I found that the real error is:
and is comming from this code in nativescript-angular/router.js: about the |
After providing the missing js file, everything is still working as expected. Reviewing your logs once again, I suppose that we could have an issue with the slashes in the path. Maybe you are running this on Windows? |
@DimitarTachev do you have any idea to fix this? Maybe a webpack plugin that can perform a replace with regex? Do you know where I can find the code that generate this code? I think it's in the new version of nativescript-dev-webpack? All the team is on windows... no possibility to change that :( |
Hi @Serge-SDL, Thanks for the additional details! I've opened and merges a pull request with the Windows fix. It's already available in |
Hi @DimitarTachev, again a big thank you for your answers! Everything works well! Did you manage to fix that on your projects? or do you have an idea to manage this problem? thanks! |
I'm glad I was able to help you. As far as I see, the I suppose that you will have to compile this file with the Angular AOT compiler in order to dynamically use it in Angular apps with AOT compilation. If you have any problems with that, please open a new issue as it's not related to the dynamic imports with Webpack anymore. |
Any update about this? I'm trying use dynamic imports on vue router... |
Hi,
I need to use dynamic import in my project to perform semthing like this (load a module js file stored in app documents):
this code work fine with useLegacyWorkflow: true, but failed with useLegacyWorkflow: false
-> I got the error: "Not supported" on android
-> I got the error: "Could not resolve the module specifier" on IOS
The webpack generate code looks ok:
how can I fix this? I dont understand the error (what is not supported ?)
thanks!
The text was updated successfully, but these errors were encountered: