timocov / ts-transformer-properties-rename

TypeScript custom transformer to rename properties
MIT License
70 stars 3 forks source link

Error: Cannot find source file #20

Closed hexxone closed 3 years ago

hexxone commented 3 years ago

Bug report

Hello there, I'm tring to set up TypeScript function minification using this lib.

However for some reason it just won't "find" the entry source files I give to it....

My setup is using webpack. See relevant configs below.

I have already tried using the following methods in most combinations:

However, the error message always stays the same: Error: Cannot find source file.

The weird part is, that the .getSourceFile(...) method definetley exists on program when I do a console.log. It just does not seem to return or actually find the file.

Am I missing something about the config perhaps?

Thanks in advance! I'm really at a loss with this...

tsconfig.json

{
    "compilerOptions": {
        "outDir": "./dist/tsc",
        "target": "es2020",
        "module": "commonjs", // commonjs | esnext
        "moduleResolution": "node",
        "esModuleInterop": true, // TODO
        "emitDecoratorMetadata": false,
        "sourceMap": false,
        "removeComments": true,
        "baseUrl": ".",
        "paths": {
            "three": [
                "node_modules/three/src/Three"
            ]
        },
        "lib": [
            "dom",
            "ES5"
        ],
        "plugins": [
            { 
                "transform": "ts-transformer-properties-rename",
                "entrySourceFiles": ["./src/AudiOrbi.ts"]
            }
        ]
    },
    "include": [
        "./src/**/*.ts"
    ],
    "exclude": [
        "**/*.asc",
        "**/better-docs/**"
    ]
}

webpack.config.js


const path = require('path');

const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');

const TerserPlugin = require('terser-webpack-plugin');

const ENTRY_FILE = path.resolve(__dirname, 'src', 'AudiOrbi.ts');
const BUILD_PATH = path.resolve(__dirname, 'dist');

module.exports = (env) => {
    const prod = env.production || false;

    return {
        mode: 'production',
        entry: {
            audiorbits: ENTRY_FILE,
        },
        output: {
            chunkFilename: '[id].bundle.js',
            path: BUILD_PATH,
        },
        resolve: {
            extensions: ['.ts', '.js'],
        },
        module: {
            rules: [
                // TypeScript loader
                {
                    test: /\.tsx?$/,
                    loader: require.resolve('ts-loader'),
                    options: {
                        compiler: 'ttypescript',
                        configFile: 'tsconfig.json',
                        transpileOnly: true,
                    },
                },
                ......
            ],
        },
        plugins: [
            // The TS Syntax checking
            new ForkTsCheckerWebpackPlugin({}),
            .......
        ],
        // remove dead code in production
        optimization: prod ? {
            minimizer: [
                new TerserPlugin({
                    // https://github.com/webpack-contrib/terser-webpack-plugin#terseroptions
                    terserOptions: {
                        parse: {},
                        compress: {
                            unsafe: true,
                            pure_funcs: ['console.warn'], // ~40kb of three.js messages... errors will stil come through.
                        },
                        mangle: {
                            properties: {
                                regex: /^_(private|internal)_/, // the same prefixes like for custom transformer
                            },
                        },
                        module: true,
                        sourceMap: false,
                    },
                }),
            ],
        }: {},
    };
};

Error output

// place your code here
./src/AudiOrbi.ts 39 bytes [built] [code generated] [1 error]

ERROR in ./src/AudiOrbi.ts
Module build failed (from ./node_modules/ts-loader/index.js):
Error: Cannot find source file ./src/AudiOrbi.ts
    at ExportsSymbolTree.computeTreeForExports (D:\MyProject\node_modules\ts-transformer-properties-rename\dist\exports-symbol-tree.js:27:23)
    at new ExportsSymbolTree (D:\MyProject\node_modules\ts-transformer-properties-rename\dist\exports-symbol-tree.js:10:14)
    at createTransformerFactory (D:\MyProject\node_modules\ts-transformer-properties-rename\dist\transformer.js:38:29)
    at propertiesRenameTransformer (D:\MyProject\node_modules\ts-transformer-properties-rename\dist\transformer.js:32:12)
    at createTransformerFromPattern (D:\MyProject\node_modules\ttypescript\lib\PluginCreator.js:40:19)
    at PluginCreator.createTransformers (D:\MyProject\node_modules\ttypescript\lib\PluginCreator.js:123:31)
    at Object.newEmit [as emit] (D:\MyProject\node_modules\ttypescript\lib\patchCreateProgram.js:68:52)
    at Object.transpileModule (D:\MyProject\node_modules\typescript\lib\typescript.js:131713:17)
    at getTranspilationEmit (D:\MyProject\node_modules\ts-loader\dist\index.js:388:75)
    at successLoader (D:\MyProject\node_modules\ts-loader\dist\index.js:37:11)
timocov commented 3 years ago

transpileOnly: true,

Did you tried to disable it? The transformer requires type information so it won't work in "transpile only" mode.

hexxone commented 3 years ago

transpileOnly: true,

Did you tried to disable it? The transformer requires type information so it won't work in "transpile only" mode.

Damn, I feel stupid now. Of course this makes sense... Thanks for the tip!

I just tested this, but it still did not find the entry using ts-loader for some reason.

So I tried again with awesome-typescript-loader instead, and now it works like a charme! Maybe making this one the preffered loader would be a good idea? ^^ 🤔

Anyways, tyvm 😄 nice project you have going here!

timocov commented 3 years ago

Maybe making this one the preffered loader would be a good idea? ^^ 🤔

I don't think that the loader matters here. Is it possible to create a repro for this case? If it's possible I think I can debug it to figure out what's wrong.

hexxone commented 3 years ago

I don't think that the loader matters here. Is it possible to create a repro for this case? If it's possible I think I can debug it to figure out what's wrong.

Sure, I have pushed a minimal-example here: https://github.com/hexxone/ts_renamer_dbg

Thanks for taking the time if you do :) But dont stress it, since awesome-typescript-loader works anyways ^^

timocov commented 3 years ago

Hi @hexxone,

sorry for late reply.

I finally dig into the issue and found why it is going so.

For some reason, ts-loader uses loader.context as a "current directory" for the compiler, which is ./src folder (I think this path is generated by webpack itself), and it leads for the issue that when the transformer is trying to get a source file by path ./src/AudiOrbi.ts (provided by you from the options) it fails with the error above.

It seems that there is no way to override loader.context, so you could try to create an issue in ts-loader repo, or as a workaround you can fix a path provided to the transformer from ./src/AudiOrbi.ts to ./AudiOrbi.ts (since src is already in the path). Alternatively, you can use another loaders without such issue 🙂