@nitro-web/webpack 0.0.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +83 -0
  2. package/webpack.config.js +356 -0
package/package.json ADDED
@@ -0,0 +1,83 @@
1
+ {
2
+ "name": "@nitro-web/webpack",
3
+ "version": "0.0.23",
4
+ "repository": "github:boycce/nitro-web",
5
+ "homepage": "https://boycce.github.io/nitro-web/",
6
+ "main": "./webpack.config.js",
7
+ "type": "module",
8
+ "dependencies": {
9
+ "@babel/core": "^7.8.0",
10
+ "@babel/plugin-syntax-dynamic-import": "^7.8.3",
11
+ "@babel/plugin-transform-runtime": "^7.17.0",
12
+ "@babel/preset-env": "^7.8.0",
13
+ "@babel/preset-react": "^7.9.4",
14
+ "@babel/preset-typescript": "^7.24.7",
15
+ "@emotion/babel-plugin": "^11.11.0",
16
+ "@emotion/eslint-plugin": "^11.11.0",
17
+ "@emotion/react": "^11.11.4",
18
+ "@emotion/styled": "^11.11.4",
19
+ "@pmmmwh/react-refresh-webpack-plugin": "^0.5.15",
20
+ "@svgr/webpack": "^8.1.0",
21
+ "@types/react": "^19.0.2",
22
+ "@types/react-dom": "^19.0.2",
23
+ "@typescript-eslint/eslint-plugin": "^8.18.1",
24
+ "@typescript-eslint/parser": "^8.18.1",
25
+ "@uiw/color-convert": "^2.3.0",
26
+ "@uiw/react-color-hue": "^2.3.0",
27
+ "@uiw/react-color-saturation": "^2.3.0",
28
+ "autoprefixer": "^10.4.21",
29
+ "babel-loader": "^8.0.6",
30
+ "babel-plugin-macros": "^3.1.0",
31
+ "babel-plugin-react-html-attrs": "^2.1.0",
32
+ "clean-terminal-webpack-plugin": "https://github.com/boycce/clean-terminal-webpack-plugin.git",
33
+ "clean-webpack-plugin": "^4.0.0",
34
+ "copy-webpack-plugin": "^12.0.2",
35
+ "core-js": "^3.12.1",
36
+ "css-loader": "^3.6.0",
37
+ "csv-loader": "^3.0.5",
38
+ "eslint": "^8.57.1",
39
+ "eslint-plugin-import": "^2.26.0",
40
+ "eslint-plugin-react": "^7.19.0",
41
+ "eslint-plugin-react-hooks": "^4.0.0",
42
+ "eslint-webpack-plugin": "^2.7.0",
43
+ "html-loader": "^1.3.2",
44
+ "html-webpack-plugin": "^5.6.0",
45
+ "mini-css-extract-plugin": "^2.9.0",
46
+ "nodemon": "^2.0.1",
47
+ "postcss": "^8.4.49",
48
+ "postcss-for": "^2.1.1",
49
+ "postcss-import": "^16.1.0",
50
+ "postcss-loader": "^8.1.1",
51
+ "postcss-nested": "^7.0.2",
52
+ "react-currency-input-field": "^3.8.0",
53
+ "react-day-picker": "^9.6.4",
54
+ "react-number-format": "^5.4.0",
55
+ "react-refresh": "^0.14.2",
56
+ "react-router-dom": "6.24.1",
57
+ "react-select": "^5.9.0",
58
+ "react-tracked": "^1.3.0",
59
+ "sort-route-addresses-nodeps": "0.0.4",
60
+ "string-replace-loader": "^3.1.0",
61
+ "tailwind-merge": "^2.6.0",
62
+ "typescript": "^5.5.2",
63
+ "webpack": "^5.92.1",
64
+ "webpack-cli": "^5.1.4",
65
+ "webpack-dev-server": "^4.6.0",
66
+ "webpack-node-externals": "^1.7.2"
67
+ },
68
+ "peerDependencies": {
69
+ "@headlessui/react": "^2.2.0",
70
+ "@heroicons/react": "^2.2.0",
71
+ "color": "^4.2.3",
72
+ "react": "^18.3.1",
73
+ "react-dom": "^18.3.1",
74
+ "tailwindcss": "^3.4.17",
75
+ "twin.macro": "^3.4.1"
76
+ },
77
+ "eslintConfig": {
78
+ "extends": "../core/.eslintrc.json"
79
+ },
80
+ "engines": {
81
+ "node": ">=18"
82
+ }
83
+ }
@@ -0,0 +1,356 @@
1
+ // import axios from '@hokify/axios'
2
+ // import axiosRetry from 'axios-retry'
3
+ import autoprefixer from 'autoprefixer'
4
+ import CleanTerminalPlugin from 'clean-terminal-webpack-plugin'
5
+ import { CleanWebpackPlugin } from 'clean-webpack-plugin'
6
+ import CopyWebpackPlugin from 'copy-webpack-plugin'
7
+ import HtmlWebpackPlugin from 'html-webpack-plugin'
8
+ import MiniCssExtractPlugin from 'mini-css-extract-plugin'
9
+ import webpackNodeExternals from 'webpack-node-externals'
10
+ import path from 'path'
11
+ // import ReactRefreshWebpackPlugin from '@pmmmwh/react-refresh-webpack-plugin'
12
+ import tailwindcss from 'tailwindcss'
13
+ import webpack from 'webpack'
14
+ import ESLintPlugin from 'eslint-webpack-plugin'
15
+ import postcssImport from 'postcss-import'
16
+ import postcssNested from 'postcss-nested'
17
+ import postcssFor from 'postcss-for'
18
+ import { createRequire } from 'module'
19
+ import { getDirectories } from 'nitro-web/util'
20
+
21
+ const _require = createRequire(import.meta.url)
22
+ const pick = (object, list) => list.reduce((o, e) => ((o[e] = object[e]), o), {})
23
+ const isBuild = process.env.NODE_ENV == 'production'
24
+
25
+ // axiosRetry(axios, {
26
+ // retries: 10,
27
+ // retryDelay: () => 150,
28
+ // retryCondition: (e) => e.code == 'ECONNREFUSED',
29
+ // })
30
+
31
+ // process.traceDeprecation = true
32
+ export const getConfig = (config) => {
33
+ const { clientDir, componentsDir, distDir, imgsDir } = getDirectories(path, config.pwd)
34
+ const publicPath = getPublicPath(config.env, config.homepage, config.publicPath)
35
+
36
+ return (env, argv) => [{
37
+ devtool: isBuild ? false : 'source-map',
38
+ entry: clientDir + 'index.ts',
39
+ // entry: isBuild ? './client/index.tsx' : ['webpack-plugin-serve/client', './client/index.tsx'], // check this
40
+ mode: isBuild ? 'production' : 'development',
41
+ // target=node ignores node_modules
42
+ externals: argv.target?.[0] == 'node' ? [webpackNodeExternals()] : [],
43
+ // target=node ignores builtin modules
44
+ target: argv.target?.[0] || 'web',
45
+ devServer: {
46
+ // needed when connecting to the devserver through a domain
47
+ allowedHosts: 'all',
48
+ client: {
49
+ logging: 'warn', // 'info'
50
+ overlay: true,
51
+ },
52
+ compress: false,
53
+ devMiddleware: {
54
+ writeToDisk: false,
55
+ },
56
+ historyApiFallback: true,
57
+ host: '0.0.0.0',
58
+ hot: true,
59
+ port: 3000,
60
+ proxy: {
61
+ '/api': {
62
+ logLevel: 'silent',
63
+ target: 'http://0.0.0.0:3001',
64
+ // bypass: async function (req, res, proxyOptions) {
65
+ // // // wait for pong, indicating express has restarted
66
+ // // // all non-asset routes are triggered (even the main page)
67
+ // // // another method: https://codeburst.io/dont-use-nodemon-there-are-better-ways-fc016b50b45e
68
+ // // if (!req.url.match(/^\/api\//)) return
69
+ // // await axios.get('http://0.0.0.0:3001/ping')
70
+ // },
71
+ },
72
+ '/server/email/templates': {
73
+ logLevel: 'silent',
74
+ target: 'http://0.0.0.0:3001',
75
+ },
76
+ },
77
+ },
78
+ infrastructureLogging: {
79
+ level: 'info',
80
+ // debug: [(name) => !name.match(/webpack-dev-server/)],
81
+ // console: (a, b, c) => { // webpack v5.3.1
82
+ // console.log(1, a, b, c)
83
+ // },
84
+ },
85
+ module: {
86
+ rules: [
87
+ {
88
+ test: /\.css$/,
89
+ use: [
90
+ { loader: MiniCssExtractPlugin.loader },
91
+ { loader: 'css-loader', options: { sourceMap: true } },
92
+ { loader: 'postcss-loader', options: {
93
+ postcssOptions: {
94
+ plugins: [
95
+ postcssImport,
96
+ // postcssImport({
97
+ // resolve: postcssImportResolver({
98
+ // alias: {
99
+ // 'nitro-web/client/css/components.css': path.resolve(nitroDir, 'client/css/components.css'),
100
+ // 'nitro-web/client/css/fonts.css': path.resolve(nitroDir, 'client/css/fonts.css'),
101
+ // },
102
+ // }),
103
+ // }),
104
+ postcssNested,
105
+ postcssFor,
106
+ tailwindcss({ config: path.resolve(config.pwd, 'tailwind.config.js') }),
107
+ autoprefixer,
108
+ ],
109
+ },
110
+ sourceMap: true,
111
+ }},
112
+ ],
113
+ },
114
+ {
115
+ test: /\.(m?js|jsx|ts|tsx)$/,
116
+ exclude: (
117
+ (path) => {
118
+ // Dont transpile node modules except for date-fns, which uses ES6
119
+ return path.includes('node_modules')
120
+ && !path.includes('node_modules/date-fns')
121
+ && !path.includes('node_modules/nitro-web')
122
+ }
123
+ ),
124
+ use: [
125
+ {
126
+ loader: 'babel-loader',
127
+ options: {
128
+ presets: [
129
+ ['@babel/preset-env', { debug: false }],
130
+ ['@babel/preset-typescript', { allowNamespaces: true }],
131
+ ['@babel/preset-react', { runtime: 'automatic', importSource: '@emotion/react', development: !isBuild }],
132
+ ],
133
+ plugins: [
134
+ 'react-html-attrs',
135
+ '@babel/plugin-syntax-dynamic-import',
136
+ '@babel/plugin-transform-runtime',
137
+ '@emotion/babel-plugin',
138
+ // Below allows us to reference tailwindcss theme variables in emotion blocks
139
+ // https://github.com/ben-rogerson/twin.macro/blob/master/docs/options.md
140
+ // https://medium.com/fredwong-it/emotion-tailwind-twin-macro-7fdc5f2ae5f9
141
+ // https://github.com/ben-rogerson/twin.examples/tree/master/react-emotion-typescript#complete-the-typescript-setup
142
+ ['babel-plugin-macros', {
143
+ 'twin': {
144
+ preset: 'emotion',
145
+ config: path.resolve(config.pwd, 'tailwind.config.js'),
146
+ },
147
+ }],
148
+ // 'react-refresh/babel', // !isBuild && _require.resolve('react-refresh/babel'),
149
+ ].filter(Boolean),
150
+ },
151
+ },
152
+ ],
153
+ },
154
+ {
155
+ // Workaround to hide emotion's pseudo console noise (bug)
156
+ // https://github.com/emotion-js/emotion/issues/1105#issuecomment-547247291
157
+ test: /node_modules\/@emotion\/cache\/(src|dist)/,
158
+ loader: 'string-replace-loader',
159
+ options: {
160
+ search: 'if (unsafePseudoClasses',
161
+ replace: 'if (false && unsafePseudoClasses',
162
+ },
163
+ },
164
+ {
165
+ test: /\.csv$/,
166
+ use: [
167
+ {
168
+ loader: 'csv-loader',
169
+ options: {
170
+ delimiter: ',',
171
+ header: true,
172
+ skipEmptyLines: true,
173
+ },
174
+ },
175
+ ],
176
+ },
177
+ {
178
+ test: /\.(png|jpe?g|gif|woff|woff2|ttf|eot)$/i,
179
+ type: 'asset/resource',
180
+ generator: {
181
+ filename: 'assets/images/[name].[ext][query]',//[hash][ext][query]
182
+ },
183
+ },
184
+ {
185
+ test: /\.svg$/,
186
+ use: [
187
+ {
188
+ loader: '@svgr/webpack',
189
+ options: {
190
+ svgoConfig: {
191
+ plugins: [
192
+ {
193
+ name: 'preset-default',
194
+ params: {
195
+ overrides: {
196
+ removeViewBox: false,
197
+ convertPathData: false,
198
+ cleanupNumericValues: false,
199
+ convertShapeToPath: false,
200
+ },
201
+ },
202
+ },
203
+ // 'removeDimensions', (dont need this, just use width/height={undefined} if required)
204
+ // {
205
+ // name: 'addClassesToSVGElement',
206
+ // params: {
207
+ // className: (node, info) => {
208
+ // console.log(info)
209
+ // return `svg-${info?.path?.split('.')[0]}`
210
+ // },
211
+ // },
212
+ // },
213
+ {
214
+ // until svgr updated to svgo@4 for addClassesToSVGElement fns support
215
+ name: 'add-data-id-to-svg-icons',
216
+ fn: (root, _params, info) => {
217
+ const { basename } = info.path.match(/.*\/(?<basename>.*)\.svg$/).groups
218
+ if (root.children[0].name === 'svg') {
219
+ root.children[0].attributes.className = `svg-${basename}`
220
+ }
221
+ },
222
+ },
223
+ ],
224
+ },
225
+ },
226
+ },
227
+ ],
228
+ },
229
+ ],
230
+ },
231
+ node: {
232
+ __filename: true,
233
+ },
234
+ output: {
235
+ // devtoolModuleFilenameTemplate: (info) => {
236
+ // // DevTools doesn't link webpack:// sourcemap filepaths, force file://.
237
+ // let path = 'file://' + encodeURI(info.absoluteResourcePath)
238
+ // // React only: append ?2 to all paths so devtools can link to the CSS-in-JS
239
+ // if (path.match(/\.jsx$/)) return path + '?2'
240
+ // // Vue only: fix Vue:CSS paths
241
+ // else return path.replace(/(\/components\/).*?components\//, '$1')
242
+ // },
243
+ // We are outputing assets into a handy subdir to allow for easier asset cache control. We can't
244
+ // simply use `path` because webpack-dev-server won't work when writeFiles=false (in memory).
245
+ // Because of this we manually need to prefix all output filenames with `assets/`.
246
+ filename: `assets/bundle.[name]${isBuild ? '.[contenthash]' : ''}.js`,
247
+ path: distDir,
248
+ publicPath: publicPath,
249
+ },
250
+ performance: {
251
+ hints: false,
252
+ },
253
+ optimization: {
254
+ // Split chunks into seperate emitted assets
255
+ splitChunks: {
256
+ cacheGroups: {
257
+ vendor: {
258
+ test: /[\\/]node_modules[\\/].*\.js/,
259
+ name: 'vendor',
260
+ chunks: 'all',
261
+ },
262
+ },
263
+ },
264
+ minimize: isBuild,
265
+ },
266
+ plugins: [
267
+ // new (require('webpack-bundle-analyzer').BundleAnalyzerPlugin)(),
268
+ new CleanWebpackPlugin(),
269
+ new CopyWebpackPlugin({
270
+ patterns: [
271
+ { from: imgsDir + 'favicon.png', to: './favicon.png' },
272
+ { from: imgsDir, to: './assets/imgs' },
273
+ ],
274
+ }),
275
+ new webpack.DefinePlugin({
276
+ ISDEMO: !!process.env.isDemo,
277
+ INJECTED: JSON.stringify({
278
+ ...pick(config, config.inject ? config.inject.split(' ') : []),
279
+ }),
280
+ }),
281
+ new ESLintPlugin({
282
+ extensions: ['js', 'mjs', 'jsx'],
283
+ exclude: ['node_modules'],
284
+ }),
285
+ new MiniCssExtractPlugin({ filename: `assets/bundle.[name]${isBuild ? '.[contenthash]' : ''}.css` }),
286
+ new HtmlWebpackPlugin({ template: clientDir + 'index.html', filename: distDir + 'index.html' }),
287
+ new InterpolateHtmlPlugin(HtmlWebpackPlugin, { PUBLIC_PATH: publicPath, NAME: config.name }),
288
+ new CleanTerminalPlugin({ skipFirstRun: true }),
289
+ // !isBuild && new ReactRefreshWebpackPlugin({ overlay: false }),
290
+ ].filter(Boolean),
291
+ resolve: {
292
+ extensions: ['.js', '.jsx', '.ts', '.tsx'],
293
+ // fallback: { fs: false },
294
+ alias: {
295
+ // required for auto-importing page components into nitro app.js router
296
+ 'componentsDir': componentsDir,
297
+ },
298
+ },
299
+ stats: {
300
+ all: false,
301
+ assets: !process.env.WEBPACK_SERVE,
302
+ errors: true,
303
+ errorDetails: true,
304
+ timings: !process.env.WEBPACK_SERVE,
305
+ warnings: true,
306
+ },
307
+ watchOptions: {
308
+ aggregateTimeout: 50,
309
+ ignored: new RegExp(`(${componentsDir}.*\\.api\\.js$|node_modules/(?!cherry))`),
310
+ },
311
+ }]
312
+ }
313
+
314
+ class InterpolateHtmlPlugin {
315
+ constructor(htmlWebpackPlugin, replacements) {
316
+ this.htmlWebpackPlugin = htmlWebpackPlugin
317
+ this.replacements = replacements
318
+ }
319
+ apply(compiler) {
320
+ compiler.hooks.compilation.tap('InterpolateHtmlPlugin', compilation => {
321
+ this.htmlWebpackPlugin
322
+ .getHooks(compilation)
323
+ .afterTemplateExecution.tap('InterpolateHtmlPlugin', data => {
324
+ // Run HTML through a series of user-specified string replacements.
325
+ for (const key in this.replacements) {
326
+ const value = this.replacements[key]
327
+ data.html = data.html.replace(
328
+ new RegExp('{' + key + '}', 'g'),
329
+ value
330
+ )
331
+ }
332
+ })
333
+ })
334
+ }
335
+ }
336
+
337
+ function getPublicPath(env, homepage, publicPath) {
338
+ /**
339
+ * Returns public path used for webapck (we can't use relative paths)
340
+ * @param {string} env - 'development', 'staging', 'production'
341
+ * @param {string} publicPath - proces.env.publicPath
342
+ * @param {string} homepage - package.json homepage
343
+ */
344
+ if (publicPath) {
345
+ const publicPathObj = new URL(publicPath, 'https://domain.com')
346
+ return publicPathObj.pathname
347
+
348
+ } else if (homepage) {
349
+ const homepageObj = new URL(homepage, 'https://domain.com')
350
+ if (env === 'development') return '/'
351
+ else return homepageObj.pathname
352
+
353
+ } else {
354
+ return '/'
355
+ }
356
+ }