@sum.cumo/nuxt-custom-route-folder
Advanced tools
Comparing version 2.0.0 to 2.1.0
# Changelog @sum.cumo/nuxt-custom-route-folder | ||
## Version 2.1.0 | ||
### 🆕 New Features | ||
→ **add import and meta mappers** | ||
> these can be used to use a different file or attach loaders for the route component import | ||
> and also add meta informations to the route | ||
> | ||
> BREAKGIN CHANGE: | ||
> transform api has been removed in favour of custom inline loaders | ||
> migration: rewrite transformers to webpack loaders and apply these using mapImport | ||
> | ||
> | ||
### 🐞 Bug Fixes | ||
→ **re-use same watcher for multiple custom folders** | ||
> in order to hopefully dont cause an segmentation fault again | ||
> | ||
> | ||
→ **use latest version of rxjs** | ||
> and also stop using extendRoutes callback | ||
> (seems not to work as expected in nuxt@2) | ||
> | ||
> | ||
## Version 2.0.0 | ||
@@ -4,0 +32,0 @@ |
@@ -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,10 +18,8 @@ | ||
var _fs = require('fs'); | ||
var _lodash = require('lodash.kebabcase'); | ||
var _fs2 = _interopRequireDefault(_fs); | ||
var _lodash2 = _interopRequireDefault(_lodash); | ||
var _crypto = require('crypto'); | ||
var _rxjs = require('rxjs'); | ||
var _crypto2 = _interopRequireDefault(_crypto); | ||
var _minimatch = require('minimatch'); | ||
@@ -31,8 +29,4 @@ | ||
var _lodash = require('lodash.kebabcase'); | ||
var _operators = require('rxjs/operators'); | ||
var _lodash2 = _interopRequireDefault(_lodash); | ||
var _rxjs = require('rxjs'); | ||
var _observe = require('./observe'); | ||
@@ -46,4 +40,6 @@ | ||
var _common = require('./common'); | ||
var _chunkPrefix = require('./chunkPrefix'); | ||
var _chunkPrefix2 = _interopRequireDefault(_chunkPrefix); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -58,6 +54,8 @@ | ||
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, | ||
@@ -71,2 +69,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, | ||
@@ -76,4 +78,2 @@ filter = _ref$filter === undefined ? function () { | ||
} : _ref$filter, | ||
_ref$transformExt = _ref.transformExt, | ||
transformExt = _ref$transformExt === undefined ? 'js' : _ref$transformExt, | ||
_ref$srcDir = _ref.srcDir, | ||
@@ -84,25 +84,16 @@ srcDir = _ref$srcDir === undefined ? nuxt.options.srcDir : _ref$srcDir, | ||
var routes = (0, _getRoutes2.default)(extendRoutes); | ||
var routes = (0, _getRoutes2.default)(nuxt); | ||
var observer = (0, _observe2.default)(glob, watch); | ||
var watch$ = (0, _observe2.default)(srcDir, watch).pipe((0, _operators.filter)(function (_ref2) { | ||
var event = _ref2.event, | ||
file = _ref2.file; | ||
var readyObs = _rxjs.Observable.create(function (obs) { | ||
var subscription = observer.filter(function (_ref2) { | ||
var event = _ref2.event; | ||
return event === 'ready'; | ||
}).subscribe(function () { | ||
setTimeout(function () { | ||
obs.next(); | ||
obs.complete(); | ||
}, 250); | ||
subscription.unsubscribe(); | ||
}); | ||
}); | ||
return event === 'ready' || (0, _minimatch2.default)(file, glob); | ||
})); | ||
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; | ||
@@ -113,87 +104,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, | ||
@@ -203,12 +129,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); | ||
}; | ||
@@ -218,105 +144,96 @@ }(); | ||
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; | ||
var readyFilter = (0, _operators.filter)(function (_ref8) { | ||
var event = _ref8.event; | ||
return event === 'ready'; | ||
}); | ||
var ready$ = watch$.pipe(readyFilter, (0, _operators.delay)(1)); | ||
var ready$$ = _rxjs.Observable.create(function (obs) { | ||
var sub = ready$.subscribe({ | ||
next: function next() { | ||
obs.complete(); | ||
sub.unsubscribe(); | ||
} | ||
}); | ||
}); | ||
return event === 'ready' || (0, _minimatch2.default)(file, glob); | ||
}).buffer(readyObs).take(Infinity).concatMap(function (messages) { | ||
var fs$ = watch$.pipe((0, _operators.bufferWhen)(function () { | ||
return ready$$; | ||
}), (0, _operators.take)(Infinity), (0, _operators.concatMap)(function (messages) { | ||
return Promise.all(messages.map(processFileMsg)); | ||
}).flatMap(function (m) { | ||
return m; | ||
}).filter(function (m) { | ||
return m; | ||
}); | ||
}), (0, _operators.flatMap)(function (x) { | ||
return x; | ||
}), (0, _operators.filter)(function (x) { | ||
return x; | ||
})); | ||
fsEvents.filter(function (_ref14) { | ||
var event = _ref14.event; | ||
fs$.pipe((0, _operators.filter)(function (_ref9) { | ||
var event = _ref9.event; | ||
return event !== 'ready'; | ||
}).forEach(function (_ref15) { | ||
var event = _ref15.event, | ||
route = _ref15.route, | ||
proxyPath = _ref15.proxyPath; | ||
})).forEach(function (_ref10) { | ||
var event = _ref10.event, | ||
route = _ref10.route; | ||
@@ -328,14 +245,13 @@ 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; | ||
return event === 'ready'; | ||
}).subscribe(function () { | ||
setTimeout(resolve, 0); | ||
}, reject); | ||
var sub = fs$.pipe(readyFilter, (0, _operators.delay)(2)).subscribe({ | ||
next: function next() { | ||
resolve(); | ||
sub.unsubscribe(); | ||
}, | ||
error: reject | ||
}); | ||
}); | ||
@@ -342,0 +258,0 @@ } |
@@ -8,34 +8,59 @@ 'use strict'; | ||
var _common = require('./common'); | ||
var _chunkPrefix = require('./chunkPrefix'); | ||
function getRoutes(extendRoutes) { | ||
var _chunkPrefix2 = _interopRequireDefault(_chunkPrefix); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function getRoutes(nuxt) { | ||
if (!getRoutes.cache) { | ||
var customRoutes = []; | ||
var builder = null; | ||
var initialUpdate = false; | ||
var update = function update() { | ||
extendRoutes(function (routes) { | ||
customRoutes.forEach(function (route) { | ||
if (!routes.includes(route)) { | ||
var conflictingRoute = routes.find(function (_ref) { | ||
var p = _ref.path; | ||
return p === route.path; | ||
}); | ||
if (builder) { | ||
builder.generateRoutesAndFiles(); | ||
} else if (nuxt.options.dev) { | ||
initialUpdate = true; | ||
} | ||
}; | ||
if (!conflictingRoute) { | ||
routes.push(route); | ||
} else if ((conflictingRoute.priority || -1) <= route.priority) { | ||
routes.splice(routes.indexOf(conflictingRoute), 1); | ||
routes.push(route); | ||
} | ||
} | ||
}); | ||
nuxt.hook('build:done', function (b) { | ||
builder = b; | ||
if (initialUpdate) { | ||
// eslint-disable-next-line no-console | ||
console.log('routes got updated in initial build - rebuilding.'); | ||
b.generateRoutesAndFiles(); | ||
} | ||
}); | ||
routes.forEach(function (route) { | ||
if (route.chunkName.indexOf(_common.CHUNK_PREFIX) === 0 && !customRoutes.includes(route)) { | ||
routes.splice(routes.indexOf(route), 1); | ||
nuxt.hook('build:extendRoutes', function (routes) { | ||
if (nuxt.options.dev && !builder) { | ||
return; | ||
} | ||
customRoutes.forEach(function (route) { | ||
if (!routes.includes(route)) { | ||
var conflictingRoute = routes.find(function (_ref) { | ||
var p = _ref.path; | ||
return p === route.path; | ||
}); | ||
if (!conflictingRoute) { | ||
routes.push(route); | ||
} else if ((conflictingRoute.priority || -1) <= route.priority) { | ||
routes.splice(routes.indexOf(conflictingRoute), 1); | ||
routes.push(route); | ||
} | ||
}); | ||
} | ||
}); | ||
}; | ||
routes.forEach(function (route) { | ||
if (route.chunkName.indexOf(_chunkPrefix2.default) === 0 && !customRoutes.includes(route)) { | ||
routes.splice(routes.indexOf(route), 1); | ||
} | ||
}); | ||
}); | ||
getRoutes.cache = { | ||
@@ -42,0 +67,0 @@ add: function add(route) { |
@@ -8,2 +8,6 @@ 'use strict'; | ||
var _path = require('path'); | ||
var path = _interopRequireWildcard(_path); | ||
var _rxjs = require('rxjs'); | ||
@@ -17,9 +21,16 @@ | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } | ||
var cache = {}; | ||
function observe(glob, watch) { | ||
if (!cache[glob]) { | ||
cache[glob] = _rxjs.Observable.create(function (observer) { | ||
var done = false; | ||
var watcher = _chokidar2.default.watch(glob);['add', 'change', 'unlink'].forEach(function (event) { | ||
function observe(srcDir, watch) { | ||
if (!cache[srcDir]) { | ||
var watcher = _chokidar2.default.watch(path.join(srcDir, '**'), { | ||
ignored: path.join(srcDir, '**', 'node_modules', '**') | ||
}); | ||
watcher.setMaxListeners(30); | ||
cache[srcDir] = _rxjs.Observable.create(function (observer) { | ||
var done = false;['add', 'change', 'unlink'].forEach(function (event) { | ||
watcher.on(event, function (file) { | ||
@@ -46,2 +57,3 @@ observer.next({ | ||
setTimeout(function () { | ||
done = true; | ||
watcher.close(); | ||
@@ -61,3 +73,3 @@ observer.complete(); | ||
return cache[glob]; | ||
return cache[srcDir]; | ||
} |
{ | ||
"name": "@sum.cumo/nuxt-custom-route-folder", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"main": "index.js", | ||
@@ -15,3 +15,3 @@ "scripts": { | ||
"minimatch": "^3.0.4", | ||
"rxjs": "^5.5.8" | ||
"rxjs": "^6.2.0" | ||
}, | ||
@@ -18,0 +18,0 @@ "devDependencies": { |
import path from 'path' | ||
import fs from 'fs' | ||
import crypto from 'crypto' | ||
import minimatch from 'minimatch' | ||
import kebabCase from 'lodash.kebabcase' | ||
import { Observable } from 'rxjs' | ||
import minimatch from 'minimatch' | ||
import { | ||
concatMap, | ||
filter as rxFilter, | ||
take, | ||
delay, | ||
flatMap, | ||
bufferWhen, | ||
} from 'rxjs/operators' | ||
import observe from './observe' | ||
import getRoutes from './getRoutes' | ||
import { tmpDir, CHUNK_PREFIX, readFile, writeFile } from './common' | ||
import CHUNK_PREFIX from './chunkPrefix' | ||
@@ -14,68 +20,23 @@ 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, | ||
watch = nuxt.options.dev, | ||
}) { | ||
const routes = getRoutes(extendRoutes) | ||
const routes = getRoutes(nuxt) | ||
const observer = observe(glob, watch) | ||
const watch$ = observe(srcDir, watch).pipe( | ||
rxFilter(({ event, file }) => { | ||
return event === 'ready' || minimatch(file, glob) | ||
}) | ||
) | ||
const readyObs = Observable.create((obs) => { | ||
const subscription = observer | ||
.filter(({ event }) => event === 'ready') | ||
.subscribe(() => { | ||
setTimeout(() => { | ||
obs.next() | ||
obs.complete() | ||
}, 250) | ||
subscription.unsubscribe() | ||
}) | ||
}) | ||
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 | ||
@@ -88,5 +49,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), | ||
]) | ||
@@ -98,2 +61,3 @@ | ||
component, | ||
meta, | ||
chunkName, | ||
@@ -115,33 +79,30 @@ 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), | ||
} | ||
} | ||
const fsEvents = observer | ||
.filter(({ file, event }) => { | ||
return event === 'ready' || minimatch(file, glob) | ||
const readyFilter = rxFilter(({ event }) => event === 'ready') | ||
const ready$ = watch$.pipe(readyFilter, delay(1)) | ||
const ready$$ = Observable.create((obs) => { | ||
const sub = ready$.subscribe({ | ||
next() { | ||
obs.complete() | ||
sub.unsubscribe() | ||
}, | ||
}) | ||
.buffer(readyObs) | ||
.take(Infinity) | ||
.concatMap((messages) => { | ||
return Promise.all(messages.map(processFileMsg)) | ||
}) | ||
.flatMap((m) => { | ||
return m | ||
}) | ||
.filter((m) => m) | ||
}) | ||
fsEvents | ||
.filter(({ event }) => event !== 'ready') | ||
.forEach(({ event, route, proxyPath }) => { | ||
const fs$ = watch$.pipe( | ||
bufferWhen(() => ready$$), | ||
take(Infinity), | ||
concatMap((messages) => Promise.all(messages.map(processFileMsg))), | ||
flatMap((x) => x), | ||
rxFilter((x) => x) | ||
) | ||
fs$ | ||
.pipe(rxFilter(({ event }) => event !== 'ready')) | ||
.forEach(({ event, route }) => { | ||
if (event === 'unlink') { | ||
@@ -152,11 +113,12 @@ routes.unlink(route) | ||
} | ||
if (proxyPath && event === 'unlink') { | ||
fs.unlink(proxyPath) | ||
} | ||
}) | ||
return new Promise((resolve, reject) => { | ||
fsEvents.filter(({ event }) => event === 'ready').subscribe(() => { | ||
setTimeout(resolve, 0) | ||
}, reject) | ||
const sub = fs$.pipe(readyFilter, delay(2)).subscribe({ | ||
next() { | ||
resolve() | ||
sub.unsubscribe() | ||
}, | ||
error: reject, | ||
}) | ||
}) | ||
@@ -163,0 +125,0 @@ } |
@@ -1,35 +0,56 @@ | ||
import { CHUNK_PREFIX } from './common' | ||
import CHUNK_PREFIX from './chunkPrefix' | ||
export default function getRoutes(extendRoutes) { | ||
export default function getRoutes(nuxt) { | ||
if (!getRoutes.cache) { | ||
const customRoutes = [] | ||
let builder = null | ||
let initialUpdate = false | ||
const update = () => { | ||
extendRoutes((routes) => { | ||
customRoutes.forEach((route) => { | ||
if (!routes.includes(route)) { | ||
const conflictingRoute = routes.find( | ||
({ path: p }) => p === route.path | ||
) | ||
if (builder) { | ||
builder.generateRoutesAndFiles() | ||
} else if (nuxt.options.dev) { | ||
initialUpdate = true | ||
} | ||
} | ||
if (!conflictingRoute) { | ||
routes.push(route) | ||
} else if ((conflictingRoute.priority || -1) <= route.priority) { | ||
routes.splice(routes.indexOf(conflictingRoute), 1) | ||
routes.push(route) | ||
} | ||
} | ||
}) | ||
nuxt.hook('build:done', (b) => { | ||
builder = b | ||
if (initialUpdate) { | ||
// eslint-disable-next-line no-console | ||
console.log('routes got updated in initial build - rebuilding.') | ||
b.generateRoutesAndFiles() | ||
} | ||
}) | ||
routes.forEach((route) => { | ||
if ( | ||
route.chunkName.indexOf(CHUNK_PREFIX) === 0 && | ||
!customRoutes.includes(route) | ||
) { | ||
routes.splice(routes.indexOf(route), 1) | ||
nuxt.hook('build:extendRoutes', (routes) => { | ||
if (nuxt.options.dev && !builder) { | ||
return | ||
} | ||
customRoutes.forEach((route) => { | ||
if (!routes.includes(route)) { | ||
const conflictingRoute = routes.find( | ||
({ path: p }) => p === route.path | ||
) | ||
if (!conflictingRoute) { | ||
routes.push(route) | ||
} else if ((conflictingRoute.priority || -1) <= route.priority) { | ||
routes.splice(routes.indexOf(conflictingRoute), 1) | ||
routes.push(route) | ||
} | ||
}) | ||
} | ||
}) | ||
} | ||
routes.forEach((route) => { | ||
if ( | ||
route.chunkName.indexOf(CHUNK_PREFIX) === 0 && | ||
!customRoutes.includes(route) | ||
) { | ||
routes.splice(routes.indexOf(route), 1) | ||
} | ||
}) | ||
}) | ||
getRoutes.cache = { | ||
@@ -36,0 +57,0 @@ add(route) { |
@@ -0,1 +1,2 @@ | ||
import * as path from 'path' | ||
import { Observable } from 'rxjs' | ||
@@ -6,7 +7,12 @@ import chokidar from 'chokidar' | ||
export default function observe(glob, watch) { | ||
if (!cache[glob]) { | ||
cache[glob] = Observable.create((observer) => { | ||
export default function observe(srcDir, watch) { | ||
if (!cache[srcDir]) { | ||
const watcher = chokidar.watch(path.join(srcDir, '**'), { | ||
ignored: path.join(srcDir, '**', 'node_modules', '**'), | ||
}) | ||
watcher.setMaxListeners(30) | ||
cache[srcDir] = Observable.create((observer) => { | ||
let done = false | ||
const watcher = chokidar.watch(glob) | ||
;['add', 'change', 'unlink'].forEach((event) => { | ||
@@ -34,2 +40,3 @@ watcher.on(event, (file) => { | ||
setTimeout(() => { | ||
done = true | ||
watcher.close() | ||
@@ -49,3 +56,3 @@ observer.complete() | ||
return cache[glob] | ||
return cache[srcDir] | ||
} |
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
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
339173
16
2
615
+ Addedrxjs@6.6.7(transitive)
+ Addedtslib@1.14.1(transitive)
- Removedrxjs@5.5.12(transitive)
- Removedsymbol-observable@1.0.1(transitive)
Updatedrxjs@^6.2.0