简体   繁体   English

Webpack 从父文件夹加载错误

[英]Webpack loads in wrong from parent folder

I am trying to run a watch command on my Webpack in order to compile my code but I always face an issue with my file.我试图在我的 Webpack 上运行 watch 命令以编译我的代码,但我的文件总是遇到问题。 Even trying to change the relative paths with some ../ did not work即使尝试使用某些../更改相对路径也不起作用

ERROR in code
Module not found: Error: Can't resolve '../src/code.ts' in '/Users/giardiv/Lines/f-variables'

ERROR in ui
Module not found: Error: Can't resolve '../src/ui.tsx' in '/Users/giardiv/Lines/f-variables'

ERROR in   Error: Child compilation failed:
  Module not found: Error: Can't resolve '/Users/giardiv/Lines/src/ui.html' in '  /Users/giardiv/Lines/f-variables':
  Error: Can't resolve '/Users/giardiv/Lines/src/ui.html' in '/Users/giardiv/Lin  es/f-variables'
  ModuleNotFoundError: Module not found: Error: Can't resolve '/Users/giardiv/Li  nes/src/ui.html' in '/Users/giardiv/Lines/f-variables'

While my folder tree is quite basic, f-variables is the src and other files are directly in it虽然我的文件夹树非常基本,但f-variablessrc ,其他文件直接在其中

在此处输入图像描述

This is the webpack config这是 webpack 配置

const HtmlWebpackInlineSourcePlugin = require('html-webpack-inline-source-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const path = require('path')

module.exports = (env, argv) => ({
  mode: argv.mode === 'production' ? 'production' : 'development',

  // This is necessary because Figma's 'eval' works differently than normal eval
  devtool: argv.mode === 'production' ? false : 'inline-source-map',

  entry: {
    ui: "../src/ui.tsx", // "./src/ui.tsx" calls the same error
    code: "../src/code.ts" // "./src/code.ts" calls the same error
  },

  module: {
    rules: [
      // Converts TypeScript code to JavaScript
      { test: /\.tsx?$/, use: 'ts-loader', exclude: /node_modules/ },

      // Enables including CSS by doing "import './file.css'" in your TypeScript code
      { test: /\.css$/, use: ['style-loader', { loader: 'css-loader' }] },

      // Allows you to use "<%= require('./file.svg') %>" in your HTML code to get a data URI
      { test: /\.(png|jpg|gif|webp|svg)$/, loader: 'url-loader' },
    ],
  },

  // Webpack tries these extensions for you if you omit the extension like "import './file'"
  resolve: { extensions: ['.tsx', '.ts', '.jsx', '.js'] },

  output: {
    filename: '[name].js',
    path: path.resolve(__dirname, 'dist'), // Compile into a folder called "dist"
    publicPath: '/',
  },

  // Tells Webpack to generate "ui.html" and to inline "ui.ts" into it
  plugins: [
    new HtmlWebpackPlugin({
      template: '../src/ui.html',
      filename: 'ui.html',
      inlineSource: '.(js)$',
      chunks: ['ui'],
    }),
    new HtmlWebpackInlineSourcePlugin(HtmlWebpackPlugin),
  ],
})

Because the paths in entry are wrong in your webpack.config.js file.因为您的webpack.config.js文件中的entry路径错误。

It should be set as below since you have webpack.config.js besides src directory.它应该设置如下,因为除了src目录之外还有webpack.config.js

entry: {
    ui: "./src/ui.tsx",
    code: "./src/code.ts"
},

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

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