wilsonlewis / require-context Goto Github PK
View Code? Open in Web Editor NEWRequire a group of modules for Node.js using Webpack syntax.
Require a group of modules for Node.js using Webpack syntax.
My Karma config does not transpile my node_modules directory. Switching the let to a var should allow testing in es5.
Line 5 in e00c4d3
If an empty folder exists inside the recursed structure it causes this error:
node_modules/node-dir/lib/paths.js:92
results.files = results.files.concat(res.files);
TypeError: Cannot read property 'files' of undefined
at subloop (/git/mobile.reactnative.templateapp/node_modules/node-dir/lib/paths.js:92:57)
at /git/mobile.reactnative.templateapp/node_modules/node-dir/lib/paths.js:107:13
Error reproduced on MAC with node 6 and node 8
I noticed in index.js at 10 line:
basepath = path.join(__dirname, directory)
My node resolves __dirname as relative path of require-context's directory. For me it's unexpected behaviour, I think when I use relative path it should be relative of file where I call requireContext function. Is it true?
I have a use case for allowing multiple directories, which you can read about a the vuejs-localization github repository. However, my fork is throwing up Critical dependency: require function is used in a way in which dependencies cannot be statically extracted
... and I'm not sure why. I revised it to test in the browser and it seems to work just fine:
c = function(d, recursive, regExp) {
if (!Array.isArray(d)) {
d = [d]
}
const keysMap = {}
keysMap.keys = []
keysMap.directories = {}
d.forEach(directory => {
let basepath = directory
const dirKeys = dir[basepath]
.filter(file => file.match(regExp || /\.(json|js)$/))
.map(file => '.' + '/' + file.slice(basepath.length + 1))
dirKeys.forEach(file => keysMap.directories[file] = directory)
keysMap.keys = keysMap.keys.concat(dirKeys)
})
const context = function(key) {
return context.resolve(key)
}
context.keysMap = keysMap
context.resolve = function(key) {
return keysMap.directories[key] + '/' + key
}
context.keys = function() {
return keysMap.keys
}
return context
}
dir = { a: ['a/a.en', 'a/b.js'], 'c/a': ['c/a/c.js'], b: ['b/abc'] }
r = c(['a', 'c/a', 'b'], true)
r('./b.js')
//VM11433:1 "a/./b.js"
r.keys().forEach(key => console.log(r(key)))
//VM11433:1 a/./b.js
//VM11433:1 c/a/./c.js
This doesn't work. It throws error
const direc = `../posts${directory}`;
require.context(direc, false, /\.test\.js$/);
Any workaround for a dynamic directory?
I have a big project where i need to import some files with some extensions. Something like that:
-src
* DirA
* DirAA
* FileToGet.ext
* DirBB
* AlotOfFilesAndDirectoriesThatIDontWantToLoad (such as node_modules)
* DirB
*DirBB
....
So, when i do require.ensure('./src', true, /ext$/)
it gets the right file. The problem is that it becomes too slow because this require is maping all folders structures.
However you can set false if you dont want to look in subdirectories, but i need to search inside level 1 subdirectories. ¿Maybe could be 2nd parameter a number indicating depth instead of a boolean?. 0 stills being false and other numbers still being true.
Mismatch between readme and actual API?
In the docs you said I could do...
require.context('./menues', false, /\.\/menu(\d+)\.jsx?$/i)
But you are not exporting context
Using a trailing slash on a dir name like
require.context("./test/", false, /\.test\.js$/);
causes the first character of keys()
to be dropped
Since there are a lot of outstanding issues and pull requests, and the repository is clearly not maintained, I'd like to offer my services and take over the repo/package. If this won't be possible in a considerable amount of time, I'll have to fork it and release a new package. Thanks for understanding!
I would like to exclude directories.
How would I update the following line to import all the stories from components/**
, except stories from components/evil-component-1
and components/evil-component-2
?
const req = require.context('../components/', true, /.stories.js$/);
Please add OSS compatible license :)
This is what I'm trying to do.
loading this into mocha to find tests:
require('coffee-babel');
requireContext = require('require-context');
const testsContext = requireContext('./src', true, /.test.coffee$/)
testsContext.keys().forEach(testsContext);
This is the error I get:
fs.js:965
binding.stat(pathModule.toNamespacedPath(path));
^
Error: ENOENT: no such file or directory, stat '/Users/jameshessin/local/websites/com.grillbrickstudios/node_modules/require-context/src'
at Object.fs.statSync (fs.js:965:11)
at Object.files (/Users/jameshessin/local/websites/com.grillbrickstudios/node_modules/node-dir/lib/paths.js:161:21)
at module.exports (/Users/jameshessin/local/websites/com.grillbrickstudios/node_modules/require-context/index.js:17:6)
at Object.<anonymous> (/Users/jameshessin/local/websites/com.grillbrickstudios/test/index.js:4:22)
at Module._compile (internal/modules/cjs/loader.js:654:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:665:10)
at Module.load (internal/modules/cjs/loader.js:566:32)
at tryModuleLoad (internal/modules/cjs/loader.js:506:12)
at Function.Module._load (internal/modules/cjs/loader.js:498:3)
at Module.require (internal/modules/cjs/loader.js:598:17)
at require (internal/modules/cjs/helpers.js:11:18)
at /Users/jameshessin/.config/yarn/global/node_modules/mocha/lib/mocha.js:231:27
at Array.forEach (<anonymous>:null:null)
at Mocha.loadFiles (/Users/jameshessin/.config/yarn/global/node_modules/mocha/lib/mocha.js:228:14)
at Mocha.run (/Users/jameshessin/.config/yarn/global/node_modules/mocha/lib/mocha.js:536:10)
at Object.<anonymous> (/Users/jameshessin/.config/yarn/global/node_modules/mocha/bin/_mocha:582:18)
at Module._compile (internal/modules/cjs/loader.js:654:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:665:10)
at Module.load (internal/modules/cjs/loader.js:566:32)
at tryModuleLoad (internal/modules/cjs/loader.js:506:12)
at Function.Module._load (internal/modules/cjs/loader.js:498:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:695:10)
at startup (internal/bootstrap/node.js:201:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:516:3)
I am using the latest version:
node: 9.11.1
npm: 5.6.0
yarn: 1.5.1
macOS: 10.13.4
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.