简体   繁体   English

WebPack 1.x - 使用来自外部 webpack 根目录的文件

[英]WebPack 1.x - Using a file from outside webpack root

I have this project architecture:我有这个项目架构:

Root
    Project1
        package.json
        webpack.config
    Project2
        package.json
        webpack.config
    Common
        file1.js

I want to be able to use file1.js in side each project.我希望能够在每个项目中使用file1.js

I am using old webpack 1.13.1 .我正在使用旧的webpack 1.13.1

I have tried using an alias:我曾尝试使用别名:

resolve: {
    alias: {
      commons: path.resolve(__dirname, ".../../Common/")
    }
}

I have tried both ways:我尝试了两种方式:

import ProcessTree from '../../Common/file1';
import ProcessTree from 'commons/file1';

Any ideas?有任何想法吗?

this is my webpack config:这是我的 webpack 配置:

import webpack from 'webpack';
import path from 'path';

let commonPath= path.resolve(__dirname, '..', '..', 'Common');

export default {
  devtool: 'eval-source-map',
  entry: [
    './src/index'
  ],
  target: 'web',
  output: {
    path: __dirname + '/dist', // Note: Physical files are only output by the production build task `npm run build`.
    publicPath: '/',
    filename: 'bundle.js'
  },
  resolve: {
    alias: {
      commons: commonPath
    }
  },
  devServer: {
    contentBase: './src'
  },
  module: {
    loaders: [
      { 
        test: /\.js$/, 
        include: [
          path.join(__dirname, 'src'), 
          commonPath
        ],
        loaders: ['babel'] 
      },
      { test: /(\.css)$/, loaders: ['style', 'css?sourceMap'] },
      { test: /(\.scss)$/, loaders: ['style', 'css?sourceMap', 'sass?sourceMap'] },
      { test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: 'file' },
      { test: /\.(woff|woff2)$/, loader: 'url?prefix=font/&limit=5000' },
      { test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&mimetype=application/octet-stream' },
      { test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?mimetype=image/svg+xml' }
    ]
  },
  node: {
    fs: "empty",
    child_process: 'empty',
  }
};

Make sure the path is included in whichever module loader rule you want to process files within the Common folder with.确保该路径包含在您要用于处理Common文件夹中文件的任何模块加载器规则中。

For example:例如:

module: {
  loaders: [
    {
      test: /\.js?$/,
      include: [
        path.resolve(__dirname),
        path.resolve(__dirname, "../../Common")
      ],
      exclude: /node_modules/,
      loader: "babel-loader"
    }
  ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM