Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: upgrade to Webpack 5 #34

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"less": "^4.1.0",
"less-loader": "^7.3.0",
"memory-fs": "^0.5.0",
"mini-css-extract-plugin": "^1.0.0",
"mini-css-extract-plugin": "^1.3.5",
"node-fetch": "^2.6.1",
"node-sass": "^4.14.1",
"np": "^6.5.0",
Expand All @@ -93,11 +93,11 @@
"svelte": "^3.29.0",
"svelte-loader": "^2.13.6",
"terser": "^5.3.5",
"terser-webpack-plugin": "^4.2.3",
"terser-webpack-plugin": "^5.1.1",
"vm2": "^3.9.1",
"vue-loader": "^15.9.3",
"vue-loader": "^16.1.2",
"vue-template-compiler": "^2.6.12",
"webpack": "^4.44.2",
"webpack": "^5.19.0",
"write-file-webpack-plugin": "^4.5.1"
}
}
76 changes: 37 additions & 39 deletions src/config/makeWebpackConfig.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import autoprefixer from 'autoprefixer'
import TerserPlugin from 'terser-webpack-plugin'
// import TerserPlugin from 'terser-webpack-plugin'
import MiniCssExtractPlugin from 'mini-css-extract-plugin'
import CssoWebpackPlugin from 'csso-webpack-plugin'
import WriteFilePlugin from 'write-file-webpack-plugin'
// import WriteFilePlugin from 'write-file-webpack-plugin'

const log = require('debug')('bp:webpack')
import escapeRegex from 'escape-string-regexp'
import builtinModules from 'builtin-modules'
// import builtinModules from 'builtin-modules'
import webpack, { Entry } from 'webpack'
// @ts-ignore
import VueLoaderPlugin from 'vue-loader/lib/plugin'
import VueLoaderPlugin from 'vue-loader/dist/plugin'

import { Externals } from '../common.types'

Expand Down Expand Up @@ -41,30 +40,29 @@ export default function makeWebpackConfig({

log('external packages %o', externalsRegex)

const builtInNode: NodeBuiltIn = {}
builtinModules.forEach(mod => {
builtInNode[mod] = 'empty'
})
// const builtInNode: NodeBuiltIn = {}
// builtinModules.forEach(mod => {
// builtInNode[mod] = false;
// })

builtInNode['setImmediate'] = false
builtInNode['console'] = false
builtInNode['process'] = false
builtInNode['Buffer'] = false
// builtInNode['setImmediate'] = false
// builtInNode['console'] = false
// builtInNode['process'] = false
// builtInNode['Buffer'] = false

// Don't mark an import as built in if it is the name of the package itself
// eg. `events`
if (builtInNode[packageName]) {
builtInNode[packageName] = false
}
// if (builtInNode[packageName]) {
// builtInNode[packageName] = false
// }

// @ts-ignore
// @ts-ignore
return {
entry: entry,
mode: 'production',
// bail: true,
optimization: {
namedChunks: true,
chunkIds: 'named',
// namedChunks: true,
runtimeChunk: { name: 'runtime' },
minimize: true,
splitChunks: {
Expand All @@ -78,29 +76,29 @@ export default function makeWebpackConfig({
},
},
minimizer: [
new TerserPlugin({
parallel: true,
terserOptions: {
ie8: false,
output: {
comments: false,
},
},
}),
// new TerserPlugin({
// parallel: true,
// terserOptions: {
// ie8: false,
// output: {
// comments: false,
// },
// },
// }),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can reference the default (terser) with "..." in webpack 5.

// @ts-ignore: Appears that the library might have incorrect definitions
new CssoWebpackPlugin({ restructure: false }),
],
},
plugins: [
new webpack.IgnorePlugin(/^electron$/),
new webpack.IgnorePlugin({ resourceRegExp: /^electron$/ }),
new VueLoaderPlugin(),
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// both options are optional
filename: '[name].bundle.css',
chunkFilename: '[id].bundle.css',
}),
...(debug ? [new WriteFilePlugin()] : []),
}) as any,
// ...(debug ? [new WriteFilePlugin()] : []),
],
resolve: {
modules: ['node_modules'],
Expand Down Expand Up @@ -138,19 +136,19 @@ export default function makeWebpackConfig({
{
test: /\.(html|svelte)$/,
use: {
loader: require.resolve('svelte-loader'),
loader: 'svelte-loader',
options: {
emitCss: true,
},
},
},
{
test: /\.vue$/,
loader: require.resolve('vue-loader'),
use: 'vue-loader',
},
{
test: /\.(scss|sass)$/,
loader: [
use: [
MiniCssExtractPlugin.loader,
require.resolve('css-loader'),
{
Expand All @@ -174,7 +172,7 @@ export default function makeWebpackConfig({
},
{
test: /\.less$/,
loader: [
use: [
MiniCssExtractPlugin.loader,
require.resolve('css-loader'),
{
Expand Down Expand Up @@ -211,14 +209,14 @@ export default function makeWebpackConfig({
},
],
},
node: builtInNode,
node: false,
output: {
filename: 'bundle.js',
pathinfo: false,
},
externals: (context, request, callback) =>
isExternalRequest(request)
? callback(null, 'commonjs ' + request)
externals: ({ context, request }, callback) =>
isExternalRequest(request!)
? callback(undefined, 'commonjs ' + request)
: callback(),
}
}
Expand Down
120 changes: 61 additions & 59 deletions src/fixed/parseReference.js
Original file line number Diff line number Diff line change
Expand Up @@ -5244,73 +5244,75 @@
})
},
}),
r.each({ scrollLeft: 'pageXOffset', scrollTop: 'pageYOffset' }, function (
a,
b
) {
var c = 'pageYOffset' === b
r.fn[a] = function (d) {
return T(
this,
function (a, d, e) {
var f
return (
r.isWindow(a) ? (f = a) : 9 === a.nodeType && (f = a.defaultView),
void 0 === e
? f
? f[b]
: a[d]
: void (f
? f.scrollTo(c ? f.pageXOffset : e, c ? e : f.pageYOffset)
: (a[d] = e))
)
},
a,
d,
arguments.length
)
r.each(
{ scrollLeft: 'pageXOffset', scrollTop: 'pageYOffset' },
function (a, b) {
var c = 'pageYOffset' === b
r.fn[a] = function (d) {
return T(
this,
function (a, d, e) {
var f
return (
r.isWindow(a)
? (f = a)
: 9 === a.nodeType && (f = a.defaultView),
void 0 === e
? f
? f[b]
: a[d]
: void (f
? f.scrollTo(c ? f.pageXOffset : e, c ? e : f.pageYOffset)
: (a[d] = e))
)
},
a,
d,
arguments.length
)
}
}
}),
),
r.each(['top', 'left'], function (a, b) {
r.cssHooks[b] = Pa(o.pixelPosition, function (a, c) {
if (c) return (c = Oa(a, b)), Ma.test(c) ? r(a).position()[b] + 'px' : c
})
}),
r.each({ Height: 'height', Width: 'width' }, function (a, b) {
r.each({ padding: 'inner' + a, content: b, '': 'outer' + a }, function (
c,
d
) {
r.fn[d] = function (e, f) {
var g = arguments.length && (c || 'boolean' != typeof e),
h = c || (e === !0 || f === !0 ? 'margin' : 'border')
return T(
this,
function (b, c, e) {
var f
return r.isWindow(b)
? 0 === d.indexOf('outer')
? b['inner' + a]
: b.document.documentElement['client' + a]
: 9 === b.nodeType
? ((f = b.documentElement),
Math.max(
b.body['scroll' + a],
f['scroll' + a],
b.body['offset' + a],
f['offset' + a],
f['client' + a]
))
: void 0 === e
? r.css(b, c, h)
: r.style(b, c, e, h)
},
b,
g ? e : void 0,
g
)
r.each(
{ padding: 'inner' + a, content: b, '': 'outer' + a },
function (c, d) {
r.fn[d] = function (e, f) {
var g = arguments.length && (c || 'boolean' != typeof e),
h = c || (e === !0 || f === !0 ? 'margin' : 'border')
return T(
this,
function (b, c, e) {
var f
return r.isWindow(b)
? 0 === d.indexOf('outer')
? b['inner' + a]
: b.document.documentElement['client' + a]
: 9 === b.nodeType
? ((f = b.documentElement),
Math.max(
b.body['scroll' + a],
f['scroll' + a],
b.body['offset' + a],
f['offset' + a],
f['client' + a]
))
: void 0 === e
? r.css(b, c, h)
: r.style(b, c, e, h)
},
b,
g ? e : void 0,
g
)
}
}
})
)
}),
r.fn.extend({
bind: function (a, b, c) {
Expand Down
43 changes: 26 additions & 17 deletions src/getDependencySizeTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,41 +104,50 @@ type StatsTree = {
children: StatsChild[]
}

async function bundleSizeTree(stats: webpack.Stats.ToJsonOutput) {
async function bundleSizeTree(stats: webpack.Stats) {
// const stats = statsObj.toJson();
let statsTree: StatsTree = {
packageName: '<root>',
sources: [],
children: [],
}

if (!stats.modules) return []
if (!stats.compilation.modules) return []

// extract source path for each module
let modules: MakeModule[] = []
const makeModule = (mod: webpack.Stats.FnModules): MakeModule => {
const makeModule = (mod: webpack.Module): MakeModule => {
// Uglifier cannot minify a json file, hence we need
// to make it valid javascript syntax
const isJSON = mod.identifier.endsWith('.json')
const source = isJSON ? `$a$=${mod.source}` : mod.source
const isJSON = mod.identifier().endsWith('.json')
const rawSource = mod
.source(
stats.compilation.dependencyTemplates,
stats.compilation.runtimeTemplate
)
.source()
.toString()
const source = isJSON ? `$a$=${rawSource}` : rawSource

return {
path: modulePath(mod.identifier),
path: modulePath(mod.identifier()),
sources: [source || ''],
source: source || '',
}
}

stats.modules
.filter(mod => !mod.name.startsWith('external'))
.forEach(mod => {
if (mod.modules) {
mod.modules.forEach(subMod => {
modules.push(makeModule(subMod))
})
} else {
modules.push(makeModule(mod))
}
})
for (const mod of stats.compilation.modules) {
if (mod.nameForCondition()?.startsWith('external')) {
continue
}
// if (mod.modules) {
// mod.modules.forEach(subMod => {
// modules.push(makeModule(subMod))
// })
// } else {
modules.push(makeModule(mod))
// }
}

modules.sort((a, b) => {
if (a === b) {
Expand Down
2 changes: 1 addition & 1 deletion src/getPackageExportSizes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export async function getPackageExportSizes(

return {
...builtDetails,
assets: builtDetails.assets.map(asset => ({
assets: builtDetails.assets.map((asset: any) => ({
...asset,
path: exportMap[asset.name],
})),
Expand Down
Loading