Code Monkey home page Code Monkey logo

flat-loader's Introduction

flat-loader

Webpack 3 includes the ModuleConcatenationPlugin, which results in similar bundles as this loader, but is much better in dealing with split bundles and more complex setups. If possible, it's probably best to use that!

https://github.com/webpack/webpack/releases/tag/v3.0.0

A fancier Rollup loader for Webpack. This loader uses Rollup for bundling only.

Install

npm install --save-dev flat-loader

Configuration

Note: If you are using Hot Reloading in development, make sure to only apply flat-loader in your production build. flat-loader presents all modules that are bundled by Rollup to Webpack as a single module, so hot reloading any of the bundled modules will force a reload of the entire bundle.

If I've done things right, using this loader should be very simple. At the top of your module.rules block in your Webpack config, add this:

{ include: [ /* Full paths to entry files */ ], use: 'flat-loader' }

It's probably clearer in a larger example:

module.exports = {
  context: path.join(__dirname, 'src'),
  entry: './app.js',
  module: {
    rules: [
      {
        include: [
          // List the full paths to your application's entry points.
          // In this example we only have src/app.js.
          path.join(__dirname, 'src', 'app.js')
        ],
        // Important: ONLY use `flat-loader` here!
        use: 'flat-loader'
      },
      // No need to touch your other loader configs.
      ...
    ]
  }
}

You don't need to do any Rollup configuration.

You can see a small example project that uses flat-loader in the example/ folder.

How It Works

Your app entry points are passed into Rollup. A custom Rollup plugin is added that uses Webpack to resolve and load imported modules, so your Webpack resolver configuration and loaders all apply. ES Modules are bundled by Rollup, and all other types (UMD/CommonJS/etc) are treated as externals. These will be bundled by Webpack.

License

MIT

flat-loader's People

Contributors

goto-bus-stop avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

flat-loader's Issues

Make it work well with chunks

eg. given this:

// app.js
import './something'

import('./other-chunk').then(() => {})
// other-chunk.js
import './whatever'

both app.js and other-chunk.js could be rollup-ed.

"Module not found" on Windows when importing not from context dir

How to reproduce:

  1. PC or VM with Windows
  2. unzip example.zip
  3. npm install
  4. npm start

ERROR in ./src/app.js
Module not found: Error: Can't resolve './stylesstyle.css' in 'c:\work\example\src'
@ ./src/app.js 2:0-39
@ multi (webpack)-dev-server/client?http://localhost:8080 ./app.js

What happens:
Substitution of external module at https://github.com/goto-bus-stop/flat-loader/blob/master/webpack-plugin.js#L86 uses relative path which, in case of Windows, results into import "./style\styles.css" emitted into output. Backslash(es) then get interpreted as escape characters, not as path separators.

Proposed fix:
Backslashes should be replaced with '/'.
In the webpack-plugin.js, replace

return ${EXTERNAL_IDENTIFIER}./${relative.replace(/\.js$/, '')}

with

return ${EXTERNAL_IDENTIFIER}./${relative.replace(/\.js$/, '').replace(/\\/g, '/')}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.