jaredpalmer / jaredpalmer/backpack

Can use fs.readFile ?

Open
#102 7 comments 5 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
4.4k
Forks
164
PR merge metrics
No merged PRs in 30d

Description

I use backpack with [sequelize](http://docs.sequelizejs.com/) .
This code generate file like

```javascript
var fs = require('fs');
var path = require('path');
var Sequelize = require('sequelize');
var basename = path.basename(__filename);
var env = process.env.NODE_ENV || 'development';
var config = require(__dirname + '/../config/config.json')[env];
var db = {};

if (config.use_env_variable) {
var sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
var sequelize = new Sequelize(config.database, config.username, config.password, config);
}

fs.readdirSync(__dirname).filter(function(file) {
var noExtFileName = path.basename(__filename, '.js')
return (file.indexOf('.') !== 0) && !(file.match(noExtFileName))
}).forEach(function(file) {
if (file.slice(-3) !== '.js') return;
var model = sequelize['import'](path.join(__dirname, file));
db[model.name] = model;
});
```
and this code needs reading static files.

These code doesn't work after build because __dirname points relative path from build path which differ from pre-located .

This happens as well as other codes using reading files. Is there any idea?
Js dynamic import case , I resolve this situation by using require.context
like `require.context(__dirname ,false,/\.\/(?!index).*\.js$/)`.

But, it's uneasy to manage codes. Is there any idea?

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the generated Sequelize model-loader and static-file reads after a Backpack build, comparing __dirname before and after compilation. Review Backpack's build configuration and the mentioned require.context approach; done should mean a documented or implemented way to access these files after building.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js, webpack
Domain
build-system
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.