gpt4 book ai didi

webpack - 无法导入 vuejs 库

转载 作者:行者123 更新时间:2023-12-04 03:07:07 25 4
gpt4 key购买 nike

我正在制作一个小型 vuejs 库,只是为了学习如何制作一个。我们称它为 swing,这就是文件的结构。
enter image description hereconf/ , dist/ , test/文件为空
我已将此库推送到 github 并发布到 npm。
我使用它安装到我的应用程序中

npm install --save vuejs-swing
我可以看到它安装在 package.jsonnode_modules文件夹,但是当我这样做时:
import swing from 'vuejs-swing' 
我收到此错误:
 ERROR  Failed to compile with 1 errors                                                                                11:42:41 AM

This dependency was not found:

* vuejs-swing in ./node_modules/babel-loader/lib!./node_modules/vue-loader/lib/selector.js?type=script&index=0&bustCache!./src/c
omponents/HelloWorld.vue

To install it, you can run: npm install --save vuejs-swing
它显然是安装的,所以它必须是一个配置问题
这是我的 webpack.config.js
var path = require('path')
var webpack = require('webpack')

module.exports = {
module: {
rules: [
// use babel-loader for js files
{ test: /\.js$/, use: 'babel-loader' },
// use vue-loader for .vue files
{ test: /\.vue$/, use: 'vue-loader' }
]
},
// default for pretty much every project
context: __dirname,
// specify your entry/main file
entry: {
app: './src/swing.js',

},
output: {
// specify your output directory...
path: path.resolve(__dirname, 'output'),
// and filename
filename: 'index.min.js'
},
resolve: {
// this is optional, but it lets you import .vue files without the .vue extension.
extensions: ['.js', '.json', '.vue']
}
}



if (process.env.NODE_ENV === 'production') {
module.exports.devtool = '#source-map'
// http://vue-loader.vuejs.org/en/workflow/production.html
module.exports.plugins = (module.exports.plugins || []).concat([
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"'
}
}),
new webpack.optimize.UglifyJsPlugin({
sourceMap: true,
compress: {
warnings: false
}
}),
new webpack.LoaderOptionsPlugin({
minimize: true
})
])
}
编辑:
我会将赏金奖励给能够提供在 npm 中发布包所需的工具和配置并能够导入/使用它的人。

最佳答案

跟着这些步骤:

  • 确保您的 output git 不会忽略文件夹(不应在 .gitignore 文件中)
  • 为生产构建你的包(运行 Webpack)
  • 在您的 package.json指向main文件到 ./output/index.min.js不是 ./src/swing.js

  • {
    "name": "vuejs-swing",
    "version": "0.1.2",
    "main": "./output/index.min.js",
    // ...
    }

    不要让你的包的用户为你构建源代码(运行 Webpack),你需要将它们指向一个预打包的 javascript 文件。

    作为旁注,包 vuejs-swing目前尚未在 npm 上公开发布。我不知道您是否更改了包名称或在私有(private)存储库上工作。

    关于webpack - 无法导入 vuejs 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47328143/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com