Using Vite With Laravel 7, But Just as a Bundler & Not Using #Vite - laravel-7

Shot in the dark. I was wondering if it's possible to replace Mix with Vite in such a way that it's only being used as a bundler and doesn't use the #vite directive?
My project is a monolith and has multiple JS files that are by themselves Vue apps. So at the moment I'm using Mix to map the input/output like so:
.js('resources/js/client/quotation.js', 'public/js/client')
My current Vite setup is:
import { defineConfig } from 'vite';
import { createVuePlugin } from 'vite-plugin-vue2'
import laravel from 'laravel-vite-plugin';
import path from 'path';
export default defineConfig({
build: {
outDir: path.resolve(__dirname, 'public'),
emptyOutDir: false,
rollupOptions: {
output: {
entryFileNames: 'js/[name].js',
assetFileNames: 'css/[name].css',
},
},
},
resolve: {
alias: {
'#': path.join(__dirname, 'resources/js'),
'~': path.join(__dirname, 'resources/sass'),
},
},
plugins: [
laravel({
input: [
'resources/sass/admin.scss',
'resources/js/app.js',
'resources/js/admin/realtime_alerting/realtime_alerting_dashboard.js',
'resources/js/common/realtime_alerting/realtime_alerting_edit.js'
],
refresh: true
}),
createVuePlugin()
],
});
This is then compiling these various files into production ready files:
https://gyazo.com/c4ed72f538935c7e396484a864ab670b
public/manifest.json 0.96 KiB
public/js/app.js 0.02 KiB / gzip: 0.04 KiB
public/css/realtime_alerting_edit.css 3.47 KiB / gzip: 1.12 KiB
public/css/realtime_alerting_dashboard.css 0.41 KiB / gzip: 0.21 KiB
public/css/vueComponentNormalizer.css 4.38 KiB / gzip: 1.34 KiB
public/css/admin.css 61.99 KiB / gzip: 11.75 KiB
public/js/realtime_alerting_edit.js 141.12 KiB / gzip: 44.58 KiB
public/js/realtime_alerting_dashboard.js 575.73 KiB / gzip: 176.65 KiB
public/assets/vueComponentNormalizer.91ca281a.js 988.54 KiB / gzip: 275.28 KiB
I can then reference them like so in a blade file:
#section('footer')
<script type="module" src="/core/public/js/realtime_alerting_dashboard.js"></script>
#endsection
However you can see that some of them are still being cache-busted and strangely some JS files are being converted into CSS files. I gather I've either got a config issue here or that someone will tell me this just isn't possible using Vue 2 & Laravel 7.
Any help would be very appreciated.

Related

Webpack devServer: Change to test files does not trigger browser reload (mocha-loader)

My reduced app looks like this:
\---app
| .env
| data.json
| package-lock.json
| package.json
|
+---dist
| app.html
| favicon.svg
| test.html (***)
|
+---src
| | app.js
| | serve.js
| | Server.js
| | test.js (**)
| |
| \---webpack
| webpack.config.js (*)
| WebpackDevServer.js
|
\---test
utl.test.js
(*) webpack.config.js
{
entry: {
app: "./app.js",
test: "./test.js",
},
output: {
filename: "[name].bundle.js",
path: path.resolve(__dirname, "../../dist"),
},
mode: "development",
devServer: {
static: {
directory: path.resolve(__dirname, "../../dist"),
},
watchFiles: [path.resolve(__dirname, "../../test")], // <----- (x) ------
compress: true,
},
module: {
rules: [
{
exclude: /node_modules/,
test: /\.test\.js$/,
use: "mocha-loader",
},
],
},
}
(**) test.js
const context = require.context('../test', true, /\.test\.js$/);
context.keys().forEach(context);
module.exports = context;
(***) test.html
<!DOCTYPE html>
<html>
<head>
<script src="test.bundle.js"></script>
</head>
</html>
Keeping line (x) inside webpack.config.js and everything is working fine: Whenever I update any test file *.test.js, for example utl.test.js, webpack recompiles and the browser is updated automatically.
My question now is, why do I actually need line (x)? If I remove it, updates on utl.test.js still trigger webpack compiler but I do have to reload the browser manually...
I would expect that setting the entry point test.js (which requires all *.test.js files) should be sufficient. And again, even without setting watchFiles, webpack is recompiling. But why do I need to reload the browser?
That's the webpack console output after *.test.js updates without setting up watchFiles (x).
assets by status 1.83 KiB [cached] 1 asset
assets by path *.js 3.47 MiB
assets by chunk 1.9 MiB (name: app)
asset app.bundle.js 1.9 MiB [emitted] (name: app)
asset app.89624c3f09c546a14ea9.hot-update.js 842 bytes [emitted] [immutable] [hmr] (name: app)
assets by chunk 1.57 MiB (name: test)
asset test.bundle.js 1.57 MiB [emitted] (name: test)
asset test.89624c3f09c546a14ea9.hot-update.js 1.42 KiB [emitted] [immutable] [hmr] (name: test)
assets by path *.json 55 bytes
asset test.89624c3f09c546a14ea9.hot-update.json 28 bytes [emitted] [immutable] [hmr]
asset app.89624c3f09c546a14ea9.hot-update.json 27 bytes [emitted] [immutable] [hmr]
Entrypoint app 1.9 MiB (1.83 KiB) = app.bundle.js 1.9 MiB app.89624c3f09c546a14ea9.hot-update.js 842 bytes 1 auxiliary asset
Entrypoint test 1.57 MiB = test.bundle.js 1.57 MiB test.89624c3f09c546a14ea9.hot-update.js 1.42 KiB
cached modules 2.71 MiB (javascript) 1.83 KiB (asset) [cached] 211 modules
runtime modules 54.6 KiB 27 modules
javascript modules 427 bytes
../test/ sync \.test\.js$ 178 bytes [built]
../test/utl.test.js 249 bytes [built] [code generated]
webpack 5.74.0 compiled successfully in 79 ms
And that's the corresponding output in the browser before manual reload:
[webpack-dev-server] App updated. Recompiling... 2 index.js:551:22
[webpack-dev-server] App hot update... index.js:551:22
[HMR] Checking for updates on the server... log.js:24:12
[webpack-dev-server] App hot update... index.js:551:22
[HMR] Checking for updates on the server... log.js:24:12
[HMR] Nothing hot updated. log.js:24:12
[HMR] App is up to date. log.js:24:12
[HMR] Updated modules: log.js:24:12
[HMR] - ../test/utl.test.js?9798 log.js:24:12
[HMR] - ../test/utl.test.js?a7c9 log.js:24:12
[HMR] App is up to date. log.js:24:12
Here are the respective logs when setting watchFiles. (I did already run them through a diffing tool.)

Nest.js framework hot reload doesn't work

I've followed the steps of the documentation:
https://docs.nestjs.com/techniques/hot-reload
I'm running this command: npm run webpack but it closes, it returns me the prompt and it doesn't stay watching the files:
gabriel#roraima-tv:/var/www/studying/tera-ping-pong$ npm run webpack
> tera-ping-pong#0.0.0 webpack /var/www/studying/tera-ping-pong
> webpack --config webpack.config.js
webpack is watching the files…
Hash: 6e13d56ba7d77331e5c2
Version: webpack 4.23.1
Time: 3014ms
Built at: 11/01/2018 1:39:11 PM
Asset Size Chunks Chunk Names
dist/app.controller.d.ts 177 bytes [emitted]
dist/app.module.d.ts 35 bytes [emitted]
dist/app.service.d.ts 56 bytes [emitted]
dist/main.d.ts 11 bytes [emitted]
dist/main.hmr.d.ts 11 bytes [emitted]
server.js 39 KiB main [emitted] main
Entrypoint main = server.js
[0] multi webpack/hot/poll?1000 ./src/main.hmr.ts 40 bytes {main} [built]
[./node_modules/webpack/hot/log-apply-result.js] (webpack)/hot/log-apply-result.js 1.27 KiB {main} [built]
[./node_modules/webpack/hot/log.js] (webpack)/hot/log.js 1.11 KiB {main} [built]
[./node_modules/webpack/hot/poll.js?1000] (webpack)/hot/poll.js? 1000 1.15 KiB {main} [built]
[./src/app.controller.ts] 1.44 KiB {main} [built]
[./src/app.module.ts] 1.03 KiB {main} [built]
[./src/app.service.ts] 883 bytes {main} [built]
[./src/main.hmr.ts] 1.07 KiB {main} [built]
[#nestjs/common] external "#nestjs/common" 42 bytes {main} [built]
[#nestjs/core] external "#nestjs/core" 42 bytes {main} [built]
gabriel#roraima-tv:/var/www/studying/tera-ping-pong$
Therefore, whenever I add my *.ts files changes and they aren't being reloaded until the server restarts.
you can just use this command in the CLI, it comes by default :
npm run start:dev
You can run nest in watch mode
nest start --watch
the problem is that you used
npm start
only instead of
npm start:dev
which runs the server in watch mode:
First install the required packages:
npm i --save-dev webpack-node-externals start-server-webpack-plugin
Once the installation is complete, create a webpack-hmr.config.js file in the root directory of your application.
const webpack = require('webpack');
const nodeExternals = require('webpack-node-externals');
const StartServerPlugin = require('start-server-webpack-plugin');
module.exports = function(options) {
return {
...options,
entry: ['webpack/hot/poll?100', options.entry],
watch: true,
externals: [
nodeExternals({
allowlist: ['webpack/hot/poll?100'],
}),
],
plugins: [
...options.plugins,
new webpack.HotModuleReplacementPlugin(),
new webpack.WatchIgnorePlugin([/\.js$/, /\.d\.ts$/]),
new StartServerPlugin({ name: options.output.filename }),
],
};
};
To enable HMR, open the application entry file (main.ts) and add the following webpack-related instructions:
declare const module: any;
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
if (module.hot) {
module.hot.accept();
module.hot.dispose(() => app.close());
}
}
bootstrap();
The documentation for hot reloading in Nest.js has been updated and it is clearly described step by step how to make this work
You can read more here: https://docs.nestjs.com/recipes/hot-reload

Load native npm x509 package using webpack in serverless

I'm testing the aws iot features using serverless framework. So for one usecase I wasn't to analyse the x509 certificates iot devices register with aws from aws lambdas, I saw npm package named x509 to do it easily. It's a native package, so I'm building it in Ubuntu 64 bit box to make it compatible with aws environment.
Issues is the webpack is not identifying this npm package even after my package importing it.
The error it saying is,
$ sls package
Serverless: Bundling with Webpack... Time: 245ms Built at: 05/05/2018 11:04:55 AM
Asset Size Chunks Chunk Names
jitr/index.js 11.9 KiB 0 [emitted] jitr/index jitr/build/Release/x509.node 44.2 KiB [emitted] Entrypoint jitr/index = jitr/index.js [0] ./jitr/index.js 7.39 KiB {0} [built] [1] external "aws-sdk" 42 bytes {0} [built] [2] ./jitr/node_modules/x509/index.js 1.29 KiB {0} [built] [3] external "fs" 42 bytes {0} [built]
ERROR in ./jitr/node_modules/x509/index.js Module not found: Error: Can't resolve './build/Release/x509' in '/home/serverless/jitr/node_modules/x509' # ./jitr/node_modules/x509/index.js 1:11-42 # ./jitr/index.js
Error --------------------------------------------------
Webpack compilation error, see above
For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.
Get Support --------------------------------------------
Docs: docs.serverless.com
Bugs: github.com/serverless/serverless/issues
Forums: forum.serverless.com
Chat: gitter.im/serverless/serverless
Your Environment Information -----------------------------
OS: linux
Node Version: 8.10.0
Serverless Version: 1.26.1
My package dir structure is,
- myproject
-- webpack.config.js
-- serverless.yml
-- jitr
--- index.js <- Here I'm importing x509
--- node_modules
---- x509
----- index.js <- First line: var x509 = require('./build/Release/x509')
----- build/Release/x509.node
My webpack config file
const path = require("path");
const slsw = require("serverless-webpack");
const nodeExternals = require("webpack-node-externals");
const CopyWebpackPlugin = require("copy-webpack-plugin");
module.exports = {
entry: slsw.lib.entries,
target: "node",
output: {
libraryTarget: "commonjs2",
path: path.resolve(__dirname, ".webpack"),
filename: "[name].js"
},
module: {
rules: [
{
test: /\.json$/,
loader: "json-loader"
},
{
test: /\.node$/,
loader: "node-loader"
}
]
},
mode: "none",
externals: [
nodeExternals({
whitelist: ["x509"]
})
],
plugins: [
new CopyWebpackPlugin([
{
from: "jitr/node_modules/x509/build/Release/x509.node",
to: "jitr/build/Release/x509.node",
toType: "file"
}
])
]
};
What might be going wrong here ?. I was looking for more debugging steps when doing webpack command, but here i'm using a serverless plugin named serverless-webpack to package and deploy the lambdas.
Aws lambda have access to raw openssl command, if nothing works I have to use that command from nodejs to do the things.

How to suppress built modules from webpack terminal output?

When using webpack2.x to build my project , the terminal console the build log like this:
Hash: d09758ddb088e1f8cd3b
Version: webpack 2.2.1
Time: 9450ms
Asset Size Chunks Chunk Names
app.d09758ddb088e1f8cd3b.js 28.7 kB 0 [emitted] app
vendor.d09758ddb088e1f8cd3b.js 206 kB 1 [emitted] vendor
style.d09758ddb088e1f8cd3b.css 1.89 kB 0 [emitted] app
index_bundle.html 852 bytes [emitted]
[1] ./~/vue/dist/vue.common.js 226 kB {1} [built]
[1] ./~/vue/dist/vue.common.js 226 kB {1} [built]
[3] ./~/vue-loader/lib/component-normalizer.js 1.12 kB {0} [built]
[4] ./~/process/browser.js 5.3 kB {1} [built]
[5] ./~/lodash/lodash.js 540 kB {1} [built]
[7] ./~/axios/index.js 40 bytes {1} [built]
[8] ./~/vue-router/dist/vue-router.common.js 56.1 kB {1} [built]
How can I remove messages like ./~/vue/dist/vue.common.js 226 kB {1} [built] and get just this output:
Hash: d09758ddb088e1f8cd3b
Version: webpack 2.2.1
Time: 9450ms
Asset Size Chunks Chunk Names
app.d09758ddb088e1f8cd3b.js 28.7 kB 0 [emitted] app
vendor.d09758ddb088e1f8cd3b.js 206 kB 1 [emitted] vendor
style.d09758ddb088e1f8cd3b.css 1.89 kB 0 [emitted] app
index_bundle.html 852 bytes [emitted]
You can use the stats option maxModules and set it 0 so it won't show any module built. This option is currently undocumented. In your webpack config add:
stats: {
maxModules: 0
}
Theoretically you should be able to use modules: false and chunkModules: false but that does not appear to work with the webpack CLI, at least it works with the webpack-dev-middleware.
The option is now documented at Configuration - Stats.
using webpack node.js API:
const compiler = webpack(config);
compiler.run((err, stats) => {
if (err) {
console.error(err.stack || err);
if (err.details) console.error(err.details);
process.exit(1);
}
process.stdout.write(stats.toString({
chunks: false,
colors: true
}) + '\n');
if (stats.hasErrors()) {
process.exit(2);
}
});
When using webpack CLI, you can try --hide-modules option.
Since the --hide-modules seems to be removed, adding the following to your webpack.config.js:
stats: {
modules: false
}
This option is documented here: Stats | webpack
Actually, there are plenty of options how to configurate your webpack cli output. You can set up it in stats object in your webbapck.config file. Unfortunately I was not able to find the option which hides the lines you want.
See Docs about setting up cli output

Is there a way to make webpack not show all the chunks it's compiling?

The default webpack CLI output is way too verbose for my liking. As soon as I import React from one of my files, the output explodes, showing all of the chunks (?) being packed:
webpack result is served from /
content is served from /Users/me/myproject
Hash: aaaf5afc6582f3222f55
Version: webpack 1.12.14
Time: 1175ms
Asset Size Chunks Chunk Names
index.js 677 kB 0 [emitted] main
chunk {0} index.js (main) 643 kB [rendered]
[0] ./src/app.js 574 bytes {0} [built] [1 error]
[1] ./~/react/react.js 56 bytes {0} [built]
[2] ./~/react/lib/React.js 1.49 kB {0} [built]
[3] ./~/react/lib/ReactDOM.js 3.71 kB {0} [built]
[4] ./~/process/browser.js 2.06 kB {0} [built]
...
[155] ./~/fbjs/lib/mapObject.js 1.47 kB {0} [built]
[156] ./~/react/lib/onlyChild.js 1.21 kB {0} [built]
[157] ./~/react/lib/deprecated.js 1.77 kB {0} [built]
[158] ./~/react-dom/index.js 63 bytes {0} [built]
[159] ./src/component.js 339 bytes {0} [built] [1 error]
I really don't care about all of that extra information. I'd be happy with a way to either:
Disable the chunks altogether, just showing the overall progress
Only show my own code, not stuff I'm importing from my node_modules
At the moment my webpack command is webpack-dev-server --progress. My webpack config is pretty basic, just specifying entry, output, and loaders for babel and eslint.
From the command line, I haven't been able to find a way to do this.
However, if you have a webpack.config.js file, there are two options for suppressing the list of modules ..
devServer: {
stats: 'errors-only'
}
or
devServer: {
stats: { chunks: false }
}
Hope that helps.
If you have a webpack.config.js in your project, add the following to the returned object of the exports function:
devServer: {
stats: {
assets: false,
children: false,
chunks: false,
hash: false,
modules: false,
publicPath: false,
timings: false,
version: false,
warnings: true,
colors: {
green: '\u001b[32m'
}
}
}
This worked in my case:
webpack --hide-modules
There is webpack undocumented option --hide-modules.
You can pass it to webpack from webpack-dev-server by adding this line to webpack config:
devServer: { 'hide-modules': true }
Since Webpack 3+ there is --display flag which allows fine-grained control over output:
--display=(verbose|detailed|normal|minimal|errors-only|none)
In the webpack.config.ts, you can try
devServer : {
stats: 'minimal',
watchOptions: {
poll: undefined,
aggregateTimeout: 300,
ignored: /node_modules/
}
}
Hope that works.

Resources