简体   繁体   English

使用webpack-dev-server观看本地依赖项

[英]Watch local dependencies with webpack-dev-server

I'm creating a Javascript library. 我正在创建一个Javascript库。 Inside my project there's a folder that contains examples of how to use the library. 在我的项目中,有一个文件夹,其中包含有关如何使用该库的示例。 Inside each of the examples is a webpack config file with the entire purpose of bundling that example and serving it over webpack-dev-server with hot reloading. 每个示例中都有一个webpack配置文件,其目的是捆绑该示例并通过热重载在webpack-dev-server提供该文件。 Each of these examples also has the library (at the root of the project) listed as a local NPM dependency. 这些示例中的每一个都具有作为项目NPM依赖项列出的库(位于项目的根目录)。 I have hot reloading working for each example and I have babel compiling the library at the root on a watch command. 我对每个示例都进行了热重装,并且babel在watch命令的根目录下编译了库。

  • Primary question: Is there a way that I can have the hot reloader of webpack-dev-server respond to changes in that local NPM dependency? 主要问题:有没有一种方法可以让webpack-dev-server的热重载webpack-dev-server响应该本地NPM依赖项中的更改?
  • Secondary question: Is this intended to be the default behavior of webpack? 次要问题:这是否打算成为webpack的默认行为? If so, what is could be wrong with my machine/config file? 如果是这样,我的机器/配置文件怎么了?
  • General/vague question: Am I doing this wrong? 一般/含糊的问题:我做错了吗? I feel like it should be a lot easier to serve local examples (I'm not interested in using Storybook either as the examples I'm writing aren't in React, Vue, Angular, etc... it's all straight-up vanilla Javascript). 我觉得提供本地示例应该容易得多(我对使用Storybook也不感兴趣,因为我正在编写的示例不在React,Vue,Angular等中...这些都是纯正的香草Javascript)。

Here's my webpack.config.js file: 这是我的webpack.config.js文件:

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = (env, argv) => ({
  mode: argv.mode,
  entry: './index.js',
  output: {
    path: path.join(__dirname, '/dist'),
    filename: 'index.bundle.js'
  },
  devtool: argv.mode === 'development' ? '#eval-source-map' : 'source-map',
  devServer: {
    port: 8080,
    hot: true,
    open: true,
    stats: {
      children: false, // Hide children information
      maxModules: 0 // Set the maximum number of modules to be shown
    }
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader'
        }
      }
    ]
  },
  plugins: [new HtmlWebpackPlugin({ template: './index.html' })]
});

And my package.json file (note that syft.js is the local dependency I want to watch for changes): 还有我的package.json文件(请注意syft.js是我要监视更改的本地依赖项):

{
  "name": "with-grid",
  "version": "1.0.0",
  "private": true,
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "webpack-dev-server --mode development",
    "build": "rm -rf dist && webpack --mode production",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "",
  "devDependencies": {
    "babel-loader": "^8.0.6",
    "html-webpack-plugin": "^3.2.0",
    "path": "^0.12.7",
    "webpack": "^4.39.1",
    "webpack-cli": "^3.3.7",
    "webpack-dev-server": "^3.7.2"
  },
  "dependencies": {
    "syft.js": "file:../.."
  }
}

Folder structure is like such: 文件夹结构如下:

  • dist dist
    • index.js (Babel-generated file, what package.json points to, and the file that I want to be watched) index.js(Babel生成的文件,package.json指向什么以及我要监视的文件)
  • examples 例子
    • with-grid 并网
      • webpack.config.js (referenced above) webpack.config.js(上面已引用)
      • package.json (referenced above) package.json(上面已引用)
  • src src
    • index.js (main src file) index.js(主要src文件)

  • Operating System: MacOS 10.14.6 作业系统:MacOS 10.14.6
  • Browser: Chrome 76 浏览器:Chrome 76
  • Node: 12.8.0 节点:12.8.0
  • NPM: 6.10.3 NPM:6.10.3
  • Yarn: 1.17.3 纱:1.17.3

I opted for a different strategy that now seems very obvious in retrospect. 我选择了另一种策略,现在回想起来很明显。 Rather than treating my library like a local node dependency that needs to be resolved, I can just simply import it locally. 与其像需要解决的本地节点依赖项那样对待我的库,不如将其导入本地。

When importing from my example folder, I do: 从示例文件夹导入时,我会执行以下操作:

import syft from '../../src'; // Like a relative file

Instead of: 代替:

import syft from 'syft.js'; // Like an NPM package

After this small change, everything reloads as expected. 进行此小更改后,所有内容将按预期重新加载。

You can use npm link to create symbolic links between your React app and your local dependencies. 您可以使用npm link链接在React应用程序和本地依赖项之间创建符号链接。 Just make sure you build your local dependency to trigger reload in your React app. 只需确保您建立了本地依赖关系即可触发React应用程序中的重新加载。

  1. Your local dependency should have a "main" and a "name" attributes in the package.json. 您的本地依赖项在package.json中应具有“ main”和“ name”属性。 webpack-dev-server will reload based on changes in your "main" webpack-dev-server将根据您的“主”中的更改重新加载
{
  "name": "my-dep",
  "main": "lib/index.js",
}
  1. Run npm link next to the dependency package.json . 运行依赖项package.json旁边的npm link
  2. Run npm link my-dep in your React project - it will create a symbolic link between the two projects. 在您的React项目中运行npm link my-dep它会在两个项目之间创建一个符号链接。
  3. import myDep from 'my-dep in your React project. import myDep from 'my-dep React项目中的import myDep from 'my-dep Reload will be triggered when you change lib/index.js 更改lib/index.js时将触发重新加载

You can read more here https://medium.com/dailyjs/how-to-use-npm-link-7375b6219557 npm link : https://docs.npmjs.com/cli/link 您可以在这里阅读更多信息https://medium.com/dailyjs/how-to-use-npm-link-7375b6219557 npm linkhttps : npm link

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM