npm使用index.js源的错误路径为React app运行构建

时间:2018-05-08 00:29:01

标签: npm build nwb

我的npm run build失败了:

✖ 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/Users/david/git/demoapp/lib,ES6版本为/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"
},

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

另一个令人困惑的是,一些帖子和教程声明了一个名为&#39; dist&#39;的目录。用于从&nbsp运行构建&#39;?

输出构建的代码

1 个答案:

答案 0 :(得分:0)

react-app不支持将文件导入src文件夹之外。答案这么短;您将不得不退出。

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