Skip to content

Commit

Permalink
webpack plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
AMontagu committed Feb 5, 2020
1 parent c97511b commit f0e96f3
Show file tree
Hide file tree
Showing 15 changed files with 11,786 additions and 0 deletions.
72 changes: 72 additions & 0 deletions simple-webpack-plugin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
'use strict';
const zip = new require('node-zip')();
const path = require('path');
const fs = require('fs');

class BuildError {
constructor(name, message) {
this.name = name;
this.message = message;
this.type = "HeaseWebpackPluginError";
}
}

/**
* Find all files inside a dir, recursively.
* @function getAllFiles
* @param {string} dir Dir path string.
* @return {string[]} Array with all file names that are inside the directory.
*/
const getAllFiles = dir =>
fs.readdirSync(dir).reduce((files, file) => {
const name = path.join(dir, file);
const isDirectory = fs.statSync(name).isDirectory();
return isDirectory ? [...files, ...getAllFiles(name)] : [...files, name];
}, []);


class SimpleWebpackPlugin {
constructor(options) {
this.options = options || {};

if (!this.options.buildPath) {
this.options.buildPath = "dist";
}
}

apply(compiler) {
compiler.hooks.entryOption.tap("SimpleWebpackPluginGetEntry", (context, entry) => {
console.log(context, entry)
if (!this.options.context) {
this.options.context = context;
}else{
this.options.context = path.resolve(context, this.options.context);
}

this.entry = entry;
});

compiler.hooks.emit.tap("SimpleWebpackPluginEmitBuild", (compilation) => {
try {
this.buildAppZip(this.entry)
} catch (e) {
compilation.errors.push(new Error(`${e.type}: ${e.name} -> ${e.message}`));
}
});
}

buildAppZip() {
if (!fs.existsSync(this.options.buildPath)) {
fs.mkdirSync(this.options.buildPath);
}

const allFiles = getAllFiles(path.join(this.options.context, "src"));
allFiles.forEach(file => {
zip.file(file.substring(this.options.context.length + 1, file.length), fs.readFileSync(file));
})
const data = zip.generate({base64: false, compression: 'DEFLATE'});
fs.writeFileSync(path.join(this.options.buildPath, `archive.zip`), data, 'binary');
}
}

module.exports = SimpleWebpackPlugin;
29 changes: 29 additions & 0 deletions simple-webpack-plugin/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions simple-webpack-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "simple-webpack-plugin",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": ""
},
"keywords": [],
"author": "",
"dependencies": {
"node-zip": "^1.1.1"
}
}
21 changes: 21 additions & 0 deletions use-custom-webpack-plugin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
.DS_Store
node_modules
/dist

# local env files
.env.local
.env.*.local

# Log files
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Editor directories and files
.idea
.vscode
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
29 changes: 29 additions & 0 deletions use-custom-webpack-plugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# use-custom-webpack-plugin

## Project setup
```
npm install
```

### Compiles and hot-reloads for development
```
npm run serve
```

### Compiles and minifies for production
```
npm run build
```

### Run your tests
```
npm run test
```

### Lints and fixes files
```
npm run lint
```

### Customize configuration
See [Configuration Reference](https://cli.vuejs.org/config/).
5 changes: 5 additions & 0 deletions use-custom-webpack-plugin/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
presets: [
'@vue/cli-plugin-babel/preset'
]
}
Loading

0 comments on commit f0e96f3

Please sign in to comment.