Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
._*
.DS_Store
.git
node_modules
node_modules
dist
package-lock.json
13 changes: 13 additions & 0 deletions examples/rspack-example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "bugfreejs-rspack-example",
"version": "1.0.0",
"private": true,
"scripts": {
"build": "rspack build"
},
"devDependencies": {
"bugfreejs": "file:../../",
"@rspack/cli": "^1.0.0",
"@rspack/core": "^1.0.0"
}
}
16 changes: 16 additions & 0 deletions examples/rspack-example/rspack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const path = require('path');
const BugfreePlugin = require('bugfreejs/rspack-plugin');

module.exports = {
mode: 'production',
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},
plugins: [
new BugfreePlugin({
faith: 'default', // 'default' | 'alpaca' | 'god'
}),
],
};
5 changes: 5 additions & 0 deletions examples/rspack-example/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function hello() {
console.log('Hello from bugfreejs rspack example!');
}

hello();
13 changes: 13 additions & 0 deletions examples/webpack-example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "bugfreejs-webpack-example",
"version": "1.0.0",
"private": true,
"scripts": {
"build": "webpack"
},
"devDependencies": {
"bugfreejs": "file:../../",
"webpack": "^5.90.0",
"webpack-cli": "^5.1.4"
}
}
5 changes: 5 additions & 0 deletions examples/webpack-example/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function hello() {
console.log('Hello from bugfreejs webpack example!');
}

hello();
16 changes: 16 additions & 0 deletions examples/webpack-example/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const path = require('path');
const BugfreePlugin = require('bugfreejs/webpack-plugin');

module.exports = {
mode: 'production',
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},
plugins: [
new BugfreePlugin({
faith: 'default', // 'default' | 'alpaca' | 'god'
}),
],
};
Empty file modified index.js
100644 → 100755
Empty file.
76 changes: 76 additions & 0 deletions lib/BugfreePlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
const fs = require('fs');
const path = require('path');

const PLUGIN_NAME = 'BugfreePlugin';
const FAITH_TYPES = ['default', 'alpaca', 'god'];

class BugfreePlugin {
/**
* @param {object} [options]
* @param {'default'|'alpaca'|'god'} [options.faith='default'] - Comment style
* @param {boolean} [options.enable=true] - Whether to enable the plugin
*/
constructor(options = {}) {
this.faith = FAITH_TYPES.includes(options.faith) ? options.faith : 'default';
this.enable = options.enable !== false;
}

apply(compiler) {
if (!this.enable) return;

const commentContent = fs.readFileSync(
path.join(__dirname, '..', 'commentFile', 'comment_' + this.faith + '_utf8.txt'),
{ encoding: 'utf8' }
);

compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
compilation.hooks.processAssets.tap(
{
name: PLUGIN_NAME,
// Use SUMMARIZE stage (1000) to run after minification/optimization
stage: compiler.webpack
? compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE
: 1000,
},
(assets) => {
var RawSource;
if (compiler.webpack && compiler.webpack.sources) {
RawSource = compiler.webpack.sources.RawSource;
} else {
RawSource = require('webpack').sources.RawSource;
}

var names = Object.keys(assets);
for (var i = 0; i < names.length; i++) {
var name = names[i];

var prefix, wrapStart = '', wrapEnd = '';

if (/\.js(\?.*)?$/.test(name)) {
prefix = '// ';
} else if (/\.css(\?.*)?$/.test(name)) {
prefix = ' * ';
wrapStart = '/*\n';
wrapEnd = ' */\n';
} else {
continue;
}

var lines = commentContent.split('\n');
var banner = wrapStart
+ lines.map(function (line) { return prefix + line; }).join('\n')
+ '\n' + wrapEnd;

var asset = compilation.getAsset(name);
compilation.updateAsset(
name,
new RawSource(banner + asset.source.source())
);
}
}
);
});
}
}

module.exports = BugfreePlugin;
29 changes: 27 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
{
"name": "bugfreejs",
"version": "1.2.4",
"description": "add some special comment to your js file. You will love it.",
"version": "2.0.0",
"description": "add some special comment to your js file. You will love it. Now available as webpack/rspack plugin!",
"main": "index.js",
"exports": {
".": "./index.js",
"./webpack-plugin": "./webpack-plugin.js",
"./rspack-plugin": "./rspack-plugin.js"
},
"bin": {
"bugfreejs": "index.js"
},
"files": [
"index.js",
"lib/",
"webpack-plugin.js",
"rspack-plugin.js",
"commentFile/",
"config.json"
],
"scripts": {
"test": "node index.js testFile.js testFile_gbk.js"
},
Expand All @@ -15,6 +28,18 @@
"is-utf8": "^0.2.0"
},
"devDependencies": {},
"peerDependencies": {
"webpack": "^5.0.0 || ^4.0.0",
"@rspack/core": ">=1.0.0"
},
"peerDependenciesMeta": {
"webpack": {
"optional": true
},
"@rspack/core": {
"optional": true
}
},
"repository": {
"type": "git",
"url": "https://github.com/ottomao/bugfreejs.git"
Expand Down
1 change: 1 addition & 0 deletions rspack-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./lib/BugfreePlugin');
1 change: 1 addition & 0 deletions webpack-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./lib/BugfreePlugin');