为什么不用webpack-dev-server,是因为没法用,所以可以使用nodemon来解决,它可以在我们开发期间重新启动Node.js的应用程序,一样我们先来安装,依然需要 -d
- npm i -D nodemon-webpack-plugin
然后重新配置webpack.config.js
- // webpack.config.js
- 'use strict';
- const NodemonPlugin = require('nodemon-webpack-plugin');
- module.exports = (env = {}) => {
- const config = {
- entry: ['./src/main.js'],
- mode: env.development ? 'development' : 'production',
- target: 'node',
- devtool: env.development ? 'cheap-eval-source-map' : false,
- resolve: { // tells Webpack what files to watch.
- modules: ['node_modules', 'src', 'package.json'],
- },
- plugins: [] // required for config.plugins.push(...);
- };
- if (env.nodemon) {
- config.watch = true;
- config.plugins.push(new NodemonPlugin());
- }
- return config;
- };
Webpack 监视配置将在我们更改文件时重建应用程序,nodemon在我们构建完成重新启动应用程序,需要重新配置下package.json
- "scripts": {
- "start": "webpack --progress --env.development --env.nodemon",
- "start:prod": "webpack --progress --env.nodemon",
- "build": "webpack --progress --env.development",
- "build:prod": "webpack --progress",
- "build:ci": "webpack"
- },
使用TypeScript
先安装依赖项
- npm i -D typescript ts-loader @types/node@^10.0.0
ts-loader(ts加载器)
因为要用ts-loader Webpack插件来编译我们的TypeScript,所以得让Webpack知道我们是使用了ts-loader插件来处理TypeScript文件的,更新之前的webpack.config.js
- // webpack.config.js
- 'use strict';
- const NodemonPlugin = require('nodemon-webpack-plugin');
- module.exports = (env = {}) => {
- const config = {
- entry: ['./src/main.ts'],
- mode: env.development ? 'development' : 'production',
- target: 'node',
- devtool: env.development ? 'cheap-eval-source-map' : false,
- resolve: {
- // Tells Webpack what files to watch
- extensions: ['.ts', '.js'],
- modules: ['node_modules', 'src', 'package.json'],
- },
- module: {
- rules: [
- {
- test: /.ts$/,
- use: 'ts-loader',
- },
- ],
- },
- plugins: [], // Required for config.plugins.push(...);
- };
- if (env.nodemon) {
- config.watch = true;
- config.plugins.push(new NodemonPlugin());
- }
- return config;
- };
tsconfig.json
(编辑:ASP站长网)
|