Personal Website - jakemurzy.com
Latest Blog Posts
Follow me on Twitter @jmurzy
Transforms asset imports at compile time using Babel 📦
License: MIT License
Personal Website - jakemurzy.com
Latest Blog Posts
Follow me on Twitter @jmurzy
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.
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) => {
Does this plugin support .js files as a valid extension?
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.
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
I am able to get it setup and running but say I have an import that is transformed. How can I use that in my react components on the server side?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.