質問編集履歴

2

エラー追加

2020/01/25 03:04

投稿

kagepedia
kagepedia

スコア16

test CHANGED
File without changes
test CHANGED
@@ -503,3 +503,17 @@
503
503
 
504
504
 
505
505
  ```
506
+
507
+
508
+
509
+ 以下のようなエラーがでます。
510
+
511
+ ```error
512
+
513
+ ERROR in chunk assets/css/main.css [entry]
514
+
515
+ assets/css/main.css
516
+
517
+ Conflict: Multiple chunks emit assets to the same filename assets/css/main.css (chunks 0 and 0)
518
+
519
+ ```

1

修正したwebpack.config.jsとpackage.json githubでソース公開

2020/01/25 03:04

投稿

kagepedia
kagepedia

スコア16

test CHANGED
File without changes
test CHANGED
@@ -78,138 +78,428 @@
78
78
 
79
79
 
80
80
 
81
+ ※追記
82
+
83
+ webpack4で完結させたく開発を進め他のですが、CSSファイルだけ出力されません。
84
+
85
+ 分かる方いましたらお願いいたします。
86
+
87
+ ↓オープンソースとして公開しています。
88
+
89
+ https://github.com/kagepedia/frontend-dev-env
90
+
91
+
92
+
81
93
  ```webpack
82
94
 
83
- const path = require('path');
95
+ const path = require("path");
84
-
96
+
85
- const webpack = require('webpack');
97
+ const globule = require('globule');
86
-
87
- const ExtracktTextPlugin = require('mini-css-extract-plugin');
88
98
 
89
99
  const HtmlWebpackPlugin = require('html-webpack-plugin');
90
100
 
91
-
92
-
93
- module.exports = {
94
-
95
- entry: {
96
-
97
- main: ['./source/assets/js/main.js', './source/assets/scss/main.scss', './source/template/index.pug']
98
-
99
- },
100
-
101
- output: {
102
-
103
- path: path.resolve(__dirname, 'public'),
104
-
105
- filename: './assets/js/[name].bundle.js'
106
-
107
- },
108
-
109
- devServer: {
110
-
111
- port: 3000,
112
-
113
- },
114
-
115
- module: {
116
-
117
- rules: [
118
-
119
- {
120
-
121
- test: /.pug$/,
122
-
123
- use: {
124
-
125
- loader: 'pug-loader',
126
-
127
- options: {
128
-
129
- pretty: true
101
+ const MiniCssExtractPlugin = require('mini-css-extract-plugin');
102
+
103
+ const CopyWebpackPlugin = require('copy-webpack-plugin');
104
+
105
+ const { CleanWebpackPlugin } = require('clean-webpack-plugin');
106
+
107
+ const FixStyleOnlyEntriesPlugin = require("webpack-fix-style-only-entries");
108
+
109
+
110
+
111
+ const targetTypes = {
112
+
113
+ pug: 'html',
114
+
115
+ scss: 'css',
116
+
117
+ js: 'js'
118
+
119
+ };
120
+
121
+
122
+
123
+ const getEntriesList = (targetTypes) => {
124
+
125
+ const entriesList = {};
126
+
127
+ for(const [ srcType, targetType ] of Object.entries(targetTypes)) {
128
+
129
+ const filesMatched = globule.find([`**/*.${srcType}`, `!**/_*.${srcType}`], { cwd : `${__dirname}/source` });
130
+
131
+
132
+
133
+ for(const srcName of filesMatched) {
134
+
135
+ let targetName = srcName.replace(new RegExp(`.${srcType}$`, 'i'), `.${targetType}`);
136
+
137
+ if (targetName.indexOf('template/') !== -1) {
138
+
139
+ targetName = targetName.replace('template/', '');
140
+
141
+ }
142
+
143
+ if (targetName.indexOf('assets/scss/') !== -1) {
144
+
145
+ targetName = targetName.replace('assets/scss/', 'assets/css/');
146
+
147
+ }
148
+
149
+ if (targetName.indexOf('assets/js/') !== -1) {
150
+
151
+ targetName = targetName.replace('assets/js/', 'assets/js/');
152
+
153
+ }
154
+
155
+ entriesList[targetName] = `${__dirname}/source/${srcName}`;
156
+
157
+ }
158
+
159
+ }
160
+
161
+ return entriesList;
162
+
163
+ }
164
+
165
+
166
+
167
+ console.log(getEntriesList({ scss : 'css' }));
168
+
169
+
170
+
171
+ const app = (env, argv) => {
172
+
173
+ let sourceMap = 'source-map'
174
+
175
+ if(argv.mode === 'production') {
176
+
177
+ sourceMap = ''
178
+
179
+ }
180
+
181
+
182
+
183
+ const settings = [
184
+
185
+ // HTML
186
+
187
+ {
188
+
189
+ devServer: {
190
+
191
+ contentBase: path.join(__dirname, './public'),
192
+
193
+ open: true
194
+
195
+ },
196
+
197
+ entry : getEntriesList({ pug : 'html' }),
198
+
199
+ output : {
200
+
201
+ filename : '[name]',
202
+
203
+ path : `${__dirname}/public/`
204
+
205
+ },
206
+
207
+ module : {
208
+
209
+ rules : [
210
+
211
+ {
212
+
213
+ test : /.pug$/,
214
+
215
+ use : [
216
+
217
+ {
218
+
219
+ loader: 'pug-loader',
220
+
221
+ options: {
222
+
223
+ pretty: true
224
+
225
+ }
226
+
227
+ }
228
+
229
+ ]
130
230
 
131
231
  }
132
232
 
133
- }
233
+ ]
134
234
 
135
235
  },
136
236
 
237
+ plugins: [
238
+
239
+ new CopyWebpackPlugin(
240
+
241
+ [{ from : `${__dirname}/source` }],
242
+
243
+ { ignore : Object.keys(targetTypes).map((ext) => `*.${ext}`) }
244
+
245
+ ),
246
+
247
+ new CleanWebpackPlugin({
248
+
249
+ cleanAfterEveryBuildPatterns: ['public'],
250
+
251
+ exclude: ['public/assets/img']
252
+
253
+ }),
254
+
255
+ ]
256
+
257
+ },
258
+
259
+ // CSS
260
+
137
- {
261
+ {
262
+
138
-
263
+ entry : getEntriesList({ scss : 'css' }),
264
+
265
+ output : {
266
+
267
+ filename : '[name]',
268
+
269
+ path : `${__dirname}/public/`,
270
+
271
+ },
272
+
273
+ module: {
274
+
275
+ rules: [
276
+
277
+ {
278
+
139
- test:/.js$/,
279
+ test : /.scss$/,
140
-
141
- exclude: '/node_modules/',
280
+
142
-
143
- use: {
281
+ use : [
282
+
144
-
283
+ MiniCssExtractPlugin.loader,
284
+
285
+ {
286
+
145
- loader: 'babel-loader',
287
+ loader: "css-loader",
146
-
288
+
147
- options: {
289
+ options: {
290
+
148
-
291
+ url: false,
292
+
293
+ sourceMap: true,
294
+
295
+ }
296
+
297
+ },
298
+
299
+ {
300
+
149
- presets: ['babel-preset-env']
301
+ loader: 'sass-loader',
302
+
303
+ options: {
304
+
305
+ sourceMap: true
306
+
307
+ }
308
+
309
+ }
310
+
311
+ ]
150
312
 
151
313
  }
152
314
 
153
- }
315
+ ]
154
316
 
155
317
  },
156
318
 
319
+ devtool: sourceMap,
320
+
321
+ plugins: [
322
+
323
+ new FixStyleOnlyEntriesPlugin(),
324
+
325
+ new MiniCssExtractPlugin({
326
+
327
+ filename: '[name]',
328
+
329
+ })
330
+
331
+ ]
332
+
333
+ },
334
+
335
+ // JS
336
+
157
- {
337
+ {
338
+
158
-
339
+ entry : getEntriesList({ js : 'js' }),
340
+
341
+ output: {
342
+
159
- test: /.scss$/,
343
+ filename : '[name]',
344
+
160
-
345
+ path : `${__dirname}/public/`
346
+
347
+ },
348
+
349
+ module: {
350
+
161
- use: [
351
+ rules: [
162
-
163
- ExtracktTextPlugin.loader,
164
352
 
165
353
  {
166
354
 
355
+ test : /.js$/,
356
+
357
+ exclude: '/node_modules/',
358
+
359
+ use: {
360
+
167
- loader: 'css-loader',
361
+ loader: 'babel-loader',
168
-
362
+
169
- options: {
363
+ options: {
170
-
171
- url: false,
364
+
172
-
173
- //minimize: true,
365
+ presets: ['babel-preset-env']
174
-
366
+
175
- sourceMap: true
367
+ }
176
368
 
177
369
  }
178
370
 
179
- },
180
-
181
- {
182
-
183
- loader: 'sass-loader',
184
-
185
371
  }
186
372
 
187
373
  ]
188
374
 
375
+ },
376
+
377
+ devtool: sourceMap,
378
+
379
+ plugins: [
380
+
381
+ ]
382
+
383
+ },
384
+
385
+ ];
386
+
387
+
388
+
389
+ // pug -> html
390
+
391
+ for(const [ targetName, srcName ] of Object.entries(getEntriesList({ pug : 'html' }))) {
392
+
393
+ settings[0].plugins.push(new HtmlWebpackPlugin({
394
+
395
+ filename : targetName,
396
+
397
+ template : srcName
398
+
399
+ }));
400
+
189
- }
401
+ }
190
-
191
- ]
402
+
192
-
193
- },
194
-
195
- plugins: [
196
-
197
- new ExtracktTextPlugin({
403
+ return settings;
198
-
199
- filename: './assets/css/[name].css'
200
-
201
- }),
202
-
203
- new HtmlWebpackPlugin({
204
-
205
- filename: './index.html',
206
-
207
- template: './source/template/index.pug'
208
-
209
- })
210
-
211
- ]
212
404
 
213
405
  }
214
406
 
407
+
408
+
409
+ module.exports = app;
410
+
411
+
412
+
215
413
  ```
414
+
415
+ ```packagejson
416
+
417
+ {
418
+
419
+ "name": "frontend",
420
+
421
+ "version": "1.0.0",
422
+
423
+ "private": true,
424
+
425
+ "description": "",
426
+
427
+ "main": "index.js",
428
+
429
+ "scripts": {
430
+
431
+ "build": "webpack --mode=production",
432
+
433
+ "dev": "webpack --mode=development",
434
+
435
+ "watch": "webpack --mode=development --watch",
436
+
437
+ "start:dev": "webpack-dev-server --mode=none"
438
+
439
+ },
440
+
441
+ "author": "",
442
+
443
+ "license": "ISC",
444
+
445
+ "devDependencies": {
446
+
447
+ "@babel/core": "^7.8.3",
448
+
449
+ "@babel/preset-env": "^7.8.3",
450
+
451
+ "apply-loader": "^2.0.0",
452
+
453
+ "autoprefixer": "^9.7.4",
454
+
455
+ "babel-core": "^6.26.3",
456
+
457
+ "babel-loader": "^8.0.6",
458
+
459
+ "babel-preset-env": "^1.7.0",
460
+
461
+ "clean-webpack-plugin": "^3.0.0",
462
+
463
+ "copy-webpack-plugin": "^5.1.1",
464
+
465
+ "css-loader": "^3.4.2",
466
+
467
+ "globule": "^1.3.0",
468
+
469
+ "html-webpack-plugin": "^3.2.0",
470
+
471
+ "mini-css-extract-plugin": "^0.9.0",
472
+
473
+ "node-sass": "^4.13.1",
474
+
475
+ "postcss-loader": "^3.0.0",
476
+
477
+ "pug": "^2.0.4",
478
+
479
+ "pug-loader": "^2.4.0",
480
+
481
+ "sass-loader": "^8.0.2",
482
+
483
+ "style-loader": "^1.1.3",
484
+
485
+ "webpack": "^4.41.5",
486
+
487
+ "webpack-cli": "^3.3.10",
488
+
489
+ "webpack-dev-server": "^3.10.1",
490
+
491
+ "webpack-fix-style-only-entries": "^0.4.0"
492
+
493
+ },
494
+
495
+ "dependencies": {
496
+
497
+ "jquery": "^3.4.1"
498
+
499
+ }
500
+
501
+ }
502
+
503
+
504
+
505
+ ```