WebOptimizer icon indicating copy to clipboard operation
WebOptimizer copied to clipboard

ExpandGlobs add prefix '/'

Open dean-shanghai opened this issue 5 years ago • 10 comments

following inputFiles + debug mode (no bundling and minification), the output js paths missing prefix "/"! { "outputFileName": "/lang.min.js", "inputFiles": [ "/i18n/**/*.js" ], "minify": { "enabled": false } },

dean-shanghai avatar Feb 21 '20 07:02 dean-shanghai

Does this mean that all href and src attributes will always be rooted with a / at the beginning? Are there any consequences to that?

madskristensen avatar Feb 21 '20 16:02 madskristensen

What problem is this trying to solve?

skimedic avatar Feb 21 '20 23:02 skimedic

Does this mean that all href and src attributes will always be rooted with a / at the beginning? Are there any consequences to that?

  1. in release mode (bundle + minify), there is no problem, because the outputFileName path will be used;
  2. while in debug mode (NO bundling + NO minification)
  • without the prefix '/': those unbundled css/js files' path will be relative to the current page Url, which will cause 404!
  • and all the css/js resource files are in the wwwroot folder, so rooted with a '/' is OK.

dean-shanghai avatar Feb 22 '20 02:02 dean-shanghai

What problem is this trying to solve?

Trying to fix the path problems in DEBUG mode when using wildcards inputFiles like the following: "inputFiles": [ "/jsfolder/**/*.js" ]

dean-shanghai avatar Feb 22 '20 02:02 dean-shanghai

Aren't you using services.AddWebOptimizer(false, false); in development mode?

skimedic avatar Feb 22 '20 03:02 skimedic

Aren't you using services.AddWebOptimizer(false, false); in development mode?

No, i'm not specifying the minify flag, just let the running mode decide to minify or not.

I'm USING WebOptimizer.Core and WebOptimizer.Core.Sass Source Code from github; NOT using nuget packages (because of the path problem);

Code using:

        services.AddWebOptimizer(pipeline =>
        {
            var bundles = BundleHandler.GetBundles(Configuration["SiteSettings:BundleConfig"]); //using nuget package: BundlerMinifier.Core
            foreach (var bundle in bundles)
            {
                if (bundle.OutputFileName.EndsWith(".css"))
                {
                    pipeline.AddScssBundle(bundle.OutputFileName, bundle.InputFiles.ToArray());
                }
                else if (bundle.OutputFileName.EndsWith(".js"))
                {
                    pipeline.AddJavaScriptBundle(bundle.OutputFileName, bundle.InputFiles.ToArray());
                }
            }
        });

dean-shanghai avatar Feb 22 '20 04:02 dean-shanghai

This PR fixes the problem by always prefixing a slash, but what if you DO want the bundle to be on a relative path? The fix should check if the globbing pattern contains the prefix, and only prepend it when that's the case.

mrdnote avatar Jun 03 '21 20:06 mrdnote

This PR fixes the problem by always prefixing a slash, but what if you DO want the bundle to be on a relative path? The fix should check if the globbing pattern contains the prefix, and only prepend it when that's the case.

check on what standard? there is no extra info for you to know whether to add the slash or not.

you can use the bundle (release or dev mode) on root level pages (www.domain.com/aaa), or pages in subfolders (www.domain.com/aaa/bbb), so relative path will cause problems.

I can think of two ways to solve this problem:

  1. always generate relative paths to the root directory (not to the current directory), which is the easiest way, everyone follow this rule and it's ok;
  2. add flags (indicating whether or not to add this prefix slash) to every config node in the bundleconfig.json file?

dean-shanghai avatar Jun 04 '21 02:06 dean-shanghai

Check on whether the globbing pattern is relative or not. If it contains a forward slash, also apply this slash to the output files, if not, then don't apply it. I think this can be achieved inside Asset.ExpandGlobs.

mrdnote avatar Jun 04 '21 08:06 mrdnote

Seems quite possible that this fixes #199 Is there anything I can do to help get this pr merged?

Nouwan avatar Nov 12 '21 10:11 Nouwan