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
Honor the public path by removing "/" from generated CSS and JS files #1717
Comments
I'm having the same problem in laravel mix 2.1. This is problematic if you want to insert script tags into a html using html webpack plugin. I tried laravel mix 2.0 and the issue is not present in that version. Can someone help? my webpack.mix.js code:
output in terminal: (the js file uri's should not start with a '/')
EDIT: |
Having the same issue with laravel mix 2.1.14. I am trying to inject my script via the html-webpack-plugin, but i end up with I did try to manipulate the webpack config just to see whats going on and found out that the entry file (which should be @JeffreyWay It would be nice to know if the leading slash is intentional. At the moment it just causes problems, at least for me.. Steps To ReproduceI am using a basic mix setup with a self written plugin called 'mix-html-builder' as an additional dependency. webpack.mix.js
In
And as I said, the path of my css file looks fine. |
I'm having the exact same problem as @philicevic . |
As a hacky workaround, I've added the following to my webpack.mix.js
Not too proud of the solution but it works. Hopefully now that @JeffreyWay has finished his site updates, he'll have a bit of time to take a look. |
is there any way to apply versions from |
Any update about this issue ? |
@tyler36 your workaround doesn't work for me because precache-manifest file name contain a unique hash |
@donmbelembe mine doesn't have a hash.
|
@JeffreyWay ping |
Having same problem. Furthermore, if I include chunk in the file name it also adds it to app.js and vendor.js files and then I can't use those files in index blade on Laravel. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Hi for anyone still having this issue. I could fix the problem by manually overwriting the publicPath setting like this: mix.webpackConfig({
output: {
publicPath: "",
},
}); Be sure to apply the right publicPath for your setup! |
Same problem here but setting up the Digging into the core of Laravel mix, I found out that there is an unnecessary leading slash in the "entry": {
"/main": [
"/Users/lorisleiva/projects/acme/src/main.js"
]
}, This is fixed by chaining another // src/builder/Entry.js
createName(output) {
let name = output
.pathFromPublic(Config.publicPath)
.replace(/\.js$/, "")
.replace(/\\/g, "/")
.replace(/^\//, ""); // <- Adding this line makes it work.
this.base = path.parse(name).dir;
return name;
} This now outputs: "entry": {
"main": [
"/Users/lorisleiva/projects/acme/src/main.js"
]
}, I'm happy to make a PR for this but I have no ideas if this is going to break something for non-standalone application. Edit: In the meantime, you can add this override function to your mix file. mix.override(config => {
config.entry = Object.keys(config.entry).reduce((acc, key) => {
acc[key.replace(/^\//, "")] = config.entry[key];
return acc;
}, {});
}); |
@lorisleiva Thanks so much for sharing. I would certainly support a PR or fork. I have the same issue where setting publicPath empty screws up my chunk files. Pretty frustrating. When I applied your suggestion it solved the problem slash in front of my js file "/js/app.js" but I still have a slash in front of my css file "//css/app.css". Just curious if you had a similar issue with css, and if so can you share how you got around it? Thanks. |
Argh sorry to hear that. No I’m afraid on that particular project I’ve got all my CSS defined within single file components in Vue.js. The code I pasted above should remove the trailing slashes of all the keys in the |
@lorisleiva for me the override function won't work. I use mix 3.0. I'm stuck because I cannot update because of the empty css file bug... |
Thanks @lorisleiva your solution is working in my case. Because of this problem I had to add .webpackConfig({
output: {
filename: 'js/[name].js',
chunkFilename: 'js/[name].js?id=[chunkhash]',
path: path.resolve(__dirname, 'public'),
},
}) As an addition to the discussion, I've seen that the slash appeared also under Chunk Names in the logs, and it disappeared after the fix. |
As a solution, I've added Altho I'd like to see a solution from Symfony/Asset side, I've updated on the Mix side to strip prefix from items. const collect = require('collect.js');
/**
* Update manifest to remove leading slash of key => value pairs
* @author Elan Ruusamäe <glen@pld-linux.org>
* @see https://github.com/symfony/symfony/issues/36234
*/
mix.extend('updateManifestPathsRelative', (config) => {
config.plugins.push(new class {
apply(compiler) {
compiler.plugin('done', () => {
const manifest = {};
collect(Mix.manifest.get()).each((value, key) => {
key = this.normalizePath(key);
value = this.normalizePath(value);
manifest[key] = value;
});
Mix.manifest.manifest = manifest;
Mix.manifest.refresh();
});
}
/**
* @param {string} filePath
*/
normalizePath(filePath) {
if (filePath.startsWith('/')) {
filePath = filePath.substring(1);
}
return filePath;
}
})
});
mix.updateManifestPathsRelative(); refs: |
This seem to be related to this: #2862. |
I'll open a PR with @lorisleiva fix. Hope this get merged. |
npm list --depth=0
)node -v
): v8.11.3npm -v
): 6.1.0Reference:
This issue was also mentioned in Workbox repository:
GoogleChrome/workbox#1534
Description:
Up until v1.7.2, Laravel-Mix was outputting the
js/app.js
andcss/app.css
with a forward slash like:When upgraded to v2.1.11, I noticed a double slash in the path:
This ends up resulting these two files to be loaded from
http://js/app.js
andhttp://css/app.css
which results a 404 error.However, this works fine for anyone running out-of-the box installation of Laravel. But when the application starts growing and you want to integrate a service worker plugin (i.e. Workbox), then the real monster comes to play (see the Steps To Reproduce section below).
This is only because Laravel-Mix isn't honoring the public path in the first place. Regardless of v1.7.2 or v2.1.11, it shouldn't set the path to "/" after compiling the scripts and sass files. It should rather set the paths relative, such as: "js/app.js" and "css/app.css" instead of "/js/app.js" and "/css/app.css".
I noticed the fonts and images aren't using this leading "/" while outputting them in the public directory. Only the js and css files are including it.
To understand what's being generated from the Workbox plugin, take a look at my precache manifest file generated by the workbox plugin (refer to the Steps To Reproduce section below) while using Laravel Mix v1.7.2:
This worked for me until the forward slashes doubled in Laravel Mix v2.1.11 only for js and css files:
If Laravel Mix returns the JS and CSS file paths without any leading slash, that will solve this issue. I hope @JeffreyWay will look into this.
Steps To Reproduce:
Install the
workbox-webpack-plugin
and then update thewebpack.mix.js
file to use it for generating the service worker file:The text was updated successfully, but these errors were encountered: