Skip to content

Commit

Permalink
tmp: using webpack directly
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtPooki committed Feb 8, 2024
1 parent ade322c commit 634d5e4
Show file tree
Hide file tree
Showing 9 changed files with 9,620 additions and 1,858 deletions.
13 changes: 13 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { build } from "esbuild";
import inlineImage from "esbuild-plugin-inline-image";

build({
entryPoints: ["./src/index.js"],
outfile: "./dist/.js",
minify: true,
bundle: true,
loader: {
".js": "jsx",
},
plugins: [inlineImage()],
}).catch(() => process.exit(1));
79 changes: 79 additions & 0 deletions build2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// import { analyzeMetafile, build, serve } from 'esbuild';
import { createServer, request, ServerResponse, IncomingMessage } from 'http';
import { relative } from 'path';
import esbuildPkg from 'esbuild';
const { analyzeMetafile, build, serve } = esbuildPkg;
// import { spawn } from 'child_process'

const clients = [];
/**
* @type {Map<string, string>}
*/
const contentMap = new Map();

const rebuild = (error, result) => {
contentMap.clear();
for (const content of result.outputFiles) {
contentMap.set(relative(".", content.path), content.text);
}
clients.forEach((res) => res.write('data: update\n\n'))
clients.length = 0
analyzeMetafile(result.metafile)
.then((text) => {
console.log(text)
console.log(error ? error : `Rebuilt at ${new Date().toLocaleString()}`)
});
}

build({
entryPoints: ['./src/index.ts'],
bundle: true,
outfile: 'bundle.js',
write: false,
incremental: true,
metafile: true,
banner: { js: ' (() => new EventSource("/esbuild").onmessage = () => location.reload())();' },
watch: {
onRebuild(error, result) {
rebuild(error, result)
},
},
})
.then(result => rebuild(null, result))
.catch(() => process.exit(1))

serve({ servedir: './' }, {}).then(() => {
createServer((req, res) => {
const { url, method, headers } = req;
const isDir = url.endsWith('/') || url.endsWith('\\');
const relativeUrl = relative("/", url);
if (req.url === '/esbuild') {
return clients.push(
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
Connection: 'keep-alive',
})
)
} else if (contentMap.has(relativeUrl)) {
res.write(contentMap.get(relativeUrl));
res.end();
return;
}
const readPath = `${relativeUrl}${isDir ? '/' : ''}`;
console.log(`Reading: ${readPath}`);
req.pipe(
request({ hostname: '0.0.0.0', port: 8000, path: `/${readPath}`, method, headers }, (prxRes) => {
res.writeHead(prxRes.statusCode || 0, prxRes.headers)
prxRes.pipe(res, { end: true })
}),
{ end: true }
)
}).listen(3000)

/* setTimeout(() => {
const op = { darwin: ['open'], linux: ['xdg-open'], win32: ['cmd', '/c', 'start'] }
const ptf = process.platform
if (clients.length === 0) spawn(op[ptf][0], [...[op[ptf].slice(1)], `http://localhost:3000`])
}, 1000) //open the default browser only if it is not opened yet */
})
126 changes: 63 additions & 63 deletions config-overrides.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @see https://github.com/facebook/create-react-app/issues/11756#issuecomment-1184657437
* @see https://alchemy.com/blog/how-to-polyfill-node-core-modules-in-webpack-5
*/
import webpack from 'webpack'
// import webpack from 'webpack'

const PURE_ESM_MODULES = [
'ipfs-geoip',
Expand Down Expand Up @@ -73,40 +73,40 @@ function modifyBabelLoaderRuleForTest (rules) {
})
}

function webpackOverride (config) {
const fallback = config.resolve.fallback || {}
export function webpackOverride (config) {
// const fallback = config.resolve.fallback || {}

Object.assign(fallback, {
stream: 'stream-browserify',
os: 'os-browserify/browser',
path: 'path-browserify',
crypto: 'crypto-browserify'
})
// Object.assign(fallback, {
// stream: 'stream-browserify',
// os: 'os-browserify/browser',
// path: 'path-browserify',
// crypto: 'crypto-browserify'
// })

config.resolve.fallback = fallback
// config.resolve.fallback = fallback

config.plugins = (config.plugins || []).concat([
new webpack.ProvidePlugin({
process: 'process/browser',
Buffer: ['buffer', 'Buffer']
})
])
// config.plugins = (config.plugins || []).concat([
// new webpack.ProvidePlugin({
// process: 'process/browser',
// Buffer: ['buffer', 'Buffer']
// })
// ])

config.module.rules = modifyBabelLoaderRuleForBuild(config.module.rules)
config.module.rules.push({
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel-loader',
options: { presets: ['@babel/env', '@babel/preset-react'] }
})
// config.module.rules.push({
// test: /\.jsx?$/,
// exclude: /(node_modules|bower_components)/,
// loader: 'babel-loader',
// options: { presets: ['@babel/env', '@babel/preset-react'] }
// })

config.module.rules.push({
test: /\.m?js$/,
type: 'javascript/auto',
resolve: {
fullySpecified: false
}
})
// config.module.rules.push({
// test: /\.m?js$/,
// type: 'javascript/auto',
// resolve: {
// fullySpecified: false
// }
// })

// Instrument for code coverage in development mode
const REACT_APP_ENV = process.env.REACT_APP_ENV ?? process.env.NODE_ENV ?? 'development'
Expand All @@ -127,38 +127,38 @@ function webpackOverride (config) {
return config
}

const configOverride = {
webpack: webpackOverride,
jest: (config) => {
/**
* @type {import('jest').Config}
*/
return ({
...config,
setupFiles: [...config.setupFiles, 'fake-indexeddb/auto'],
moduleNameMapper: {
...config.moduleNameMapper,
'multiformats/basics': '<rootDir>/node_modules/multiformats/dist/src/basics.js',
'multiformats/bases/base32': '<rootDir>/node_modules/multiformats/dist/src/bases/base32.js',
'multiformats/bases/base58': '<rootDir>/node_modules/multiformats/dist/src/bases/base58.js',
'multiformats/cid': '<rootDir>/node_modules/multiformats/dist/src/cid.js',
'multiformats/hashes/digest': '<rootDir>/node_modules/multiformats/dist/src/hashes/digest.js',
'multiformats/hashes/sha2': '<rootDir>/node_modules/multiformats/dist/src/hashes/sha2.js',
'uint8arrays/alloc': '<rootDir>/node_modules/uint8arrays/dist/src/alloc.js',
'uint8arrays/concat': '<rootDir>/node_modules/uint8arrays/dist/src/concat.js',
'uint8arrays/equals': '<rootDir>/node_modules/uint8arrays/dist/src/equals.js',
'uint8arrays/from-string': '<rootDir>/node_modules/uint8arrays/dist/src/from-string.js',
'uint8arrays/to-string': '<rootDir>/node_modules/uint8arrays/dist/src/to-string.js',
'@chainsafe/is-ip/parse': '<rootDir>/node_modules/@chainsafe/is-ip/lib/parse.js',
// eslint-disable-next-line quote-props
'eventemitter3': '<rootDir>/node_modules/eventemitter3/dist/eventemitter3.esm.js'
},
transformIgnorePatterns: [
'node_module/(?!(eventemitter3)/).+\\.(js|jsx|mjs|cjs|ts|tsx)$',
'^.+\\.module\\.(css|sass|scss)$' // default
]
})
}
}
// const configOverride = {
// webpack: webpackOverride,
// jest: (config) => {
// /**
// * @type {import('jest').Config}
// */
// return ({
// ...config,
// setupFiles: [...config.setupFiles, 'fake-indexeddb/auto'],
// moduleNameMapper: {
// ...config.moduleNameMapper,
// 'multiformats/basics': '<rootDir>/node_modules/multiformats/dist/src/basics.js',
// 'multiformats/bases/base32': '<rootDir>/node_modules/multiformats/dist/src/bases/base32.js',
// 'multiformats/bases/base58': '<rootDir>/node_modules/multiformats/dist/src/bases/base58.js',
// 'multiformats/cid': '<rootDir>/node_modules/multiformats/dist/src/cid.js',
// 'multiformats/hashes/digest': '<rootDir>/node_modules/multiformats/dist/src/hashes/digest.js',
// 'multiformats/hashes/sha2': '<rootDir>/node_modules/multiformats/dist/src/hashes/sha2.js',
// 'uint8arrays/alloc': '<rootDir>/node_modules/uint8arrays/dist/src/alloc.js',
// 'uint8arrays/concat': '<rootDir>/node_modules/uint8arrays/dist/src/concat.js',
// 'uint8arrays/equals': '<rootDir>/node_modules/uint8arrays/dist/src/equals.js',
// 'uint8arrays/from-string': '<rootDir>/node_modules/uint8arrays/dist/src/from-string.js',
// 'uint8arrays/to-string': '<rootDir>/node_modules/uint8arrays/dist/src/to-string.js',
// '@chainsafe/is-ip/parse': '<rootDir>/node_modules/@chainsafe/is-ip/lib/parse.js',
// // eslint-disable-next-line quote-props
// 'eventemitter3': '<rootDir>/node_modules/eventemitter3/dist/eventemitter3.esm.js'
// },
// transformIgnorePatterns: [
// 'node_module/(?!(eventemitter3)/).+\\.(js|jsx|mjs|cjs|ts|tsx)$',
// '^.+\\.module\\.(css|sass|scss)$' // default
// ]
// })
// }
// }

export default configOverride
// export default configOverride
2 changes: 1 addition & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const config: Config = {
clearMocks: false,

// Indicates whether the coverage information should be collected while executing the test
collectCoverage: true,
collectCoverage: false,

// An array of glob patterns indicating a set of files for which coverage information should be collected
// collectCoverageFrom: undefined,
Expand Down
Loading

0 comments on commit 634d5e4

Please sign in to comment.