Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack: use data from parent directory without copying

I have a project folder structured like this:

project-name/   
  data/
   data.csv
  dist/
   index.js
  src/
   index.js

And want a remote directory like this:

project-name/   
  data/
   data.csv
  dist/
   index.js
    > `doSomething("../data/data.csv")`

How do I make this work in both webpack-dev-server and in the production path? If I use copywebpack plugin, then the data goes inside the dist/, which I don't want. But if I use a relative directory without copying the data, then the build fails.

like image 260
Kyouma Avatar asked Aug 27 '21 14:08

Kyouma


People also ask

How do I copy modified files in a Webpack build?

Copies files, regardless of modification when using watch or webpack-dev-server. All files are copied on first build, regardless of this option. ℹ️ By default, we only copy modified files during a webpack --watch or webpack-dev-server build. Setting this option to true will copy all files.

How to write files to the output directory during development in Webpack?

ℹ️ If you want webpack-dev-server to write files to the output directory during development, you can force it with the writeToDisk option or the write-file-webpack-plugin. ℹ️ You can get the original source filename from Asset Objects. Glob or path from where we copy files. Output path.

What is the default path to cache in Webpack?

Default path to cache directory: node_modules/.cache/copy-webpack-plugin. Enables/Disable transform caching. Enables transform caching and setup cache directory and invalidation keys. You can setup invalidation keys using a function. Allows you to modify the contents of multiple files and save the result to one file.

How do I copy a file to the parent folder?

This covers hidden files, as well. Show activity on this post. cp -R file_name $ (pwd)/../ This command will copy your file, and it will print the current directory and go to the parent folder. If you want to access the grandparent folder, you just need to add one more "../"


Video Answer


3 Answers

Actually, you should use Webpack csv-loader in your Webpack configuration file like below:

module: {
  rules: [
    {
      test: /\.csv$/,
      loader: 'csv-loader',
      options: {
      dynamicTyping: true,
        header: true,
        skipEmptyLines: true,
      },
    },
  ],
},

And then use it inside your code like below:

import csvPath from './project-name/data/foo.csv'
like image 73
AmerllicA Avatar answered Oct 20 '22 20:10

AmerllicA


Can you use an alias?

webpack.config.js:

  const path = require("path");

  module.exports = {
  ...

    resolve: {
      alias: {
        Data: path.resolve(__dirname, "./project-name/data/"),
      },
    },
  }

src/index.js:

  import data from "Data/data.csv"
like image 29
meghaman Avatar answered Oct 20 '22 18:10

meghaman


Have you tried excluding it in the test? Something like this:

   {
    entry: "./src/index.js",
    module: {
      rules: [
        {
          test: /\.js$/,
          use: ["babel-loader or whatever loader you use"],
        },
        {
          test: /\.csv$/,
          exclude: ["./data/"],
        },
      ],
    },
    output: {
      filename: "index.js",
      path: path.resolve(__dirname, "dist"),
    },
   },

There is a similar question on how to not bundle files in webpack here

like image 1
mpmcintyre Avatar answered Oct 20 '22 19:10

mpmcintyre