I have the following nex.config.js configuration
const withImages = require("next-images");
module.exports = withImages({
fileExtensions: ["jpg", "jpeg", "png", "gif"],
assetPrefix: "https://cdn.mydomain.co.za",
webpack(config) {
return config;
},
});
I am trying to pull my images from my cdn instead of locally. The problem with this is that this plugin also tries to pull javascript and css from _next/static/chunks
and therefore my website breaks because files like this one below dont exist on my cdn
https://cdn.mydomain.co.za/_next/static/chunks/main.js?ts=1605264866650
Is there a way to specify in the config that I only want to pull images from my CDN and not all assets?
Related
Is it possible to add a local html file in the nativescript webview ?
If yes How can I do it using javascript ?
When I add online page it works , I can add www.google.com in the webview it works .But I want to add a local page but I don't find a way to do this .
Yes, it's possible. You need to consider that all NativeScript apps are build by default with Webpack and the default webpack.config.js will take care of certain files (like the ones in a fonts folder or like all images with *.png and *..jpg extensions). The webpack build will bundle all JavaScript files and in the case of the Angular flavor will also cognitively include the Angular related HTML files. However, the default webpack.config.js won't "know" about your custom HTML file.
The solution is to let Webpack know that it should copy the specific HTML file. This should be done via the CopyWebpackPlugin section in webpack.config.js file.
Example (assuming we have a file called test.html in the app directory)
new CopyWebpackPlugin([
{ from: { glob: "test.html" } }, // HERE
{ from: { glob: "fonts/**" } },
{ from: { glob: "**/*.jpg" } },
{ from: { glob: "**/*.png" } },
], { ignore: [`${relative(appPath, appResourcesFullPath)}/**`] }),
For real-life example see this config where this HTML file is the one we are targeting.
I'm trying to set up GZIP for my static files (primarily JavaScript) which have been pre-compressed using Webpack's compression plugin (compression-webpack-plugin).
I have followed this article on serving pre-compressed static files using .NET Core - https://dzone.com/articles/serving-pre-compressed-static-files-in-aspnet-core.
I have the following in my Startup.Configure method:
var mimeTypeProvider = new FileExtensionContentTypeProvider();
app.UseStaticFiles(new StaticFileOptions
{
OnPrepareResponse = context =>
{
var headers = context.Context.Response.Headers;
var contentType = headers["Content-Type"];
if (contentType != "application/x-gzip" && !context.File.Name.EndsWith(".gz"))
{
return;
}
var fileNameToTry = context.File.Name.Substring(0, context.File.Name.Length - 3);
if (mimeTypeProvider.TryGetContentType(fileNameToTry, out var mimeType))
{
headers.Add("Content-Encoding", "gzip");
headers["Content-Type"] = mimeType;
}
}
});
This doesn't seem to have helped, my assets are being served without compression. When adding a breakpoint into the OnPrepareResponse action method, it is not hit for any JavaScript files. It does get hit for my favicon and my logo image though.
I've also tried this NuGet package - https://github.com/AnderssonPeter/CompressedStaticFiles - which also didn't seem to solve my problem.
Here's a screenshot of my wwwroot folder for reference:
I'm trying to serve those JS files in the /dist folder using the following references in my _Layout.cshtml view:
<script src="~/dist/vendors~main.bundle.js"></script>
<script src="~/dist/main.bundle.js"></script>
Finally, here's an example of the headers for vendors~main.bundles.js (taken from Chrome dev tools):
Any ideas on why this isn't working?
UPDATE 08/05/2019
It seems that the static files middleware is conflicting with the Webpack Dev Middleware somehow. If I remove the following lines of code from my Startup Configure method then it works and the breakpoint in OnPrepareResponse is hit for all files as expected:
app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions
{
HotModuleReplacement = true
});
Is this a bug or am I just doing something wrong?
I have to use Webpack for one of my projects to build front-end bundles for js, css and other static assets. It does the job well, but in my early stages of the project I've got only some css and static images and no js files yet. Here is my full webpack.config.js
const Webpack = require("webpack");
const Glob = require("glob");
const path = require("path");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const configurator = {
entries: function(){
var entries = {
application: [
'./assets/dummy.js',
],
}
return entries
},
plugins() {
var plugins = [
new CopyWebpackPlugin([{from: "./assets",to: ""}], {copyUnmodified: true,ignore: ["css/**", "js/**", "**.js"] }),
];
return plugins
},
moduleOptions: function() {
return {
rules: [
]
}
},
buildConfig: function(){
const env = process.env.NODE_ENV || "development";
var config = {
mode: env,
entry: configurator.entries(),
output: {filename: "[name].[hash].js", path: `${__dirname}/public/assets`},
plugins: configurator.plugins(),
module: configurator.moduleOptions()
}
return config
}
}
module.exports = configurator.buildConfig()
Practically what it does for me is copying assets to public dir. I don't have any javascripts yet, but they will be in the future. So, I tried commenting entry, setting it to null or empty string with no luck. It seems Webpack needs to process js files so badly. My current solution is creating an empty dummy.js file and feeding it to Webpack. Annoyingly it generates some 3.3kb application.afff4a3748b8d5d33a3a.js file with some boilerplate js code, despite that my source js file is totally empty.
I understand that this is an edge use case for Webpack and Webpack was primarily created for processing javascript, but I bet many people still use it not just for bundling javascripts. So, my question, is there a better, more elegant way to skip bundling js files in Webpack?
p.s.
I think, I've found a related question without an answer here How to make WebPack copy a library instead of bundling?
p.s.#2
The suggested duplicate question has an accepted answer with an invalid Webpack config Invalid configuration object., so, I can't use it to solve my issue.
Moreover, the answer reads
webpack will create a dummy javascript file
and I'm specifically asking how to avoid creating unnecessary files.
I've recently changed my library code to es6 using webpack, babel, and all of this family.
So my production is single file in the end.. compares to before that I had a lot of files that were being loaded dynamically by demand.
My problem is that I'm using another library that loads some of their class dynamically, and in order to customise them I need to provide URL to my custom objects/classes.
It was not a problem at first, because I had a lot of files, but now my code is bundled into a single file.
Is there a way js/es6/... to give some content/string and create a fake url to it, so the other library will 'load' it ?
for example:
My classes are
file ../myLib/CustomLayer2D.js
Class CustomLayer2D {
...
}
file ../myLib/CustomLayer3D.js
Class CustomLayer3D {
...
}
file ../myLib/CustomLayer.js
This is the use of the other library
Accessor.createSubClass([layer], {
viewModulePaths:{
"2d": "need to provide here a url to 2d layer file",
"3d": "need to provide here a url to 3d layer file"
}
});
file ../myLib/CustomLayer.js
Class CustomLayer2D {
...
}
Class CustomLayer3D {
...
}
Accessor.createSubClass([], {
viewModulePaths:{
"2d": "need to provide here a path to 2d layer",
"3d": "need to provide here a path to 3d layer"
}
});
I'd prefer finding a way and not exclude my custom classes from the bundle (my last resort).
Webpack can bundle all assets in one js file, but you can still use separate js files with Webpack or Webpack-dev-server.
You can use webpack-dev-server's config or just put your ../myLib/CustomLayer3D.js beside the webpack bundle file to dynamically load 2D or 3D.js file in you original way instead of require/import them in CustomLayer.js.
Besides what's your another library? maybe it has some other way to load a class dynamically.
Thanks for #Kaiido comment and a bit of more research I've came to a full solution.
In our webpack.config.js we are adding our files to run through the raw-loader - so instead of bundling the real code it will bundle the file as it is - Text file.
In our code we simply import our file/s and put it in <<The Code>>
URL.createObjectURL(new Blob([<<The Code>>], {type: 'text/plain'}));
It looks like this
webpack.config.js
under rules property
{
test: /myLib\\CustomLayer2D.js|myLib\\CustomLayer3D.js/,
loader: 'raw-loader',
}
file myLib/CustomLayer.js
import layer2DCode from './myLib/CustomLayer2D.js'
import layer3DCode from './myLib/CustomLayer3D.js'
Accessor.createSubClass([], {
viewModulePaths:{
"2d": URL.createObjectURL(new Blob([layer2DCode], {type: 'text/plain'})),
"3d": URL.createObjectURL(new Blob([layer3DCode], {type: 'text/plain'}))
}
});
So I'm using Laravel 5.4 and I use webpack to compile multiple .js files in 1 big js file.
const { mix } = require('laravel-mix');
// Compile all CSS file from the theme
mix.styles([
'resources/assets/theme/css/bootstrap.min.css',
'resources/assets/theme/css/main.css',
'resources/assets/theme/css/plugins.css',
'resources/assets/theme/css/themes.css',
'resources/assets/theme/css/themes/emerald.css',
'resources/assets/theme/css/font-awesome.min.css',
], 'public/css/theme.css');
// Compile all JS file from the theme
mix.scripts([
'resources/assets/theme/js/bootstrap.min.js',
'resources/assets/theme/js/app.js',
'resources/assets/theme/js/modernizr.js',
'resources/assets/theme/js/plugins.js',
], 'public/js/theme.js');
This is my webpack.mix.js to do it (same for css). But I want to get something like: resources/assets/theme/js/* to get all files from a folder. So when I make a new js file in the folder that webpack automatically finds it, and compile it when I run the command.
Does someone know how to this?
Thanks for helping.
If anyone wants the code to compile all sass/less/js files in a directory to a different directory with the same filename you can use this:
// webpack.mix.js
let fs = require('fs');
let getFiles = function (dir) {
// get all 'files' in this directory
// filter directories
return fs.readdirSync(dir).filter(file => {
return fs.statSync(`${dir}/${file}`).isFile();
});
};
getFiles('directory').forEach(function (filepath) {
mix.js('directory/' + filepath, 'js');
});
Wildcards are actually allowed using the mix.scripts() method, as confirmed by the creator in this issue. So your call should look like this:
mix.scripts(
'resources/assets/theme/js/*.js',
'public/js/theme.js');
I presume it works the same for styles, since they use the same method to combine the files.
Hope this helps you.