我有一个我想在npm发布的组件,我只是通过从项目的components文件夹中导入它来测试。
我设法发布了它,但现在我得到了:
>错误在./node_modules/@//index.js模块 解析失败:意外的令牌(11:8)您可能需要一个合适的令牌 加载器来处理这种文件类型。
我的index.js如下:
import React from "react"
import Main from "./bootstrap/containers/main"
export default class BootstrapTable extends React.Component {
constructor(props) {
super(props)
}
render() {
return (
<Main {...this.props} changePage={(p) => this.props.changePage(p)}/>
)
}
}
旁注:我不需要更改webpack配置,因为它应该可以正常工作,就像我目前使用的许多其他包一样。
答案 0 :(得分:2)
我解决了这个问题,感谢@zerkms我研究所需的第一步。
<强>步骤:强>
var path = require('path'); module.exports = { mode: 'production', entry: './src/index.js', output: { path: path.resolve('dist'), filename: 'index.js', libraryTarget: 'commonjs2' }, module: { rules: [{ test: /\.js?$/, exclude: /(node_modules)/, use: { loader: 'babel-loader' } }] }, externals: { 'react': 'react', 'react-dom': 'react-dom', 'react-bootstrap': 'react-bootstrap' } }
{ "presets": [ "react", "env", "stage-0" ] }
src .babelrc webpack.config.js
{ "name": "@scope/package-name", "version": "1.0.0", "description": "My component", "main": "dist/index.js", "scripts": { "build": "webpack" }, "repository": { "type": "git", "url": "git+https://github.com/myrepo.git" }, "keywords": [ "react", "bootstrap" ], "author": "Me", "license": "MIT", "peerDependencies": { "react": "^16.4.0", "react-bootstrap": "^0.32.1", "react-dom": "^16.4.0" }, "devDependencies": { "react": "^16.4.0", "react-bootstrap": "^0.32.1", "react-dom": "^16.4.0", "babel-core": "^6.21.0", "babel-loader": "^7.1.4", "babel-preset-env": "^1.6.1", "babel-preset-react": "^6.16.0", "babel-preset-stage-0": "^6.24.1", "path": "^0.12.7", "webpack": "^4.5.0", "webpack-cli": "^2.0.14" } }
完成!
这些文章很有帮助: