[英]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命令的根目录下编译了库。
webpack-dev-server
respond to changes in that local NPM dependency? 主要问题:有没有一种方法可以让webpack-dev-server
的热重载webpack-dev-server
响应该本地NPM依赖项中的更改? 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: 文件夹结构如下:
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应用程序中的重新加载。
webpack-dev-server
will reload based on changes in your "main" webpack-dev-server
将根据您的“主”中的更改重新加载 {
"name": "my-dep",
"main": "lib/index.js",
}
npm link
next to the dependency package.json
. 运行依赖项package.json
旁边的npm link
。 npm link my-dep
in your React project - it will create a symbolic link between the two projects. 在您的React项目中运行npm link my-dep
它会在两个项目之间创建一个符号链接。 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 link
: https : npm link
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.