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

开启treeShaking后 构建h5出现两份 #638

Closed
ben-lau opened this issue Aug 8, 2019 · 14 comments
Closed

开启treeShaking后 构建h5出现两份 #638

ben-lau opened this issue Aug 8, 2019 · 14 comments
Labels
enhancement New feature or request

Comments

@ben-lau
Copy link

ben-lau commented Aug 8, 2019

问题描述
开启treeShaking后,运行npm run build:h5,先出现一份index.js chunk-vendor.js 然后出现第二份哈希值的 html内引用指向第二份,关闭treeshaking后就没问题

复现步骤
[复现问题的步骤]

  1. 启动 '...'
  2. 点击 '....'
  3. 查看

[或者可以直接贴源代码]

预期结果
[使用简洁清晰的语言描述你希望生效的预期结果]

实际结果
[这里请贴上你的报错截图或文字]
image

image

系统信息:

Environment Info:

System:
OS: Windows 10
CPU: (4) x64 Intel(R) Core(TM) i3-4160 CPU @ 3.60GHz
Binaries:
Node: 10.15.3 - C:\Program Files\nodejs\node.EXE
Yarn: Not Found
npm: 6.10.1 - C:\Program Files\nodejs\npm.CMD
Browsers:
Edge: 44.18362.267.0
npmPackages:
@dcloudio/uni-app-plus: * => 0.0.249
@dcloudio/uni-app-plus-nvue: 0.0.1
@dcloudio/uni-app-plus-nvue-v8: 0.0.1
@dcloudio/uni-cli-shared: * => 0.2.983
@dcloudio/uni-h5: * => 0.7.4
@dcloudio/uni-mp-alipay: * => 0.0.823
@dcloudio/uni-mp-baidu: * => 0.0.853
@dcloudio/uni-mp-qq: * => 0.0.107
@dcloudio/uni-mp-toutiao: * => 0.0.347
@dcloudio/uni-mp-weixin: * => 0.0.969
@dcloudio/uni-template-compiler: * => 0.9.181
@dcloudio/vue-cli-plugin-hbuilderx: latest => 1.0.131
@dcloudio/vue-cli-plugin-uni: latest => 0.9.511
@dcloudio/vue-cli-plugin-uni-optimize: latest => 0.1.6
@dcloudio/webpack-uni-mp-loader: * => 0.3.639
@dcloudio/webpack-uni-nvue-loader: 0.0.1
@dcloudio/webpack-uni-pages-loader: * => 0.2.866
@vue/babel-helper-vue-jsx-merge-props: 1.0.0
@vue/babel-plugin-transform-vue-jsx: 1.0.0
@vue/babel-preset-app: 3.10.0
@vue/babel-preset-jsx: 1.1.0
@vue/babel-sugar-functional-vue: 1.0.0
@vue/babel-sugar-inject-h: 1.0.0
@vue/babel-sugar-v-model: 1.0.0
@vue/babel-sugar-v-on: 1.1.0
@vue/cli-overlay: 3.10.0
@vue/cli-plugin-babel: 3.5.1 => 3.5.1
@vue/cli-service: ^3.9.0 => 3.10.0
@vue/cli-shared-utils: 3.10.0
@vue/component-compiler-utils: 2.6.0 (3.0.0)
@vue/preload-webpack-plugin: 1.1.1
@vue/web-component-wrapper: 1.2.0
mpvue-page-factory: 1.0.1
mpvue-template-compiler: 1.0.13
uni-h5-vue: 2.6.10
uni-mp-vue: 2.6.10
vue: ^2.6.10 => 2.6.10
vue-hot-reload-api: 2.3.3
vue-loader: 15.7.1
vue-router: 3.0.1
vue-style-loader: 4.1.2
vue-template-compiler: ^2.6.10 => 2.6.10
vue-template-es2015-compiler: 1.9.1
vuex: ^3.0.1 => 3.1.1
vuex-persistedstate: ^2.5.4 => 2.5.4
npmGlobalPackages:
@vue/cli: Not Found

补充信息
[可选]
[根据你的分析,出现这个问题的原因可能在哪里?]

@D-WYQ
Copy link

D-WYQ commented Aug 8, 2019

目前是这样的,这个不影响实际运行效果

@ben-lau
Copy link
Author

ben-lau commented Aug 8, 2019

新的问题 开启treeshaking后 request showLoading等一大堆api显示未实现 构建后的工程直接无法运行 估计把你们自己的基础库都丢了

@hbcui1984
Copy link
Contributor

hbcui1984 commented Aug 8, 2019

新的问题 开启treeshaking后 request showLoading等一大堆api显示未实现 构建后的工程直接无法运行 估计把你们自己的基础库都丢了

测试没重现问题,你的项目使用了uni.request的话,应该会扫描打包的,你是在控制台测试调用,还是程序运行报错?

@ben-lau
Copy link
Author

ben-lau commented Aug 9, 2019

新的问题 开启treeshaking后 request showLoading等一大堆api显示未实现 构建后的工程直接无法运行 估计把你们自己的基础库都丢了

测试没重现问题,你的项目使用了uni.request的话,应该会扫描打包的,你是在控制台测试调用,还是程序运行报错?

是运行报错 没在控制台调试
image

@fxy060608
Copy link
Collaborator

你调用这些 api 的方式是什么?是 uni.request 这样调用的吗

@ben-lau
Copy link
Author

ben-lau commented Aug 9, 2019

image
image
部分api我自己做了层promisify封装 因为你们声明文件没写返回Promise的重载 但是setStorageSync这是同步方法我没封装的啊 用在vuex插件里了

@fxy060608
Copy link
Collaborator

fxy060608 commented Aug 12, 2019

image
image
部分api我自己做了层promisify封装 因为你们声明文件没写返回Promise的重载 但是setStorageSync这是同步方法我没封装的啊 用在vuex插件里了

从代码来看,storage 相关的应该可以扫描出来,request 目前扫描不出来,后续可以考虑支持

目前支持扫描的使用方式有:

uni.request()
uni['request']()
wx.request()
wx['request']()

@hbcui1984 hbcui1984 added the enhancement New feature or request label Aug 12, 2019
@ben-lau
Copy link
Author

ben-lau commented Aug 13, 2019

那storage应该没问题的才对吧 showLoading我也是加了promisify封装

@fxy060608
Copy link
Collaborator

那storage应该没问题的才对吧 showLoading我也是加了promisify封装

你可以弄一个简单的工程验证下,理论上是这样的

@ben-lau
Copy link
Author

ben-lau commented Aug 17, 2019

那storage应该没问题的才对吧 showLoading我也是加了promisify封装

你可以弄一个简单的工程验证下,理论上是这样的

额不行,还是显示那些api未实现,storage的api只有这里用了 用在vuex插件里vuex-persistedstate

@fxy060608
Copy link
Collaborator

你可以把测试工程放到 GitHub 上或上传 ask 社区,贴一下地址,我来验证下:https://ask.dcloud.net.cn/explore/

@fxy060608
Copy link
Collaborator

那storage应该没问题的才对吧 showLoading我也是加了promisify封装

你可以弄一个简单的工程验证下,理论上是这样的

额不行,还是显示那些api未实现,storage的api只有这里用了 用在vuex插件里vuex-persistedstate

你的这些 api 的封装是在 node_modules 里边?如果是的话,可以 npm update 一下,已修复 node-modules 未被扫描的问题

@ben-lau
Copy link
Author

ben-lau commented Aug 17, 2019

你的这些 api 的封装是在 node_modules 里边?如果是的话,可以 npm update 一下,已修复 node-modules 未被扫描的问题

不是 就是我的业务代码里的 在store里 使用vuex插件内

@ben-lau
Copy link
Author

ben-lau commented Aug 21, 2019

已经知道问题所在了 我的项目是cli创建的 在你们出treeshaking前的事 babel.config没更新
这是之前的
`const plugins = []

if (process.env.UNI_PLATFORM === 'app-plus' && process.env.UNI_USING_V8) {
const path = require('path')

const isWin = /^win/.test(process.platform)

const normalizePath = path => (isWin ? path.replace(/\/g, '/') : path)

const input = normalizePath(process.env.UNI_INPUT_DIR)
try {
plugins.push([
require('@dcloudio/vue-cli-plugin-hbuilderx/packages/babel-plugin-console'),
{
file (file) {
file = normalizePath(file)
if (file.indexOf(input) === 0) {
return path.relative(input, file)
}
return false
}
}
])
} catch (e) {}
}

process.UNI_LIBRARIES = process.UNI_LIBRARIES || ['@dcloudio/uni-ui']
process.UNI_LIBRARIES.forEach(libraryName => {
plugins.push([
'import',
{
'libraryName': libraryName,
'customName': (name) => {
return ${libraryName}/lib/${name}/${name}
}
}
])
})
module.exports = {
presets: [
[
'@vue/app',
{
modules: 'commonjs',
useBuiltIns: 'entry'
}
]
],
plugins: [
...plugins,
['@babel/plugin-proposal-decorators', { legacy: true }],
['@babel/plugin-proposal-class-properties', { loose: true }]
]
}
`

这是现在的
`const plugins = [];

if (process.env.UNI_OPT_TREESHAKINGNG) {
plugins.push(
require('@dcloudio/vue-cli-plugin-uni-optimize/packages/babel-plugin-uni-api/index.js')
);
}

if (process.env.UNI_PLATFORM === 'app-plus' && process.env.UNI_USING_V8) {
const path = require('path');

const isWin = /^win/.test(process.platform);

const normalizePath = path => (isWin ? path.replace(/\/g, '/') : path);

const input = normalizePath(process.env.UNI_INPUT_DIR);
try {
plugins.push([
require('@dcloudio/vue-cli-plugin-hbuilderx/packages/babel-plugin-console'),
{
file(file) {
file = normalizePath(file);
if (file.indexOf(input) === 0) {
return path.relative(input, file);
}
return false;
}
}
]);
} catch (e) {}
}

process.UNI_LIBRARIES = process.UNI_LIBRARIES || ['@dcloudio/uni-ui'];
process.UNI_LIBRARIES.forEach(libraryName => {
plugins.push([
'import',
{
libraryName: libraryName,
customName: name => {
return ${libraryName}/lib/${name}/${name};
}
}
]);
});
module.exports = {
presets: [
[
'@vue/app',
{
modules: 'commonjs',
useBuiltIns: process.env.UNI_PLATFORM === 'h5' ? 'usage' : 'entry'
}
]
],
plugins: [
...plugins,
['@babel/plugin-proposal-decorators', { legacy: true }],
['@babel/plugin-proposal-class-properties', { loose: true }]
]
};
`
改了就可以了 现在连我包裹过的api也可以了是你们修复了么

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

No branches or pull requests

4 participants