From 2440a0b919a384f39bb25a316bbffc3e7625b1b5 Mon Sep 17 00:00:00 2001 From: Rob Eisenberg Date: Wed, 22 Jun 2016 21:10:22 -0400 Subject: [PATCH] chore(all): prepare release 1.0.0-beta.3.0.0 --- dist/build-resources.js | 548 +++++++++++++++++++++++ dist/index.js | 37 +- dist/resolve-template.js | 912 --------------------------------------- package.json | 2 +- 4 files changed, 582 insertions(+), 917 deletions(-) create mode 100644 dist/build-resources.js delete mode 100644 dist/resolve-template.js diff --git a/dist/build-resources.js b/dist/build-resources.js new file mode 100644 index 0000000..61d1677 --- /dev/null +++ b/dist/build-resources.js @@ -0,0 +1,548 @@ +'use strict'; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.processAll = undefined; + +var _getOwnPropertyNames = require('babel-runtime/core-js/object/get-own-property-names'); + +var _getOwnPropertyNames2 = _interopRequireDefault(_getOwnPropertyNames); + +var _getIterator2 = require('babel-runtime/core-js/get-iterator'); + +var _getIterator3 = _interopRequireDefault(_getIterator2); + +var _regenerator = require('babel-runtime/regenerator'); + +var _regenerator2 = _interopRequireDefault(_regenerator); + +var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator'); + +var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2); + +var _assign = require('babel-runtime/core-js/object/assign'); + +var _assign2 = _interopRequireDefault(_assign); + +var processAll = exports.processAll = function () { + var ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(options) { + var dependencies, nodeModules, packageJson; + return _regenerator2.default.wrap(function _callee$(_context) { + while (1) { + switch (_context.prev = _context.next) { + case 0: + modulesProcessed = []; + optionsGlobal = options; + dependencies = {}; + nodeModules = path.join(options.root, 'node_modules'); + packageJson = path.join(options.root, 'package.json'); + + + debugDetail('starting resolution: ' + options.root); + + if (!(modulePaths.length === 0)) { + _context.next = 11; + break; + } + + _context.next = 9; + return installedLocalModulePaths(); + + case 9: + modulePaths = _context.sent; + + moduleNames = modulePaths.map(function (line) { + var split = line.split('/node_modules/'); + return split[split.length - 1]; + }); + + case 11: + + debugDetail(moduleNames); + debugDetail(modulePaths); + + try { + baseVendorPkg = JSON.parse(fileSystem.readFileSync(packageJson, 'utf8')); + moduleRootOverride = baseVendorPkg && baseVendorPkg.aurelia && baseVendorPkg.aurelia.build && baseVendorPkg.aurelia.build.moduleRootOverride || {}; + } catch (_) {} + + getResourcesOfPackage(dependencies, options.root, path.relative(options.root, options.src)); + _context.next = 17; + return autoresolveTemplates(dependencies, options.root, options.src); + + case 17: + return _context.abrupt('return', dependencies); + + case 18: + case 'end': + return _context.stop(); + } + } + }, _callee, this); + })); + return function processAll(_x) { + return ref.apply(this, arguments); + }; +}(); + +var autoresolveTemplates = function () { + var ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee2(resources, packagePath, srcPath) { + var templates, srcRelativeToRoot, _iterator5, _isArray5, _i5, _ref5, htmlFilePath, templateResources, _iterator6, _isArray6, _i6, _ref6, resource; + + return _regenerator2.default.wrap(function _callee2$(_context2) { + while (1) { + switch (_context2.prev = _context2.next) { + case 0: + _context2.next = 2; + return getFilesRecursively(srcPath, '.html'); + + case 2: + templates = _context2.sent; + srcRelativeToRoot = path.relative(packagePath, srcPath); + _iterator5 = templates, _isArray5 = Array.isArray(_iterator5), _i5 = 0, _iterator5 = _isArray5 ? _iterator5 : (0, _getIterator3.default)(_iterator5); + + case 5: + if (!_isArray5) { + _context2.next = 11; + break; + } + + if (!(_i5 >= _iterator5.length)) { + _context2.next = 8; + break; + } + + return _context2.abrupt('break', 34); + + case 8: + _ref5 = _iterator5[_i5++]; + _context2.next = 15; + break; + + case 11: + _i5 = _iterator5.next(); + + if (!_i5.done) { + _context2.next = 14; + break; + } + + return _context2.abrupt('break', 34); + + case 14: + _ref5 = _i5.value; + + case 15: + htmlFilePath = _ref5; + templateResources = resolveTemplateResources(htmlFilePath, srcPath); + _iterator6 = templateResources, _isArray6 = Array.isArray(_iterator6), _i6 = 0, _iterator6 = _isArray6 ? _iterator6 : (0, _getIterator3.default)(_iterator6); + + case 18: + if (!_isArray6) { + _context2.next = 24; + break; + } + + if (!(_i6 >= _iterator6.length)) { + _context2.next = 21; + break; + } + + return _context2.abrupt('break', 32); + + case 21: + _ref6 = _iterator6[_i6++]; + _context2.next = 28; + break; + + case 24: + _i6 = _iterator6.next(); + + if (!_i6.done) { + _context2.next = 27; + break; + } + + return _context2.abrupt('break', 32); + + case 27: + _ref6 = _i6.value; + + case 28: + resource = _ref6; + + processFromPath(resources, resource.path, resource, packagePath, srcRelativeToRoot); + + case 30: + _context2.next = 18; + break; + + case 32: + _context2.next = 5; + break; + + case 34: + case 'end': + return _context2.stop(); + } + } + }, _callee2, this); + })); + return function autoresolveTemplates(_x7, _x8, _x9) { + return ref.apply(this, arguments); + }; +}(); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +var path = require('upath'); +var fileSystem = require('fs'); +var readdir = require('recursive-readdir'); +var assign = _assign2.default || require('object.assign'); +var Promise = require('bluebird'); +var cheerio = require('cheerio'); +var execa = require('execa'); +var debug = require('debug')('webpack-plugin'); +var debugDetail = require('debug')('webpack-plugin/details'); + +var modulesProcessed = []; + +var optionsGlobal = {}; +var baseVendorPkg = void 0; +var moduleRootOverride = {}; +var modulePaths = []; +var moduleNames = []; + +function installedLocalModulePaths() { + return execa('npm', ['ls', '--parseable'], { cwd: optionsGlobal.root }).then(function (res) { + return res.stdout.split('\n').filter(function (line, i) { + return i !== 0; + }); + }).catch(function (res) { + return res.stdout.split('\n').filter(function (line, i) { + return i !== 0; + }); + }); +} + +function getFilesRecursively(targetDir, extension) { + return new Promise(function (resolve, reject) { + return readdir(targetDir, [function (file, stats) { + return path.extname(file) !== extension && !stats.isDirectory(); + }], function (error, files) { + return error ? reject(error) : resolve(files); + }); + }); +} + +function ensurePathHasExtension(fullPath) { + var stats = void 0; + var fullPathTest = fullPath; + + debugDetail('testing file for existence: ' + fullPath); + + try { + stats = fileSystem.statSync(fullPathTest); + } catch (_) {} + + if (!stats) try { + stats = fileSystem.statSync(fullPathTest = fullPath + '.js'); + } catch (_) {} + + if (!stats) try { + stats = fileSystem.statSync(fullPathTest = fullPath + '.ts'); + } catch (_) {} + + if (stats && stats.isFile()) { + return fullPathTest; + } + return null; +} + +function getPackageJson(packagePath) { + var packageJson = null; + if (!packageJson) { + try { + packageJson = JSON.parse(fileSystem.readFileSync(path.join(packagePath, 'package.json'), 'utf8')); + } catch (_) {} + } + return packageJson; +} + +function getPackageAureliaResources(packageJson) { + return packageJson.aurelia && packageJson.aurelia.build && packageJson.aurelia.build.resources || []; +} + +function getPackageMainDir(packagePath) { + var packageJson = getPackageJson(packagePath); + var packageMain = packageJson.main || packageJson.browser; + return packageMain ? path.dirname(path.join(packagePath, packageMain)) : null; +} + +function pathIsLocal(pathToCheck) { + return pathToCheck.indexOf('.') === 0; +} + +function getRealPathUniversal(fromPath, packagePath, relativeToDir) { + var realPath = getRealPath(fromPath, packagePath, relativeToDir); + if (!realPath && !pathIsLocal(fromPath)) realPath = getRealModulePath(fromPath); + return realPath; +} + +function getRealModulePath(fromPath) { + var fullPath = void 0; + var fromPathSplit = fromPath.split('/'); + var moduleName = fromPathSplit.shift(); + var modulePathIndex = moduleNames.indexOf(moduleName); + var modulePath = void 0; + if (modulePathIndex !== -1) { + modulePath = modulePaths[modulePathIndex]; + if (fromPathSplit.length === 0) { + return { path: fromPath, source: moduleName, moduleName: moduleName, modulePath: modulePath }; + } else { + fromPath = fromPathSplit.join('/'); + if (moduleRootOverride[moduleName]) { + fullPath = path.join(modulePath, moduleRootOverride[moduleName], fromPath); + } else { + fullPath = path.join(modulePath, fromPath); + } + fullPath = ensurePathHasExtension(fullPath); + if (!fullPath) { + var fullMainRelativeRootDir = getPackageMainDir(modulePath); + if (fullMainRelativeRootDir) { + fullPath = path.join(fullMainRelativeRootDir, fromPath); + fullPath = ensurePathHasExtension(fullPath); + } + } + } + } + return fullPath && modulePath ? { path: fromPath, source: fullPath, moduleName: moduleName, modulePath: modulePath } : undefined; +} + +function getRealPath(fromPath, packagePath, relativeToDir) { + var fullPath = path.join(relativeToDir ? path.joinSafe(packagePath, relativeToDir) : packagePath, fromPath); + var pathWithExt = ensurePathHasExtension(fullPath); + return pathWithExt ? { path: fromPath, source: pathWithExt } : undefined; +} + +function extractBundleResourceData(resource) { + var out = {}; + if (resource.hasOwnProperty('bundle')) { + out.bundle = resource.bundle; + } + if (resource.hasOwnProperty('lazy')) { + out.lazy = resource.lazy; + } + return out; +} + +function processFromPath(resources, fromPath, resource, packagePath, relativeToDir, overrideBlock) { + if (resources[fromPath]) return; + + var realPath = getRealPathUniversal(fromPath, packagePath, relativeToDir); + var initialRealPath = realPath; + + if (realPath) { + debug('<' + path.basename(packagePath) + '> ' + fromPath + ' => ' + path.relative(packagePath, realPath.source)); + resources[fromPath] = (0, _assign2.default)({}, resource, realPath, overrideBlock || {}); + + var localSrcPath = realPath.modulePath || path.join(packagePath, relativeToDir); + var localRelativeToDir = relativeToDir; + + if (realPath.modulePath) { + getResourcesOfPackage(resources, realPath.modulePath, undefined, overrideBlock || extractBundleResourceData(resource), realPath.moduleName); + + if (moduleRootOverride[realPath.moduleName]) { + localRelativeToDir = moduleRootOverride[realPath.moduleName]; + localSrcPath = path.join(realPath.modulePath, localRelativeToDir); + } + } + + if (path.changeExt(realPath.source, 'html') !== realPath.source) { + var fromPathHtml = path.addExt(fromPath, 'html'); + if (!resources[fromPathHtml]) { + realPath = getRealPathUniversal(fromPathHtml, packagePath, relativeToDir); + if (realPath) { + debug('<' + path.basename(packagePath) + '> ' + realPath.path + ' => ' + path.relative(packagePath, realPath.source)); + resources[fromPathHtml] = (0, _assign2.default)({}, resource, realPath, overrideBlock || {}); + } + } + } + + if (realPath) { + var htmlResources = resolveTemplateResources(realPath.source, localSrcPath, realPath.moduleName); + for (var _iterator = htmlResources, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) { + var _ref; + + if (_isArray) { + if (_i >= _iterator.length) break; + _ref = _iterator[_i++]; + } else { + _i = _iterator.next(); + if (_i.done) break; + _ref = _i.value; + } + + var htmlResource = _ref; + + processFromPath(resources, htmlResource.path, htmlResource, packagePath, localRelativeToDir, overrideBlock || extractBundleResourceData(htmlResource)); + } + } + + var fromPathJs = path.addExt(fromPath, 'js'); + realPath = getRealPathUniversal(fromPathJs, packagePath, relativeToDir); + if (realPath) { + debug('<' + path.basename(packagePath) + '> ' + realPath.path + ' => ' + path.relative(packagePath, realPath.source)); + resources[fromPathJs] = (0, _assign2.default)({}, resource, realPath, overrideBlock || {}); + } + + var fromPathTs = path.addExt(fromPath, 'ts'); + realPath = getRealPathUniversal(fromPathTs, packagePath, relativeToDir); + if (realPath) { + debug('<' + path.basename(packagePath) + '> ' + realPath.path + ' => ' + path.relative(packagePath, realPath.source)); + resources[fromPathTs] = (0, _assign2.default)({}, resource, realPath, overrideBlock || {}); + } + + var fromPathCss = path.addExt(fromPath, 'css'); + realPath = getRealPathUniversal(fromPathCss, packagePath, relativeToDir); + if (realPath) { + debug('<' + path.basename(packagePath) + '> ' + realPath.path + ' => ' + path.relative(packagePath, realPath.source)); + resources[fromPathCss] = (0, _assign2.default)({}, resource, realPath, overrideBlock || {}); + } + } else { + console.error('Unable to resolve', fromPath); + } +} + +function getResourcesOfPackage() { + var resources = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; + var packagePath = arguments.length <= 1 || arguments[1] === undefined ? undefined : arguments[1]; + var relativeToDir = arguments.length <= 2 || arguments[2] === undefined ? '' : arguments[2]; + var overrideBlock = arguments.length <= 3 || arguments[3] === undefined ? undefined : arguments[3]; + var externalModule = arguments.length <= 4 || arguments[4] === undefined ? undefined : arguments[4]; + + if (modulesProcessed.indexOf(packagePath) !== -1) { + return; + } + modulesProcessed.push(packagePath); + + var packageJson = void 0; + if (!packageJson) { + try { + packageJson = JSON.parse(fileSystem.readFileSync(path.join(packagePath, 'package.json'), 'utf8')); + } catch (_) {} + } + + if (packageJson) { + if (packageJson.aurelia && packageJson.aurelia.build && packageJson.aurelia.build.resources) { + for (var _iterator2 = packageJson.aurelia.build.resources, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : (0, _getIterator3.default)(_iterator2);;) { + var _ref2; + + if (_isArray2) { + if (_i2 >= _iterator2.length) break; + _ref2 = _iterator2[_i2++]; + } else { + _i2 = _iterator2.next(); + if (_i2.done) break; + _ref2 = _i2.value; + } + + var resource = _ref2; + + resource = resource instanceof Object && !Array.isArray(resource) ? resource : { path: resource }; + var fromPaths = Array.isArray(resource.path) ? resource.path : [resource.path]; + for (var _iterator3 = fromPaths, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : (0, _getIterator3.default)(_iterator3);;) { + var _ref3; + + if (_isArray3) { + if (_i3 >= _iterator3.length) break; + _ref3 = _iterator3[_i3++]; + } else { + _i3 = _iterator3.next(); + if (_i3.done) break; + _ref3 = _i3.value; + } + + var fromPath = _ref3; + + debug('<' + (externalModule || path.basename(packagePath)) + '> [resolving] \'' + fromPath + '\''); + + if (externalModule) { + if (fromPath.indexOf('.') !== 0) fromPath = fixRelativeFromPath(fromPath, undefined, undefined, externalModule);else fromPath = path.join(externalModule, fromPath); + } + + processFromPath(resources, fromPath, resource, packagePath, relativeToDir, overrideBlock); + } + } + } + + if (packageJson.dependencies) { + for (var _iterator4 = (0, _getOwnPropertyNames2.default)(packageJson.dependencies), _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : (0, _getIterator3.default)(_iterator4);;) { + var _ref4; + + if (_isArray4) { + if (_i4 >= _iterator4.length) break; + _ref4 = _iterator4[_i4++]; + } else { + _i4 = _iterator4.next(); + if (_i4.done) break; + _ref4 = _i4.value; + } + + var moduleName = _ref4; + + var modulePathIndex = moduleNames.indexOf(moduleName); + if (modulePathIndex !== -1) { + var modulePath = modulePaths[modulePathIndex]; + getResourcesOfPackage(resources, modulePath, undefined, undefined, moduleName); + } + } + } + } +} + +function fixRelativeFromPath(fromPath, realSrcPath, realParentPath, externalModule) { + var modulePathIndex = moduleNames.indexOf(fromPath.split('/')[0]); + if (modulePathIndex !== -1) { + return fromPath; + } else { + if (fromPath.indexOf('.') == 0) { + fromPath = path.joinSafe('./', path.relative(realSrcPath, realParentPath), fromPath); + } + return externalModule ? path.join(externalModule, fromPath) : fromPath; + } +} + +function resolveTemplateResources(htmlFilePath, srcPath, externalModule) { + var html = fileSystem.readFileSync(htmlFilePath); + var $ = cheerio.load(html); + var relativeParent = path.dirname(htmlFilePath); + var resources = []; + + var requireTags = $('require'); + requireTags.each(function (index) { + var fromPath = requireTags[index].attribs.from; + var isLazy = requireTags[index].attribs.hasOwnProperty('lazy'); + var bundle = requireTags[index].attribs.bundle; + if (fromPath) resources.push({ path: fixRelativeFromPath(fromPath, srcPath, relativeParent, externalModule), lazy: isLazy, bundle: bundle }); + }); + + var viewModelRequests = $('[view-model]'); + viewModelRequests.each(function (index) { + var fromPath = viewModelRequests[index].attribs['view-model']; + var isLazy = viewModelRequests[index].attribs.hasOwnProperty('lazy'); + var bundle = viewModelRequests[index].attribs.bundle; + if (fromPath) resources.push({ path: fixRelativeFromPath(fromPath, srcPath, relativeParent, externalModule), lazy: isLazy, bundle: bundle }); + }); + + var viewRequests = $('[view]'); + viewRequests.each(function (index) { + var fromPath = viewRequests[index].attribs.view; + var isLazy = viewRequests[index].attribs.hasOwnProperty('lazy'); + var bundle = viewRequests[index].attribs.bundle; + if (fromPath) resources.push({ path: fixRelativeFromPath(fromPath, srcPath, relativeParent, externalModule), lazy: isLazy, bundle: bundle }); + }); + + return resources; +} \ No newline at end of file diff --git a/dist/index.js b/dist/index.js index 3c2f3df..f79eb9c 100644 --- a/dist/index.js +++ b/dist/index.js @@ -16,7 +16,36 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de var path = require('upath'); var ContextElementDependency = require('webpack/lib/dependencies/ContextElementDependency'); -var resolveTemplates = require('./resolve-template'); +var resolveTemplates = require('./build-resources'); + +function getPath(resolvedResource) { + var input = resolvedResource.source; + var lazy = resolvedResource.lazy; + var bundle = resolvedResource.bundle; + + var extension = path.extname(input); + var output = ''; + + switch (extension) { + case ".css": + output += '!!css!'; + break; + case ".scss": + output += '!!sass!'; + break; + case ".less": + output += '!!less!'; + break; + } + + if (lazy || bundle) output += 'bundle?'; + if (lazy) output += 'lazy'; + if (lazy && bundle) output += '&'; + if (bundle) output += 'name=' + bundle; + if (lazy || bundle) output += '!'; + + return '' + output + input; +} var AureliaWebpackPlugin = function () { function AureliaWebpackPlugin() { @@ -93,9 +122,9 @@ var AureliaWebpackPlugin = function () { var requireRequestPath = _ref2; - var webpackRequestPath = contextElements[requireRequestPath]; - var newDependency = new ContextElementDependency(webpackRequestPath, requireRequestPath); - newDependency.optional = true; + var resource = contextElements[requireRequestPath]; + var newDependency = new ContextElementDependency(getPath(resource), path.joinSafe('./', requireRequestPath)); + if (resource.hasOwnProperty('optional')) newDependency.optional = !!resource.optional;else newDependency.optional = true; var previouslyAdded = dependencies.findIndex(function (dependency) { return dependency.userRequest === requireRequestPath; }); diff --git a/dist/resolve-template.js b/dist/resolve-template.js deleted file mode 100644 index 5ca5601..0000000 --- a/dist/resolve-template.js +++ /dev/null @@ -1,912 +0,0 @@ -'use strict'; - -var _regenerator = require('babel-runtime/regenerator'); - -var _regenerator2 = _interopRequireDefault(_regenerator); - -var _getIterator2 = require('babel-runtime/core-js/get-iterator'); - -var _getIterator3 = _interopRequireDefault(_getIterator2); - -var _getOwnPropertyNames = require('babel-runtime/core-js/object/get-own-property-names'); - -var _getOwnPropertyNames2 = _interopRequireDefault(_getOwnPropertyNames); - -var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator'); - -var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2); - -var _assign = require('babel-runtime/core-js/object/assign'); - -var _assign2 = _interopRequireDefault(_assign); - -var processAll = function () { - var ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(options) { - var dependencies, nodeModules, packageJson, _iterator, _isArray, _i, _ref, moduleName, vendorPath, vendorPkgPath, vendorPkg, _iterator2, _isArray2, _i2, _ref2, resource, fromPaths, _iterator3, _isArray3, _i3, _ref3, fromPath, _moduleName, rootAlias; - - return _regenerator2.default.wrap(function _callee$(_context) { - while (1) { - switch (_context.prev = _context.next) { - case 0: - filesProcessed = []; - modulesProcessed = []; - optionsGlobal = options; - dependencies = {}; - nodeModules = path.join(options.root, 'node_modules'); - packageJson = path.join(options.root, 'package.json'); - - - if (!baseVendorPkg) { - try { - baseVendorPkg = JSON.parse(fileSystem.readFileSync(packageJson, 'utf8')); - } catch (_) {} - } - - _context.t0 = dependencies; - _context.next = 10; - return autoresolveTemplates(options.src, nodeModules, options.lazy, options.bundle); - - case 10: - _context.t1 = _context.sent; - assign(_context.t0, _context.t1); - - if (!baseVendorPkg) { - _context.next = 80; - break; - } - - if (!baseVendorPkg.dependencies) { - _context.next = 39; - break; - } - - _iterator = (0, _getOwnPropertyNames2.default)(baseVendorPkg.dependencies), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator); - - case 15: - if (!_isArray) { - _context.next = 21; - break; - } - - if (!(_i >= _iterator.length)) { - _context.next = 18; - break; - } - - return _context.abrupt('break', 39); - - case 18: - _ref = _iterator[_i++]; - _context.next = 25; - break; - - case 21: - _i = _iterator.next(); - - if (!_i.done) { - _context.next = 24; - break; - } - - return _context.abrupt('break', 39); - - case 24: - _ref = _i.value; - - case 25: - moduleName = _ref; - - if (!(modulesProcessed.indexOf(moduleName) === -1)) { - _context.next = 37; - break; - } - - modulesProcessed.push(moduleName); - vendorPath = path.resolve(options.root, 'node_modules', moduleName); - vendorPkgPath = path.resolve(vendorPath, 'package.json'); - vendorPkg = JSON.parse(fileSystem.readFileSync(vendorPkgPath, 'utf8')); - - if (!(vendorPkg.browser || vendorPkg.main)) { - _context.next = 37; - break; - } - - _context.t2 = dependencies; - _context.next = 35; - return getDependency(moduleName, options.root, options.root, [nodeModules], null, packageJson, options.lazy, options.bundle, undefined, undefined, true); - - case 35: - _context.t3 = _context.sent; - assign(_context.t2, _context.t3); - - case 37: - _context.next = 15; - break; - - case 39: - if (!(baseVendorPkg.aurelia && baseVendorPkg.aurelia.build && baseVendorPkg.aurelia.build.resources)) { - _context.next = 80; - break; - } - - _iterator2 = baseVendorPkg.aurelia.build.resources, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : (0, _getIterator3.default)(_iterator2); - - case 41: - if (!_isArray2) { - _context.next = 47; - break; - } - - if (!(_i2 >= _iterator2.length)) { - _context.next = 44; - break; - } - - return _context.abrupt('break', 80); - - case 44: - _ref2 = _iterator2[_i2++]; - _context.next = 51; - break; - - case 47: - _i2 = _iterator2.next(); - - if (!_i2.done) { - _context.next = 50; - break; - } - - return _context.abrupt('break', 80); - - case 50: - _ref2 = _i2.value; - - case 51: - resource = _ref2; - fromPaths = resource instanceof Object ? [resource.path] : [resource]; - - if (fromPaths[0] instanceof Array) { - fromPaths = fromPaths[0]; - } - _iterator3 = fromPaths, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : (0, _getIterator3.default)(_iterator3); - - case 55: - if (!_isArray3) { - _context.next = 61; - break; - } - - if (!(_i3 >= _iterator3.length)) { - _context.next = 58; - break; - } - - return _context.abrupt('break', 78); - - case 58: - _ref3 = _iterator3[_i3++]; - _context.next = 65; - break; - - case 61: - _i3 = _iterator3.next(); - - if (!_i3.done) { - _context.next = 64; - break; - } - - return _context.abrupt('break', 78); - - case 64: - _ref3 = _i3.value; - - case 65: - fromPath = _ref3; - _moduleName = fromPath.split(pathSep)[0]; - - if (!(modulesProcessed.indexOf(_moduleName) === -1)) { - _context.next = 76; - break; - } - - modulesProcessed.push(_moduleName); - rootAlias = resource.root ? path.resolve(options.root, 'node_modules', _moduleName, resource.root) : undefined; - - if (!rootAlias && baseVendorPkg.aurelia.build.moduleRootOverride && baseVendorPkg.aurelia.build.moduleRootOverride[_moduleName]) { - rootAlias = path.resolve(options.root, 'node_modules', _moduleName, baseVendorPkg.aurelia.build.moduleRootOverride[_moduleName]); - } - _context.t4 = dependencies; - _context.next = 74; - return getDependency(fromPath, options.src, options.src, [nodeModules], null, packageJson, options.lazy || resource.lazy, options.bundle || resource.bundle, rootAlias); - - case 74: - _context.t5 = _context.sent; - assign(_context.t4, _context.t5); - - case 76: - _context.next = 55; - break; - - case 78: - _context.next = 41; - break; - - case 80: - return _context.abrupt('return', dependencies); - - case 81: - case 'end': - return _context.stop(); - } - } - }, _callee, this); - })); - return function processAll(_x) { - return ref.apply(this, arguments); - }; -}(); - -var autoresolveTemplates = function () { - var ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee2(srcPath, nodeModules, isLazy, bundleName) { - var dependencies, templates, _iterator4, _isArray4, _i4, _ref4, htmlFilePath; - - return _regenerator2.default.wrap(function _callee2$(_context2) { - while (1) { - switch (_context2.prev = _context2.next) { - case 0: - dependencies = {}; - _context2.next = 3; - return getFilesRecursively(srcPath, '.html'); - - case 3: - templates = _context2.sent; - _iterator4 = templates, _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : (0, _getIterator3.default)(_iterator4); - - case 5: - if (!_isArray4) { - _context2.next = 11; - break; - } - - if (!(_i4 >= _iterator4.length)) { - _context2.next = 8; - break; - } - - return _context2.abrupt('break', 23); - - case 8: - _ref4 = _iterator4[_i4++]; - _context2.next = 15; - break; - - case 11: - _i4 = _iterator4.next(); - - if (!_i4.done) { - _context2.next = 14; - break; - } - - return _context2.abrupt('break', 23); - - case 14: - _ref4 = _i4.value; - - case 15: - htmlFilePath = _ref4; - _context2.t0 = dependencies; - _context2.next = 19; - return resolveTemplate(htmlFilePath, srcPath, [nodeModules], undefined, isLazy, bundleName); - - case 19: - _context2.t1 = _context2.sent; - assign(_context2.t0, _context2.t1); - - case 21: - _context2.next = 5; - break; - - case 23: - return _context2.abrupt('return', dependencies); - - case 24: - case 'end': - return _context2.stop(); - } - } - }, _callee2, this); - })); - return function autoresolveTemplates(_x2, _x3, _x4, _x5) { - return ref.apply(this, arguments); - }; -}(); - -var resolveTemplate = function () { - var ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee3(htmlFilePath, srcPath, nodeModulesList, fromWithinModule, isParentLazy, bundleName, rootAlias) { - var dependencies, html, $, relativeParent, resources, requireTags, viewModelRequests, viewRequests, _iterator5, _isArray5, _i5, _ref5, resource; - - return _regenerator2.default.wrap(function _callee3$(_context3) { - while (1) { - switch (_context3.prev = _context3.next) { - case 0: - dependencies = {}; - html = fileSystem.readFileSync(htmlFilePath); - $ = cheerio.load(html); - relativeParent = path.dirname(htmlFilePath); - resources = []; - requireTags = $('require'); - - requireTags.each(function (index) { - var fromPath = requireTags[index].attribs.from; - var isLazy = requireTags[index].attribs.hasOwnProperty('lazy'); - var bundle = requireTags[index].attribs.bundle; - if (fromPath) resources.push({ path: fromPath, lazy: isLazy, bundle: bundle }); - }); - - viewModelRequests = $('[view-model]'); - - viewModelRequests.each(function (index) { - var fromPath = viewModelRequests[index].attribs['view-model']; - var isLazy = viewModelRequests[index].attribs.hasOwnProperty('lazy'); - var bundle = viewModelRequests[index].attribs.bundle; - if (fromPath) resources.push({ path: fromPath, lazy: isLazy, bundle: bundle }); - }); - - viewRequests = $('[view]'); - - viewRequests.each(function (index) { - var fromPath = viewRequests[index].attribs.view; - var isLazy = viewRequests[index].attribs.hasOwnProperty('lazy'); - var bundle = viewRequests[index].attribs.bundle; - if (fromPath) resources.push({ path: fromPath, lazy: isLazy, bundle: bundle }); - }); - - _iterator5 = resources, _isArray5 = Array.isArray(_iterator5), _i5 = 0, _iterator5 = _isArray5 ? _iterator5 : (0, _getIterator3.default)(_iterator5); - - case 12: - if (!_isArray5) { - _context3.next = 18; - break; - } - - if (!(_i5 >= _iterator5.length)) { - _context3.next = 15; - break; - } - - return _context3.abrupt('break', 30); - - case 15: - _ref5 = _iterator5[_i5++]; - _context3.next = 22; - break; - - case 18: - _i5 = _iterator5.next(); - - if (!_i5.done) { - _context3.next = 21; - break; - } - - return _context3.abrupt('break', 30); - - case 21: - _ref5 = _i5.value; - - case 22: - resource = _ref5; - _context3.t0 = dependencies; - _context3.next = 26; - return getDependency(resource.path, relativeParent, srcPath, nodeModulesList, fromWithinModule, htmlFilePath, isParentLazy || resource.lazy, bundleName || resource.bundle, rootAlias); - - case 26: - _context3.t1 = _context3.sent; - assign(_context3.t0, _context3.t1); - - case 28: - _context3.next = 12; - break; - - case 30: - return _context3.abrupt('return', dependencies); - - case 31: - case 'end': - return _context3.stop(); - } - } - }, _callee3, this); - })); - return function resolveTemplate(_x6, _x7, _x8, _x9, _x10, _x11, _x12) { - return ref.apply(this, arguments); - }; -}(); - -var getDependency = function () { - var ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee5(fromPath, relativeParent, srcPath, nodeModulesList, fromWithinModule, requestedBy, isLazy, bundleName, rootAlias, triedToCorrectPath, doNotAdd) { - var addDependency = function () { - var ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee4(webpackRequireString, webpackPath, htmlCounterpart, rootAlias, moduleName, modulePath) { - var htmlWebpackRequireString; - return _regenerator2.default.wrap(function _callee4$(_context4) { - while (1) { - switch (_context4.prev = _context4.next) { - case 0: - if (webpackRequireString.indexOf('..') == -1) { - dependencies[webpackRequireString] = webpackPath; - console.log((fromWithinModule ? '<' + fromWithinModule + '> ' + '[' + path.basename(requestedBy) : '[' + requestedByRelativeToSrc) + '] required "' + webpackRequireString + '" from "' + webpackPath.replace(optionsGlobal.root + pathSep, '') + '".'); - filesProcessed.push(webpackRequireString); - } - - if (!htmlCounterpart) { - _context4.next = 11; - break; - } - - htmlWebpackRequireString = './' + getPathWithoutExtension(webpackRequireString) + '.html'; - - - dependencies[htmlWebpackRequireString] = getPath(htmlCounterpart, isLazy, bundleName); - console.log((fromWithinModule ? '<' + fromWithinModule + '> ' + '[' + path.basename(requestedBy) : '[' + requestedByRelativeToSrc) + '] required "' + htmlWebpackRequireString + '" from "' + htmlCounterpart.replace(optionsGlobal.root + pathSep, '') + '".'); - - filesProcessed.push(htmlWebpackRequireString); - - _context4.t0 = dependencies; - _context4.next = 9; - return resolveTemplate(htmlCounterpart, modulePath || srcPath, nodeModulesList, moduleName || fromWithinModule, isLazy, bundleName, rootAlias); - - case 9: - _context4.t1 = _context4.sent; - assign(_context4.t0, _context4.t1); - - case 11: - case 'end': - return _context4.stop(); - } - } - }, _callee4, this); - })); - return function addDependency(_x24, _x25, _x26, _x27, _x28, _x29) { - return ref.apply(this, arguments); - }; - }(); - - var dependencies, requestedByRelativeToSrc, split, webpackPath, webpackRequireString, fullPathOrig, fullPath, fullPathNoExt, extOrig, pathIsLocal, htmlCounterpart, stats, extension, moduleName, modulePath, packagesOwnNodeModules, nodeModulesIndex, nodeModules, packageJson, vendorPkg, mainDir, ownPath, _iterator6, _isArray6, _i6, _ref6, resource, resourcePath, useRootAlias, pathParts, _moduleName2, relativeRootAlias, relativeRootSplit, rootedFromPath; - - return _regenerator2.default.wrap(function _callee5$(_context5) { - while (1) { - switch (_context5.prev = _context5.next) { - case 0: - dependencies = {}; - requestedByRelativeToSrc = path.relative(srcPath, requestedBy); - split = requestedByRelativeToSrc.split(pathSep); - - if (split[0] == 'node_modules') { - nodeModulesList = nodeModulesList.concat([path.join(srcPath, 'node_modules')]); - srcPath = path.join(srcPath, split[0], split[1]); - fromWithinModule = split[1]; - } - - webpackPath = void 0; - webpackRequireString = void 0; - fullPathOrig = path.join(relativeParent, fromPath); - fullPath = fullPathOrig; - fullPathNoExt = getPathWithoutExtension(fullPath); - extOrig = path.extname(fullPath); - pathIsLocal = fromPath.startsWith('./') || fromPath.startsWith('../'); - htmlCounterpart = void 0; - stats = void 0; - extension = void 0; - moduleName = void 0; - modulePath = void 0; - packagesOwnNodeModules = void 0; - - - try { - stats = fileSystem.statSync(fullPath); - } catch (_) {} - - if (!stats) try { - stats = fileSystem.statSync(fullPath = fullPathOrig + '.js'); - } catch (_) {} - - if (!stats) try { - stats = fileSystem.statSync(fullPath = fullPathOrig + '.ts'); - } catch (_) {} - - if (extOrig != '.html') { - try { - fileSystem.statSync(fullPathNoExt + '.html'); - htmlCounterpart = fullPathNoExt + '.html'; - } catch (_) {} - } - - if (!(stats && stats.isFile())) { - _context5.next = 43; - break; - } - - extension = path.extname(fullPath); - - webpackPath = getPath(fullPath, isLazy, bundleName); - - if (fromWithinModule) { - _context5.next = 34; - break; - } - - webpackRequireString = './' + (pathIsLocal ? path.relative(srcPath, path.join(relativeParent, fromPath)) : fromPath); - _context5.next = 28; - return addDependency(webpackRequireString, webpackPath, htmlCounterpart, rootAlias); - - case 28: - if (!rootAlias) { - _context5.next = 32; - break; - } - - webpackRequireString = './' + (pathIsLocal ? path.relative(rootAlias, path.join(relativeParent, fromPath)) : fromPath); - _context5.next = 32; - return addDependency(webpackRequireString, webpackPath, htmlCounterpart, rootAlias); - - case 32: - _context5.next = 41; - break; - - case 34: - - webpackRequireString = './' + fromWithinModule + '/' + path.relative(srcPath, path.join(relativeParent, fromPath)); - _context5.next = 37; - return addDependency(webpackRequireString, webpackPath, htmlCounterpart, rootAlias); - - case 37: - if (!rootAlias) { - _context5.next = 41; - break; - } - - webpackRequireString = './' + fromWithinModule + '/' + path.relative(rootAlias, path.join(relativeParent, fromPath)); - _context5.next = 41; - return addDependency(webpackRequireString, webpackPath, htmlCounterpart, rootAlias); - - case 41: - _context5.next = 102; - break; - - case 43: - if (pathIsLocal) { - _context5.next = 102; - break; - } - - stats = undefined; - nodeModulesIndex = nodeModulesList.length; - - while (!stats && nodeModulesIndex--) { - nodeModules = nodeModulesList[nodeModulesIndex]; - - fullPathOrig = path.join(nodeModules, fromPath); - fullPath = fullPathOrig; - fullPathNoExt = getPathWithoutExtension(fullPath); - - try { - stats = fileSystem.statSync(fullPath); - } catch (_) {} - - if (!stats) try { - stats = fileSystem.statSync(fullPath = fullPathOrig + '.js'); - } catch (_) {} - - if (extOrig != '.html') { - try { - fileSystem.statSync(fullPathNoExt + '.html'); - htmlCounterpart = fullPathNoExt + '.html'; - } catch (_) {} - } - } - - if (!stats) { - _context5.next = 102; - break; - } - - extension = path.extname(fullPath); - - if (stats.isDirectory() && path.basename(path.dirname(fullPath)) === 'node_modules') { - moduleName = path.basename(fullPath); - modulePath = fullPath; - - webpackPath = getPath(moduleName, isLazy, bundleName); - webpackRequireString = './' + fromPath; - } else if (stats.isFile()) { - moduleName = fromPath.split(pathSep)[0]; - modulePath = path.resolve(nodeModulesList[nodeModulesIndex], moduleName); - - webpackPath = getPath(fullPath, isLazy, bundleName); - webpackRequireString = './' + fromPath; - } - - if (!(moduleName && modulePath)) { - _context5.next = 99; - break; - } - - packageJson = path.resolve(modulePath, 'package.json'); - vendorPkg = void 0; - - try { - vendorPkg = JSON.parse(fileSystem.readFileSync(packageJson, 'utf8')); - } catch (_) {} - - if (!vendorPkg) { - _context5.next = 97; - break; - } - - mainDir = vendorPkg.main ? path.resolve(modulePath, path.dirname(vendorPkg.main)) : null; - - - if (!rootAlias) { - if (vendorPkg.aurelia && vendorPkg.aurelia.build && vendorPkg.aurelia.build.root) { - rootAlias = path.resolve(modulePath, vendorPkg.aurelia.build.root); - } else if (baseVendorPkg && baseVendorPkg.aurelia && baseVendorPkg.aurelia.build && baseVendorPkg.aurelia.build.moduleRootOverride && baseVendorPkg.aurelia.build.moduleRootOverride[moduleName]) { - rootAlias = path.resolve(srcPath, baseVendorPkg.aurelia.build.moduleRootOverride[moduleName]); - } else { - rootAlias = mainDir; - } - - if (rootAlias === modulePath) rootAlias = null; - } - - if (doNotAdd) { - _context5.next = 60; - break; - } - - _context5.next = 60; - return addDependency(webpackRequireString, webpackPath, htmlCounterpart, rootAlias, moduleName, modulePath); - - case 60: - if (!(rootAlias && stats.isFile())) { - _context5.next = 68; - break; - } - - webpackRequireString = './' + moduleName + '/' + path.relative(rootAlias, fullPath); - _context5.next = 64; - return addDependency(webpackRequireString, webpackPath, htmlCounterpart, rootAlias); - - case 64: - if (!(extension == '.js' || extension == '.ts')) { - _context5.next = 68; - break; - } - - webpackRequireString = './' + moduleName + '/' + path.relative(rootAlias, getPathWithoutExtension(fullPath)); - _context5.next = 68; - return addDependency(webpackRequireString, webpackPath, htmlCounterpart, rootAlias); - - case 68: - if (!(modulesProcessed.indexOf(modulePath) == -1)) { - _context5.next = 97; - break; - } - - modulesProcessed.push(modulePath); - - try { - ownPath = path.resolve(modulePath, 'node_modules'); - - fileSystem.statSync(ownPath); - packagesOwnNodeModules = ownPath; - } catch (_) {} - - if (!(vendorPkg.aurelia && vendorPkg.aurelia.build && vendorPkg.aurelia.build.resources)) { - _context5.next = 97; - break; - } - - _iterator6 = vendorPkg.aurelia.build.resources, _isArray6 = Array.isArray(_iterator6), _i6 = 0, _iterator6 = _isArray6 ? _iterator6 : (0, _getIterator3.default)(_iterator6); - - case 73: - if (!_isArray6) { - _context5.next = 79; - break; - } - - if (!(_i6 >= _iterator6.length)) { - _context5.next = 76; - break; - } - - return _context5.abrupt('break', 97); - - case 76: - _ref6 = _iterator6[_i6++]; - _context5.next = 83; - break; - - case 79: - _i6 = _iterator6.next(); - - if (!_i6.done) { - _context5.next = 82; - break; - } - - return _context5.abrupt('break', 97); - - case 82: - _ref6 = _i6.value; - - case 83: - resource = _ref6; - resourcePath = resource instanceof Object ? resource.path : resource; - useRootAlias = rootAlias; - - if (vendorPkg.aurelia.build.moduleRootOverride && vendorPkg.aurelia.build.moduleRootOverride[moduleName]) { - useRootAlias = path.resolve(modulePath, vendorPkg.aurelia.build.moduleRootOverride[moduleName]); - } - - if (resource.root) { - useRootAlias = path.resolve(modulePath, resource.root); - } - - if (baseVendorPkg && baseVendorPkg.aurelia && baseVendorPkg.aurelia.build.moduleRootOverride && baseVendorPkg.aurelia.build.moduleRootOverride[moduleName]) { - useRootAlias = path.resolve(modulePath, baseVendorPkg.aurelia.build.moduleRootOverride[moduleName]); - } - if (useRootAlias === modulePath) { - useRootAlias = null; - } - - _context5.t0 = dependencies; - _context5.next = 93; - return getDependency(resourcePath, modulePath, modulePath, packagesOwnNodeModules ? nodeModulesList.concat(packagesOwnNodeModules) : nodeModulesList, moduleName, packageJson, isLazy || resource.lazy, bundleName || resource.bundle, useRootAlias); - - case 93: - _context5.t1 = _context5.sent; - assign(_context5.t0, _context5.t1); - - case 95: - _context5.next = 73; - break; - - case 97: - _context5.next = 102; - break; - - case 99: - if (doNotAdd) { - _context5.next = 102; - break; - } - - _context5.next = 102; - return addDependency(webpackRequireString, webpackPath, htmlCounterpart, rootAlias, moduleName, modulePath); - - case 102: - if (webpackPath) { - _context5.next = 119; - break; - } - - pathParts = fromPath.split(pathSep); - - if (!(!pathIsLocal && pathParts.length > 1 && rootAlias && rootAlias !== srcPath)) { - _context5.next = 116; - break; - } - - _moduleName2 = pathParts.shift(); - relativeRootAlias = path.relative(srcPath, rootAlias); - relativeRootSplit = relativeRootAlias.split(pathSep); - - if (relativeRootSplit[0] == '..') { - relativeRootSplit.shift(); - } - while (relativeRootSplit[0] == 'node_modules') { - relativeRootSplit.shift(); - relativeRootSplit.shift(); - } - relativeRootAlias = relativeRootSplit.join('/'); - - rootedFromPath = path.join(_moduleName2, relativeRootAlias, pathParts.join(pathSep)); - - if (!(rootedFromPath !== fromPath && !triedToCorrectPath)) { - _context5.next = 116; - break; - } - - _context5.next = 115; - return getDependency(rootedFromPath, relativeParent, srcPath, nodeModulesList, fromWithinModule, requestedBy, isLazy, bundleName, rootAlias, true); - - case 115: - return _context5.abrupt('return', _context5.sent); - - case 116: - console.error('[' + (fromWithinModule ? '<' + fromWithinModule + '>' : path.relative(srcPath, requestedBy)) + '] wants to require "' + fromPath + '", which does not exist.'); - _context5.next = 125; - break; - - case 119: - if (!(extension == ".html")) { - _context5.next = 125; - break; - } - - _context5.t2 = dependencies; - _context5.next = 123; - return resolveTemplate(fullPath, modulePath || srcPath, packagesOwnNodeModules ? nodeModulesList.concat(packagesOwnNodeModules) : nodeModulesList, moduleName || fromWithinModule, isLazy, bundleName, rootAlias); - - case 123: - _context5.t3 = _context5.sent; - assign(_context5.t2, _context5.t3); - - case 125: - return _context5.abrupt('return', dependencies); - - case 126: - case 'end': - return _context5.stop(); - } - } - }, _callee5, this); - })); - return function getDependency(_x13, _x14, _x15, _x16, _x17, _x18, _x19, _x20, _x21, _x22, _x23) { - return ref.apply(this, arguments); - }; -}(); - -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } - -var path = require('upath'); -var fileSystem = require('fs'); -var readdir = require('recursive-readdir'); -var assign = _assign2.default || require('object.assign'); -var Promise = require('bluebird'); -var cheerio = require('cheerio'); -var pathSep = '/'; - -var filesProcessed = []; -var modulesProcessed = []; - -var optionsGlobal = {}; -var baseVendorPkg = void 0; - -function getFilesRecursively(targetDir, extension) { - return new Promise(function (resolve, reject) { - return readdir(targetDir, [function (file, stats) { - return path.extname(file) !== extension && !stats.isDirectory(); - }], function (error, files) { - return error ? reject(error) : resolve(files); - }); - }); -} - -function getPath(input, lazy, bundle) { - var extension = path.extname(input); - var output = ''; - - if (extension == ".css") output += '!!css!'; - if (lazy || bundle) output += 'bundle?'; - if (lazy) output += 'lazy'; - if (lazy && bundle) output += '&'; - if (bundle) output += 'name=' + bundle; - if (lazy || bundle) output += '!'; - return '' + output + input; -} - -function getPathWithoutExtension(input) { - return path.trimExt(input); -} - -module.exports = { - getFilesRecursively: getFilesRecursively, - processAll: processAll, - autoresolveTemplates: autoresolveTemplates, - resolveTemplate: resolveTemplate, - getDependency: getDependency -}; \ No newline at end of file diff --git a/package.json b/package.json index 2acab72..4aa259d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "aurelia-webpack-plugin", - "version": "1.0.0-beta.2.0.4", + "version": "1.0.0-beta.3.0.0", "description": "A plugin for webpack that enables bundling Aurelia applications.", "keywords": [ "aurelia",