Skip to content

Commit

Permalink
Use webpack bundled version to build the package
Browse files Browse the repository at this point in the history
  • Loading branch information
thachnn committed Sep 24, 2022
2 parents 7796983 + 2ef9d49 commit 0e20406
Show file tree
Hide file tree
Showing 3 changed files with 279 additions and 3,481 deletions.
29 changes: 14 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nthachus/serve",
"version": "12.1.0",
"version": "12.1.1",
"description": "CLI bundle of vercel/serve, static file serving and directory listing",
"keywords": [
"vercel",
Expand All @@ -18,29 +18,28 @@
"vendor"
],
"engines": {
"node": ">= 6"
"node": ">= 4"
},
"scripts": {
"build": "node node_modules/webpack-cli/bin/cli.js --progress --display-modules",
"clean": "node node_modules/rimraf/bin.js index.js* vendor node_modules/.cache"
"clean": "node node_modules/rimraf/bin.js index.js vendor node_modules/.cache"
},
"devDependencies": {
"@babel/core": "^7.6.4",
"@babel/preset-env": "^7.6.4",
"@babel/core": "thachnn/webpack-bundle#_babel/core-7.10.5",
"@babel/preset-env": "thachnn/webpack-bundle#_babel/preset-env-7.10.4",
"babel-loader": "^8.0.6",
"copy-webpack-plugin": "^5.0.5",
"rimraf": "^2.2.8",
"serve": "^12.0.1",
"string-replace-loader": "^2.2.0",
"util.promisify": "^1.0.0",
"webpack": "^4.35.3",
"webpack-cli": "^3.2.2"
"tiny-promisify": "^0.1.1",
"webpack": "thachnn/webpack-bundle#v4.46.0",
"webpack-cli": "thachnn/webpack-bundle#cli-3.3.12"
},
"resolutions": {
"**/assert/util": "^0.11.0",
"**/cliui/wrap-ansi": "~3.0.0",
"**/debug/ms": "^2.1.1",
"**/define-property/is-descriptor": "^1.0.0",
"serve/boxen/term-size/execa": "^0.8.0",
"serve/serve-handler/mime-types": "~2.1.24"
"**/boxen/term-size/execa": "^1.0.0",
"**/serve-handler/mime-types": "~2.1.24",
"@babel/**/browserslist": "thachnn/webpack-bundle#browserslist-4.14.7",
"@babel/**/regexpu-core": "thachnn/webpack-bundle#regexpu-core-4.7.1",
"babel-loader/find-cache-dir": "thachnn/webpack-bundle#find-cache-dir-2.1.0"
}
}
220 changes: 194 additions & 26 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const path = require('path');
const webpack = require('webpack');
const TerserPlugin = require('terser-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
'use strict';

const { BannerPlugin, CopyPlugin, TerserPlugin } = require('webpack');
const pkg = require('./package.json');

const patches = [
Expand Down Expand Up @@ -39,68 +38,237 @@ const patches = [
replace:
"$1if (args['--virtual-path']) args['--virtual-path'] = args['--virtual-path'].replace(/^\\/|\\/$$/g, '');\n$&",
},
{ search: '^(\\s*config)\\.etag = .*', flags: 'm', replace: "$&\n$1.virtualPath = args['--virtual-path'];" },
];

module.exports = {
mode: 'production',
entry: './node_modules/serve/bin/serve',
output: {
path: __dirname,
filename: 'index.js',
entry: {
index: './node_modules/serve/bin/serve',
'vendor/ajv': './node_modules/ajv/lib/ajv',
'vendor/chalk': './node_modules/chalk/index',
'vendor/execa': './node_modules/execa/index',
},
output: { path: __dirname },
context: __dirname,
target: 'node',
node: {
__filename: false,
__dirname: false,
},
node: { __filename: false, __dirname: false },
externals: {
'spawn-sync': 'commonjs spawn-sync',
ajv: 'commonjs2 ./vendor/ajv',
chalk: 'commonjs2 ./vendor/chalk',
execa: 'commonjs2 ./vendor/execa',
'mime-db': 'commonjs2 ./vendor/mime-db.json',
},
module: {
rules: [
{
// transpile ES6-8 into ES5
test: /\.m?js$/,
exclude:
/node_modules.(ajv|color-convert|cross-spawn|debug|es-abstract|isexe|negotiator|object|signal-exit|uri-js|util)\b/,
test: [
/node_modules.(ansi|chalk|has|supports)\b.*\.m?js$/i,
/node_modules.(cross|execa|get|npm|path-key)\b.*\.m?js$/i,
/node_modules.(arg|boxen|camelcase|clipboardy|is-(docker|wsl)|serve|string|term|tiny|update|widest)\b.*\.m?js$/i,
/node_modules.(registry-auth-token.base64|strip-ansi.index)\.js$/i,
],
loader: 'babel-loader',
options: {
cacheDirectory: true,
presets: [
['@babel/preset-env', { targets: { node: pkg.engines.node.replace(/^[^\d]*/, '') } }], // esmodules
[
'@babel/preset-env',
{
targets: { node: /\d[\d.]*/.exec(pkg.engines.node)[0] },
modules: false,
loose: true,
exclude: [/^transform-(classes|for-of|regenerator|arrow|function)\b/],
},
],
],
},
},
{
test: /node_modules.serve.bin.serve\.js$/i,
loader: 'string-replace-loader',
options: {
multiple: [...patches, { search: '^#!.*[\\r\\n]+', flags: '', replace: '' }],
},
},
{
test: /node_modules.serve-handler.src.index\.js$/i,
loader: 'string-replace-loader',
options: {
multiple: [
{
search: '^\\s*details\\.relative = path\\.join\\(',
flags: 'm',
replace: "$&config.virtualPath ? `/${config.virtualPath}` : '', ",
},
{
search: '^(\\s*url:) ([^,\\n]*)',
flags: 'm',
replace: "$1 (config.virtualPath ? `${config.virtualPath}/` : '') + ($2)",
},
{
search: "const relative = path.join('/'",
replace: "const relative = path.join(`/${config.virtualPath || ''}`",
},
],
},
},
{
test: path.resolve(__dirname, 'node_modules/serve/bin/serve.js'),
test: /node_modules.(serve.bin.serve|(serve-handler.src|update-check).index)\.js$/i,
loader: 'string-replace-loader',
options: {
multiple: [...patches, { search: '^#!.*', flags: '', replace: "require('util.promisify/shim')();" }],
search: "const {promisify} = require('util')",
replace: "const promisify = require('util').promisify || require('tiny-promisify')",
},
},
{
test: path.resolve(__dirname, 'node_modules/serve/package.json'),
test: /node_modules.serve.package\.json$/i,
loader: 'string-replace-loader',
options: {
search: '[\\s\\S]*',
search: '^[\\s\\S]*',
flags: '',
replace: JSON.stringify({ name: pkg.name, version: pkg.version, description: pkg.description }),
},
},
{
test: /node_modules.clipboardy\b.*\.js$/,
test: /node_modules.clipboardy\b.*\.js$/i,
loader: 'string-replace-loader',
options: { search: '\\.+/fallbacks/', flags: 'g', replace: 'vendor/' },
},
{
test: /node_modules.chalk.index\.js$/i,
loader: 'string-replace-loader',
options: {
search: '^for \\(const (\\w+) of (Object\\.keys\\(.*?\\))\\) (\\{[\\s\\S]*?\\n\\})$',
flags: 'gm',
replace: (_, m1, m2, m3) => `${m2}.forEach(${m1} => ${m3.replace(/^(\s+)continue;?$/m, '$1return;')});`,
},
},
{
test: /node_modules.fast-url-parser.src.urlparser\.js$/i,
loader: 'string-replace-loader',
options: { search: ' require("punycode")', replace: ' require("../../punycode")' },
},
// optimize output
{
test: /node_modules.uri-js.dist.es5.uri\.all\.js$/i,
loader: 'string-replace-loader',
options: {
multiple: [
{ search: "^[\\s\\S]*?\\{ ('use strict';[\\s\\S]*)\\n\\}\\){3};\\n.*", flags: '', replace: '$1' },
{ search: '^exports\\.(\\w+) = (?!parse|serialize|resolve\\b)\\1;$', flags: 'gm', replace: '// $&' },
],
},
},
{
test: /node_modules.punycode.punycode\.js$/i,
loader: 'string-replace-loader',
options: {
multiple: [
{
search: '^[\\s\\S]*?\\sroot = freeGlobal;\\s*\\}([\\s\\S]*)\\s/\\*+ Expose `punycode` \\*/[\\s\\S]*',
flags: '',
replace: '$1module.exports = punycode;',
},
{ search: "^\\s+'(\\w+)': (?!toASCII)(ucs2)?\\1,?$", flags: 'gm', replace: '//$&' },
],
},
},
{
test: /node_modules.ansi-styles.index\.js$/i,
loader: 'string-replace-loader',
options: {
search: "^Object\\.defineProperty\\(module, 'exports'[\\s\\S]*\\bget: (assembleStyles)[\\s\\S]*",
flags: 'm',
replace: 'module.exports = $1();',
},
},
{
test: /node_modules.cross-spawn.index\.js$/i,
loader: 'string-replace-loader',
options: { search: '^module\\.exports(\\.[^_]\\w*)? = spawn\\w*;$', flags: 'm', replace: '// $&' },
},
{
test: /node_modules.cross-spawn.lib.enoent\.js$/i,
loader: 'string-replace-loader',
options: { search: '^[ \\t]+(verifyENOENTSync),$', flags: 'gm', replace: '//$&' },
},
{
test: /node_modules.semver.semver\.js$/i,
loader: 'string-replace-loader',
options: { search: '^exports\\.([a-z]\\w*) = (?!satisfies)\\1$', flags: 'gm', replace: '// $&' },
},
{
test: /node_modules.rc.index\.js$/i,
loader: 'string-replace-loader',
options: { search: "= require('minimist')(process.argv.slice(2))", replace: '= {}' },
},
{
test: /node_modules.ini.ini\.js$/i,
loader: 'string-replace-loader',
options: { search: '^exports\\.(?!parse|unsafe)\\w+ = \\w+', flags: 'gm', replace: '// $&' },
},
{
test: /node_modules.boxen.index\.js$/i,
loader: 'string-replace-loader',
options: { search: "= require('cli-boxes')", replace: "= require('cli-boxes/boxes.json')" },
},
{
test: /node_modules.compression.index\.js$/i,
loader: 'string-replace-loader',
options: { search: "= require('debug')", replace: "= require('debug/src/node')" },
},
{
test: /node_modules.mime-types.index\.js$/i,
loader: 'string-replace-loader',
options: { search: '^exports\\.(extension) = \\1;?$', flags: 'gm', replace: '// $&' },
},
{
test: /node_modules.content-disposition.index\.js$/i,
loader: 'string-replace-loader',
options: { search: '^module\\.exports\\.(parse) = \\1;?$', flags: 'gm', replace: '// $&' },
},
{
test: /node_modules.serve-handler.src.(directory|error)\.js$/i,
loader: 'string-replace-loader',
options: {
multiple: [
{ search: '^\\(function\\(\\)\\{', flags: '', replace: '' },
{
search:
'(\\}var itself=(\\w+), _encodeHTML=\\([\\s\\S]*\\);)if\\(typeof module[^)]*\\) (module\\.exports=)itself;.*$',
flags: '',
replace: '$1\n$3$2;',
},
{ search: " typeof _encodeHTML !== 'undefined'", replace: 'true' },
{ search: ' html><head>', replace: ' html><html lang="en"><head> <meta charset="utf-8"/>' },
{ search: ', user-scalable=no"/> <style', replace: '"/> <title>Error</title> <style' },
{ search: "</body>';return ", replace: "</body></html>';return " },
{
search: '( a\\.file\\.svg::before \\{ content: url\\("data:image/svg\\+xml;utf8,).*?(</svg>"\\))',
flags: '',
replace:
"$1<svg xmlns=\\'http://www.w3.org/2000/svg\\' width=\\'15.2\\' height=\\'19\\' viewBox=\\'0 0 409.6 512\\'><path d=\\'M256.14 0H64A64 64 0 0 0 0 64v384a64 64 0 0 0 64 64h281.6a64 64 0 0 0 64-64V153.6zm104.15 140.8H307.2a38.44 38.44 0 0 1-38.4-38.4V49.08zM384 448a38.44 38.44 0 0 1-38.4 38.4H64A38.44 38.44 0 0 1 25.6 448V64A38.44 38.44 0 0 1 64 25.6h179.2v76.8a64 64 0 0 0 64 64H384zM275.2 243.2H134.4a32 32 0 0 0-32 32v115.2a32 32 0 0 0 32 32h140.8a32 32 0 0 0 32-32V275.2a32 32 0 0 0-32-32zm6.4 147.2a6.41 6.41 0 0 1-6.4 6.4H134.4a6.41 6.41 0 0 1-6.4-6.4V275.2a6.41 6.41 0 0 1 6.4-6.4h140.8a6.41 6.41 0 0 1 6.4 6.4zM140.8 384h128l-38.4-64-38.4 38.4-12.8-12.8z\\'/>$2",
},
{
search: '\\bul a\\.lambda::before \\{ content: url\\("data:image/svg\\+xml;.*?</svg>"\\)',
flags: '',
replace: 'ul a.directory { font-weight: bold; }ul a.directory::before { content: ""',
},
],
},
},
],
},
plugins: [
new webpack.BannerPlugin({ banner: '#!/usr/bin/env node', raw: true }),
new BannerPlugin({ banner: '#!/usr/bin/env node', raw: true, test: /index\.js$/i }),
new CopyPlugin([
{ from: 'node_modules/clipboardy/fallbacks', to: 'vendor/', ignore: ['.DS_Store'] },
{ from: 'node_modules/term-size/vendor', to: 'vendor/' },
{ from: 'node_modules/term-size/vendor', to: 'vendor/', ignore: ['.DS_Store'] },
{ from: 'node_modules/mime-db/db.json', to: 'vendor/mime-db.json' },
]),
new BannerPlugin({ banner: 'module.exports =', raw: true, test: /(?<!index)\.js$/i }),
],
optimization: {
nodeEnv: false,
Expand All @@ -109,8 +277,8 @@ module.exports = {
new TerserPlugin({
cache: true,
parallel: true,
terserOptions: { mangle: false, output: { beautify: true } },
extractComments: { condition: /^\**!|@preserve|@license|@cc_on/i, banner: false },
terserOptions: { mangle: false, output: { comments: false, beautify: true, indent_level: 2 } },
extractComments: false,
}),
],
},
Expand Down
Loading

0 comments on commit 0e20406

Please sign in to comment.