Skip to content
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

请问能不能支持把config放到script标签内部? #58

Closed
KarnaughK opened this issue Nov 1, 2019 · 3 comments
Closed

请问能不能支持把config放到script标签内部? #58

KarnaughK opened this issue Nov 1, 2019 · 3 comments

Comments

@KarnaughK
Copy link

类似 wepy 那样。。。
提示这个 issues 的主要原因和 #55 是一样的,webstorm折腾了半天还是没法格式化 标签内的代码,自己又不会写插件。。。如果能把 config 放到 script 内,作为一种可选的形式,那就真的太感谢了

@KarnaughK

This comment has been minimized.

@KarnaughK
Copy link
Author

自己尝试写了个loader,发现还是不能用,头疼。。。

@imyelo
Copy link
Member

imyelo commented Nov 2, 2019

抱歉我最近精力不足以支持这类改动,但如果感兴趣的话,非常欢迎你做些尝试。我的思路是这样的:

  1. 如果要将 config 放在 script 里,比较好的设计是在 script 中用 export 导出,例如:

    export const config = {
      // ... value of <config>
    }

    为了保持一致,Page.define 也应改用这种方式设计,即:

    import { Page } from '@tinajs/tina'
    
    Page.define({
      // ...
    })

    改为

    export const config = {
      // ... value of <config>
    }
    
    export default {
      // ... arguments of Page.define
    )

    这样的话,就需要用到 loader 来读取 <script>export 的值;对 export default 而言还需要能够自动导入 @tinajs/tina,插入 Page.define(...)

    这个操作应该由谁来做呢?交给 mina-loader 是不适合的,因为会因此增加它和 tina 之间的耦合度。但可以利用 mina-loader 中的 transform 参数为各个区块添加额外处理。这个参数的设计初衷是为了和 https://github.com/tinajs/translators/ 组合支持转译跨生态小程序的,例如支持支付宝小程序。对于 请问能不能支持把config放到script标签内部? #58 这个场景,也恰好类似。有兴趣的话可以参考下这个文件:https://github.com/tinajs/translators/blob/593ffa7/packages/%40tinajs/translator-ant/lib/translations/script.js

  2. 除了 loader 的问题之外,还需要对 mina-entry-webpack-plugin 做改动。

    这个插件会遍历 <config> 的内容,生成一份包含所有页面和组件的列表。既然 config 的值移至了 <script> 当中,那么插件也就需要对应地转至 <script> 中读取。在 mina-entry-webpack-plugin 中,读取的操作已经抽象成了 ConfigReader,例如 https://github.com/tinajs/mina-webpack/blob/de639d0/packages/mina-entry-webpack-plugin/src/index.ts#L142 ,但可惜这个接口还不支持接受外部的值,所以如果需要支持这个需求,还应先对 mina-entry-webpack-plugin 做改动,支持传入自定义的 ConfigReader (类似 mina-loader 的 transform)。

希望有帮助。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants