简体   繁体   English

我如何使用vue-cli要求webpack使用requirejs生成js文件?

[英]How to require webpack generate js file with requirejs?I use vue-cli

I make my project by vue-cli. 我通过vue-cli进行项目。

vue init webpack vue-demo
cd vue-demo
npm install 
npm run dev

Now I want to devolop some components. 现在,我想开发一些组件。 And i want to use them in requirejs. 我想在requirejs中使用它们。 webpack config webpack配置

entry: {
    app: './src/main.js'
  },
  output: {
    path: config.build.assetsRoot,
    publicPath: process.env.NODE_ENV === 'production' ? config.build.assetsPublicPath : config.dev.assetsPublicPath,
    filename: '[name].js',
    libraryTarget: 'umd',
    library:'senyint'
  }

Q1:It generate three files. Q1:它生成三个文件。 app.js manifest.js vendor.js The demo has a Hello.vue . app.js manifest.js vendor.js该演示具有Hello.vue。 I want to require the js file by what webpack generate. 我想通过webpack生成js文件。 But I require them,it's undefiend . 但是我要求他们,这是不容置疑的。 Why? 为什么? What's the wrong? 怎么了 Where I should export ? 我应该在哪里出口? Now I export in main.js like this. 现在我像这样在main.js中导出。

import Hello from 'components/Hello'
   module.exports = {
Hello
}

Q2:I dont want to package without vue. Q2:我不想打包没有任何提示。 So i configure this 所以我配置这个

externals: {
    vue: 'vue'
}

If i do this, when npm run dev show error "Uncaught TypeError: Cannot read property 'config' of undefined" It cause cant find Vue. 如果我这样做,当npm运行dev时显示错误“未捕获的TypeError:无法读取未定义的属性'config'”,这会导致找不到Vue。 If i configure externals vue how to make it run? 如果我配置外部vue,如何使其运行?

Q1: Open the javascript file app.js i found 问题1:打开我发现的JavaScript文件app.js

define("senyint", ["vue"], factory);   

at this line. 在这一行。 The 'senyint' is a package name,webpack generate the js,but it doesnt work. 'senyint'是一个包名,webpack生成js,但是不起作用。 I modify the code like this 我这样修改代码

define(["vue"], factory);

Require it and it works. 需要它,它可以工作。 But I dont know why.... I just can resolve this problem; 但是我不知道为什么。。。我可以解决这个问题。 main.js export .vue components main.js导出.vue组件

import Hello from 'components/Hello.vue'

export const scom = {
  Hello 
}

requirejs config requirejs配置

requirejs.config({
  baseUrl: 'js/common',
  paths: {
    module: '../modules'
  },
  shim: {
    app: {
      deps:['vue','manifest','vendor']
    }
  }
})

requirejs(['module/demo', 'app'], function (demojs, app) {
  debugger
  console.log(app)
})

Q2: I builded my project with vue-cli webpack template. 问题2:我使用vue-cli webpack模板构建了项目。 (not webpack-simple) In build directory has 'webpack.base.conf.js' and 'webpack.prod.conf.js' Modify webpack.prod.conf.js add (不是webpack-simple)在构建目录中有“ webpack.base.conf.js”和“ webpack.prod.conf.js”。修改webpack.prod.conf.js添加

externals: {
    vue: {
      root: 'Vue',
      commonjs: 'vue',
      commonjs2: 'vue',
      amd: 'vue'
    }
  },

and dont add the code in 'webpack.base.conf.js' .Then npm run build it will package without vue.js .Npm run dev use webpack.base.conf.js ,it will run with vue 并不要在'webpack.base.conf.js'中添加代码。然后npm run build将不带vue.js打包。Npm run dev使用webpack.base.conf.js,它将与vue一起运行

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

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