-
-
Notifications
You must be signed in to change notification settings - Fork 431
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multiple TS config/projects, entry files, and shared code #647
Comments
That's not the worst of configs even if it's a bit verbose. Completely on a side note but I'd been thinking about how ts-loader might well be used with service workers (I love a PWA) and this is a nice approach.
See #267 - this is not supported at present although if @tkrotoff has a go then it could happen and that might improve your config. |
Do you want to try using the
I believe ts-loader should support this too |
The "shared" code doesn't have a TS project of its own, but it is implicitly part of the dependency graph of both the "browser" and "service worker" TS projects. However, using the configuration above, webpack/ts-loader will attempt to find a unique TS config file for "shared", leading to errors:
If I remove the rule for "shared", it will fail to compile the TypeScript altogether:
Ideally, when webpack traces an entry file, it will re-use the same TS config for the dependency graph of that entry file. This is what happens when I run I can workaround this by creating a TS config file for "shared", however this means my IDE/ |
Try setting your rules like this:
|
@dbettini That does the trick! Using One improvement could be to remove the need for |
@OliverJAsh just to be sure, did you only add the |
@dbettini I added import * as path from 'path';
import * as webpack from 'webpack';
const ROOT_PATH = path.join(__dirname, '..');
const TARGET_PATH = path.join(ROOT_PATH, './target-webpack');
const SHARED_SOURCE_PATH = path.join(ROOT_PATH, './src/shared');
const BROWSER_SOURCE_PATH = path.join(ROOT_PATH, './src/browser');
const SERVICE_WORKER_SOURCE_PATH = path.join(ROOT_PATH, './src/service-worker');
const config: webpack.Configuration = {
devtool: 'source-map',
entry: {
browser: path.join(BROWSER_SOURCE_PATH, './index.ts'),
'service-worker': path.join(SERVICE_WORKER_SOURCE_PATH, './index.ts'),
},
output: {
path: TARGET_PATH,
filename: '[name].js',
},
resolve: {
extensions: [
// start defaults
'.js',
'.json',
// end defaults
'.ts',
],
},
module: {
rules: [
{
test: /\.ts$/,
include: [BROWSER_SOURCE_PATH, SHARED_SOURCE_PATH],
loader: 'ts-loader',
options: {
instance: 'browser',
configFile: path.join(BROWSER_SOURCE_PATH, 'tsconfig.json'),
},
},
{
test: /\.ts$/,
include: [SERVICE_WORKER_SOURCE_PATH, SHARED_SOURCE_PATH],
loader: 'ts-loader',
options: {
instance: 'service-worker',
configFile: path.join(SERVICE_WORKER_SOURCE_PATH, 'tsconfig.json'),
},
},
],
},
};
export default config; |
@OliverJAsh yep, as expected. The reason it's needed is because webpack never had |
@dbettini Just spotted a problem: for some reason, webpack says there is an error but provides no details why (using the config above):
Note the "[1 error]" beside /src/browser/index.ts. If I enable only one entry point and the corresponding rule entry, neither "browser" or "service-worker" will error. But when I run both using the above config, webpack reports an error for the file "/src/browser/index.ts" but with no further details. @johnnyreilly Do you have any ideas here? Is there a way to log more info about the error? |
@OliverJAsh like I said, I use at-loader, I always get all errors logged by default when using it. If ts-loader doesn't log errors by default, maybe you can try changing logLevel |
It should log all errors - I'd advise forking ts-loader and adding in some extra logging to diagnose. Do you have a minimal repro repo you could share? |
@johnnyreilly Here is a minimal repo: https://github.com/OliverJAsh/webpack-ts-loader-shared |
For some reason, specifying In any case, it would be good to get the error message printed. |
One problem with the suggested config above is the "shared" code will only be type checked once, using whichever rule is found first. Both "browser" and "service-worker" |
Edit: upon further testing, this is wrong. Ignore this comment.
Example: https://github.com/OliverJAsh/webpack-ts-loader-shared/tree/awesome-typescript-loader Here is an example using at-loader where "shared" is type checked for both |
Sorry, does it work with at or not? The module thing might be because (at present) ts-loader has different module defaults to tsc. We plan to rectify this with V3 (being worked on now - see #637) |
At this time I can't say it does work any better with at-loader—sorry to cause confusion! |
No worries 😄 |
Closing as a solution was found (with trade offs). Thanks again. |
Could you share the solution for others please? Always helps! |
~3 years later, sorry! My solution was just to use one (We still want to keep separate projects, but project references still have issues with VS Code.) |
Haha! Props for replying after 3 years 😄 |
Thought I'd keep this one alive just a bit longer; I'm running ts-loader alongside serverless framework, specifically the Any thoughts on what I can do to prevent that from happening? |
I have 3 dirs:
service-worker
andbrowser
have their owntsconfig.json
. This is necessary because I need to specify a differentlib
compiler option to files in these projects, as they are ran in different environments (service workers and DOM/window/browser, respectively).I initially thought I could just specify multiple entry points and ts-loader would use the correct
tsconfig.json
relative to each entry point. Any files it discovers in the dependency graph of the entry files would use the same configuration as the respective entry file.Unfortunately this won't work—ts-loader will re-use the first TypeScript instance (and therefore config) it creates for both entry points.
I then discovered ts-loader's
instance
option, which forces ts-loader to create different instances. However, the "shared" code is difficult to place: it can be compiled with either TS configs!In any case, below is what I have ended up with. Have you got any ideas how I could clean this up? Or how this might be made easier in the future?
Ideally, this would be as simple as
tsc
makes it. Feed it a configuration file, and it will discover the entry file(s) throughfiles
and compile all files in the dependency graph with the same configuration.The text was updated successfully, but these errors were encountered: