質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
TypeScript

TypeScriptは、マイクロソフトによって開発された フリーでオープンソースのプログラミング言語です。 TypeScriptは、JavaScriptの構文の拡張であるので、既存の JavaScriptのコードにわずかな修正を加えれば動作します。

Q&A

解決済

1回答

2374閲覧

typescriptとWebpackを導入すると画像が使えなくなる

21212121

総合スコア61

TypeScript

TypeScriptは、マイクロソフトによって開発された フリーでオープンソースのプログラミング言語です。 TypeScriptは、JavaScriptの構文の拡張であるので、既存の JavaScriptのコードにわずかな修正を加えれば動作します。

0グッド

0クリップ

投稿2020/05/17 06:08

現在React、Typescript、Webpackを導入したWebアプリケーションを作っております。
そこで、IMGタグの導入がうまくいきません。

エラーコードは

ERROR in ./src/assets/img/rebase.jpg 1:0 Module parse failed: Unexpected character '�' (1:0)

こちらです。
海外のStackoverflowを拝見したところ、Webpackに問題があるのではないかと言うことで、 test: /.(jpg|JPG|jpeg|png|gif|mp3|svg|ttf|woff2|woff|eot)$/gi,のように試して見ましたがいずれもうまくいきませんでした。

Webpack

1const path = require('path'); 2 3module.exports = { 4 mode: "development", 5 entry: { 6 app: "./src/index.tsx" 7 }, 8 output: { 9 path: __dirname + '/public/js', 10 filename: "[name].js" 11 }, 12 devServer: { 13 contentBase: __dirname + '/public', 14 port: 8080, 15 publicPath: '/js/' 16 }, 17 devtool: "eval-source-map", 18 mode: 'development', 19 module: { 20 rules: [ 21 { 22 test: /.jsx?$/, 23 exclude: /(node_modules|bower_components)/, 24 loader: 'babel-loader', 25 query: { 26 presets: ['react', 'es2015', 'stage-0'], 27 plugins: ['react-html-attrs', 'transform-class-properties', 'transform-decorators-legacy'], 28 } 29 }, 30 { 31 test: /.(jpg|JPG|jpeg|png|gif|mp3|svg|ttf|woff2|woff|eot)$/gi, 32 loaders: null 33 } 34 ], 35 rules: [{ 36 test: /.js$/, 37 enforce: "pre", 38 exclude: /node_modules/, 39 loader: "eslint-loader" 40 }, 41 { 42 test: /.css$/, 43 loader: ["style-loader","css-loader"] 44 }, { 45 test: /.js$/, 46 exclude: /node_modules/, 47 loader: 'babel-loader' 48 }, { 49 test: /.tsx?$/, 50 // TypeScript をコンパイルする 51 use: { 52 loader:'ts-loader', 53 options: { 54 configFile: path.resolve(__dirname, 'tsconfig.json'), 55 }, 56 } 57 }] 58 }, 59 resolve: { 60 extensions: ['.ts', '.tsx', '.js', '.jsx'], 61 //絶対パス設定 62 alias: { 63 '~': path.resolve(__dirname, '') 64 } 65 66 } 67};

tsconfig

1{ 2 "compilerOptions": { 3 /* Basic Options */ 4 // "incremental": true, /* Enable incremental compilation */ 5 "target": "es5", 6 /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */ 7 "module": "es2015", 8 /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */ 9 "lib": [ 10 "es2020", 11 "dom" 12 ], 13 /* Specify library files to be included in the compilation. */ 14 // "allowJs": true, /* Allow javascript files to be compiled. */ 15 // "checkJs": true, /* Report errors in .js files. */ 16 "jsx": "react", 17 /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ 18 // "declaration": true, /* Generates corresponding '.d.ts' file. */ 19 // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ 20 "sourceMap": true, 21 /* Generates corresponding '.map' file. */ 22 // "outFile": "./", /* Concatenate and emit output to single file. */ 23 // "outDir": "./", /* Redirect output structure to the directory. */ 24 // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ 25 // "composite": true, /* Enable project compilation */ 26 // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */ 27 // "removeComments": true, /* Do not emit comments to output. */ 28 // "noEmit": true, /* Do not emit outputs. */ 29 // "importHelpers": true, /* Import emit helpers from 'tslib'. */ 30 // "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */ 31 // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ 32 33 /* Strict Type-Checking Options */ 34 "strict": true, 35 /* Enable all strict type-checking options. */ 36 // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */ 37 // "strictNullChecks": true, /* Enable strict null checks. */ 38 // "strictFunctionTypes": true, /* Enable strict checking of function types. */ 39 // "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */ 40 // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */ 41 // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */ 42 // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */ 43 44 /* Additional Checks */ 45 // "noUnusedLocals": true, /* Report errors on unused locals. */ 46 "noUnusedParameters": false, 47 /* Report errors on unused parameters. */ 48 // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ 49 // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ 50 51 /* Module Resolution Options */ 52 "moduleResolution": "node", 53 /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ 54 //"baseUrl": ".", /* Base directory to resolve non-absolute module names. */ 55 /*"paths": { 56 "~/*":["./*"] 57 },*/ 58 /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ 59 // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ 60 // "typeRoots": [], /* List of folders to include type definitions from. */ 61 // "types": [], /* Type declaration files to be included in compilation. */ 62 // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ 63 "esModuleInterop": true, 64 /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ 65 // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ 66 // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ 67 68 /* Source Map Options */ 69 // "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */ 70 // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ 71 // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */ 72 // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */ 73 74 /* Experimental Options */ 75 // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */ 76 // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */ 77 78 /* Advanced Options */ 79 "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */ , 80 // こちら画像読み込みエラーの解除するため作りました。 81 "typeRoots": [ 82 "node_modules/@types", 83 "types" 84 ], 85 "baseUrl": "src", 86 }, 87 "extends": "./tsconfig.paths.json", 88 "include": [ 89 "src/**/*", 90 "types/**/*" 91 ] 92}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

loaders: nullではなく、file-loaderurl-loaderといった、ファイル読み込み用のローダーを使ってください。

投稿2020/05/17 06:22

maisumakun

総合スコア145183

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

21212121

2020/05/17 07:52

{ test: /.(jpg|JPG|jpeg|png|gif|mp3|svg|ttf|woff2|woff|eot)$/gi, loaders: "file-loader" } こちらに変更しても同様のエラーが出てしまいます。
maisumakun

2020/05/17 07:57

module: 以下に、rules:が複数あります。1つの配列にまとめてください。
21212121

2020/05/17 08:06

const path = require('path'); module.exports = { mode: "development", entry: { app: "./src/index.tsx" }, output: { path: __dirname + '/public/js', filename: "[name].js" }, devServer: { contentBase: __dirname + '/public', port: 8080, publicPath: '/js/' }, devtool: "eval-source-map", mode: 'development', module: { rules: [ { test: /.jsx?$/, exclude: /(node_modules|bower_components)/, loader: 'babel-loader', query: { presets: ['react', 'es2015', 'stage-0'], plugins: ['react-html-attrs', 'transform-class-properties', 'transform-decorators-legacy'], } }, { test: /.(jpg|JPG|jpeg|png|gif|mp3|svg|ttf|woff2|woff|eot)$/gi, loader: "file-loader" } , { rules: { test: /.js$/, enforce: "pre", exclude: /node_modules/, loader: "eslint-loader" } }, { test: /.css$/, loader: ["style-loader","css-loader"] }, { test: /.js$/, exclude: /node_modules/, loader: 'babel-loader' }, { test: /.tsx?$/, // TypeScript をコンパイルする use: { loader:'ts-loader', options: { configFile: path.resolve(__dirname, 'tsconfig.json'), }, } } ] }, resolve: { extensions: ['.ts', '.tsx', '.js', '.jsx'], //絶対パス設定 alias: { '~': path.resolve(__dirname, '') } } };
21212121

2020/05/17 08:07

こちらの認識であってますか?
21212121

2020/05/17 08:18 編集

ありがとうございます。 配列を一つにまとめたら解決しました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問