is it possible to configure content in the html file when using requirejs optimizer? - node.js

I see it is possible to get the contents of the js files using onBuildWrite but I am needing a way to inject values into our root html page, is this possible? EG we would like to swap out our less file for a css version.

It's possible with node.js:
var fs = require('fs');
// read html file
var fileContents = fs.readFileSync('index.html', 'utf8');
// replace rel attribute for less file with rel for css
fileContents = fileContents.replace(/stylesheet\/less/gi, 'stylesheet');
// replace file name
fileContents = fileContents.replace(/css\/application.less/gi, 'css/application.css');
// remove less runtime compiler (if needed)
fileContents = fileContents.replace(/<script.*?\bless\b[^"']*?\.js.*?<\/script>/g, '');
// write file back
fs.writeFileSync('index.html', fileContents, 'utf8');
Just add that as a part of your build script, along with calling r.js.

Related

how can I extract the name of a webpack chunk along with its hash to a external file like manifest.json

I'm using the splitChunks optimization setting and also the chunkFilename property like so chunkFilename: '[id].[chunkhash].js' to give each chunk a hash. Is there a way to reference the chunked file by a common name so that I can dynamically update my pages when the hash changes?
something like:
manifest.json
{
vendorFile: venderFile.1902tjg.js
}
Where I can pull in that file and reference it in my template file
index.pug
<script src=#{manfiest.vendorFile} />
If you have a folder with your webpack build result like:
venderFile.1902tjg.js
some.css
etc.etc
Which is ready before the start of your nodejs + pug templates, you could create your manifest.json with these steps:
Get the unique .js file name in your build folder. If there are several .js files, use a kind of regex
var path = require('path');
var fs = require('fs');
var targetFiles = fs.readdirSync(__dirname).filter(function(file) {
return path.extname(file).toLowerCase() === '.js';
});
create your manifest.json using the unique file found
var fs = require('fs');
var obj = {vendorFile: targetFiles[0]};
fs.writeFileSync('../../manifest.json', JSON.stringify(obj));
You could use the previous snippet as a package.json script
"scripts": {
"manifest": "node update_manifest.json"
}
Or just as a code before the start of your express application.

Read all JSON files contained in a dynamically updated folder

I've got multiple json files contained within a directory that will dynamically be updated by users. The users can add categories which will create new json files in that directory, and they can also remove categories which would delete json files in that directory. I'm looking for a method to read all json files contained in that folder directory, and push all the json files into a single object array. I imagine asynchronously would be desirable too.
I'm very new to using fs. I've management to read single json files by directory using
const fs = require('fs');
let data = fs.readFileSync('./sw_lbi/categories/category1.json');
let categories = JSON.parse(data);
console.log(categories);
But of course this will only solve the synchronous issue when using require()
As I'll have no idea what json files will be contained in the directory because the users will also name them, I'll need a way to read all the json files by simply calling the folder directory which contains them.
I'm imagining something like this (which obviously is foolish)
const fs = require('fs');
let data = fs.readFileSync('./sw_lbi/categories');
let categories = JSON.parse(data);
console.log(categories);
What would be the best approach to achieve this?
Thanks in advance.
First of all you need to scan this directory for files, next you need to filter them and select only JSONs, and at the end just read every file and do what you need to do
const fs = require('fs');
const path = require('path')
const jsonsInDir = fs.readdirSync('./sw_lbi/categories').filter(file => path.extname(file) === '.json');
jsonsInDir.forEach(file => {
const fileData = fs.readFileSync(path.join('./sw_lbi/categories', file));
const json = JSON.parse(fileData.toString());
});

NodeJS Read File Name is Japanese Characters

I have a file name is momo1.json. I can read them in Nodejs
var fs = require('fs');
var jsonWelcome = fs.readFileSync(WELCOME_JSON, UTF8_FORMAT);
var dataWelcome = JSON.parse(jsonWelcome);
But now i change file name is ももたろう.json. I cant read file anymore, tell my why and solutions i can read file name by japanese charaters
Try to use require() instead:
var dataWelcome = require("./ももたろう.json");
//output the data to see if it works
console.log(JSON.stringify(dataWelcome));
Live Demo

Can't write/append to JSON file in Node Webkit

I want to have persistent memory (store the user's progress) in a .json file in %AppData%. I tried doing this according to this post, but it doesn't work. For testing purposes I'm only working with storing one object.
The code below doesn't work at all. If I use fs.open(filePath, "w", function(err, data) { ... instead of readFile(..., it does create a json file in %AppData%, but then it doesn't write anything to it, it's always 0 bytes.
var nw = require('nw.gui');
var fs = require('fs');
var path = require('path');
var file = "userdata.json";
var filePath = path.join(nw.App.dataPath, file);
console.log(filePath); // <- This shows correct path in Application Data.
fs.readFile(filePath ,function (err, data) {
var idVar = "1";
var json = JSON.parse(data);
json.push("id :" + idVar);
fs.writeFile(filePath, JSON.stringify(json));
});
If anyone has any idea where I'm messing this up, I'd be grateful..
EDIT:
Solved, thanks to kailniris.
I was simply trying to parse an empty file
There is no json in the file you try to read. Before parsing data check if the file is empty. If it is then create an empty json, push the new data into it then write it to the file else parse the json in the file.

Parse.com Node js failed to load json file

I'm trying to read a json file using node js/ express and deploying it to parseCloud but i keep getting
*Failed to load filters.json with: Could not find file filters.json *
here is my code:
var fs = require('fs');
var obj = JSON.parse(fs.readFileSync('cloud/filters.json', 'utf8'));
or this
var filterJson = require('cloud/filters.json');
thanks
Looks like parse.com doesn't allow .json files. You can save your file as .js and load it as plain text file (doesn't work with require()).
var fs = require('fs');
var parsedObject = JSON.parse(fs.readFileSync('cloud/path/json_file.js'));
It looks ugly, but works for me. :)
Try to add ./
fs.readFileSync('./cloud/filters.json', 'utf8')

Resources