diff --git a/.gitignore b/.gitignore index 3a71f89..59ce784 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,6 @@ ._* .DS_Store .git -node_modules \ No newline at end of file +node_modules +dist +package-lock.json \ No newline at end of file diff --git a/examples/rspack-example/package.json b/examples/rspack-example/package.json new file mode 100644 index 0000000..ba8a2ea --- /dev/null +++ b/examples/rspack-example/package.json @@ -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" + } +} diff --git a/examples/rspack-example/rspack.config.js b/examples/rspack-example/rspack.config.js new file mode 100644 index 0000000..5da35f0 --- /dev/null +++ b/examples/rspack-example/rspack.config.js @@ -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' + }), + ], +}; diff --git a/examples/rspack-example/src/index.js b/examples/rspack-example/src/index.js new file mode 100644 index 0000000..20dbfb2 --- /dev/null +++ b/examples/rspack-example/src/index.js @@ -0,0 +1,5 @@ +function hello() { + console.log('Hello from bugfreejs rspack example!'); +} + +hello(); diff --git a/examples/webpack-example/package.json b/examples/webpack-example/package.json new file mode 100644 index 0000000..208f0f5 --- /dev/null +++ b/examples/webpack-example/package.json @@ -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" + } +} diff --git a/examples/webpack-example/src/index.js b/examples/webpack-example/src/index.js new file mode 100644 index 0000000..c422360 --- /dev/null +++ b/examples/webpack-example/src/index.js @@ -0,0 +1,5 @@ +function hello() { + console.log('Hello from bugfreejs webpack example!'); +} + +hello(); diff --git a/examples/webpack-example/webpack.config.js b/examples/webpack-example/webpack.config.js new file mode 100644 index 0000000..cc99864 --- /dev/null +++ b/examples/webpack-example/webpack.config.js @@ -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' + }), + ], +}; diff --git a/index.js b/index.js old mode 100644 new mode 100755 diff --git a/lib/BugfreePlugin.js b/lib/BugfreePlugin.js new file mode 100644 index 0000000..18c285e --- /dev/null +++ b/lib/BugfreePlugin.js @@ -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; diff --git a/package.json b/package.json index 8372fe4..f8cb266 100644 --- a/package.json +++ b/package.json @@ -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" }, @@ -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" diff --git a/rspack-plugin.js b/rspack-plugin.js new file mode 100644 index 0000000..505816c --- /dev/null +++ b/rspack-plugin.js @@ -0,0 +1 @@ +module.exports = require('./lib/BugfreePlugin'); diff --git a/webpack-plugin.js b/webpack-plugin.js new file mode 100644 index 0000000..505816c --- /dev/null +++ b/webpack-plugin.js @@ -0,0 +1 @@ +module.exports = require('./lib/BugfreePlugin');