1.0.4 • Published 5 years ago

file-push-webpack-plugin v1.0.4

Weekly downloads
5
License
MIT
Repository
github
Last release
5 years ago

This is webpack plugin for sending a zip file to server.

Usage

Example

module.export = {
  ...
  plugins: [
    ...
    new FilePushWebpackPlugin({
      url: 'the url you want to send the zip',
      regex: /.*\.map$/,
      shouldRemoveFiles: true,
    }),
  ]
}

Accepting Params

paramdetaildefault value
regexfind files with a regex.null
shouldRemoveFilesdelete certain files after uploading zip-filetrue
urlthe url which your zip-file will be sent tonull
zipFileNamename for zip filetemp.zip
successsuccess callbacknull
failerror callbacknull

For Koa user

If you use koa as your node server, the example below may help you to get the zip-file.

Maybe you use koa-body or others lib that can parse formdata. And, you may need a tool to decompress your zip. I recommend the adm-zip for your first choice.

I assume that you use koa-body and adm-zip.

const router = require('koa-router')();
const koaBody = require('koa-body');
const fs = require('fs');
const admZip = require('adm-zip');

router.post('/xxx', koaBody({
  multipart: true,
  formidable: {
      maxFileSize: 2000*1024*1024,	// you decide
  }
}), async (ctx) => {
  // This Plugin will send the file with name 'zipFile'
  const { zipFile } = ctx.request.files;
  if (!zipFile) {
    throw Error('no zip');
  }
  try {
    const [filename] = zipFile.name.split('.');
    const zip = new admZip(zipFile.path);
    const dir = `./${filename}`;
    if (!fs.existsSync(dir)) {
      fs.mkdirSync(dir);
    }
    zip.extractAllTo(dir);
  } catch (e) {
    console.log(e);
  }
});

For Express user

const multipart = require('connect-multiparty');
const multipartMiddleware = multipart();
app.use('/xxx', multipartMiddleware, function(req, res) {
  const { zipFile } = req.files;
  if (!zipFile) {
    throw Error('no zip');
  }
  try {
    const { path: filePath, originalFilename } = zipFile;
    const [filename] = originalFilename.split('.');
    const zip = new admZip(filePath);
    const dir = path.join(__dirname, `${filename}`);
    if (!fs.existsSync(dir)) {
      fs.mkdirSync(dir);
    }
    zip.extractAllTo(dir);
    fs.unlinkSync(filePath);
  } catch (e) {
    console.log(e);
  }
  res.end();
});

License

MIT.

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

6 years ago