ES6 import from root

后端 未结 8 1639
别跟我提以往
别跟我提以往 2020-12-08 18:28

I\'m currently playing around with React Native. I\'m trying to structure my app, however it\'s starting to get messy with imports.

--app/
    -- /components         


        
相关标签:
8条回答
  • 2020-12-08 19:10

    In Webpack 3 the config is slightly diffrent:

    import webpack from 'webpack';
    import {resolve} from 'path';
    

    ...

    module: {
        loaders: [
            {
                test: /\.js$/,
                use: ["babel-loader"]
            },
            {
                test: /\.scss$|\.css$/,
                use: ["style-loader", "css-loader", "sass-loader"]
            }
        ]
    },
    resolve: {
        extensions: [".js"],
        alias: {
            ["~"]: resolve(__dirname, "src")
        }
    },
    
    0 讨论(0)
  • 2020-12-08 19:11

    The react documentation explain how to do that: https://create-react-app.dev/docs/importing-a-component/#absolute-imports

    just add a jsconfig.json in your project root:

    {
       "compilerOptions": {
          "baseUrl": "src"
       },
      "include": ["src"]
    }
    
    0 讨论(0)
提交回复
热议问题