简体   繁体   English

更改browserify-shim名称会导致编译错误

[英]changing browserify-shim name causes compilation error

Okay, so I've been trying my hand at compiling stuff with browserify and vueify and I've stumbled upon something strange. 好的,所以我一直在尝试用browserify和vueify编译东西,但我偶然发现了一些奇怪的东西。 So first things first, I've been trying to get the browserify-simple example found here to work. 所以首先,我一直在努力使此处找到的browserify-simple示例起作用。

I've set things up and was able to compile and get things to work no problem. 我已经设置好东西,并且能够编译并使它们正常工作。 However, I decided to try adding browserify-shim to the lot so as to avoid having the vue.js library embedded in the final code. 但是,我决定尝试向其中添加browserify-shim,以避免在最终代码中嵌入vue.js库。

Here's where things get a little strange. 在这里,事情变得有些奇怪。 Here's a functional version of my package.json file. 这是我的package.json文件的功能版本。 Most of it was already present from the example, I've only added the necessary config to get browserify-shim working: 该示例中已经包含了大多数内容,我仅添加了必要的配置以使browserify-shim起作用:

{
  "name": "un-test",
  "description": "Un test",
  "author": "John Doe",
  "private": true,
  "scripts": {
    "watchify": "watchify -vd -p browserify-hmr -e src/main.js -o dist/build.js",
    "serve": "http-server -o -s -c 1 -a localhost",
    "dev": "npm-run-all --parallel watchify serve",
    "build": "cross-env NODE_ENV=production browserify -g envify src/main.js | uglifyjs -c warnings=false -m > dist/build.js"
  },
  "dependencies": {
    "vue": "^2.0.1"
  },
  "devDependencies": {
    "babel-core": "^6.0.0",
    "babel-preset-es2015": "^6.0.0",
    "babelify": "^7.2.0",
    "browserify": "^13.0.1",
    "browserify-hmr": "^0.3.1",
    "browserify-shim": "^3.8.14",
    "cross-env": "^1.0.6",
    "envify": "^3.4.1",
    "http-server": "^0.9.0",
    "npm-run-all": "^2.1.2",
    "uglify-js": "^2.5.0",
    "vueify": "^9.1.0",
    "watchify": "^3.4.0"
  },
  "browserify": {
    "transform": [
      "vueify",
      "babelify",
      "browserify-shim"
    ]
  },
  "browserify-shim": {
    "vue": "global:Vue" 
  }
}

As I said, this works. 正如我所说,这可行。 On the other hand, if I do this inside the package.json: 另一方面,如果我在package.json中这样做:

"browserify-shim": {
        "myVue": "global:Vue" 
      }

And if I modify the main.js file accordingly like this: 如果我像这样修改main.js文件:

import Vue from 'myVue'
import App from './App.vue'

new Vue({
  el: '#app',
  render: h => h(App)
})

I get the following error when I compile: 编译时出现以下错误:

Error: Cannot find module 'vue' from 'C:\\wamp\\www\\VueTest\\src' 错误:无法从“ C:\\ wamp \\ www \\ VueTest \\ src”中找到模块“ vue”

Can anybody tell me why this is happening? 谁能告诉我为什么会这样吗? I didn't think the name I put inside the browserify-shim section really mattered other than for importing the library I want. 除了导入所需的库外,我认为放在浏览器修饰区中的名称并不重要。

Well, I was able to track down the problem down to the vueify module. 好吧,我能够找到问题所在到vueify模块。 There's a hardcoded reference to "vue" module in the compiler.js file of said module. 在该模块的compile.js文件中,有一个对“ vue”模块的硬编码引用。 This forces you to name your module exactly "vue" in browserify-shim otherwise you'll get an error if you compile in dev mode. 这将迫使您在browserify-shim中将模块命名为“ vue”,否则,如果在开发模式下进行编译,则会出现错误。

I opened an issue here for those who are interested. 在这里为有兴趣的人开了一个问题。 But there's not been much activity lately so I don't know if/when it'll get fixed. 但是最近活动很少,所以我不知道是否/何时修复。 Anyway, if you want to avoid this error, just name your module "vue" or change your compilation mode with the NODE_ENV=production parameter. 无论如何,如果要避免此错误,只需将模块命名为“ vue”或使用NODE_ENV = production参数更改编译模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM