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

Add async webpack.resolve support #27

Merged
merged 10 commits into from
Jun 10, 2019
Merged

Conversation

ThaNarie
Copy link
Contributor

Switch the compiler to async mode, to allow webpack to resolve the
included resources. The compilation is done in multiple passes until
all paths are resolved (most of the time just 1 extra pass).

Change the compiler export to a factory function so data can be passed
from the loader to the compiler. Because of this structure the mapcache
is not needed anymore.

Create a util function to correctly generate template IDs that are the
same between the loader and the compiler (previously they could differ).
They always use the resolved webpack path as source for the ID.

Skip dependencies in the token parsing that are not just a string. They
will be handled at runtime, and are the responsibility from the template
author.

Also export the template tokens on the template function, so they can be
used to register the template under a different name at runtime. This is
useful when dealing with dynamic templates where you want to use
require.context to bundle and register all your templates in your
application bootstrap.

Updated the test helper to make empty context available.

Added a lot of comments in the code.

re #26

Switch the compiler to async mode, to allow webpack to resolve the
included resources. The compilation is done in multiple passes until
all paths are resolved (most of the time just 1 extra pass).

Change the compiler export to a factory function so data can be passed
from the loader to the compiler. Because of this structure the mapcache
is not needed anymore.

Create a util function to correctly generate template IDs that are the
same between the loader and the compiler (previously they could differ).
They always use the resolved webpack path as source for the ID.

Skip dependencies in the token parsing that are not just a string. They
will be handled at runtime, and are the responsibility from the template
author.

Also export the template tokens on the template function, so they can be
used to register the template under a different name at runtime. This is
useful when dealing with dynamic templates where you want to use
`require.context` to bundle and register all your templates in your
application bootstrap.

Updated the test helper to make empty context available.

Added a lot of comments in the code.

re zimmo-be#26
@webberig
Copy link
Member

webberig commented Mar 12, 2017 via email

@ThaNarie
Copy link
Contributor Author

ThaNarie commented Mar 12, 2017

  • failed tests: Sure! Forgot to look at the Travis result (they passed locally on node 6 :P), I see we still need to support 0.10 :) (without starting a discussion, isn't this something we should drop, as 0.12 was already end-of-life at 2016-12-31)

  • extra tests I will give it a try, I hope the current 'mock' setup can handle the new webpack resolve features.

  • indenting Ah sure, I'll also add a .editorconfig so the next person won't make the same mistake by accident. Also, the original indenting for the /lib files have 4 spaces, but the tests have 2 spaces, I've now chosen 4...

Mark a failed resolve as false, so the compiler can ignore that
resource and leave it in the template as is, so it can be picked up by
Twig at runtime.
When not including `.twig` as extension, it will be added automatically.
This is to create a scenario where a file can be resolve by webpack, and
two different paths lead to the same resource.

Also add a check on disk to allow cases where a template include value
only exists at runtime as registered template.
When adding the behavior where a template must exists on disk to have
it processed by the loader, the files do need to exist in disk.

Also changed the include html to work with a resolve case where the
'.twig' extension is added by the webpack resolve function.
These new tests will all fail when run on the old code.
@ThaNarie
Copy link
Contributor Author

@webberig All your remarks have been addressed. The test setup is not ideal, but it simulates some of the webpack resolve features that show how the old/current version is breaking, and where this PR passes.

I have tested this version in my own project, but that is very limited (basically only a for loop with dynamic includes and some other includes, no blocks or embeds or template extends). If you have some projects where you use this library, I think it's wise to install this PR version (my branch) to see if those other cases behave the same as well.

The package.json version is left the same, not sure how you want to handle the release :)

README.md Outdated
@@ -26,6 +28,26 @@ module.exports = {
};
```

### Webpack 2
Copy link

@JohnAlbin JohnAlbin May 27, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Webpack 2 and later is more likely, how about this goes before the "Webpack 1" docs? Also, you'll need to say "and later" since many are using version 3 or 4.

And, yeah, I know you wrote this PR 2 years ago. :-p

@@ -1,62 +1,102 @@
var path = require("path");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR lists several files, including this one, as having “conflicts that must be resolved”.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @JohnAlbin,

I'm happy to make the required changes, but it would be good to know if this PR is going to be used at all in any way, since the original author seems afk after I made the requested changes :)

Let me know! Cheers

Copy link
Member

@webberig webberig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late reply. I've taken another look at the changes and will release this under version 0.5.0

@webberig webberig merged commit 076d990 into zimmo-be:master Jun 10, 2019
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

Successfully merging this pull request may close these issues.

None yet

3 participants