質問編集履歴
1
エラーメッセージをもう少し追記しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -8,304 +8,316 @@
|
|
8
8
|
|
9
9
|
/Users/xxxxxx/Workspace/example-frontside/webpack.config.ts:1
|
10
10
|
|
11
|
+
import path from 'path';
|
12
|
+
|
13
|
+
^^^^^^
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
SyntaxError: Cannot use import statement outside a module
|
18
|
+
|
19
|
+
at wrapSafe (internal/modules/cjs/loader.js:1116:16)
|
20
|
+
|
21
|
+
at Module._compile (internal/modules/cjs/loader.js:1164:27)
|
22
|
+
|
23
|
+
:
|
24
|
+
|
25
|
+
```
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
webpackの設定ファイルをTypeScriptで記述しています。
|
30
|
+
|
31
|
+
Javascript(webpack.config.js)で同等のは正常に起動しました。
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
故に何か設定が足りないものと思っているのですが、Typescriptの方で
|
36
|
+
|
37
|
+
もし方法がありましたらあればご教授いただきたく。
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
よろしくお願いします。
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
※「cross-env TS_NODE_PROJECT=\"./tsconfig.json\" webpack-dev-server」とかでもダメでした。
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
```
|
50
|
+
|
51
|
+
// ファイルの配置
|
52
|
+
|
53
|
+
┣ src
|
54
|
+
|
55
|
+
┃ ┗ index.ts
|
56
|
+
|
57
|
+
┣ tsconfig.json
|
58
|
+
|
59
|
+
┣ webpack.config.ts
|
60
|
+
|
61
|
+
┗ package.json
|
62
|
+
|
63
|
+
```
|
64
|
+
|
65
|
+
```json
|
66
|
+
|
67
|
+
// package.json
|
68
|
+
|
69
|
+
:
|
70
|
+
|
71
|
+
"scripts": {
|
72
|
+
|
73
|
+
"start:dev": "webpack-dev-server",
|
74
|
+
|
75
|
+
}
|
76
|
+
|
77
|
+
:
|
78
|
+
|
79
|
+
// 関係ありそうなモジュールだけ記載。(@type/〜は省略)
|
80
|
+
|
81
|
+
"devDependencies": {
|
82
|
+
|
83
|
+
"ts-loader": "^7.0.5",
|
84
|
+
|
85
|
+
"ts-node": "^8.10.1",
|
86
|
+
|
87
|
+
"tsconfig-paths": "^3.9.0",
|
88
|
+
|
89
|
+
"tsconfig-paths-webpack-plugin": "^3.2.0",
|
90
|
+
|
91
|
+
"tslib": "^2.0.0",
|
92
|
+
|
93
|
+
"typescript": "^3.9.2",
|
94
|
+
|
95
|
+
"webpack": "^4.42.1",
|
96
|
+
|
97
|
+
"webpack-cli": "^3.3.11",
|
98
|
+
|
99
|
+
"webpack-dev-server": "^3.10.3"
|
100
|
+
|
101
|
+
}
|
102
|
+
|
103
|
+
:
|
104
|
+
|
105
|
+
```
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
```typescript
|
110
|
+
|
111
|
+
// webpack.config.ts
|
112
|
+
|
11
113
|
import * as path from 'path';
|
12
114
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
webpack
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
115
|
+
import {
|
116
|
+
|
117
|
+
Configuration as WebpackConfiguration
|
118
|
+
|
119
|
+
} from 'webpack';
|
120
|
+
|
121
|
+
import { Configuration as WebpackDevServerConfiguration } from 'webpack-dev-server';
|
122
|
+
|
123
|
+
import { TsconfigPathsPlugin } from 'tsconfig-paths-webpack-plugin';
|
124
|
+
|
125
|
+
// import TsconfigPathsPlugin from 'tsconfig-paths-webpack-plugin'; // こちらもNG
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
interface Configuration extends WebpackConfiguration {
|
130
|
+
|
131
|
+
devServer?: WebpackDevServerConfiguration;
|
132
|
+
|
133
|
+
}
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
const devServer: WebpackDevServerConfiguration = {
|
138
|
+
|
139
|
+
contentBase: path.resolve(__dirname, 'public'),
|
140
|
+
|
141
|
+
host: '0.0.0.0',
|
142
|
+
|
143
|
+
port: 3000,
|
144
|
+
|
145
|
+
disableHostCheck: true,
|
146
|
+
|
147
|
+
historyApiFallback: true,
|
148
|
+
|
149
|
+
compress: true,
|
150
|
+
|
151
|
+
hotOnly: true,
|
152
|
+
|
153
|
+
proxy: {
|
154
|
+
|
155
|
+
'/api': {
|
156
|
+
|
157
|
+
target: 'http://localhost:8080',
|
158
|
+
|
159
|
+
}
|
160
|
+
|
161
|
+
}
|
162
|
+
|
163
|
+
};
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
export default (_env: never, args: Configuration) => {
|
168
|
+
|
169
|
+
const mode = args.mode || 'development';
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
const config: Configuration = {
|
174
|
+
|
175
|
+
mode,
|
176
|
+
|
177
|
+
devtool: (mode === 'development') ? 'inline-source-map' : false,
|
178
|
+
|
179
|
+
devServer: (mode === 'development') ? devServer : undefined,
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
entry: {
|
184
|
+
|
185
|
+
main: './src'
|
186
|
+
|
187
|
+
},
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
output: {
|
192
|
+
|
193
|
+
publicPath: '/',
|
194
|
+
|
195
|
+
path: path.resolve(__dirname, './public/dist'),
|
196
|
+
|
197
|
+
filename: '[name].js',
|
198
|
+
|
199
|
+
},
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
resolve: {
|
204
|
+
|
205
|
+
alias: {
|
206
|
+
|
207
|
+
'~': path.resolve('src'),
|
208
|
+
|
209
|
+
},
|
210
|
+
|
211
|
+
extensions: [
|
212
|
+
|
213
|
+
'.js',
|
214
|
+
|
215
|
+
'.json',
|
216
|
+
|
217
|
+
'.ts',
|
218
|
+
|
219
|
+
],
|
220
|
+
|
221
|
+
plugins: [new TsconfigPathsPlugin({ configFile: './tsconfig.json' })]
|
222
|
+
|
223
|
+
},
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
module: {
|
228
|
+
|
229
|
+
rules: [
|
230
|
+
|
231
|
+
{
|
232
|
+
|
233
|
+
test: /.tsx?$/,
|
234
|
+
|
235
|
+
loader: 'ts-loader',
|
236
|
+
|
237
|
+
exclude: '/node_modules/',
|
238
|
+
|
239
|
+
},
|
240
|
+
|
241
|
+
],
|
242
|
+
|
243
|
+
},
|
244
|
+
|
245
|
+
};
|
246
|
+
|
247
|
+
return config;
|
248
|
+
|
249
|
+
};
|
250
|
+
|
251
|
+
```
|
252
|
+
|
253
|
+
|
52
254
|
|
53
255
|
```json
|
54
256
|
|
55
|
-
//
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
"
|
60
|
-
|
61
|
-
"
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
:
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
"
|
70
|
-
|
71
|
-
"t
|
72
|
-
|
73
|
-
"t
|
74
|
-
|
75
|
-
"
|
76
|
-
|
77
|
-
"
|
78
|
-
|
79
|
-
"
|
80
|
-
|
81
|
-
"t
|
82
|
-
|
83
|
-
"
|
84
|
-
|
85
|
-
"
|
86
|
-
|
87
|
-
"
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
:
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
devServer?: WebpackDevServerConfiguration;
|
257
|
+
// tsconfig.json
|
258
|
+
|
259
|
+
{
|
260
|
+
|
261
|
+
"compilerOptions": {
|
262
|
+
|
263
|
+
"target": "ES5",
|
264
|
+
|
265
|
+
"module": "ESNext",
|
266
|
+
|
267
|
+
"moduleResolution": "node",
|
268
|
+
|
269
|
+
"sourceMap": true,
|
270
|
+
|
271
|
+
"strict": true,
|
272
|
+
|
273
|
+
"strictNullChecks": true,
|
274
|
+
|
275
|
+
"noImplicitAny": true,
|
276
|
+
|
277
|
+
"noImplicitThis": true,
|
278
|
+
|
279
|
+
"noImplicitReturns": true,
|
280
|
+
|
281
|
+
"noUnusedLocals": false,
|
282
|
+
|
283
|
+
"noUnusedParameters": false,
|
284
|
+
|
285
|
+
"esModuleInterop": true,
|
286
|
+
|
287
|
+
"forceConsistentCasingInFileNames": true,
|
288
|
+
|
289
|
+
"baseUrl": "./",
|
290
|
+
|
291
|
+
"paths": {
|
292
|
+
|
293
|
+
"~/*": [
|
294
|
+
|
295
|
+
"src/*"
|
296
|
+
|
297
|
+
]
|
298
|
+
|
299
|
+
},
|
300
|
+
|
301
|
+
"types": [
|
302
|
+
|
303
|
+
"@types/node"
|
304
|
+
|
305
|
+
]
|
306
|
+
|
307
|
+
},
|
308
|
+
|
309
|
+
"include": [
|
310
|
+
|
311
|
+
"src/**/*"
|
312
|
+
|
313
|
+
],
|
314
|
+
|
315
|
+
"exclude": [
|
316
|
+
|
317
|
+
"node_modules"
|
318
|
+
|
319
|
+
]
|
120
320
|
|
121
321
|
}
|
122
322
|
|
123
|
-
|
124
|
-
|
125
|
-
const devServer: WebpackDevServerConfiguration = {
|
126
|
-
|
127
|
-
contentBase: path.resolve(__dirname, 'public'),
|
128
|
-
|
129
|
-
host: '0.0.0.0',
|
130
|
-
|
131
|
-
port: 3000,
|
132
|
-
|
133
|
-
disableHostCheck: true,
|
134
|
-
|
135
|
-
historyApiFallback: true,
|
136
|
-
|
137
|
-
compress: true,
|
138
|
-
|
139
|
-
hotOnly: true,
|
140
|
-
|
141
|
-
proxy: {
|
142
|
-
|
143
|
-
'/api': {
|
144
|
-
|
145
|
-
target: 'http://localhost:8080',
|
146
|
-
|
147
|
-
}
|
148
|
-
|
149
|
-
}
|
150
|
-
|
151
|
-
};
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
export default (_env: never, args: Configuration) => {
|
156
|
-
|
157
|
-
const mode = args.mode || 'development';
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
const config: Configuration = {
|
162
|
-
|
163
|
-
mode,
|
164
|
-
|
165
|
-
devtool: (mode === 'development') ? 'inline-source-map' : false,
|
166
|
-
|
167
|
-
devServer: (mode === 'development') ? devServer : undefined,
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
entry: {
|
172
|
-
|
173
|
-
main: './src'
|
174
|
-
|
175
|
-
},
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
output: {
|
180
|
-
|
181
|
-
publicPath: '/',
|
182
|
-
|
183
|
-
path: path.resolve(__dirname, './public/dist'),
|
184
|
-
|
185
|
-
filename: '[name].js',
|
186
|
-
|
187
|
-
},
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
resolve: {
|
192
|
-
|
193
|
-
alias: {
|
194
|
-
|
195
|
-
'~': path.resolve('src'),
|
196
|
-
|
197
|
-
},
|
198
|
-
|
199
|
-
extensions: [
|
200
|
-
|
201
|
-
'.js',
|
202
|
-
|
203
|
-
'.json',
|
204
|
-
|
205
|
-
'.ts',
|
206
|
-
|
207
|
-
],
|
208
|
-
|
209
|
-
plugins: [new TsconfigPathsPlugin({ configFile: './tsconfig.json' })]
|
210
|
-
|
211
|
-
},
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
module: {
|
216
|
-
|
217
|
-
rules: [
|
218
|
-
|
219
|
-
{
|
220
|
-
|
221
|
-
test: /.tsx?$/,
|
222
|
-
|
223
|
-
loader: 'ts-loader',
|
224
|
-
|
225
|
-
exclude: '/node_modules/',
|
226
|
-
|
227
|
-
},
|
228
|
-
|
229
|
-
],
|
230
|
-
|
231
|
-
},
|
232
|
-
|
233
|
-
};
|
234
|
-
|
235
|
-
return config;
|
236
|
-
|
237
|
-
};
|
238
|
-
|
239
|
-
```
|
323
|
+
```
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
```json
|
244
|
-
|
245
|
-
// tsconfig.json
|
246
|
-
|
247
|
-
{
|
248
|
-
|
249
|
-
"compilerOptions": {
|
250
|
-
|
251
|
-
"target": "ES5",
|
252
|
-
|
253
|
-
"module": "ESNext",
|
254
|
-
|
255
|
-
"moduleResolution": "node",
|
256
|
-
|
257
|
-
"sourceMap": true,
|
258
|
-
|
259
|
-
"strict": true,
|
260
|
-
|
261
|
-
"strictNullChecks": true,
|
262
|
-
|
263
|
-
"noImplicitAny": true,
|
264
|
-
|
265
|
-
"noImplicitThis": true,
|
266
|
-
|
267
|
-
"noImplicitReturns": true,
|
268
|
-
|
269
|
-
"noUnusedLocals": false,
|
270
|
-
|
271
|
-
"noUnusedParameters": false,
|
272
|
-
|
273
|
-
"esModuleInterop": true,
|
274
|
-
|
275
|
-
"forceConsistentCasingInFileNames": true,
|
276
|
-
|
277
|
-
"baseUrl": "./",
|
278
|
-
|
279
|
-
"paths": {
|
280
|
-
|
281
|
-
"~/*": [
|
282
|
-
|
283
|
-
"src/*"
|
284
|
-
|
285
|
-
]
|
286
|
-
|
287
|
-
},
|
288
|
-
|
289
|
-
"types": [
|
290
|
-
|
291
|
-
"@types/node"
|
292
|
-
|
293
|
-
]
|
294
|
-
|
295
|
-
},
|
296
|
-
|
297
|
-
"include": [
|
298
|
-
|
299
|
-
"src/**/*"
|
300
|
-
|
301
|
-
],
|
302
|
-
|
303
|
-
"exclude": [
|
304
|
-
|
305
|
-
"node_modules"
|
306
|
-
|
307
|
-
]
|
308
|
-
|
309
|
-
}
|
310
|
-
|
311
|
-
```
|