Code Monkey home page Code Monkey logo

babel-plugin-transform-assets's Introduction

babel-plugin-transform-assets

Transforms importing of asset files at compile time using Babel. This plugin removes the need to run your server code through Webpack module bundler when using loaders such as file-loader, url-loader and building isomorphic universal apps. Aids in creating a cleaner, maintainable build process at the cost of yet another Babel plugin.

CircleCI npm version npm

Example

import file from '../file.txt';

will be transformed to

var file = 'file.txt?9LDjftP';

See the spec for more examples.

Requirements

Babel v6 or higher.

Installation

npm install -D babel-plugin-transform-assets

Usage

Via .babelrc

.babelrc

{
  "plugins": [
      ["transform-assets", {
          "extensions": ["svg"],
          "name": "[name].[ext]?[sha512:hash:base64:7]"
      }]
  ]
}

Via Node API

require('babel-core').transform('code', {
    plugins: [
        ['transform-assets', {
            extensions: ['svg'],
            name: '[name].[ext]?[sha512:hash:base64:7]',
        }]
    ]
});

Contributing

Contributions are very welcome—bug fixes, features, documentation, tests. Just make sure the tests are passing.

Related Projects

babel-plugin-css-modules-transform

babel-plugin-transform-assets's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

babel-plugin-transform-assets's Issues

server rendering file-loader assets

hello,
I'm trying to use this plugin to render file loader assets (png) for a universal app on the serverside to match webpack. It doesn't seem to be working, when i run the server code i get this error, it doesn't seem to be transforming the assets ?

SyntaxError: /Users/user/Documents/webpack/server-render2/src/components/react-webpack.png: Unexpected character '�' (1:0)

1 | �PNG
| ^

component:
<img src={require('./react-webpack.png')}/>

webpack:

    {
      test: /\.(jpg|png|gif|eot|svg|ttf|woff|woff2)(\?.*)?$/,
      include: [path.resolve('src')],
      loader: 'file',
      query: {
        name: 'assets/[name].[ext]',
      },
    },

server:

// ...
const app = express();

require('babel-core').transform('code', {
  plugins: [['transform-assets', {
              extensions: ['png'],
              name: 'assets/[name].[ext]',
            }]]
});

//...
app.get('*', (req, res) => {
  match(
    { routes, location: req.url },
    (err, redirectLocation, renderProps) => {

README doesn't describe if you're able to change the hashing algorithm

The docs don't say which hashing algorithms are available, or explicitly if you can change it - just a cryptic [file].[ext]?[sha512:hash:base64:7].

It'd be great if the docs explained how this worked, and what the various possibilities were (or at least where the options come from). At the moment, I'm just working it out through trial and error

Engine requirement too high?

You've set the engine requirement to 9.x, that seems high considering 8.x is LTS. I don't quite see what this plugin even needs such a high engine version.

Using this plugin prevents webpack from outputting images

I have been trying to solve the Unexpected character issue with SSR. This plugin helps in a way but I feel like there's a conflict with webpack file-loader or there's something I am missing. I am using "file-loader": "^0.11.2" and "babel-plugin-transform-assets": "^0.2.0". This is my loader configuration in my webpack.config.js:

   rules: [
      {
        test: /\.(png|jpg|gif)$/,
        use: {
          loader: 'file-loader',
          options: {
            outputPath: 'images/',
            name: '[name].[ext]'
          }
        }
      },
      {
        enforce: 'pre',
        test: /\.jsx?$/,
        loader: 'eslint-loader',
        exclude: /node_modules/
      },
      {
        test: /\.jsx?$/,
        loader: 'babel-loader'
      }
    ]

Now if I simply run this without the plugin, it creates a folder in my public one where all the images are stored, so everything goes as expected. Unfortunately this throws the mentioned error if I try to do SSR.

I then remove everything that webpack generated (bundle and images) in order to simulate a clean slate, I add this plugin to my .babelrc config file as per docs, I run webpack -p to create the bundle for production BUT no image folder is generated at all this time. The Unexpected character error is now gone but I have no images. These are the plugins I am using:

 "plugins": [
    "react-hot-loader/babel",
    "syntax-dynamic-import",
    "dynamic-import-webpack",
    "transform-class-properties",
    "transform-object-rest-spread"
  ],
  "env": {
    "test": {
      "plugins": ["transform-es2015-modules-commonjs"]
    },
    "production": {
      "plugins": [
        "transform-es2015-modules-commonjs",
        ["transform-assets", {
          "extensions": ["jpg"],
          "publicPath": "images/",
          "name": '[name].[ext]'
        }]
      ]
    }
  }

These steps were pretty simple so I am not sure if I am doing something wrong (most likely) or if this plugin doesn't play well with file-loader with this setup. I even tried outputting the images directly into the public folder thus removing publicPath from the transform-assets config.

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.