【问题标题】:How do you use an npm library on the front-end with webpack ?你如何在 webpack 的前端使用 npm 库?
【发布时间】:2015-08-11 11:27:51
【问题描述】:

我见过的几个项目似乎可以在前端上使用,但文档中唯一的“安装”指令是npm install xxx 行。

这些项目使用 webpack,它要么很明显,要么需要我缺少的插件,但我在 webpack docs 中找不到与 npm 模块相关的任何内容

我的谷歌/阅读技能明显失败,有人可以请RTFM我了解如何从以下步骤开始:

npm install foo

浏览器中运行它

var foo = require("foo");

【问题讨论】:

    标签: node.js webpack


    【解决方案1】:

    嗯,原来那是因为你……几乎无事可做。

    就我而言:

    npm install -g webpack
    npm install babel-loader
    npm install redux
    

    在 webpack.config.js 中

    module.exports = {
        entry: "./public/javascripts/entry.js",
        output: {
            path: __dirname,
            filename: "./public/javascripts/bundle.js"
        },
        module: {
            loaders: [
                { test: /\.jsx?$/,
                  exclude: /(node_modules|vendor)/,
                  loader: "babel-loader"
                }
            ]
        }
    };
    

    在 entry.js 中:

    var redux = require("redux");
    console.log(redux)
    

    一切正常……将标记关闭。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-03
      • 1970-01-01
      • 2018-01-09
      • 2018-10-19
      • 1970-01-01
      • 2021-05-22
      • 2018-02-18
      相关资源
      最近更新 更多