-
Notifications
You must be signed in to change notification settings - Fork 73
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
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
178ade0
Add async webpack.resolve support
ThaNarie 182ce70
Add `.editorconfig` and revert to original formatting style
ThaNarie e5b16a4
Correctly handle case where webpack cannot resolve a file
ThaNarie b1f9fd7
Update `fakeModuleSystem` to fake webpack.resolve-like behavior
ThaNarie 12f0197
Create fixture files required by templates
ThaNarie 6c18c06
Add tests to verify `webpack.resovle` behavior
ThaNarie e737850
Change `includes` to something it that is supported in node 0.10
ThaNarie a3401a9
Merge remote-tracking branch 'upstream/master' into feature/resolve
ThaNarie 024853c
Add missing tokens after merge
ThaNarie fa18668
Update readme for webpack 2+
ThaNarie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# see editorconfig.org | ||
|
||
root = true | ||
|
||
[*.{js,twig}] | ||
|
||
indent_style = space | ||
indent_size = 4 | ||
|
||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,104 @@ | ||
var Twig = require("twig"); | ||
var path = require("path"); | ||
var hashGenerator = require("hasha"); | ||
var mapcache = require("./mapcache"); | ||
var async = require("async"); | ||
|
||
var compilerFactory = require("./compiler"); | ||
var getOptions = require("./getOptions"); | ||
var utils = require('./utils'); | ||
|
||
Twig.cache(false); | ||
|
||
module.exports = function(source) { | ||
var path = require.resolve(this.resource), | ||
id = hashGenerator(path), | ||
options = getOptions(this), | ||
tpl; | ||
// shared resolve map to store includes that are resolved by webpack | ||
// so they can be used in the compiled templates | ||
var resolveMap = {}; | ||
|
||
module.exports = function (source) { | ||
var loaderApi = this; | ||
var loaderAsyncCallback = this.async(); | ||
this.cacheable && this.cacheable(); | ||
|
||
// the path is saved to resolve other includes from | ||
var path = require.resolve(loaderApi.resource); | ||
|
||
// this will be the template id for this resource, | ||
// this id is also be generated in the copiler when this resource is included | ||
var id = utils.generateTemplateId(path, loaderApi.options.context); | ||
|
||
var options = getOptions(loaderApi); | ||
var tpl; | ||
|
||
Twig.extend(function(Twig) { | ||
var compiler = Twig.compiler; | ||
compiler.module['webpack'] = compilerFactory(options); | ||
}); | ||
|
||
mapcache.set(id, path) | ||
|
||
this.cacheable && this.cacheable(); | ||
|
||
tpl = Twig.twig({ | ||
id: id, | ||
path: path, | ||
data: source, | ||
allowInlineIncludes: true | ||
}); | ||
// compile function that can be called recursively to do multiple | ||
// compilation passes when doing async webpack resolving | ||
(function compile(templateData) { | ||
// store all the paths that need to be resolved | ||
var resolveQueue = []; | ||
var resolve = function (value) { | ||
if (resolveQueue.indexOf(value) === -1 && !resolveMap[value]) { | ||
resolveQueue.push(value); | ||
} | ||
}; | ||
|
||
tpl = tpl.compile({ | ||
module: 'webpack', | ||
twig: 'twig' | ||
}); | ||
Twig.extend(function (Twig) { | ||
var compiler = Twig.compiler; | ||
// pass values to the compiler, and return the compiler function | ||
compiler.module['webpack'] = compilerFactory({ | ||
loaderApi: loaderApi, | ||
resolve: resolve, | ||
resolveMap: resolveMap, | ||
path: path | ||
}); | ||
}); | ||
|
||
tpl = Twig.twig({ | ||
id: id, | ||
path: path, | ||
data: templateData, | ||
allowInlineIncludes: true | ||
}); | ||
|
||
tpl = tpl.compile({ | ||
module: 'webpack', | ||
twig: 'twig' | ||
}); | ||
|
||
// called when we are done resolving all template paths | ||
var doneResolving = function doneResolving() { | ||
// re-feed the parsed tokens into the next pass so Twig can skip the token parse step | ||
compile(Twig.twig({ ref: id }).tokens); | ||
}; | ||
|
||
// resolve all template async | ||
var resolveTemplates = function resolveTemplates() { | ||
async.each(resolveQueue, function (req, cb) { | ||
loaderApi.resolve(loaderApi.context, req, function (err, res) { | ||
if (err) { | ||
// could not be resolved by webpack, mark as false so it can be | ||
// ignored by the compiler | ||
resolveMap[req] = false; | ||
} else { | ||
resolveMap[req] = res; | ||
// also store the resolved value to be used | ||
resolveMap[res] = res; | ||
} | ||
cb(); | ||
}); | ||
}, doneResolving); | ||
}; | ||
|
||
this.callback(null, tpl); | ||
// if we have resolve items in our queue that have been added by this compilation pass, we need | ||
// to resolve them and do another compilation pass | ||
if (resolveQueue.length) { | ||
resolveTemplates(); | ||
} else { | ||
// nothing to resolve anymore, return the template source | ||
loaderAsyncCallback(null, tpl); | ||
} | ||
})(source); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
var path = require('path'); | ||
var hashGenerator = require("hasha"); | ||
|
||
// This prefix is used in the compiler to detect already resolved include paths | ||
// when dealing with multiple compilation passes where some of the resources could | ||
// already be resolved, and others might not. | ||
var HASH_PREFIX = '$resolved:'; | ||
|
||
/** | ||
* Generate a template id from a path, so the source path is not visible in the output | ||
* @param templatePath {string} A resolved path by webpack | ||
* @param context {string} The webpack context path | ||
* @return {string} | ||
*/ | ||
function generateTemplateId(templatePath, context) { | ||
// strip context (base path) to remove any 'local' filesystem values in the path | ||
// also generate a hash to hide the path | ||
// add the source filename for debugging purposes | ||
return HASH_PREFIX + hashGenerator(templatePath.replace(context, '')) + ':' + path.basename(templatePath); | ||
} | ||
|
||
module.exports = { | ||
HASH_PREFIX: HASH_PREFIX, | ||
generateTemplateId: generateTemplateId | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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”.
There was a problem hiding this comment.
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