-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from ThaNarie/feature/resolve
Add async webpack.resolve support
- Loading branch information
Showing
33 changed files
with
421 additions
and
92 deletions.
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.