npm如何与Webpack的构建流程集成?
Hello, World!
, document.getElementById('root') ); ``` 4. 创建Webpack配置文件:在项目根目录下创建一个名为`webpack.config.js`的文件,并编写Webpack配置: ```javascript // webpack.config.js const path = require('path'); module.exports = { entry: './src/index.js', output: { filename: 'bundle.js', path: path.resolve(__dirname, 'dist'), }, module: { rules: [ { test: /\.js$/, exclude: /node_modules/, use: { loader: 'babel-loader', options: { presets: ['@babel/preset-env'], }, }, }, ], }, }; ``` 5. 在package.json中添加构建脚本:在`package.json`文件中添加一个构建脚本: ```json "scripts": { "build": "webpack --config webpack.config.js" } ``` 6. 运行构建脚本:在命令行中运行以下命令,启动Webpack构建: ```bash npm run build ``` 此时,Webpack会根据`webpack.config.js`中的配置,将`src`目录下的`index.js`文件打包成`dist`目录下的`bundle.js`文件。 通过以上步骤,我们成功将npm与Webpack的构建流程集成。这样,我们就可以利用npm管理项目依赖,同时利用Webpack进行模块打包和优化。猜你喜欢:SkyWalking