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

无法修改内置的 UglifyJsPlugin 、DefinePlugin 等插件参数 #58

Closed
soda-x opened this issue Jan 23, 2016 · 9 comments
Closed

Comments

@soda-x
Copy link

soda-x commented Jan 23, 2016

之后需要考虑下如何把这些内置且无法修改参数的插件通过一种合理的方式让用户可以覆盖默认参数,或者传递参数.

DefinePlugin 现在在业务中有大量的需求.

@soda-x
Copy link
Author

soda-x commented Jan 24, 2016

DefinePlugin 为例 补充下可以实现的细节为

  var define = {
    "default": {
      "DATAHOST": "http://localhost",
      "DEBUG": true
    },
    "test": {
      "DATAHOST": "http://x.sit.hostname.net",
      "DEBUG": true
    },
    "prod": {
      "DATAHOST": "http://x.hostname.com",
      "DEBUG": false
    },
    "string": "ooxx"
  }
  var definePluginOptionKey = define[process.env.NODE_ENV] ? process.env.NODE_ENV : define['default'] ? 'default' : '';

  if(definePluginOptionKey){
    var definePluginOption;
    var defineContent = define[definePluginOptionKey];
    if (typeof defineContent === 'object') {
      for (var i in defineContent) {
        (typeof(defineContent[i]) === 'string' || typeof(defineContent[i] === 'object')) && (defineContent[i] = JSON.stringify(defineContent[i]));
      }
    }
    webpackConfig.plugins.push(
     new webpack.DefinePlugin(defineContent)
   )
  }

那这样的好处是用户可以根据 node 的环境变量做自定义的构建.

诸如

NODE_ENV=prod npm run build

NODE_ENV=test npm run build

等等.

或者之后直接集成到 build 的构建参数 --define

@sorrycc
Copy link
Member

sorrycc commented Jan 24, 2016

可以考虑提一些配置到 webpackConfig 里,便于扩展。

@sorrycc sorrycc mentioned this issue Jan 27, 2016
11 tasks
@soda-x
Copy link
Author

soda-x commented Jan 27, 2016

目前给 autoprefixer https://github.com/ant-tool/atool-build/blob/master/src/getWebpackCommonConfig.js#L97 增加参数也非常不便利,现在 postcss 只有一个插件还好解决, 但是很难保证以后有多个且顺序会发生变更 .

目前我在覆盖上的解决方案是 http://ant-tool.github.io/spm-to-atool-build.html#spmautoprefixer 很麻烦 - -!

这边也同步一份 o.o

@jaredleechn
Copy link
Member

在尝试把 atool-doc 生成的 .js 文件对应的样式打包到文件里时遇到相同问题

修改 extract-text-webpack-plugin 非常麻烦,不仅要遍历 webpackConfig.plugins 还要遍历 webpackConfig.module.loaders

考虑这里也加一个开关么?

@sorrycc
Copy link
Member

sorrycc commented Mar 24, 2016

@jaredleechn 现在的方案修改 extract-text-webpack-plugin 没啥好方法,你有啥建议不? 后续会考虑 #114

@jaredleechn
Copy link
Member

想到加开关的方式

if(args.extract !== false) {
   webpackConfig.module.loaders.push(...)
}

但这种恐怕加不完

这个 #114 挺不错的,我们把 key 约定好 就可以

@sorrycc
Copy link
Member

sorrycc commented Mar 24, 2016

开关是加不完的。。等 #114 吧。

@sorrycc sorrycc closed this as completed Mar 24, 2016
@jaredleechn
Copy link
Member

补充下我现在 atool-doc 里面 hack 的做法

plugins 是直接覆盖,loader 遍历替换

@soda-x
Copy link
Author

soda-x commented Mar 28, 2016

注明:

DefinePlugin 是一个可以被覆盖的插件 . 所以不需考虑先去除原有逻辑再重新 new

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

3 participants