alloy-loader
Advanced tools
Comparing version 0.2.6 to 0.2.7
@@ -7,9 +7,7 @@ /* eslint-disable no-unreachable */ | ||
const modelLoader = require('./loaders/modelLoader'); | ||
const { | ||
entryRegex, | ||
internalsRegex, | ||
componentRegex, | ||
modelRegex | ||
} = require('./utils'); | ||
const entryRegex = /app[/\\]alloy\.(j|t)s$/; | ||
const internalsRegex = /node_modules[/\\]alloy[/\\]/; | ||
const componentRegex = /(?:[/\\]widgets[/\\][^/\\]+)?[/\\](?:controllers|views)[/\\](.*)/; | ||
const modelRegex = /(?:[/\\]widgets[/\\][^/\\]+)?[/\\]models[/\\](.*)/; | ||
const appControllerRequestPattern = '\'/alloy/controllers/\' \\+ '; | ||
@@ -16,0 +14,0 @@ const widgetControllerRequestPattern = '\'/alloy/widgets/\'.*?\'/controllers/\' \\+ '; |
@@ -1,5 +0,3 @@ | ||
const asyncLib = require('neo-async'); | ||
const path = require('path'); | ||
const ContextElementDependency = require('webpack/lib/dependencies/ContextElementDependency'); | ||
const { mergeI18n } = require('./utils'); | ||
const I18nPlugin = require('./plugins/I18nPlugin'); | ||
const WidgetsPlugin = require('./plugins/WidgetsPlugin'); | ||
@@ -20,2 +18,5 @@ /** | ||
this.alloyCompiler = options.compiler; | ||
this.i18nPlugin = new I18nPlugin(options); | ||
this.widgetsPlugin = new WidgetsPlugin(options); | ||
} | ||
@@ -39,208 +40,7 @@ | ||
this.applyWidgetsHandling(compiler); | ||
this.applyI18nHandling(compiler); | ||
this.i18nPlugin.apply(compiler); | ||
this.widgetsPlugin.apply(compiler); | ||
} | ||
applyWidgetsHandling(compiler) { | ||
const widgets = this.alloyCompiler.compilationMeta.widgets; | ||
if (widgets.size === 0) { | ||
return; | ||
} | ||
compiler.hooks.contextModuleFactory.tap('AlloyLoaderPlugin', cmf => { | ||
cmf.hooks.afterResolve.tap('AlloyLoaderPlugin', result => { | ||
if (!/alloy[/\\]widgets/.test(result.request)) { | ||
return; | ||
} | ||
result.resolveDependencies = (fs, options, callback) => { | ||
let resource = options.resource; | ||
let resourceQuery = options.resourceQuery; | ||
let regExp = options.regExp; | ||
let include = /(controllers|styles)[/\\]/; | ||
const addDirectory = (directory, callback) => { | ||
fs.readdir(directory, (err, files) => { | ||
if (err) { | ||
return callback(err); | ||
} | ||
files = cmf.hooks.contextModuleFiles.call(files); | ||
if (!files || files.length === 0) { | ||
return callback(null, []); | ||
} | ||
asyncLib.map( | ||
files.filter(p => p.indexOf('.') !== 0), | ||
(entry, callback) => { | ||
const subResource = path.join(directory, entry); | ||
if (directory.endsWith('widgets') && !widgets.has(subResource)) { | ||
return callback(); | ||
} | ||
fs.stat(subResource, (err, stat) => { | ||
if (err) { | ||
if (err.code === 'ENOENT') { | ||
// ENOENT is ok here because the file may have been deleted between | ||
// the readdir and stat calls. | ||
return callback(); | ||
} else { | ||
return callback(err); | ||
} | ||
} | ||
if (stat.isDirectory()) { | ||
addDirectory.call(null, subResource, callback); | ||
} else if (stat.isFile() && subResource.match(include)) { | ||
let request; | ||
if (/node_modules/.test(subResource)) { | ||
request = subResource.substr(subResource.lastIndexOf('node_modules') + 13); | ||
} else { | ||
request = `.${subResource.substr(resource.length).replace(/\\/g, '/')}`; | ||
} | ||
const obj = { | ||
context: resource, | ||
request | ||
}; | ||
if (/node_modules/.test(subResource)) { | ||
obj.request = subResource.substr(subResource.lastIndexOf('node_modules') + 13); | ||
obj.isNpmWidget = true; | ||
} | ||
cmf.hooks.alternatives.callAsync( | ||
[ obj ], | ||
(err, alternatives) => { | ||
if (err) { | ||
return callback(err); | ||
} | ||
alternatives = alternatives | ||
.filter(obj => (obj.isNpmWidget ? true : regExp.test(obj.request))) | ||
.map(obj => { | ||
const userRequest = obj.isNpmWidget | ||
? obj.request.replace(/^alloy-widget-/, './') | ||
: obj.request; | ||
const dep = new ContextElementDependency( | ||
obj.request + resourceQuery, | ||
userRequest | ||
); | ||
dep.optional = true; | ||
return dep; | ||
}); | ||
callback(null, alternatives); | ||
} | ||
); | ||
} else { | ||
callback(); | ||
} | ||
}); | ||
}, | ||
(err, result) => { | ||
if (err) { | ||
return callback(err); | ||
} | ||
if (!result) { | ||
return callback(null, []); | ||
} | ||
const dependencies = []; | ||
for (const item of result) { | ||
if (item) { | ||
dependencies.push(...item); | ||
} | ||
} | ||
callback(null, dependencies); | ||
} | ||
); | ||
}); | ||
}; | ||
const tasks = []; | ||
tasks.push(done => addDirectory(resource, done)); | ||
widgets.forEach((widget, widgetPath) => { | ||
if (/node_modules/.test(widgetPath)) { | ||
tasks.push(done => addDirectory(widgetPath, done)); | ||
} | ||
}); | ||
asyncLib.series(tasks, (err, result) => { | ||
if (err) { | ||
return callback(err); | ||
} | ||
const dependencies = []; | ||
for (const item of result) { | ||
if (item) { | ||
dependencies.push(...item); | ||
} | ||
} | ||
callback(null, dependencies); | ||
}); | ||
}; | ||
}); | ||
}); | ||
} | ||
/** | ||
* Initializes the processing of i18n files. | ||
* | ||
* Collects all available `i18n` directories from app, theme and widgets and | ||
* merges the containing xml files. After emitting the merged files the | ||
* `i18n` directories will be watched for changes to run the merge again. | ||
* | ||
* @param {Object} compiler Webpack compiler instance | ||
*/ | ||
applyI18nHandling(compiler) { | ||
let i18nSources = []; | ||
let fileDependencies = []; | ||
const { config } = this.alloyCompiler; | ||
i18nSources.push({ | ||
path: path.posix.join(config.dir.home, 'i18n') | ||
}); | ||
if (config.theme) { | ||
i18nSources.push({ | ||
path: path.posix.join(config.dir.home, 'themes', config.theme, 'i18n'), | ||
override: true | ||
}); | ||
} | ||
this.alloyCompiler.compilationMeta.widgets.forEach(w => { | ||
const widgetI18nFolder = path.posix.join(w.dir, 'i18n'); | ||
i18nSources.push({ path: widgetI18nFolder }); | ||
}); | ||
compiler.hooks.emit.tap('AlloyLoaderPlugin', compilation => { | ||
const { files, content, errors, warnings } = mergeI18n(i18nSources); | ||
fileDependencies = files; | ||
if (errors.length > 0) { | ||
compilation.errors.push(...errors); | ||
return; | ||
} | ||
if (warnings.length > 0) { | ||
compilation.warnings.push(...warnings); | ||
} | ||
content.forEach((xmlContent, identifier) => { | ||
const targetPath = path.join('..', 'i18n', identifier); | ||
compilation.assets[targetPath] = { | ||
size() { | ||
return xmlContent.length; | ||
}, | ||
source() { | ||
return xmlContent; | ||
} | ||
}; | ||
}); | ||
}); | ||
compiler.hooks.afterEmit.tap('AlloyLoaderPlugin', compilation => { | ||
const addDependencies = (target, deps) => { | ||
if ('addAll' in target) { | ||
target.addAll(deps); | ||
} else { | ||
for (const dep of deps) { | ||
target.add(dep); | ||
} | ||
} | ||
}; | ||
// watch directories for newly added files | ||
addDependencies(compilation.contextDependencies, i18nSources.map(s => s.path)); | ||
// watch existing files for changes | ||
addDependencies(compilation.fileDependencies, fileDependencies); | ||
}); | ||
} | ||
} | ||
module.exports = AlloyLoaderPlugin; |
{ | ||
"name": "alloy-loader", | ||
"version": "0.2.6", | ||
"version": "0.2.7", | ||
"description": "Webpack loader for Alloy components", | ||
@@ -30,3 +30,4 @@ "main": "lib/index.js", | ||
"neo-async": "^2.6.1", | ||
"source-map": "^0.7.3" | ||
"source-map": "^0.7.3", | ||
"webpack-sources": "^1.4.3" | ||
}, | ||
@@ -37,3 +38,6 @@ "devDependencies": { | ||
"webpack": "^4.41.5" | ||
} | ||
}, | ||
"files": [ | ||
"lib" | ||
] | ||
} |
@@ -24,2 +24,2 @@ # alloy-loader | ||
Make sure to prune to production dependencies when you link this package, for example to test it in `appcd-plugin-webpack`. The plugin contained in this loader will create instances of [`ContextElementDependency`](https://github.com/webpack/webpack/blob/master/lib/dependencies/ContextElementDependency.js) and adds them to the Webpack compilation. It is important that these are required from the same Webpack module that actually runs the compilation, or else you'll see cryptic errors about callbacks that were called twice. | ||
Make sure to prune to production dependencies when you link this package, for example to test it in [`@titanium-sdk/webpack-plugin-alloy`](https://github.com/appcelerator/webpack-plugin-alloy). The plugin contained in this loader will create instances of [`ContextElementDependency`](https://github.com/webpack/webpack/blob/master/lib/dependencies/ContextElementDependency.js) and adds them to the Webpack compilation. It is important that these are required from the same Webpack module that actually runs the compilation, or else you'll see cryptic errors about callbacks that were called twice. |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
532
31306
7
10
1
+ Addedwebpack-sources@^1.4.3
+ Addedsource-list-map@2.0.1(transitive)
+ Addedwebpack-sources@1.4.3(transitive)