@sum.cumo/nuxt-custom-route-folder
Advanced tools
Comparing version 2.0.0 to 3.0.0-beta.1
@@ -7,6 +7,6 @@ 'use strict'; | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
exports.default = createCustomRoutesFromFolder; | ||
@@ -18,14 +18,2 @@ | ||
var _fs = require('fs'); | ||
var _fs2 = _interopRequireDefault(_fs); | ||
var _crypto = require('crypto'); | ||
var _crypto2 = _interopRequireDefault(_crypto); | ||
var _minimatch = require('minimatch'); | ||
var _minimatch2 = _interopRequireDefault(_minimatch); | ||
var _lodash = require('lodash.kebabcase'); | ||
@@ -45,4 +33,6 @@ | ||
var _common = require('./common'); | ||
var _chunkPrefix = require('./chunkPrefix'); | ||
var _chunkPrefix2 = _interopRequireDefault(_chunkPrefix); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -57,6 +47,9 @@ | ||
glob = _ref.glob, | ||
transform = _ref.transform, | ||
extendRoutes = _ref.extendRoutes, | ||
_ref$priority = _ref.priority, | ||
priority = _ref$priority === undefined ? 0 : _ref$priority, | ||
_ref$mapImport = _ref.mapImport, | ||
mapImport = _ref$mapImport === undefined ? function (p) { | ||
return p; | ||
} : _ref$mapImport, | ||
_ref$mapRouteName = _ref.mapRouteName, | ||
@@ -70,2 +63,6 @@ mapRouteName = _ref$mapRouteName === undefined ? function (basePath) { | ||
} : _ref$mapRoutePath, | ||
_ref$mapMeta = _ref.mapMeta, | ||
mapMeta = _ref$mapMeta === undefined ? function () { | ||
return {}; | ||
} : _ref$mapMeta, | ||
_ref$filter = _ref.filter, | ||
@@ -75,4 +72,2 @@ filter = _ref$filter === undefined ? function () { | ||
} : _ref$filter, | ||
_ref$transformExt = _ref.transformExt, | ||
transformExt = _ref$transformExt === undefined ? 'js' : _ref$transformExt, | ||
_ref$srcDir = _ref.srcDir, | ||
@@ -100,8 +95,7 @@ srcDir = _ref$srcDir === undefined ? nuxt.options.srcDir : _ref$srcDir, | ||
var transformFile = function () { | ||
var _ref4 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(_ref3, route) { | ||
var component = _ref3.file, | ||
event = _ref3.event; | ||
var getRoute = function () { | ||
var _ref4 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(_ref3) { | ||
var file = _ref3.file; | ||
var proxyPath, retVal, _ref5, _ref6, contents; | ||
var basePath, urlPath, chunkName, _ref5, _ref6, routePath, name, meta, component; | ||
@@ -112,87 +106,22 @@ return regeneratorRuntime.wrap(function _callee$(_context) { | ||
case 0: | ||
proxyPath = _path2.default.join(_common.tmpDir, _crypto2.default.createHash('md5').update(component).digest('hex') + '.' + transformExt); | ||
retVal = { | ||
route: _extends({}, route, { | ||
component: proxyPath | ||
}), | ||
proxyPath: proxyPath | ||
}; | ||
if (!(event === 'unlink')) { | ||
_context.next = 4; | ||
break; | ||
} | ||
return _context.abrupt('return', retVal); | ||
case 4: | ||
_context.t0 = Promise; | ||
_context.t1 = transform; | ||
_context.next = 8; | ||
return (0, _common.readFile)(component); | ||
case 8: | ||
_context.t2 = _context.sent; | ||
_context.t3 = route; | ||
_context.t4 = (0, _context.t1)(_context.t2, _context.t3); | ||
_context.t5 = new Promise(function (resolve) { | ||
/* If the transform happens to fast, the file change | ||
is not picked up by nuxt watchers and the change | ||
is not visible in the preview */ | ||
setTimeout(resolve, 250); | ||
}); | ||
_context.t6 = [_context.t4, _context.t5]; | ||
_context.next = 15; | ||
return _context.t0.all.call(_context.t0, _context.t6); | ||
case 15: | ||
_ref5 = _context.sent; | ||
_ref6 = _slicedToArray(_ref5, 1); | ||
contents = _ref6[0].contents; | ||
_context.next = 20; | ||
return (0, _common.writeFile)(proxyPath, contents); | ||
case 20: | ||
return _context.abrupt('return', retVal); | ||
case 21: | ||
case 'end': | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee, _this); | ||
})); | ||
return function transformFile(_x, _x2) { | ||
return _ref4.apply(this, arguments); | ||
}; | ||
}(); | ||
var getRoute = function () { | ||
var _ref8 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(_ref7) { | ||
var component = _ref7.file; | ||
var basePath, urlPath, chunkName, _ref9, _ref10, routePath, name; | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
basePath = _path2.default.relative(srcDir, component).replace(new RegExp(_path2.default.extname(component) + '$'), ''); | ||
basePath = _path2.default.relative(srcDir, file).replace(new RegExp(_path2.default.extname(file) + '$'), ''); | ||
urlPath = basePath.split(_path2.default.sep).map(function (t) { | ||
return (0, _lodash2.default)(t); | ||
}).join('/').replace(/index$/, ''); | ||
chunkName = _common.CHUNK_PREFIX + '/' + basePath; | ||
_context2.next = 5; | ||
return Promise.all([mapRoutePath('/' + urlPath), mapRouteName(basePath, component)]); | ||
chunkName = _chunkPrefix2.default + '/' + basePath; | ||
_context.next = 5; | ||
return Promise.all([mapRoutePath('/' + urlPath), mapRouteName(basePath, file), mapMeta(basePath, file), mapImport(file)]); | ||
case 5: | ||
_ref9 = _context2.sent; | ||
_ref10 = _slicedToArray(_ref9, 2); | ||
routePath = _ref10[0]; | ||
name = _ref10[1]; | ||
return _context2.abrupt('return', { | ||
_ref5 = _context.sent; | ||
_ref6 = _slicedToArray(_ref5, 4); | ||
routePath = _ref6[0]; | ||
name = _ref6[1]; | ||
meta = _ref6[2]; | ||
component = _ref6[3]; | ||
return _context.abrupt('return', { | ||
path: routePath, | ||
priority: priority, | ||
component: component, | ||
meta: meta, | ||
chunkName: chunkName, | ||
@@ -202,12 +131,12 @@ name: name | ||
case 10: | ||
case 12: | ||
case 'end': | ||
return _context2.stop(); | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee2, _this); | ||
}, _callee, _this); | ||
})); | ||
return function getRoute(_x3) { | ||
return _ref8.apply(this, arguments); | ||
return function getRoute(_x) { | ||
return _ref4.apply(this, arguments); | ||
}; | ||
@@ -217,91 +146,67 @@ }(); | ||
var processFileMsg = function () { | ||
var _ref11 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3(message) { | ||
var include, directRoute, _ref12, route, proxyPath; | ||
return regeneratorRuntime.wrap(function _callee3$(_context3) { | ||
var _ref7 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(message) { | ||
var include; | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
if (!(message.event === 'ready')) { | ||
_context3.next = 2; | ||
_context2.next = 2; | ||
break; | ||
} | ||
return _context3.abrupt('return', message); | ||
return _context2.abrupt('return', message); | ||
case 2: | ||
_context3.t0 = message.event === 'unlink'; | ||
_context2.t0 = message.event === 'unlink'; | ||
if (_context3.t0) { | ||
_context3.next = 7; | ||
if (_context2.t0) { | ||
_context2.next = 7; | ||
break; | ||
} | ||
_context3.next = 6; | ||
_context2.next = 6; | ||
return filter(message.file); | ||
case 6: | ||
_context3.t0 = _context3.sent; | ||
_context2.t0 = _context2.sent; | ||
case 7: | ||
include = _context3.t0; | ||
include = _context2.t0; | ||
if (include) { | ||
_context3.next = 10; | ||
_context2.next = 10; | ||
break; | ||
} | ||
return _context3.abrupt('return', null); | ||
return _context2.abrupt('return', null); | ||
case 10: | ||
_context3.next = 12; | ||
_context2.t1 = _extends; | ||
_context2.t2 = {}; | ||
_context2.t3 = message; | ||
_context2.next = 15; | ||
return getRoute(message); | ||
case 12: | ||
directRoute = _context3.sent; | ||
case 15: | ||
_context2.t4 = _context2.sent; | ||
_context2.t5 = { | ||
route: _context2.t4 | ||
}; | ||
return _context2.abrupt('return', (0, _context2.t1)(_context2.t2, _context2.t3, _context2.t5)); | ||
if (!(['add', 'change', 'unlink'].includes(message.event) && transform)) { | ||
_context3.next = 19; | ||
break; | ||
} | ||
_context3.next = 16; | ||
return transformFile(message, directRoute); | ||
case 16: | ||
_context3.t1 = _context3.sent; | ||
_context3.next = 20; | ||
break; | ||
case 19: | ||
_context3.t1 = { route: directRoute }; | ||
case 20: | ||
_ref12 = _context3.t1; | ||
route = _ref12.route; | ||
proxyPath = _ref12.proxyPath; | ||
return _context3.abrupt('return', _extends({}, message, { | ||
route: route, | ||
proxyPath: proxyPath | ||
})); | ||
case 24: | ||
case 18: | ||
case 'end': | ||
return _context3.stop(); | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee3, _this); | ||
}, _callee2, _this); | ||
})); | ||
return function processFileMsg(_x4) { | ||
return _ref11.apply(this, arguments); | ||
return function processFileMsg(_x2) { | ||
return _ref7.apply(this, arguments); | ||
}; | ||
}(); | ||
var fsEvents = observer.filter(function (_ref13) { | ||
var file = _ref13.file, | ||
event = _ref13.event; | ||
return event === 'ready' || (0, _minimatch2.default)(file, glob); | ||
}).buffer(readyObs).take(Infinity).concatMap(function (messages) { | ||
var fsEvents = observer.buffer(readyObs).take(Infinity).concatMap(function (messages) { | ||
return Promise.all(messages.map(processFileMsg)); | ||
@@ -314,9 +219,8 @@ }).flatMap(function (m) { | ||
fsEvents.filter(function (_ref14) { | ||
var event = _ref14.event; | ||
fsEvents.filter(function (_ref8) { | ||
var event = _ref8.event; | ||
return event !== 'ready'; | ||
}).forEach(function (_ref15) { | ||
var event = _ref15.event, | ||
route = _ref15.route, | ||
proxyPath = _ref15.proxyPath; | ||
}).forEach(function (_ref9) { | ||
var event = _ref9.event, | ||
route = _ref9.route; | ||
@@ -328,10 +232,7 @@ if (event === 'unlink') { | ||
} | ||
if (proxyPath && event === 'unlink') { | ||
_fs2.default.unlink(proxyPath); | ||
} | ||
}); | ||
return new Promise(function (resolve, reject) { | ||
fsEvents.filter(function (_ref16) { | ||
var event = _ref16.event; | ||
fsEvents.filter(function (_ref10) { | ||
var event = _ref10.event; | ||
return event === 'ready'; | ||
@@ -338,0 +239,0 @@ }).subscribe(function () { |
@@ -8,4 +8,8 @@ 'use strict'; | ||
var _common = require('./common'); | ||
var _chunkPrefix = require('./chunkPrefix'); | ||
var _chunkPrefix2 = _interopRequireDefault(_chunkPrefix); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function getRoutes(extendRoutes) { | ||
@@ -34,3 +38,3 @@ if (!getRoutes.cache) { | ||
routes.forEach(function (route) { | ||
if (route.chunkName.indexOf(_common.CHUNK_PREFIX) === 0 && !customRoutes.includes(route)) { | ||
if (route.chunkName.indexOf(_chunkPrefix2.default) === 0 && !customRoutes.includes(route)) { | ||
routes.splice(routes.indexOf(route), 1); | ||
@@ -37,0 +41,0 @@ } |
@@ -44,2 +44,3 @@ 'use strict'; | ||
setTimeout(function () { | ||
done = true; | ||
watcher.close(); | ||
@@ -46,0 +47,0 @@ observer.complete(); |
{ | ||
"name": "@sum.cumo/nuxt-custom-route-folder", | ||
"version": "2.0.0", | ||
"version": "3.0.0-beta.1", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "scripts": { |
import path from 'path' | ||
import fs from 'fs' | ||
import crypto from 'crypto' | ||
import minimatch from 'minimatch' | ||
import kebabCase from 'lodash.kebabcase' | ||
@@ -9,3 +6,3 @@ import { Observable } from 'rxjs' | ||
import getRoutes from './getRoutes' | ||
import { tmpDir, CHUNK_PREFIX, readFile, writeFile } from './common' | ||
import CHUNK_PREFIX from './chunkPrefix' | ||
@@ -15,9 +12,9 @@ export default function createCustomRoutesFromFolder({ | ||
glob, | ||
transform, | ||
extendRoutes, | ||
priority = 0, | ||
mapImport = (p) => p, | ||
mapRouteName = (basePath) => basePath.replace(/\//g, ':'), | ||
mapRoutePath = (p) => p, | ||
mapMeta = () => ({}), | ||
filter = () => true, | ||
transformExt = 'js', | ||
srcDir = nuxt.options.srcDir, | ||
@@ -42,42 +39,6 @@ watch = nuxt.options.dev, | ||
const transformFile = async ({ file: component, event }, route) => { | ||
const proxyPath = path.join( | ||
tmpDir, | ||
`${crypto | ||
.createHash('md5') | ||
.update(component) | ||
.digest('hex')}.${transformExt}` | ||
) | ||
const retVal = { | ||
route: { | ||
...route, | ||
component: proxyPath, | ||
}, | ||
proxyPath, | ||
} | ||
if (event === 'unlink') { | ||
return retVal | ||
} | ||
const [{ contents }] = await Promise.all([ | ||
transform(await readFile(component), route), | ||
new Promise((resolve) => { | ||
/* If the transform happens to fast, the file change | ||
is not picked up by nuxt watchers and the change | ||
is not visible in the preview */ | ||
setTimeout(resolve, 250) | ||
}), | ||
]) | ||
await writeFile(proxyPath, contents) | ||
return retVal | ||
} | ||
const getRoute = async ({ file: component }) => { | ||
const getRoute = async ({ file }) => { | ||
const basePath = path | ||
.relative(srcDir, component) | ||
.replace(new RegExp(`${path.extname(component)}$`), '') | ||
.relative(srcDir, file) | ||
.replace(new RegExp(`${path.extname(file)}$`), '') | ||
const urlPath = basePath | ||
@@ -90,5 +51,7 @@ .split(path.sep) | ||
const [routePath, name] = await Promise.all([ | ||
const [routePath, name, meta, component] = await Promise.all([ | ||
mapRoutePath(`/${urlPath}`), | ||
mapRouteName(basePath, component), | ||
mapRouteName(basePath, file), | ||
mapMeta(basePath, file), | ||
mapImport(file), | ||
]) | ||
@@ -100,2 +63,3 @@ | ||
component, | ||
meta, | ||
chunkName, | ||
@@ -117,13 +81,5 @@ name, | ||
const directRoute = await getRoute(message) | ||
const { route, proxyPath } = | ||
['add', 'change', 'unlink'].includes(message.event) && transform | ||
? await transformFile(message, directRoute) | ||
: { route: directRoute } | ||
return { | ||
...message, | ||
route, | ||
proxyPath, | ||
route: await getRoute(message), | ||
} | ||
@@ -133,5 +89,2 @@ } | ||
const fsEvents = observer | ||
.filter(({ file, event }) => { | ||
return event === 'ready' || minimatch(file, glob) | ||
}) | ||
.buffer(readyObs) | ||
@@ -149,3 +102,3 @@ .take(Infinity) | ||
.filter(({ event }) => event !== 'ready') | ||
.forEach(({ event, route, proxyPath }) => { | ||
.forEach(({ event, route }) => { | ||
if (event === 'unlink') { | ||
@@ -156,5 +109,2 @@ routes.unlink(route) | ||
} | ||
if (proxyPath && event === 'unlink') { | ||
fs.unlink(proxyPath) | ||
} | ||
}) | ||
@@ -161,0 +111,0 @@ |
@@ -1,2 +0,2 @@ | ||
import { CHUNK_PREFIX } from './common' | ||
import CHUNK_PREFIX from './chunkPrefix' | ||
@@ -3,0 +3,0 @@ export default function getRoutes(extendRoutes) { |
@@ -33,2 +33,3 @@ import { Observable } from 'rxjs' | ||
setTimeout(() => { | ||
done = true | ||
watcher.close() | ||
@@ -35,0 +36,0 @@ observer.complete() |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
336215
16
2
547
2