andywer / threads-plugin

📦 Makes your threads.js code build with webpack out of the box.
Apache License 2.0
28 stars 6 forks source link

Module not found in Node.js Typescript environment. #25

Closed kevzettler closed 4 years ago

kevzettler commented 4 years ago

this is a follow up of https://github.com/andywer/threads.js/issues/311 finally got around to testing. I have it working successfully in the browser build and trying to replicate a node.js server build.

my entry point looks like this server.ts

import { Worker } from "threads";
let worker: Worker = null;

async function main(){
  worker = new Worker('./server.worker');
  console.log("Server Simulation Started...");
}
main();

the code files are adjacent in directory

kevs-mbp:src kevzettler$ ls | grep 'server'
server.ts
server.worker.ts

Seeing this error from webpack

ERROR in ./server.ts
Module not found: Error: Can't resolve './server.worker' in '/Users/kevzettler/code/hypeworks/src'
 @ ./server.ts 1:0-119

WebpackConfig looks like:

'use strict';
const webpack = require('webpack');
const path = require('path');
const fs = require('fs');
const ThreadsPlugin = require('threads-plugin');
const env = require('./env.js');

var nodeModules = {};
fs.readdirSync('node_modules')
  .filter(function(x) {
    return ['.bin'].indexOf(x) === -1;
  })
  .forEach(function(mod) {
    nodeModules[mod] = 'commonjs ' + mod;
  });

module.exports = {
  watch: true,
  cache: false,
  mode: env.NODE_ENV,
  target: "node",
  externals: nodeModules,
  devtool: 'sourcemap',
  context: path.resolve(__dirname, "../src"),
  entry: './server.ts',

  output: {
    path: path.resolve(__dirname, '../build'),
    filename: 'server.js'
  },

  module: {
    //https://github.com/webpack/webpack/issues/138#issuecomment-160638284
    noParse: /node_modules\/json-schema\/lib\/validate\.js/,

    rules: [
      {
        test: /\.tsx?$/,
        exclude: /node_modules/,
        use: {
          loader: 'ts-loader',
          options:{
            compilerOptions: {
              module: "esnext"
            }
          }
        }
      },

      {
        test: /\.kiwi$/,
        exclude: /node_modules/,
        use: [
          'raw-loader',
        ]
      },

      {
        test: /\.(glsl|frag|vert)$/,
        exclude: /node_modules/,
        use: [
          'glslify-import-loader',
          'raw-loader',
          'glslify-loader',
        ]
      },

      {
        test: /\.(png|jpeg)$/,
        exclude: /node_modules/,
        use: [
          'arraybuffer-loader',
        ]
      },

    ]
  },

  plugins: [
    new webpack.EnvironmentPlugin(env),

    new webpack.BannerPlugin({
      banner: 'require("source-map-support").install();',
      raw: true,
      entryOnly: false
    }),

    new ThreadsPlugin(),
  ],

};
andywer commented 4 years ago

Hmm, I think it might be your webpack config: You try to import from a *.ts file (?) without specifying the file extension, but you haven't set the resolve.extensions option either.

kevzettler commented 4 years ago

ah yes thanks for your great support @andywer I accidentally dropped resolve.extensions you're correct.