忽略node_modules,用于在本地编译服务器端ES6和Webpack

时间:2017-11-24 10:01:32

标签: node.js webpack server ecmascript-6 raspberry-pi

我正在使用 Raspberry Pi Zero ,这是我的服务器,正在运行我的模块。

Raspberry不是最好编译代码所以我想在本地编译服务器端代码。为此,我需要编译代码而不解析我的导入。为什么?

因为服务器模块使用只能安装在Raspberry上的其他模块(例如UPM或MRAA用于传感器)。

现在我在Pi上编译,但它很慢。

所以我尝试使用我的配置编译到ES6本地:

const path          = require("path");
const webpack       = require("webpack");
const nodeExternals = require("webpack-node-externals");

module.exports = {
    entry:
        {
            index: __dirname + "/src/index.js"
        },
    output: {
        // sourceMapFilename: "[name].map",
        path: path.resolve(__dirname, "build"),
        filename: "[name].js",
        sourceMapFilename: "[name].map",
        chunkFilename: "[id].chunk.js"
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                include: [
                    path.resolve(__dirname, "src")
                ],
                exclude: /(node_modules|bower_components)/,
                use: {
                    loader: "babel-loader",
                    options: {
                        cacheDirectory: true,
                        presets: ["env"],
                        plugins: [
                            ["transform-runtime"],
                            ["transform-es2015-modules-commonjs-simple", { // Very important
                                "noMangle": true
                            }]
                        ]
                    }
                }
            }
        ]
    },
    plugins: [
        new webpack.ProvidePlugin({Hardlets: path.resolve(__dirname, "../Hardlets/build/index")}),
        new webpack.IgnorePlugin(new RegExp("/(node_modules)/"))
        // new webpack.optimize.UglifyJsPlugin(),
        // new HtmlWebpackPlugin({template: "./src/index.html"})
    ],
    target: "node",
    externals: [nodeExternals()],
    devtool: "source-map"
};

这给了我一个错误,因为只有 devDependencies安装在本地而不是其他依赖(传感器......):

   ./src/hardlets/relays/Relays.js中的错误找不到模块:错误:   无法解析'jsupm_relay'   '/ home / stinky / Projects / gardener / Sensors / src / hardlets / relay'@   ./src/hardlets/relays/Relays.js 1:1008-1030 @   ./src/HardletInstances.js @ ./src/index.js

      ./src/hardlets/dht22/DHT22.js中的错误找不到模块:错误:无法   解决'rpi-dht-sensor'   '/ home / stinky / Projects / gardener / Sensors / src / hardlets / dht22'@   ./src/hardlets/dht22/DHT22.js 1:925-950 @ ./src/HardletInstances.js   @ ./src/index.js

我如何阻止webpack尝试解析/导入这些依赖项?必须在运行时解析依赖项。

有我的package.json:

{
  "name": "gardener",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "watch": "webpack --watch",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "http://192.168.8.100:3000/pi/Gardener.git"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-polyfill": "^6.26.0",
    "babel-preset-env": "^1.6.1",
    "babel-runtime": "^6.26.0",
    "babel-plugin-transform-runtime": "^6.23.0",
    "babel-plugin-transform-es2015-modules-commonjs-simple": "^6.7.4",
    "webpack": "^3.8.1",
    "webpack-node-externals": "^1.6.0"
  },
  "dependencies": {
    "upm": "^1.0.0",
    "rpio": "^0.9.19",
    "rpi-dht-sensor": "^0.1.1"
  }
}

1 个答案:

答案 0 :(得分:0)

nodeExternals()无法排除模块,因为它列出了/ node_modules /中的模块。但是,要排除的模块未在本地安装,因此nodeExternals()无法完成此任务。

需要通过阅读package.json来排除它们。

这将从package.json文件中读取模块&排除它们而不是node_modules目录:

// webpack.config.js
var nodeExternals = require('webpack-node-externals');
...
module.exports = {
    ...
    externals: [nodeExternals({
        modulesFromFile: true
    })], // in order to ignore all modules in package.json when bundling
    ...
};