I'd like to assemble a manifest.json
file, as used by Chrome extensions, in a "smarter," programmatic sort of way. I'm using npm for my dependency resolution, and its package.json
includes some shared fields with the manifest.json
file, including "name," "description," and "version."
Is there a way to define something like a partial manifest.json
file which includes all the Chrome-specific stuff, but fill in shared values where appropriate? I've found that this is pretty straightforward in Gulp:
var gulp = require('gulp'); var fs = require('fs'); var jeditor = require('gulp-json-editor'); gulp.task('manifest', function() { var pkg = JSON.parse(fs.readFileSync('./package.json')); gulp.src('./manifest.json') .pipe(jeditor({ 'name': pkg.name, 'description': pkg.description, 'version': pkg.version, 'author': pkg.author, 'homepage_url': pkg.homepage, })) .pipe(gulp.dest("./dist")); });
Even if there's some npm package out there designed for this purpose, can someone explain to me how something like this might be done generally? I know Webpack 2 has a built-in json loader, but I'm not clear how it would be used in a case like this.
You can bundle your JavaScript using the CLI command by providing an entry file and output path. Webpack will automatically resolve all dependencies from import and require and bundle them into a single output together with your app's script.
public class JSONLoader extends AbstractFileLoader implements BatchConverter, URLSourcedLoader. Reads a source that is in the JSON format. It automatically decompresses the data if the extension is '. json. gz'.
Webpack is used to compile JavaScript modules. Once installed, you can interact with webpack either from its CLI or API. If you're still new to webpack, please read through the core concepts and this comparison to learn why you might use it over the other tools that are out in the community.
There is actually a more elegant solution than the one by @user108471 (although it is inspired by it), and that is to use the copy-webpack-plugin
. With its transform
ability, you can add the desired values to the manifest.json
on the fly before copying it to its destination.
It has two advantages:
manifest.js
-bundle (@bronson's solution also fixes this)require
the manifest.json
in some other .js
-file (which would seem semantically backwards to me)A minimal setup could be this:
webpack.config.js
// you can just require .json, saves the 'fs'-hassle let package = require('./package.json'); function modify(buffer) { // copy-webpack-plugin passes a buffer var manifest = JSON.parse(buffer.toString()); // make any modifications you like, such as manifest.version = package.version; // pretty print to JSON with two spaces manifest_JSON = JSON.stringify(manifest, null, 2); return manifest_JSON; } module.exports = { // ... plugins: [ new CopyWebpackPlugin([ { from: "./src/manifest.json", to: "./dist/manifest.json", transform (content, path) { return modify(content) } }]) ] }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With