-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
369 additions
and
143 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
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,104 @@ | ||
var | ||
path = require('path'), | ||
fs = require('fs'), | ||
DllEntryPlugin = require('webpack/lib/DllEntryPlugin'), | ||
DllModule = require('webpack/lib/DllModule'), | ||
RawSource = require("webpack/lib/RawSource"); | ||
|
||
var pkgCache = {}; | ||
var checkPkgMain = function(dir) { | ||
if(pkgCache[dir]) { | ||
return pkgCache[dir].main; | ||
} else { | ||
try { | ||
var text = fs.readFileSync(path.join(dir, 'package.json'), {encoding:'utf8'}); | ||
pkgCache[dir] = JSON.parse(text); | ||
return pkgCache[dir].main; | ||
} catch(e) { | ||
return undefined; | ||
} | ||
} | ||
}; | ||
|
||
function normalizeModuleID(id) { | ||
var parent = path.dirname(id); | ||
var main = checkPkgMain(parent); | ||
if(main && path.resolve(id)===path.resolve(path.join(parent, main))) { | ||
id = parent; | ||
} | ||
id = id.replace(/\\/g, '/'); | ||
|
||
// Remove any leading ./node_modules prefix | ||
var nodeModulesPrefix = './node_modules/'; | ||
if(id.indexOf(nodeModulesPrefix)===0) { | ||
id = id.substring(nodeModulesPrefix.length); | ||
} | ||
if(id.indexOf('node_modules')===-1) { | ||
// Remove any js file extension | ||
if(id.indexOf('.js')===id.length-3) { | ||
id = id.substring(0, id.length-3); | ||
} | ||
// Remove any /index suffix as we want the user-accessible ID | ||
if(id.indexOf('/index')===id.length-6 && id.length>6) { | ||
id = id.substring(0, id.length-6); | ||
} | ||
} | ||
return id; | ||
} | ||
|
||
DllModule.prototype.source = function() { | ||
var header = ''; | ||
if(DllModule.entries[this.name]) { | ||
header += '__webpack_require__.load = function(loader) {\n'; | ||
header += '\tloader = loader || __webpack_require__;' | ||
for(var i=0; i<DllModule.entries[this.name].length; i++) { | ||
header += '\tloader(\'' + DllModule.entries[this.name][i] + '\');\n'; | ||
} | ||
header += '};\n'; | ||
} | ||
return new RawSource(header + 'module.exports = __webpack_require__;'); | ||
}; | ||
|
||
function EnactFrameworkPlugin(options) { | ||
this.options = options || {}; | ||
} | ||
module.exports = EnactFrameworkPlugin; | ||
EnactFrameworkPlugin.prototype.apply = function(compiler) { | ||
// Map entries to the DLLEntryPlugin | ||
DllModule.entries = {}; | ||
compiler.plugin('entry-option', function(context, entry) { | ||
function itemToPlugin(item, name) { | ||
if(Array.isArray(item)) { | ||
DllModule.entries[name] = []; | ||
for(var i=0; i<item.length; i++) { | ||
DllModule.entries[name].push(normalizeModuleID('./node_modules/' + item[i])); | ||
} | ||
return new DllEntryPlugin(context, item, name); | ||
} else { | ||
throw new Error('EnactFrameworkPlugin: supply an Array as entry'); | ||
} | ||
} | ||
if(typeof entry === 'object') { | ||
Object.keys(entry).forEach(function(name) { | ||
compiler.apply(itemToPlugin(entry[name], name)); | ||
}); | ||
} else { | ||
compiler.apply(itemToPlugin(entry, 'main')); | ||
} | ||
return true; | ||
}); | ||
|
||
// Format the internal module ID to a usable named descriptor | ||
compiler.plugin('compilation', function(compilation) { | ||
compilation.plugin('before-module-ids', function(modules) { | ||
modules.forEach(function(module) { | ||
if(module.id === null && module.libIdent) { | ||
module.id = module.libIdent({ | ||
context: this.options.context || compiler.options.context | ||
}); | ||
module.id = normalizeModuleID(module.id) | ||
} | ||
}, this); | ||
}.bind(this)); | ||
}.bind(this)); | ||
}; |
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,88 @@ | ||
var | ||
path = require('path'), | ||
ExternalsPlugin = require('webpack/lib/ExternalsPlugin'), | ||
DelegatedSourceDependency = require('webpack/lib/dependencies/DelegatedSourceDependency'), | ||
DelegatedModule = require('webpack/lib/DelegatedModule'); | ||
|
||
// Custom DelegateFactoryPlugin designed to redirect Enact framework require() calls | ||
// to the external framework | ||
function DelegatedEnactFactoryPlugin(options) { | ||
this.options = options || {}; | ||
} | ||
DelegatedEnactFactoryPlugin.prototype.apply = function(normalModuleFactory) { | ||
var name = this.options.name; | ||
var libs = this.options.libraries; | ||
normalModuleFactory.plugin('factory', function(factory) { | ||
return function(data, callback) { | ||
var request = data.dependency.request; | ||
for(var i=0; i<libs.length; i++) { | ||
if(request && request.indexOf(libs[i]) === 0) { | ||
return callback(null, new DelegatedModule(name, request, 'require', request)); | ||
} | ||
} | ||
return factory(data, callback); | ||
}; | ||
}); | ||
}; | ||
|
||
// Form a correct filepath that can be used within the build's output directory | ||
function normalizePath(dir, file, compiler) { | ||
if(path.isAbsolute(dir)) { | ||
return path.join(dir, file); | ||
} else { | ||
return path.relative(path.resolve(compiler.options.output.path), path.join(process.cwd(), dir, file)); | ||
} | ||
} | ||
|
||
// Reference plugin to handle rewiring the external Enact framework requests | ||
function EnactFrameworkRefPlugin(opts) { | ||
this.options = opts || {}; | ||
this.options.name = this.options.name || 'enact_framework'; | ||
this.options.libraries = this.options.libraries || ['@enact', 'react', 'react-dom']; | ||
this.options.external = this.options.external || {}; | ||
this.options.external.inject = this.options.external.inject || this.options.external.path; | ||
|
||
if(!process.env.ILIB_LOCALE_PATH) { | ||
process.env.ILIB_LOCALE_PATH = path.join(this.options.external.inject, 'node_module', | ||
'@enact', 'i18n', 'ilib', 'locale'); | ||
} | ||
} | ||
module.exports = EnactFrameworkRefPlugin; | ||
EnactFrameworkRefPlugin.prototype.apply = function(compiler) { | ||
var name = this.options.name; | ||
var libs = this.options.libraries; | ||
var external = this.options.external; | ||
|
||
// Declare enact_framework as an external dependency | ||
var externals = {}; | ||
externals[name] = name; | ||
compiler.apply(new ExternalsPlugin(compiler.options.output.libraryTarget || 'var', externals)); | ||
|
||
compiler.plugin('compilation', function(compilation, params) { | ||
var normalModuleFactory = params.normalModuleFactory; | ||
compilation.dependencyFactories.set(DelegatedSourceDependency, normalModuleFactory); | ||
|
||
compilation.plugin('html-webpack-plugin-alter-chunks', function(chunks, params) { | ||
// Add the framework files as a pseudo-chunk so they get injected into the HTML | ||
chunks.unshift({ | ||
names: ['enact_framework'], | ||
files: [ | ||
normalizePath(external.inject, 'enact.js', compiler), | ||
normalizePath(external.inject, 'enact.css', compiler) | ||
] | ||
}); | ||
|
||
// Store the absolute filepath to the external framework so the PrerenderPlugin can use it | ||
params.plugin.options.externalFramework = path.resolve(path.join(external.path, 'enact.js')); | ||
return chunks; | ||
}); | ||
}); | ||
|
||
// Apply the Enact factory plugin to handle the require() delagation/rerouting | ||
compiler.plugin('compile', function(params) { | ||
params.normalModuleFactory.apply(new DelegatedEnactFactoryPlugin({ | ||
name: name, | ||
libraries: libs, | ||
})); | ||
}); | ||
}; |
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
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
Oops, something went wrong.