Skip to content

Instantly share code, notes, and snippets.

@juanpablocs
Created December 21, 2017 20:12
Show Gist options
  • Save juanpablocs/65b8e82cc249b0fa4ef9f296d848b521 to your computer and use it in GitHub Desktop.
Save juanpablocs/65b8e82cc249b0fa4ef9f296d848b521 to your computer and use it in GitHub Desktop.

task:

const fs = require('fs');
const param = process.env.page;
const webpackFile = './webpack.config.js';

const msgErr = (msg)=>{
	console.log(msg);
	process.exit();
}

if(!param) {
	msgErr('param --page is required');
}

let webpackContent = fs.readFileSync(webpackFile).toString();

if(webpackContent.match(new RegExp(`entry\\['${param}'\\]`))) {
	console.log(`Page exist: ${param}\n`);
	console.log('webpack:', webpackContent);
	process.exit();
}

let webpackNewContent = webpackContent.replace('entry = {};', `entry = {};\nentry['${param}'] = './${param}.js';`);

fs.writeFileSync(`./${param}.js`,'');
fs.writeFileSync(webpackFile, webpackNewContent);

console.log(`Page create: ${param}\n`);
console.log(webpackNewContent);

webpack config file

const entry = {};
module.exports = {
	entry: entry,
	output:{
		filename:'./[name].bundle.js'
	},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment