shama / webpack-stream

:tropical_drink: Run webpack through a stream interface
MIT License
1.39k stars 122 forks source link

Configuring without src #215

Open dgateles opened 5 years ago

dgateles commented 5 years ago

I dont have this parameter return gulp.src('src/entry.js') because all src is on webpack.config file:

// Utils
const path = require('path');
const Glob = require('glob-all');
const read = require('fs-readdir-recursive');
const EntryPlus = require('webpack-entry-plus');

// Plugins
const HTMLWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const HandlebarsPlugin = require('handlebars-webpack-plugin');
const FixStyleOnlyEntriesPlugin = require('webpack-fix-style-only-entries');
const ExtraWatchWebpackPlugin = require('extra-watch-webpack-plugin');

// Função que retorna os plugins HTML gerados dinâmicamente
function generateHtmlPlugins(templateDir) {
  const templateFiles = read(path.resolve(__dirname, templateDir)).filter(item => {
    const parts = item.split('.');
    const extension = parts[1];
    if (extension != 'hbs') {
      return false;
    }
    return true;
  });

  return templateFiles.map(item => {
    const parts = item.split('.');
    const name = parts[0];
    const extension = parts[1];
    return new HTMLWebpackPlugin({
      filename: path.join(__dirname, 'html', `${name}.hbs`),
      template: path.resolve(__dirname, `${templateDir}/${name}.${extension}`),
      inject: false
    });
  });
}

// Mapeamento de diretórios
const templatesDir = path.join(process.cwd(), 'src', 'global', 'templates');
const partialsDir = path.join(process.cwd(), 'src', 'pages');
const pagesDir = path.join(process.cwd(), 'src', 'pages', '**/[^_]*.hbs');
const partialsRecursiveDir = Glob.sync([templatesDir + '/**/*.hbs', partialsDir + '/**/_*.hbs']);

// Pontos de entrada para o webpack
const entryFiles = [{
    entryFiles: Glob.sync('./src/pages/**/*.js'),
    outputName(item) {
      return item.replace('./src/pages/', '');
    },
  },
  {
    entryFiles: Glob.sync('./src/pages/**/*.scss'),
    outputName(item) {
      return item.replace('./src/pages/', '').replace('.scss', '');
    },
  },
  {
    entryFiles: Glob.sync('./build/camara-custom.js'),
    outputName(item) {
      return item.replace('./build/', 'global/').replace('camara-custom', 'camara-bundle');
    },
  },
  {
    entryFiles: Glob.sync('./build/camara-custom.scss'),
    outputName(item) {
      return item.replace('./build/', 'global/').replace('.scss', '');
    },
  },
  {
    entryFiles: Glob.sync('./build/vendor-libraries.js'),
    outputName(item) {
      return item.replace('./build/', 'global/').replace('vendor-libraries', 'vendor-bundle');
    },
  },
  {
    entryFiles: Glob.sync('./build/vendor-libraries.scss'),
    outputName(item) {
      return item.replace('./build/', 'global/').replace('vendor-libraries', 'vendor-bundle').replace('.scss', '');
    },
  },
];

// Plugins HTML gerados dinâmicamente
let plugins = generateHtmlPlugins(path.join(process.cwd(), 'src', 'global', 'templates')).concat(generateHtmlPlugins(path.join(process.cwd(), 'src', 'pages')));
plugins = plugins.concat([
  //  Gera bundle CSS
  new MiniCssExtractPlugin({
    filename: './[name].css',
  }),
  // Compila arquivos .hbs
  new HandlebarsPlugin({
    htmlWebpackPlugin: {
      enabled: true,
      prefix: 'html'
    },
    entry: pagesDir,
    //output: path.join(process.cwd(), 'www', '[name].html'),
    output: path.join(process.cwd(), 'www', '[path].html'),
    partials: partialsRecursiveDir,
    helpers: require(path.join(process.cwd(), 'src', 'global', 'node', 'helpers-handlebars.js')),
  }),
  // Copia arquivos estáticos
  new CopyWebpackPlugin([{
    from: './src/public/assets',
    to: './assets'
  }, {
    from: './src/vendor',
    to: './vendor'
  }, {
    from: './src/public',
    to: './'
  }, {
    from: './node_modules/@fortawesome/fontawesome-free/webfonts',
    to: './assets/fonts'
  }]),
  new FixStyleOnlyEntriesPlugin(),
  // Extende o watch do webpack
  new ExtraWatchWebpackPlugin({
    dirs: [path.resolve('./src')],
  }),
  // Limpa a pasta dist a cada distribuição
  new CleanWebpackPlugin({
    dry: true,
    dangerouslyAllowCleanPatternsOutsideProject: true
  })
]);

let config = {
  devtool: 'source-map',
  mode: 'development',
  entry: EntryPlus(entryFiles),
  watchOptions: {},
  output: {
    path: path.resolve(__dirname, 'www'),
    filename: './[name]'
  },
  context: path.resolve(__dirname),
  plugins: plugins,
  module: {
    rules: [{
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader'
        }
      },
      {
        test: /\.(scss|css)$/,
        use: [{
            loader: 'style-loader',
            options: {
              sourceMap: true
            }
          },
          {
            loader: MiniCssExtractPlugin.loader,
            options: {
              sourceMap: true
            }
          },
          {
            loader: 'css-loader',
            options: {
              sourceMap: true
            }
          },
          {
            loader: 'resolve-url-loader',
            options: {
              sourceMap: true
            }
          },
          {
            loader: 'postcss-loader',
            options: {
              sourceMap: true
            }
          },
          {
            loader: 'sass-loader',
            options: {
              sourceMap: true,
              sasssourceMap: true,
              includePaths: [path.resolve(__dirname, 'node_modules'), path.resolve(__dirname, 'src/global/sass')],
            }
          },
          {
            loader: 'sass-resources-loader',
            options: {
              resources: [
                path.join(__dirname, 'src', 'global', 'sass', 'includes', '_custom_bootstrap', '_variables.scss'),
                path.join(__dirname, 'src', 'global', 'sass', 'includes', 'settings', '_colors.scss'),
                path.join(__dirname, 'src', 'global', 'sass', 'includes', 'settings', '_typography.scss'),
                path.join(__dirname, 'src', 'global', 'sass', 'includes', 'tools', '_mixins.scss'),
                path.join(__dirname, 'node_modules', 'bootstrap', 'scss', '_functions.scss'),
                path.join(__dirname, 'node_modules', 'bootstrap', 'scss', '_variables.scss'),
                path.join(__dirname, 'node_modules', 'bootstrap', 'scss', 'mixins', '*.scss')
              ]
            }
          }
        ]
      },
      {
        test: /\.modernizrrc.js$/,
        use: ['modernizr-loader']
      },
      {
        test: /\.modernizrrc(\.json)?$/,
        use: ['modernizr-loader', 'json-loader']
      },
      {
        test: /\.(jpe?g|png|gif|svg)$/i,
        loader: 'url-loader'
      }
    ]
  },
  resolve: {
    alias: {
      modernizr$: path.resolve(__dirname, '.modernizrrc')
    }
  }
};

module.exports = {
  config
}

How to start without that? just return directly webpackstream, like that: return webpackst(configuration, webpack) ?

Thanks!

robchristian commented 5 years ago

@dgateles did you find a solution?

dgateles commented 5 years ago

@robchristian I left it