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

chameleon.config.js 设置webpack noParse 无效 #503

Open
lanxiaojing opened this issue Nov 26, 2020 · 1 comment
Open

chameleon.config.js 设置webpack noParse 无效 #503

lanxiaojing opened this issue Nov 26, 2020 · 1 comment
Labels
bug Something isn't working

Comments

@lanxiaojing
Copy link

bug描述
在chameleon.config.js中按文档 设置webpack noParse 配置无效。

复现bug的步骤

  1. 在chameleon.config.js修改webpackConfig如下图,最后生成的文件中还是处理了文件里的require

问题截图
chameleon.config.js中:
image

// 原始文件中的require
image

// 生成的static/js/common.js中还是会处理noParse设置的require
image

编译环境信息

  • chameleon-tool 版本:[[email protected]]
  • nodejs版本、npm版本:[node v12.16.2、npm 6.14.4]
  • 电脑操作系统: [e.g. macOS High Sierra 10.15.7]

运行环境信息

  • 端版本: [e.g. 微信小程序 7.0.3]
  • 手机机型: [e.g. iPhone6 12.0]
@lanxiaojing lanxiaojing added the bug Something isn't working label Nov 26, 2020
@lanxiaojing lanxiaojing changed the title 设置webpack noParse 无效 chameleon.config.js 设置webpack noParse 无效 Nov 26, 2020
@jimwmg
Copy link
Collaborator

jimwmg commented Dec 15, 2020

webpack关于 noParse的解释
module.noParse
防止 webpack 解析那些任何与给定正则表达式相匹配的文件。忽略的文件中不应该含有 import, require, define 的调用,或任何其他导入机制。忽略大型的 library 可以提高构建性能。

回调增加配置

cml.utils.plugin('webpackConfig', function({ type, media, webpackConfig }, cb) {
  // cb函数用于设置修改后的配置
  webpackConfig.module.noParse = function(content){
    return /test-noparse/.test(content) //测试工程化配置 noParse是否生效
  }
  cb({
    type,
    media,
    webpackConfig
  });
});

文件引用

import './test-noparse.js'

这里我简单加了一个测试案例:test-noparse.js 文件引用的 m1.js 文件,

test-noparse.js

console.log('no-parse');
const m1 = require('./m1.js')

m1.js

module.exports = {
  name:"module1"
}

打包后代码

/***/ "./src/pages/index/test-noparse.js":
/***/ (function(module, exports) {

console.log('no-parse');
var m1 = require('./m1.js');

/***/ }),

全部demo参见:https://github.com/chameleon-team/cml-web-multipage

注释掉配置,关闭终端,重新构建对比效果

webpackConfig.module.noParse = function(content){
    // return /test-noparse/.test(content) //测试工程化配置 noParse是否生效
  }

查看构建后代码

/***/ "./src/pages/index/test-noparse.js":
/***/ (function(module, exports, __webpack_require__) {

console.log('no-parse');
var m1 = __webpack_require__("./src/pages/index/m1.js");

/***/ }),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants