2

I don't undrestand what's doing this. This doesn't seem normal right? I am getting a .js file generated for every .tsx or .ts file after I build but this shouldn't be in my source. I should only see .js files in my build obviously.

My Scripts

"start": "PORT=3000 nodemon --trace-warnings --experimental-json-modules --no-warnings dist/server/server.js", "build": "NODE_ENV=production yarn lint && yarn copyData && yarn compile-server && yarn start & webpack -p --env=prod --watch", "dev": "NODE_ENV=development yarn lint && yarn copyData && yarn compile-server && yarn start & webpack-dev-server -d --watch", 

enter image description here ./webpack.config.js

const path = require('path'); const { CleanWebpackPlugin } = require('clean-webpack-plugin'); const TerserJSPlugin = require('terser-webpack-plugin'); const HtmlWebPackPlugin = require('html-webpack-plugin'); const MiniCssExtractPlugin = require('mini-css-extract-plugin'); const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin'); const CopyPlugin = require('copy-webpack-plugin'); const isProduction = process.env.NODE_ENV === 'production'; const html = () => new HtmlWebPackPlugin({ template: path.resolve(__dirname, 'src/client', 'index.html'), filename: 'index.html', hash: true, }); const copyAllOtherDistFiles = () => new CopyPlugin({ patterns: [ { from: 'src/client/assets', to: 'lib/assets' }, { from: 'package.json', to: './' }, { from: 'ext/ink-3.1.10/js/ink-all.min.js', to: 'lib/js' }, { from: 'ext/ink-3.1.10/js/autoload.min.js', to: 'lib/js' }, { from: 'ext/ink-3.1.10/css/ink-flex.min.css', to: 'lib/css' }, { from: 'ext/js/jquery-2.2.3.min.js', to: 'lib/js' }, { from: 'ext/ink-3.1.10/fonts', to: 'lib/css/fonts' }, { from: 'feed.xml', to: './' }, ], }); module.exports = { entry: './src/client/index.tsx', output: { filename: 'scripts/app.[hash].bundle.js', publicPath: '/', path: path.resolve(__dirname, 'dist'), }, resolve: { extensions: ['.ts', '.tsx', '.js'], }, devtool: 'source-map', devServer: { open: true, writeToDisk: false, publicPath: '/', compress: true, historyApiFallback: { index: '/', }, stats: 'errors-only', proxy: { '/api': { target: 'http://localhost:3000', secure: false, changeOrigin: true, logLevel: 'debug', }, }, }, optimization: { splitChunks: { cacheGroups: { styles: { name: 'styles', test: /\.css$/, chunks: 'all', enforce: true, }, }, }, }, module: { rules: [ { test: /\.(js)$/, exclude: /node_modules/, use: { loader: 'babel-loader', }, }, { test: /\.(tsx|ts)?$/, use: 'ts-loader', exclude: /node_modules/, }, { test: /\.html$/, exclude: /node_modules/, use: [ { loader: 'html-loader', }, ], }, { test: /\.less$/, exclude: /node_modules/, use: [MiniCssExtractPlugin.loader, 'css-loader', 'less-loader'], }, { test: /\.css$/, exclude: /node_modules/, use: [ { loader: MiniCssExtractPlugin.loader, options: { publicPath: '../../', outputPath: 'lib/css', }, }, 'css-loader', ], }, { test: /\.(woff(2)?|ttf|eot|otf|svg)?$/, use: [ { loader: 'file-loader', options: { name: '[name].[ext]', publicPath: '/lib/css/fonts', // <--resolve the path in css files outputPath: 'lib/css/fonts', // <-- path to place font files }, }, ], }, { test: /\.(png|svg|jpg|gif)$/, use: ['url-loader'], }, ], }, plugins: isProduction ? [ new CleanWebpackPlugin(), copyAllOtherDistFiles(), new MiniCssExtractPlugin({ filename: 'lib/css/[name].[hash].css', }), html(), ] : [ copyAllOtherDistFiles(), new MiniCssExtractPlugin({ filename: 'lib/css/[name].[hash].css', }), html(), ], }; 

./tsconfig.json

{ "compilerOptions": { "target": "ES2015", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */ "module": "es2020", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */ "lib": ["es5", "es6", "dom"], /* Specify library files to be included in the compilation. */ "moduleResolution": "node", "allowJs": false, /* Allow javascript files to be compiled. */ "jsx": "react", "noImplicitAny": false, "sourceMap": false, /* Generates corresponding '.map' file. */ "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ "removeComments": true, /* Do not emit comments to output. */ "strict": true, /* Enable all strict type-checking options. */ "noUnusedLocals": true, /* Report errors on unused locals. */ "noUnusedParameters": true, /* Report errors on unused parameters. */ "typeRoots": [ "node_modules/@types" ], /* List of folders to include type definitions from. */ "esModuleInterop": true, "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ "resolveJsonModule": true, "skipLibCheck": true, /* Skip type checking of declaration files. */ "forceConsistentCasingInFileNames": true, }, "include": [ "src" ], "exclude": [ "/node_modules", "/src/server", "/src/client/js/ink-config.js", "**/test", "dist" ] } 

./src/server/tsconfig.json

{ "extends": "../../tsconfig", "compilerOptions": { "outDir": "../../dist/server", /* Redirect output structure to the directory. */ "rootDir": "." /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ }, "module": "commonjs", "include": ["./*.ts"], "resolveJsonModule": true } 
20
  • Possible duplicate of stackoverflow.com/questions/35662777/… Commented Aug 15, 2020 at 6:01
  • not really I don't see why I'd need an outDir if webpack is building almost everything for me and bundles to its own specified output directory. I'm not asking tsconfig to output all the ts files for my build. webpack is bundling and using typescript to process my bundle. I need the bundle webpack creates, not a js file per everything webpack just bundled too right?? maybe I'm just really confused here. I only have a child tsconfig outputting something for my server, but the rest is bundled by webpack for the client side Commented Aug 15, 2020 at 6:12
  • 1
    What happens if you add "outDir": "dist" under "compilerOptions"? Commented Aug 15, 2020 at 6:26
  • Side-by-side emit is the default. There are a lot of good reason for this including it makes it much easier for people to get started. If you only want webpack to compile, use the noEmit option Commented Aug 15, 2020 at 6:28
  • If I add "outDir": "dist" then it just outputs all those .js files to dist instead of src but I don't want that as my webpack bundle already included all those js files during the build Commented Aug 15, 2020 at 6:31

2 Answers 2

5

My resolution was to:

  1. Since I was using ts-loader in webpack, it expects tsconfig to emit .js files that webpack can then process via the loader
  2. Due to #1, I couldn't just set the emit to false in tsconfig because webpack relies on processing the outputted js files so that it can create a bundle off them ultimately
  3. Since I don't want those intermediate js files that are generated by tsconfig cluttering my src folder, I set my tsconfig output directory to be build
  4. webpack's output directory is dist so I will end up with only the production build

By doing this, the intermediate TS js files are outputted to build, picked up by webpack, processed into a bundle, and bundle is outputted to dist.

./tsconfig.json

"compilerOptions": { "outDir": "./build", } 

./webpack.config.js

entry: './src/client/index.tsx', output: { filename: 'scripts/app.[hash].bundle.js', publicPath: '/', path: path.resolve(__dirname, 'dist'), }, resolve: { extensions: ['.ts', '.tsx', '.js'], }, { test: /\.(tsx|ts)?$/, use: 'ts-loader', exclude: /node_modules/, }, 

This kept everything clean.

Sign up to request clarification or add additional context in comments.

1 Comment

How you deleted already create js file for every tsx file? Manually?
0

In my case it appears Visual Studio was using it's built in TypeScript compiler to generate the js files.

Since I only want webpack to create js files in my dist folder I was able to disable visual studio's built in behavior by adding this:

 <PropertyGroup> <TypeScriptCompileBlocked>true</TypeScriptCompileBlocked> </PropertyGroup> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.