umijs / mako

An extremely fast, production-grade web bundler based on Rust.
https://makojs.dev
MIT License
1.86k stars 71 forks source link

[disscus] 如何处理 js 中 违反 strick 的语法 #586

Closed stormslowly closed 1 year ago

stormslowly commented 1 year ago
  × With statement are not allowed in strict mode
    ╭─[../node_modules/_domino@2.1.6@domino/lib/sloppy.js:14:1]
 14 │       with(this.document.defaultView || Object.create(null))
 15 │         with(this.document)
 16 │           with(this.form)
 17 │             with(this.element)
    ·             ────
 18 │               return eval(\"(function(event){\" + this.body + \"})\");
 19 │     }
 20 │     catch (err) {
    ╰────

javacsript parse 默认使用 parse_module,强制 strict 模式;

    pub fn parse_module(&mut self) -> PResult<Module> {
        let ctx = Context {
            module: true,
            can_be_module: true,
            strict: true,
            ..self.ctx()
        };

目前绕过方式, 先 parse_grogram 然后再 转换成 module

需要看下其他竞品怎么做的。

附录

// node_modules/_domino@2.1.6@domino/lib/sloppy.js

/* Domino uses sloppy-mode features (in particular, `with`) for a few
 * minor things.  This file encapsulates all the sloppiness; every
 * other module should be strict. */
/* jshint strict: false */
/* jshint evil: true */
/* jshint -W085 */
module.exports = {
  Window_run: function _run(code, file) {
    if (file) code += '\n//@ sourceURL=' + file;
    with(this) eval(code);
  },
  EventHandlerBuilder_build: function build() {
    try {
      with(this.document.defaultView || Object.create(null))
        with(this.document)
          with(this.form)
            with(this.element)
              return eval("(function(event){" + this.body + "})");
    }
    catch (err) {
      return function() { throw err; };
    }
  }
};
sorrycc commented 1 year ago

domino 这个库是应该被引用进来的吗?看起来这个库是给 node 端用的。

stormslowly commented 1 year ago

应该是 resolve 配置调整前引入的,但是这个问题还是有可能在其他场景下发生

sorrycc commented 1 year ago

https://github.com/umijs/mako/pull/617