1

我的 npm run 构建失败了:

✖ Building demo

Failed to compile with 1 error.

 ERROR  in multi ./node_modules/nwb/polyfills.js ./demo/src/index.js
Module not found: Error: Can't resolve '/Users/david/git/demoapp/demo/src/index.js' in '/Users/david/git/demoapp'
 @ multi ./node_modules/nwb/polyfills.js ./demo/src/index.js
Error running command: Build failed with errors.
Error: Build failed with errors.
at /Users/david/git/demoapp/node_modules/nwb/lib/webpackBuild.js:82:17
at emitRecords.err (/Users/david/git/demoapp/node_modules/webpack/lib/Compiler.js:269:13)
at Compiler.emitRecords (/Users/david/git/demoapp/node_modules/webpack/lib/Compiler.js:375:38)
at emitAssets.err (/Users/david/git/demoapp/node_modules/webpack/lib/Compiler.js:262:10)
at applyPluginsAsyncSeries1.err (/Users/david/git/demoapp/node_modules/webpack/lib/Compiler.js:368:12)
at next (/Users/david/git/demoapp/node_modules/tapable/lib/Tapable.js:218:11)
at Compiler.compiler.plugin (/Users/david/git/demoapp/node_modules/webpack/lib/performance/SizeLimitsPlugin.js:99:4)
at Compiler.applyPluginsAsyncSeries1 (/Users/david/git/demoapp/node_modules/tapable/lib/Tapable.js:222:13)
at Compiler.afterEmit (/Users/david/git/demoapp/node_modules/webpack/lib/Compiler.js:365:9)
at require.forEach.err (/Users/david/git/demoapp/node_modules/webpack/lib/Compiler.js:354:15)
at /Users/david/git/demoapp/node_modules/async/dist/async.js:473:16
at iteratorCallback (/Users/david/git/demoapp/node_modules/async/dist/async.js:1050:13)
at /Users/david/git/demoapp/node_modules/async/dist/async.js:958:16
at /Users/david/git/demoapp/node_modules/graceful-fs/graceful-fs.js:43:10
at FSReqWrap.oncomplete (fs.js:112:15)`

这是不正确in '/Users/david/git/demoapp'的,因为索引在ES5 版本中/Users/david/git/demoapp/src或用于ES6 版本/Users/david/git/demoapp/lib/Users/david/git/demoapp/es

我的 nwb.config.js 看起来像这样:

const path = require('path');

function excludeNodeModulesExcept (modules) {
var pathSep = path.sep;
if (pathSep === '\\') { // must be quoted for use in a regexp:
    pathSep = '\\\\';
}
var moduleRegExps = modules.map(function (modName) { return new RegExp("node_modules" + pathSep + modName);});

return function (modulePath) {
    if (/node_modules/.test(modulePath)) {
        for (var i = 0; i < moduleRegExps.length; i++) {
            if (moduleRegExps[i].test(modulePath)) {
                console.log("Allowed babel transpiling of " + modulePath);
                return false;
            }
        }
        return true;
    }
    return false;
};
}

module.exports = {
type: 'react-component',
npm: {
    esModules: true,
    umd: false
},
webpack: {
    rules: {
        babel: {
            exclude: excludeNodeModulesExcept(['kontraktor-client', 'kontraktor-common']),
            options: {
                babelrc: false,
                cacheDirectory: true
            }
        }
    }
}
};

我的 package.json 的相关部分如下所示:

{
"name": "demoapp",
"version": "1.0.0",
"description": "Demo App",
"main": "lib/index.js",
"module": "es/index.js",
"files": [
"css",
"es",
"lib",
"umd"
],
"scripts": {
"build": "nwb build-react-component",
"images": "node imagesServer/index.js",
"serve": "nwb serve-react-app",
"clean": "nwb clean-module",
"start": "npm-run-all --parallel serve images",
"test": "nwb test-react",
"test:coverage": "nwb test-react --coverage",
"test:watch": "nwb test-react --server"
},

有没有办法指定源 index.js 所在的 nwb?我想构建这个 React 应用程序的 ES5 版本。

另一个困惑是一些帖子和教程指出一个名为“dist”的目录用于从“npm run build”输出构建的代码?

4

1 回答 1

0

react-app 不支持在 src 文件夹之外导入文件。如此简短的回答;你将不得不弹出'

或者您可以使用 npm 链接,但您的模块必须导出 es5,因为 react-app 不会为您转换它。

于 2018-07-28T04:30:49.810 回答