[英]Publish a npm package that is built using webpack and babeljs
I built a very simple javascript that uses webpack for packaging and npm for publishing the project.我构建了一个非常简单的 javascript,它使用 webpack 进行打包,使用 npm 发布项目。 While I am able successfully build and publish the project, I am unable to use the exported function.
虽然我能够成功构建和发布项目,但我无法使用导出的功能。
Here is my package.json:这是我的 package.json:
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "./dist/test-prod.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack --display-error-details --mode production",
"prepublishOnly": "npm run build"
},
"author": "",
"license": "ISC",
"devDependencies": {
"webpack": "^4.28.4",
"webpack-cli": "^3.2.1"
}
}
Here is the webpack config:这是 webpack 配置:
const path = require('path');
module.exports = {
context: path.resolve(__dirname),
entry: './index.js', // Entry file that will be invoked by webpack first
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'test-prod.js'
}
};
Here is test function located inside 'src' folder I am planning to export:这是位于我计划导出的“src”文件夹内的测试函数:
export const testFunc = () => alert('Hello World!');
Here is the index.js:这是 index.js:
const testFunc = require("./src/testFunc");
module.exports = {testFunc};
This is all and well.这一切都很好。 But after performing
npm publish
or npm link
, I am unable to access the testFunc from another project.但是在执行
npm publish
或npm link
,我无法从另一个项目访问 testFunc。
I tried import * as functions from 'test';
我试过
import * as functions from 'test';
, followed by functions.testFunc()
and import {testFunc} from 'test';
, 然后是
functions.testFunc()
和import {testFunc} from 'test';
. . Both does not work.
两者都不起作用。
Please let me know how to properly export a function using webpack & npm, and access it from a different project.请让我知道如何使用 webpack 和 npm 正确导出函数,并从不同的项目访问它。 I only need the minified javascript in my published package.
我只需要我发布的包中的缩小的 javascript。
Try this.尝试这个。
In the webpack config file, add this在 webpack 配置文件中,添加这个
output: {
// your current code
library: 'test',
libraryTarget: 'umd'
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.