easy-team / egg-react-webpack-boilerplate

Egg React Server Side Render(SSR) / Client Sider Render(CSR)
https://easyjs.cn/egg-react
MIT License
671 stars 93 forks source link

Inject js/css时,提供relative path的选项? #15

Closed martin-liu closed 6 years ago

martin-liu commented 6 years ago

我发现页面render时inject到html里的css/js文件都是绝对路径, 比如/public/xxx.

但有时候我们可能在一个domain下有多个项目,比如xxx.com/projectA/,这时我们在deploy时需要设置<base href="/projectA/" >, 然后css/js的reference需要是相对路径。

我看了下,当前的code是读config/manifest.json里的deps然后inject. 所以我现在是添加了下面的code到webpack.config.js里来work around这个问题:

  done() {
    // replace `/public` to `public` for relative path
    let file = './config/manifest.json';
    let manifest = require(file);
    manifest.deps = JSON.parse(JSON.stringify(manifest.deps).replace(/"\//gm, '"'));
    fs.writeFileSync(file, JSON.stringify(manifest, null, 2), 'utf8');

    console.log('---webpack compile finish---');
  }

不过,是不是能添加一个option来选择是否inject相对路径?

hubcarl commented 6 years ago

你可以修改一下 publicPath 为 /projectA/public/xxx 方式 不知道是否满足你的要求 @martin-liu

martin-liu commented 6 years ago

@hubcarl 这个不work

Error: ENOENT: no such file or directory, open '/var/folders/9y/pb986sqd16x39vrd7ggqcc88390xc6/T/easywebpack/mytest/dev/dll/file/public/js/vendor.js'
    at Object.fs.openSync (fs.js:667:18)
    at Object.fs.readFileSync (fs.js:572:33)
    at opts.assets.forEach.chunk (/opt/code/mytest/node_modules/webpack-asset-file-plugin/lib/index.js:15:28)
    at Array.forEach (<anonymous>)
    at compiler.hooks.afterCompile.tap.compilation (/opt/code/mytest/node_modules/webpack-asset-file-plugin/lib/index.js:12:24)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/opt/code/mytest/node_modules/tapable/lib/HookCodeFactory.js:24:12), <anonymous>:9:1)
    at AsyncSeriesHook.lazyCompileHook [as _callAsync] (/opt/code/mytest/node_modules/tapable/lib/Hook.js:35:21)
    at compilation.seal.err (/opt/code/mytest/node_modules/webpack/lib/Compiler.js:483:30)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/opt/code/mytest/node_modules/tapable/lib/HookCodeFactory.js:24:12), <anonymous>:9:1)
    at AsyncSeriesHook.lazyCompileHook [as _callAsync] (/opt/code/mytest/node_modules/tapable/lib/Hook.js:35:21)
hubcarl commented 6 years ago

@martin-liu npx easy clean 一把 看看

Leotw commented 4 years ago

你可以修改一下 publicPath 为 /projectA/public/xxx 方式 不知道是否满足你的要求 @martin-liu

请问是哪边的publicPath