Using require as if is running 'cd ./folder && node file.js' - javascript

I have the following structure
├── testFolder
└──subTestFolder
test.ts
index.ts
└── foo
└── beta.ts
├──text.json
Based in this, I'm trying to require the test.js inside beta.js based in test.js absolute path.
But when I ran ts-node ./foo/beta.js, it throws Cannot find module '../text.json
test.js Content:
require("../index.js");
beta.js content:
const file = 'D:\testFolder\subTestFolder\test.js';
const dir = path.dirname(file);
process.chdir(dir);
require(file);
index.ts
const json= fs.readFileSync('../corde.json').toString();

Related

gulp-eslint not linting .js files inside of a dot directory

I have .js files inside of a dot directory that are not being linted by gulp-eslint.
Example: .foo/file1.js
I've confirmed that the glob is picking up the files inside of the dot directory.
gulp-eslint is passing successfully for the files inside of a parent dot directory even when an intentional error is introduced inside these files.
I've confirmed that directories without a . in the name of the directory (e.g. src/file.js, etc.) are failing linting, when the same intentional error is introduced.
My project structure is something like this:
project/
│
├── .foo/
│ ├──file1.js
│ └──file2.js
│
├── src/
│ ├──file1.js
│ └──file2.js
│
├── gulpfile.js
└── .eslintrc
Contents of gulpfile.js
const gulp = require('gulp');
const eslint = require('gulp-eslint');
gulp.task('lint', () => {
return gulp.src([ './src/**/*.js', './.foo/**/*.js' ])
.pipe(eslint({
configFile: './.eslintrc'
}))
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
Contents of .eslintrc
// Reducing down to a single, simple rule
{
"env": {
"es6": true
},
"rules": {
"quotes": [
"error",
"single"
]
}
}
Is there something incorrect in my config that is preventing the .js files inside of the dot directory .foo from being linted?
Thanks!
It looks to be a known "quirk" of eslint (as of 6.8.0).
The workaround (until a PR is merged to fix this) is to use an .eslintignore file to unignore dot directories explicitly:
#.eslintignore
!.foo

serverless slsw.lib.entries is always empty

I'm trying to deploy to Lambda using serverless. In my webpack.config.js I have the following entry:
const slsw = require('serverless-webpack')
module.exports = {
entry: slsw.lib.entries,
...
}
in my serverless.yml I declare the function as following:
functions:
importUsersFromS3:
handler: handlers.handler
and handlers.js looks like this:
exports.handler = async function (event, context) {
awaitcaptureAsyncFunc('handleSplitSpreadsheet', () => {
context.callbackWaitsForEmptyEventLoop = false
log('event', { event })
processLargeSpreadsheet(event, context)
})
}
when I run serverless webpack --out dist --stage dev --region us-east-1 I get the following error:
Webpack Options Validation Error -----------------------
WebpackOptionsValidationError: Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
- configuration.entry should be an non-empty object.
-> Multiple entry bundles are created. The key is the chunk name. The value can be a string or an array.
at webpack (/src/node_modules/webpack/lib/webpack.js:31:9)
at ServerlessWebpack.compile (/src/lambda/usersApi/node_modules/serverless-webpack/lib/compile.js:12:22)
From previous event:
at PluginManager.invoke (/usr/lib/node_modules/serverless/lib/classes/PluginManager.js:464:22)
at PluginManager.spawn (/usr/lib/node_modules/serverless/lib/classes/PluginManager.js:484:17)
at ServerlessWebpack.BbPromise.bind.then.then (/src/lambda/usersApi/node_modules/serverless-webpack/index.js:150:51)
at runCallback (timers.js:810:20)
at tryOnImmediate (timers.js:768:5)
at processImmediate [as _immediateCallback] (timers.js:745:
So my folder structure was like this
app
├── lambda
| ├── usersApi
| | ├── handlers.js
| | ├── serverless.yml
| | └── package.json
| ├── severalServiceFolders
| └── webpack.config.js
└── package.json
In app/lambda/usersApi/package.json I had these devDependencies:
"serverless-plugin-aws-alerts": "1.2.4",
"serverless-plugin-tracing": "^2.0.0",
"serverless-webpack": "^5.3.1"
turns out removing them made the error go away.
only reference to serverless in root package.json is "serverless-webpack": "^5.3.1"

Why does a globbed `find` command in Node (execSync) return different results to the CLI?

Example:
╔═ markl#macbook: /var/folders/xp/n5tbdrrs761ck82qqychcf61ptmq9d/T
╚═ ♪ tree tmp.7cHYDVc8rX
tmp.7cHYDVc8rX
├── file
└── subdir
├── anothersubdir
│   └── file
└── file
2 directories, 3 files
╔═ markl#macbook: /var/folders/xp/n5tbdrrs761ck82qqychcf61ptmq9d/T
╚═ ♪ find ./**/*/file -type f
./tmp.7cHYDVc8rX/file
./tmp.7cHYDVc8rX/subdir/anothersubdir/file
./tmp.7cHYDVc8rX/subdir/file
╔═ markl#macbook: /var/folders/xp/n5tbdrrs761ck82qqychcf61ptmq9d/T
╚═ ♪ node
> require('child_process').execSync("find ./**/*/file -type f").toString()
'./tmp.7cHYDVc8rX/subdir/file\n'
> %
╔═ markl#macbook: /var/folders/xp/n5tbdrrs761ck82qqychcf61ptmq9d/T
╚═ ♪ node -v
v7.9.0
You can see there are 3 files I expect it to find (as the CLI does), but in the node repl, it only returns the last file 🤔

Webpack bundle named requirejs modules

In my company we have a significant AMD RequireJS code base and I am trying to use webpack to bundle some of it.
Here is what the architecture looks like:
somedir
│ app.js
│
└───anotherdir
|
├─── module1
│ file1.js
│ file2.js
│
├─── module2
│ file3.js
│
└─── module3
file4.js
file5.js
file6.js
Each file is written like that :
define('ATAG/MODULE/ID, ['somedeps'], function (somedeps) {});
So for instance file1.js could look like
define('ATAG/module1/file1, [], function () {});
And we have a RequireJS config which maps ATAG to anotherdir and we possibly have some more config for different tags.
Now I am trying to create a bundle from app.js with webpack but I have no idea of how to replicate the behavior we have with require.config({ paths: { ATAG: 'anotherdir' } }).
So far my attempts with resolve.alias have not been successful.
Is it even possible to achieve something like this with webpack based on our usage of RequireJS (not requiring relative paths) ?
Thank you.
You were on the right path with resolve.alias:
// foo.js
var a = require('ATAG'); // resolved to anotherdir/index.js
var b = require('ATAG/bar'); // resolved to anotherdir/bar.js
// webpack.config.js
const path = require('path');
module.exports = {
entry: './foo',
output: {
filename: 'bundle.js'
},
resolve: {
alias: {
ATAG: path.join(__dirname, '/anotherdir')
}
}
};

Gulp.js: how to rewrite relative paths?

Structure:
static
├── build
│ ├── css
│ ├── fonts
│ ├── img
│ └── js
└── src
├── blocks
├── fonts
└── img
Piece of gulpfile.js:
var path = {
build: {
js: 'static/build/js',
css: 'static/build/css',
fonts: 'static/build/fonts',
img: 'static/build/img'
},
src: {
vendor_fonts: ['bower_components/**/*.{svg,woff,eot,ttf}', 'semantic/**/*.{svg,woff,eot,ttf}'],
vendor_img: ['bower_components/**/*.{png,jpg,jpeg,gif}', 'semantic/**/*.{png,jpg,jpeg,gif}']
}
};
gulp.task('vendor:img', function(){
return gulp.src(path.src.vendor_img)
.pipe(imagemin({
progressive: true,
interlaced: true,
use: [pngguant()]
}))
.pipe(gulp.dest(path.build.img))
});
gulp.task('vendor:fonts', function() {
gulp.src(path.src.vendor_fonts)
.pipe(gulp.dest(path.build.fonts))
});
When i build 3-party packages (such as fotorama or semantic ui), they have a relative paths - as a result, main.css have only relative paths and server cant't find them.
How i can solve this?
If your gulpfile.jss is in your root you should be able to just prefix your paths with nodes Global Object __dirname
__dirname#
{String}
The name of the directory that the currently executing script resides in.
Example: running node example.js from /Users/mjr
console.log(__dirname);
// /Users/mjr
__dirname isn't actually a global but rather local to each module.
https://nodejs.org/api/globals.html#globals_dirname
So if your gulpfile was in your root in your paths just do
__dirname + "/build/whatever/whatever";
This all being if I understand your question correctly.

Categories