npm运行构建问题(模块解析失败)

时间:2020-04-22 10:18:43

标签: javascript reactjs npm webpack babel-loader

我对React还是很陌生,并试图自定义一个npm模块。在npm run build阶段,它一直失败,并且我不确定是什么导致了问题。我已经研究了与“ babel-loader”有关的其他答案,但根据这些答案,我的看起来还不错。

错误:

> webpack && npm run cleancss

Hash: 4faa93666033a0f17524
Version: webpack 3.12.0
Time: 76ms
 2 assets
   [0] ./src/index.js 291 bytes {0} [built]
   [1] (webpack)/buildin/harmony-module.js 596 bytes {0} [built]
   [2] ./src/SideNav.jsx 245 bytes {0} [built] [failed] [1 error]
   [3] ./src/Toggle.jsx 243 bytes {0} [built] [failed] [1 error]
   [4] ./src/Nav.jsx 241 bytes {0} [built] [failed] [1 error]
   [5] ./src/NavItem.jsx 245 bytes {0} [built] [failed] [1 error]
   [6] ./src/NavIcon.js 250 bytes {0} [built] [failed] [1 error]
   [7] ./src/NavText.js 250 bytes {0} [built] [failed] [1 error]

ERROR in ./src/Nav.jsx
Module parse failed: Unexpected token (12:21)
You may need an appropriate loader to handle this file type.
| 
| class Nav extends PureComponent {
|     static propTypes = {
|         componentType: PropTypes.any,
| 
 @ ./src/index.js 4:0-39

ERROR in ./src/NavItem.jsx
Module parse failed: Unexpected token (15:21)
You may need an appropriate loader to handle this file type.
| 
| class NavItem extends PureComponent {
|     static propTypes = {
|         componentType: PropTypes.any,
| 
 @ ./src/index.js 5:0-47

ERROR in ./src/SideNav.jsx
Module parse failed: Unexpected token (16:21)
You may need an appropriate loader to handle this file type.
| 
| class SideNav extends PureComponent {
|     static propTypes = {
|         componentType: PropTypes.any,
| 
 @ ./src/index.js 1:0-32

ERROR in ./src/Toggle.jsx
Module parse failed: Unexpected token (7:21)
You may need an appropriate loader to handle this file type.
| 
| class Toggle extends PureComponent {
|     static propTypes = {
|         componentType: PropTypes.any,
| 
 @ ./src/index.js 3:0-45

ERROR in ./src/NavIcon.js
Module parse failed: Unexpected token (7:4)
You may need an appropriate loader to handle this file type.
| // For component matching
| NavIcon.defaultProps = {
|     ...NavIcon.defaultProps,
|     componentType: NavIcon
| };
 @ ./src/index.js 6:0-47

ERROR in ./src/NavText.js
Module parse failed: Unexpected token (7:4)
You may need an appropriate loader to handle this file type.
| // For component matching
| NavText.defaultProps = {
|     ...NavText.defaultProps,
|     componentType: NavText
| };
 @ ./src/index.js 7:0-47

webpack.config.js

var pkg = require('./package.json');
var path = require('path');
var webpack = require('webpack');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var findImports = require('find-imports');
var stylusLoader = require('stylus-loader');
var nib = require('nib');
var publicname = pkg.name.replace(/^@\w+\//, ''); // Strip out "@trendmicro/" from package name
var banner = [
    publicname + ' v' + pkg.version,
    '(c) ' + new Date().getFullYear() + ' Trend Micro Inc.',
    pkg.license,
    pkg.homepage
].join(' | ');
var localClassPrefix = publicname.replace(/^react-/, ''); // Strip out "react-" from publicname

module.exports = {
    devtool: 'source-map',
    entry: path.resolve(__dirname, 'src/index.js'),
    output: {
        path: path.join(__dirname, 'lib'),
        filename: 'index.js',
        libraryTarget: 'commonjs2'
    },
    externals: []
        .concat(findImports(['src/**/*.{js,jsx}'], { flatten: true }))
        .concat(Object.keys(pkg.peerDependencies))
        .concat(Object.keys(pkg.dependencies)),
    module: {
        rules: [
            // http://survivejs.com/webpack_react/linting_in_webpack/
            {
                test: /\.jsx?$/,
                loader: 'eslint-loader',
                enforce: 'pre',
                exclude: /node_modules/
            },
            {
                test: /\.styl$/,
                loader: 'stylint-loader',
                enforce: 'pre'
            },
            {
                test: /\.jsx?$/,
                loader: 'babel-loader',
                exclude: /node_modules/,
            },
            {
                test: /\.styl$/,
                use: ExtractTextPlugin.extract({
                    fallback: 'style-loader',
                    use: 'css-loader?camelCase&modules&importLoaders=1&localIdentName=' + localClassPrefix + '---[local]---[hash:base64:5]!stylus-loader'
                })
            },
            {
                test: /\.css$/,
                loader: 'style-loader!css-loader'
            },
            {
                test: /\.(png|jpg|svg)$/,
                loader: 'url-loader'
            }
        ]
    },
    plugins: [
        new webpack.DefinePlugin({
            'process.env': {
                // This has effect on the react lib size
                NODE_ENV: JSON.stringify('production')
            }
        }),
        new webpack.NoEmitOnErrorsPlugin(),
        new stylusLoader.OptionsPlugin({
            default: {
                // nib - CSS3 extensions for Stylus
                use: [nib()],
                // no need to have a '@import "nib"' in the stylesheet
                import: ['~nib/lib/nib/index.styl']
            }
        }),
        new ExtractTextPlugin('../dist/' + publicname + '.css'),
        new webpack.BannerPlugin(banner)
    ],
    resolve: {
        extensions: ['.js', '.json', '.jsx']
    }
};

.babelrc

{
    "presets": ["env", "stage-0", "react"],
    "plugins": [
        "transform-decorators-legacy"
    ]
}

如果有人能在这个问题上帮助我,我将非常感谢。

0 个答案:

没有答案