0

My issue is that I have a webpack project where I include typescript classes / types / css files and resources outside the "src dir" of the project. So basically only files within the "src" folder should be included, no files outside that directory or even higher in file hierarchy.

enter image description here

These files outside the project dir should not be included in the built, since they are already existing on runtime. As far as I understood this can achieve by using the resolve function.

include: path.resolve(__dirname, 'src')

That only results in the error "ReferenceError: path is not defined". I don't know where I would define it actually.

I think this is a rather simple question, but I seem not to find the solution to it. I am using webpack 4.44.1 and webpack-cli 3.3.12 .

const {CleanWebpackPlugin} = require('clean-webpack-plugin');
const path = require("path")
module.exports = {
    optimization: {
        minimize: false
    },
    entry: {
        'code': './src/code.ts'
    },
    plugins: [
        new CleanWebpackPlugin({
            cleanAfterEveryBuildPatterns: ['public/build']
        }),
    ],
    output: {
        path: __dirname + '/public',
        //filename: 'build/[name].[contenthash].js',
        filename: 'build/[name].js',
    },
    module: {
        rules: [
            {
                test: /\.ts?$/, use: 'ts-loader', exclude: /node_modules/
            },
            {
                test: /\.css$/i,
                use: ['style-loader', 'css-loader'],
            },
            {
                test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2|json|xml|ico|cur|ani)$/,
                use: ['file-loader?name=[path][name].[ext]']
            },
        ]
    },
    resolve: {
        extensions: [
            '.ts',
            '.js',
            '.json',
        ],
    },
};
4
  • 1
    It's require.resolve or const path = require('path') - there's no global called path in NodeJS Commented Jan 4, 2021 at 23:10
  • this i tried as well, the error will result in Module parse failed: Unexpected token (26:22) You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See webpack.js.org/concepts#loaders Commented Jan 4, 2021 at 23:16
  • however the idea was to make the loader ignore the types and treat them as any instead (same way as vanillaJS would do) Commented Jan 4, 2021 at 23:16
  • What is the path of the module that fails / what is the "type" of the file that fails to parse? Commented Jan 5, 2021 at 2:09

1 Answer 1

2

You need to import path library before you can use it. Please add this to top of webpack.config.js e.g const path = require("path") or if you are using ES6 you can use import path from "path";

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

2 Comments

thx for the comment, as mentioned above that is unfortunately not working (I did forgot to update the code accordingly - done now)
@marius what command do you use to run webpack build???

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.