Webpack配置 – 如何编译不同路径中的文件

huangapple go评论105阅读模式
英文:

Webpack Config - how can I compiler file in difference path

问题

  1. 我有
  2. - `./src` - 这是我的源文件index.htmlindex.tsstyle.scss
  3. - `./public` - 这是我的站点和编译器文件index.htmlbundle.jsstyle.css
  4. 我想要构建我的 `./src` 文件中的 `index.html` `./public/`将我的 `index.ts` 构建到 `./public/success/script/index.js` `style.scss` 构建到 `./public/success/style/style.css` 举个例子
  5. 我应该如何做呢
  6. `webpack.config.js`:
  7. ```js
  8. const path = require('path');
  9. const HtmlWebpackPlugin = require('html-webpack-plugin');
  10. module.exports = {
  11. entry: path.resolve(__dirname, './src/scripts/index.ts'),
  12. mode: "development",
  13. output: {
  14. path: path.resolve(__dirname, './public/'),
  15. filename: 'app.min.js',
  16. },
  17. devServer: {
  18. static:{
  19. directory: path.join(__dirname, './public')
  20. },
  21. port: 3000,
  22. open: true,
  23. hot: true
  24. },
  25. module: {
  26. rules: [
  27. {
  28. test: /\.ts|.js$/,
  29. use: ["ts-loader"],
  30. exclude: /node_modules/,
  31. },
  32. {
  33. test: /\.scss$/,
  34. use:[
  35. "sass-loader",
  36. "style-loader",
  37. "css-loader"
  38. ]
  39. }
  40. ]
  41. },
  42. plugins: [
  43. new HtmlWebpackPlugin({
  44. template: path.resolve(__dirname, './src/index.html'),
  45. filename: path.resolve(__dirname, "./public/index.html"),
  46. inject: "body"
  47. }),
  48. ],
  49. };
英文:

I have

  • ./src - it's my source files (index.html, index.ts, style.scss)
  • ./public - my site and compiler files (index.html, bundle.js, style.css)

and I wanna build my ./src files index.html into ./public/, my index.ts into ./public/success/script/index.js, and style.scss into ./public/success/style/style.css for exemple

how can I do that?

webpack.config.js:

  1. const path = require('path');
  2. const HtmlWebpackPlugin = require('html-webpack-plugin');
  3. module.exports = {
  4. entry: path.resolve(__dirname, './src/scripts/index.ts'),
  5. mode: "development",
  6. output: {
  7. path: path.resolve(__dirname, './public/'),
  8. filename: 'app.min.js',
  9. },
  10. devServer: {
  11. static:{
  12. directory: path.join(__dirname, './public')
  13. },
  14. port: 3000,
  15. open: true,
  16. hot: true
  17. },
  18. module: {
  19. rules: [
  20. {
  21. test: /\.ts|.js$/,
  22. use: ["ts-loader"],
  23. exclude: /node_modules/,
  24. },
  25. {
  26. test: /\.scss$/,
  27. use:[
  28. "sass-loader",
  29. "style-loader",
  30. "css-loader"
  31. ]
  32. }
  33. ]
  34. },
  35. plugins: [
  36. new HtmlWebpackPlugin({
  37. template: path.resolve(__dirname, './src/index.html'),
  38. filename: path.resolve(__dirname, "./public/index.html"),
  39. inject: "body"
  40. }),
  41. ],
  42. };

答案1

得分: 0

请注意,以下是翻译好的部分:

See output.filename configuration, you are allowed to use something like 'js/[name]/bundle.js' to create a folder structure.
查看 output.filename 配置,你可以使用类似 'js/[name]/bundle.js' 这样的方式来创建文件夹结构。

The filename of MiniCssExtractPlugin works like output.filename.
MiniCssExtractPluginfilename 配置与 output.filename 类似。

In order to simplify the configuration, the following example does not use ts-loader and sass-loader as they do not affect the build output.
为了简化配置,以下示例不使用 ts-loadersass-loader,因为它们不会影响构建输出。

Project structure(after building):
构建后的项目结构:

  1. $ tree -L 4 -I node_modules
  2. .
  3. ├── package-lock.json
  4. ├── package.json
  5. ├── public
  6. ├── index.html
  7. └── success
  8. ├── script
  9. └── index.js
  10. └── style
  11. └── style.css
  12. ├── src
  13. ├── index.html
  14. ├── index.js
  15. └── style.css
  16. └── webpack.config.js

src/index.js:
src/index.js

  1. import './style.css';
  2. console.log('webpack mini template');

src/index.html:
src/index.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. </body>
  11. </html>

src/style.css:
src/style.css

  1. body {
  2. background: green;
  3. }

webpack.config.js:
webpack.config.js

  1. const path = require('path');
  2. const HtmlWebpackPlugin = require('html-webpack-plugin');
  3. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  4. module.exports = {
  5. entry: path.resolve(__dirname, './src/index.js'),
  6. mode: 'development',
  7. output: {
  8. path: path.resolve(__dirname, './public/'),
  9. filename: 'success/script/index.js',
  10. clean: true,
  11. },
  12. module: {
  13. rules: [
  14. {
  15. test: /\.css$/,
  16. use: [MiniCssExtractPlugin.loader, 'css-loader'],
  17. },
  18. ],
  19. },
  20. plugins: [
  21. new HtmlWebpackPlugin({
  22. template: path.resolve(__dirname, './src/index.html'),
  23. inject: 'body',
  24. }),
  25. new MiniCssExtractPlugin({ filename: 'success/style/style.css' }),
  26. ],
  27. };

Build logs:
构建日志:

  1. > webpack
  2. asset success/script/index.js 3.23 KiB [emitted] (name: main)
  3. asset index.html 357 bytes [emitted]
  4. asset success/style/style.css 242 bytes [emitted] (name: main)
  5. Entrypoint main 3.46 KiB = success/style/style.css 242 bytes success/script/index.js 3.23 KiB
  6. orphan modules 2.76 KiB (javascript) 937 bytes (runtime) [orphan] 7 modules
  7. runtime modules 274 bytes 1 module
  8. cacheable modules 111 bytes (javascript) 29 bytes (css/mini-extract)
  9. ./src/index.js 61 bytes [built] [code generated]
  10. ./src/style.css 50 bytes [built] [code generated]
  11. css ./node_modules/css-loader/dist/cjs.js!./src/style.css 29 bytes [built] [code generated]
  12. webpack 5.88.2 compiled successfully in 203 ms

Output:
输出:

public/index.html:
public/index.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <link href="success/style/style.css" rel="stylesheet"></head>
  9. <body>
  10. <script defer src="success/script/index.js"></script></body>
  11. </html>

package.json:
package.json

  1. {
  2. "version": "1.0.0",
  3. "scripts": {
  4. "build": "webpack"
  5. },
  6. "devDependencies": {
  7. "css-loader": "^6.8.1",
  8. "html-webpack-plugin": "^5.5.3",
  9. "mini-css-extract-plugin": "^2.7.6",
  10. "webpack": "^5.80.0",
  11. "webpack-cli": "^5.0.2"
  12. }
  13. }
英文:

See output.filename configuration, you are allowed to use something like 'js/[name]/bundle.js' to create a folder structure.

The filename of MiniCssExtractPlugin works like output.filename.

In order to simplify the configuration, the following example does not use ts-loader and sass-loader as they do not affect the build output.

Project structure(after building):

  1. $ tree -L 4 -I node_modules
  2. .
  3. ├── package-lock.json
  4. ├── package.json
  5. ├── public
  6. ├── index.html
  7. └── success
  8. ├── script
  9. └── index.js
  10. └── style
  11. └── style.css
  12. ├── src
  13. ├── index.html
  14. ├── index.js
  15. └── style.css
  16. └── webpack.config.js

src/index.js:

  1. import './style.css';
  2. console.log('webpack mini template');

src/index.html:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. </body>
  11. </html>

src/style.css:

  1. body {
  2. background: green;
  3. }

webpack.config.js:

  1. const path = require('path');
  2. const HtmlWebpackPlugin = require('html-webpack-plugin');
  3. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  4. module.exports = {
  5. entry: path.resolve(__dirname, './src/index.js'),
  6. mode: 'development',
  7. output: {
  8. path: path.resolve(__dirname, './public/'),
  9. filename: 'success/script/index.js',
  10. clean: true,
  11. },
  12. module: {
  13. rules: [
  14. {
  15. test: /\.css$/,
  16. use: [MiniCssExtractPlugin.loader, 'css-loader'],
  17. },
  18. ],
  19. },
  20. plugins: [
  21. new HtmlWebpackPlugin({
  22. template: path.resolve(__dirname, './src/index.html'),
  23. inject: 'body',
  24. }),
  25. new MiniCssExtractPlugin({ filename: 'success/style/style.css' }),
  26. ],
  27. };

Build logs:

  1. > webpack
  2. asset success/script/index.js 3.23 KiB [emitted] (name: main)
  3. asset index.html 357 bytes [emitted]
  4. asset success/style/style.css 242 bytes [emitted] (name: main)
  5. Entrypoint main 3.46 KiB = success/style/style.css 242 bytes success/script/index.js 3.23 KiB
  6. orphan modules 2.76 KiB (javascript) 937 bytes (runtime) [orphan] 7 modules
  7. runtime modules 274 bytes 1 module
  8. cacheable modules 111 bytes (javascript) 29 bytes (css/mini-extract)
  9. ./src/index.js 61 bytes [built] [code generated]
  10. ./src/style.css 50 bytes [built] [code generated]
  11. css ./node_modules/css-loader/dist/cjs.js!./src/style.css 29 bytes [built] [code generated]
  12. webpack 5.88.2 compiled successfully in 203 ms

Output:

public/index.html:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <link href="success/style/style.css" rel="stylesheet"></head>
  9. <body>
  10. <script defer src="success/script/index.js"></script></body>
  11. </html>

package.json:

  1. {
  2. "version": "1.0.0",
  3. "scripts": {
  4. "build": "webpack"
  5. },
  6. "devDependencies": {
  7. "css-loader": "^6.8.1",
  8. "html-webpack-plugin": "^5.5.3",
  9. "mini-css-extract-plugin": "^2.7.6",
  10. "webpack": "^5.80.0",
  11. "webpack-cli": "^5.0.2"
  12. }
  13. }

huangapple
  • 本文由 发表于 2023年7月31日 22:03:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76804386.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定