jasononeil / webpack-haxe-loader

Webpack loader for the Haxe programming language.
MIT License
38 stars 11 forks source link

Source path included in required files generated path #77

Closed cambiata closed 3 years ago

cambiata commented 3 years ago

Thank you, guys, for a great lib!

When trying this out in a npx create-react-app context, with the following build.hxml

-js src/index.js
-p src
-L react
-L haxe-loader
-m Index
-D analyzer-optimize
-dce full

and the following Index.hx

@:keep var style = Webpack.require('./index.css');
function main() {
    ReactDOM.render(jsx('<h1>Hello from Haxe</h1>'), document.getElementById('root'));
}

I get a webpack Failed to compile error: Module not found: Can't resolve './src/index.css' in '/Users/jonasnystrom/Documents/utveckling/test-soundslice/src'

Obviously, this is caused by the source path "src" is included in the Webpack.require generated path. Is there a haxe-loader configuration to avoid this that I've missed?

(I've solved it by adding an extra compiler define -D haxe-loader-trim-root, and changed the Webpack.rebaseRelativePath method to take that into account:)

    static function rebaseRelativePath(directory:String, file:String) {
        directory = makeRelativeToCwd(directory);
        directory = '${directory}/${file}'.normalize();
        if (directory.isAbsolute() || directory.startsWith('../'))
            return directory;

        #if (haxe_loader_trim_root)
        // Fix to trim away the source path part of a required file
        // Example: ./src/index.css becomes ./index.css
        directory = directory.split('/').slice(1).join('/');
        #end

        return './$directory';
    }
elsassph commented 3 years ago

The point of Webpack.require is precisely to make requires relative to the file it's in. If you don't need this feature just use js.Lib.require, and they'll be relative to the hxml

cambiata commented 3 years ago

Ah! Thank you, Philippe!