13
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

webpackで複数のエントリーとアウトプットの設定で引っかかったこと

Last updated at Posted at 2020-01-07

ElectronとTypeScriptを使うためにwebpackを使った。

設定は以下ファイル。

webpack.config.js
const path = require('path');

module.exports = {
    mode: 'development',
    target: 'electron-main',
    entry: './src/ts/index.ts',
    output: {
        path: path.join(__dirname, "dist"),
        filename: "main.js"
    },
    module: {
        rules: [{
        test: /\.ts$/,
        use: 'ts-loader'
        }]
    },
    resolve: {
        modules: [
        "node_modules",
        ],
        extensions: [
        '.ts',
        '.js'
        ]
    },
    node: {
        __dirname: false,
        __filename: false
    },
};

module.exports = {
    mode: 'development',
    target: 'electron-renderer',
    entry: path.join(__dirname, 'src', 'ts', 'renderer', 'index'),
    output: {
      filename: 'index.js',
      path: path.resolve(__dirname, 'dist', 'scripts')
    },
    resolve: {
      extensions: ['.json', '.js', '.ts']
    },
    module: {
      rules: [{
        test: /\.(tsx|ts)$/,
        use: [
          'ts-loader'
        ],
        include: [
          path.resolve(__dirname, 'src'),
          path.resolve(__dirname, 'node_modules'),
        ],
      }]
    },
  };

この状態で、


webpack --watch

を実行しても、2つ目のmodule.exportsしか実行されていない問題に遭遇。

解決策

webpack.config.js
const path = require('path');

const main = {
    mode: 'development',
    target: 'electron-main',
    entry: './src/ts/index.ts',
    output: {
        path: path.join(__dirname, "dist"),
        filename: "main.js"
    },
    module: {
        rules: [{
        test: /\.ts$/,
        use: 'ts-loader'
        }]
    },
    resolve: {
        modules: [
        "node_modules",
        ],
        extensions: [
        '.ts',
        '.js'
        ]
    },
    node: {
        __dirname: false,
        __filename: false
    },
};

const renderer = {
    mode: 'development',
    target: 'electron-renderer',
    entry: path.join(__dirname, 'src', 'ts', 'renderer', 'index'),
    output: {
      filename: 'index.js',
      path: path.resolve(__dirname, 'dist', 'scripts')
    },
    resolve: {
      extensions: ['.json', '.js', '.ts']
    },
    module: {
      rules: [{
        test: /\.(tsx|ts)$/,
        use: [
          'ts-loader'
        ],
        include: [
          path.resolve(__dirname, 'src'),
          path.resolve(__dirname, 'node_modules'),
        ],
      }]
    },
  };

  module.exports = [
      main, renderer
  ];
13
7
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?