vuejs / vuepress

📝 Minimalistic Vue-powered static site generator
https://vuepress.vuejs.org
MIT License
22.49k stars 4.77k forks source link

Relative image url in index.styl causes build failure #1569

Open butameron opened 5 years ago

butameron commented 5 years ago

Bug report

Version

1.0.0-alpha.47

Steps to reproduce

$ yarn add vuepress@next
$ echo '# Hello VuePress' > README.md
$ mkdir .vuepress
$ mkdir .vuepress/styles
$ echo 'body { background-image: url(./test.png); }'  > .vuepress/styles/index.styl
$ wget https://upload.wikimedia.org/wikipedia/commons/5/53/Wikipedia-logo-en-big.png -O .vuepress/styles/test.png
$ npx vuepress build

What is expected?

test.png is copied to asset dir by webpack. build complete successfully.

What is actually happening?

build failed with follwing error.

$ npx vuepress build
wait Extracting site metadata...
tip Apply theme @vuepress/theme-default ...
tip Apply plugin container (i.e. "vuepress-plugin-container") ...
tip Apply plugin @vuepress/register-components (i.e. "@vuepress/plugin-register-components") ...
tip Apply plugin @vuepress/active-header-links (i.e. "@vuepress/plugin-active-header-links") ...
tip Apply plugin @vuepress/search (i.e. "@vuepress/plugin-search") ...
tip Apply plugin @vuepress/nprogress (i.e. "@vuepress/plugin-nprogress") ...
[3:37:08 AM] Compiling Client
[3:37:09 AM] Compiling Server
[3:37:17 AM] Compiled Server in 8s
[3:37:23 AM] Compiled Client in 15s
(undefined) ./node_modules/@vuepress/core/.temp/style.styl
Module build failed (from ./node_modules/mini-css-extract-plugin/dist/loader.js):
ModuleNotFoundError: Module not found: Error: Can't resolve './test.png' in '/mnt/e/Temp/vuepress-test/node_modules/@vuepress/core/.temp'
    at factory.create (/mnt/e/Temp/vuepress-test/node_modules/webpack/lib/Compilation.js:823:10)
    at factory (/mnt/e/Temp/vuepress-test/node_modules/webpack/lib/NormalModuleFactory.js:397:22)
    at resolver (/mnt/e/Temp/vuepress-test/node_modules/webpack/lib/NormalModuleFactory.js:130:21)
    at asyncLib.parallel (/mnt/e/Temp/vuepress-test/node_modules/webpack/lib/NormalModuleFactory.js:224:22)
    at /mnt/e/Temp/vuepress-test/node_modules/neo-async/async.js:2825:7
    at /mnt/e/Temp/vuepress-test/node_modules/neo-async/async.js:6886:13
    at normalResolver.resolve (/mnt/e/Temp/vuepress-test/node_modules/webpack/lib/NormalModuleFactory.js:214:25)
    at doResolve (/mnt/e/Temp/vuepress-test/node_modules/enhanced-resolve/lib/Resolver.js:184:12)
    at hook.callAsync (/mnt/e/Temp/vuepress-test/node_modules/enhanced-resolve/lib/Resolver.js:238:5)
    at _fn0 (eval at create (/mnt/e/Temp/vuepress-test/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:15:1)
    at resolver.doResolve (/mnt/e/Temp/vuepress-test/node_modules/enhanced-resolve/lib/UnsafeCachePlugin.js:37:5)
    at hook.callAsync (/mnt/e/Temp/vuepress-test/node_modules/enhanced-resolve/lib/Resolver.js:238:5)
    at _fn0 (eval at create (/mnt/e/Temp/vuepress-test/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:15:1)
    at hook.callAsync (/mnt/e/Temp/vuepress-test/node_modules/enhanced-resolve/lib/Resolver.js:238:5)
    at _fn0 (eval at create (/mnt/e/Temp/vuepress-test/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:27:1)
    at resolver.doResolve (/mnt/e/Temp/vuepress-test/node_modules/enhanced-resolve/lib/DescriptionFilePlugin.js:42:38)
 @ ./node_modules/@vuepress/core/lib/node/internal-plugins/style/client.js 2:0-26
 @ ./node_modules/@vuepress/core/.temp/app-enhancers/0.js
 @ ./node_modules/@vuepress/core/.temp/internal/app-enhancers.js
 @ ./node_modules/@vuepress/core/lib/client/app.js
 @ ./node_modules/@vuepress/core/lib/client/clientEntry.js
 @ multi ./node_modules/@vuepress/core/lib/client/clientEntry.js
Error: Failed to compile with errors.
    at webpack (/mnt/e/Temp/vuepress-test/node_modules/@vuepress/core/lib/node/build/index.js:186:16)
    at finalCallback (/mnt/e/Temp/vuepress-test/node_modules/webpack/lib/MultiCompiler.js:247:12)
    at runWithDependencies.err (/mnt/e/Temp/vuepress-test/node_modules/webpack/lib/MultiCompiler.js:270:6)
    at done (/mnt/e/Temp/vuepress-test/node_modules/neo-async/async.js:2928:13)
    at runCompilers (/mnt/e/Temp/vuepress-test/node_modules/webpack/lib/MultiCompiler.js:174:48)
    at err (/mnt/e/Temp/vuepress-test/node_modules/webpack/lib/MultiCompiler.js:181:7)
    at compiler.run (/mnt/e/Temp/vuepress-test/node_modules/webpack/lib/MultiCompiler.js:263:7)
    at finalCallback (/mnt/e/Temp/vuepress-test/node_modules/webpack/lib/Compiler.js:220:39)
    at hooks.done.callAsync.err (/mnt/e/Temp/vuepress-test/node_modules/webpack/lib/Compiler.js:236:13)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/mnt/e/Temp/vuepress-test/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:33:1)
    at AsyncSeriesHook.lazyCompileHook (/mnt/e/Temp/vuepress-test/node_modules/tapable/lib/Hook.js:154:20)
    at onCompiled (/mnt/e/Temp/vuepress-test/node_modules/webpack/lib/Compiler.js:234:21)
    at hooks.afterCompile.callAsync.err (/mnt/e/Temp/vuepress-test/node_modules/webpack/lib/Compiler.js:631:15)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/mnt/e/Temp/vuepress-test/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook (/mnt/e/Temp/vuepress-test/node_modules/tapable/lib/Hook.js:154:20)
    at compilation.seal.err (/mnt/e/Temp/vuepress-test/node_modules/webpack/lib/Compiler.js:628:31)

Other relevant information

butameron commented 5 years ago

In addition, I also tested on index.styl of theme, and it seems to have the same problem.

In case of .vuepress/styles, I think I can use .vuepress/public to store image files and write absolute url into styl file. However, in case of styles/index.styl of theme dir, I will need to add copy config to chainWebpack manually.

Even if this is not a bug, this is very inconvenience.

DaleMatthews commented 5 years ago

This is still happening in vuepress 1.0.3. Simply importing main.css in index.styl in a bare-bones example:

[WDS] Errors while compiling. Reload prevented. index.js:150:9
./node_modules/@vuepress/core/.temp/style.styl (./node_modules/css-loader/dist/cjs.js??ref--13-oneOf-1-1!./node_modules/postcss-loader/src??ref--13-oneOf-1-2!./node_modules/stylus-loader??ref--13-oneOf-1-3!./node_modules/@vuepress/core/.temp/style.styl)
Module not found: Error: Can't resolve './main.css' in '/Users/dalematthews/workspace/vuepress-blog/node_modules/@vuepress/core/.temp'
vultur commented 4 years ago

派生主题使用 background-image url(search.svg) 存在同样的问题!

临时方案: background-image url(/search.svg) // 使用绝对路径,search.svg 存放在 .vuepress/public 目录

期望结果: 可在派生主题使用相对路径资源

meteorlxy commented 4 years ago

One possible solution is to copy all "unknown" files from .vuepress to .temp.

So the workaround could be something like:

// .vuepress/styles/index.styl

body
  background-image url(./test.png)
// .vuepress/config.js

module.exports = {
  plugins: [
    [require('./workaroundPlugin')]
  ]
}
// .vuepress/workaroundPlugin.js

const fs = require('fs-extra');
const path = require('path');
const globby = require('globby');

module.exports = (options, ctx) => {
  return {
    async ready() {
      const paths = await globby(['styles/**/*.{svg,png,jpg,ttf,woff,woff2}'], {
        cwd: ctx.vuepressDir
      });

      const copyFiles = []

      paths.forEach(item => {
        copyFiles.push(
          fs.copy(
            path.resolve(ctx.vuepressDir, item),
            path.resolve(ctx.tempPath, item.replace(/^styles\//, ''))
          )
        )
      })

      await Promise.all(copyFiles)
    }
  }
}
wesgro commented 4 years ago

It does not look like its possible to reference files in your public assets directory from css?

When I try to do something like

.class {
  background-image: url('./assets/images/thing.svg');
}

I get the same errors:

./docs/.vuepress/theme/components/SidebarGroup.vue?vue&type=style&index=0&lang=
scss& (./node_modules/mini-css-extract-plugin/dist/loader.js!./node_modules/css-loader/dist
/cjs.js??ref--10-oneOf-1-1!./node_modules/vue-loader/lib/loaders/stylePostLoader.js!./node_modules/postcss-loader/src??ref--10-oneOf-1-2!/Users/-/Projects/skyline/node_modules/sass-loader/dist/cjs.js??ref--10-oneOf-1-3!/Users/-/Projects/skyline/node_modules/style-resources-loader/lib??ref--10-oneOf-1-4!./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-options!./docs/.vuepress/theme/components/SidebarGroup.vue?vue&type=style&index=0&lang=scss&)
Module build failed (from ./node_modules/mini-css-extract-plugin/dist/loader.js):
ModuleNotFoundError: Module not found: Error: Can't resolve './assets/svgs/light/home-heart.svg' in 'vuepress/docs/.vuepress/theme/components'
bozau commented 4 years ago

Hi @bencodezen,

I'm a new user to both Vue and Vuepress, but I started to do some theming and this specific issue with 'styl' files and image references nearly caused me to stop even considering using Vuepress :( after a number of hours just trying to get the 'styl' files working.

EDIT: So, attempting with the following: .class{ background-image: url( /img/bg.jpg ) } is successful with a /img directory in the .vuepress/public. This issue isn't referenced/doco'd anywhere about the url reference not having periods or tilde's prior /img/bg.jpg is it?

I was wondering if there was a supported way to fix this issue? As it might cause others who come across this issue to not use it.

Kind Regards.

Bozza.