art-template icon indicating copy to clipboard operation
art-template copied to clipboard

模板里嵌套子模板如何使用webpack定义的路径呢?

Open liangei opened this issue 7 years ago • 5 comments

我在webpack里定义了 resolve: { alias: { "@" : resolve('/src'), "com" : resolve('/src/components') } },

我希望在模板index.art里 <% include('~com/head.art', {active: 3}) %>

但发现这样不行。。 大佬们请教一下~~~

liangei avatar Jul 24 '18 08:07 liangei

同问!遇到了同样的问题

zhaohes avatar Nov 30 '18 18:11 zhaohes

<% include(require('com/head.art'), {active: 3}) %> 试试这样写,alias需要用模块加载的方式引入 @zhaohes @liangei

BiYuqi avatar Dec 01 '18 22:12 BiYuqi

@BiYuqi ,不可以吧?会报require not a function。。。

henryzp avatar Dec 17 '18 02:12 henryzp

@henryzp 需要安装html-loader这个插件配合 webpack配置

BiYuqi avatar Dec 17 '18 03:12 BiYuqi

虽然这个issue已经发了很久了 但是我还是想说 这个问题在art-template-loader的options里配置 resolveFilename选项即可 然后就能在回调里拿到include的路径 对路径进行操作即可 比如webpack.config.js

var path = require('path')
var resolveFilename = require('art-template/lib/compile/adapter/resolve-filename.js')

var srcResolve = path.resolve('./src')
var isAlias = /^@/;

module.exports = {
  // ....
  module: {
    rules: [
      {
        test: /\.art$/,
        loader: "art-template-loader",
        options: {
          resolveFilename: function (_path, context) {
            isAlias.test(_path)&&(_path= _path.replace(isAlias, srcResolve));
            return resolveFilename(_path, context);
          }
        }
      }
    ]
  },
  resolve: {
    alias: {
      '@': srcResolve
    }
  }
  // ....
}

643104191 avatar Oct 17 '19 01:10 643104191