-
Notifications
You must be signed in to change notification settings - Fork 87
Rollup with Typescript through Babel #255
Comments
You also need to pass |
Hmm rollup.config.js: import babel from 'rollup-plugin-babel';
import resolve from 'rollup-plugin-node-resolve';
export default {
input: 'src/index.ts',
output: {
file: 'lib/index.js',
format: 'cjs',
},
plugins: [
resolve(),
babel({
exclude: 'node_modules/**',
extensions: ['.js', '.jsx', '.ts', '.tsx'],
}),
],
}; .babelrc.js: module.exports = {
presets: [
['@babel/preset-env', {modules: false, targets: {node: 'current'}}],
['@babel/preset-typescript'],
],
}; Error:
|
Please share a repository with the issue reproduced, without that I won't be able to diagnose the issue. |
Faced the same issue. When running babel isolated - it successfully transpiles TypeScript to JS files.
Seems like rollup tries to load original sources and validate them, before they will be transpiled from TS to JS by babel. I'm not sure is this a plugin issue or rollup itself... babel: 7.2.2 Here is the repo.
|
Working config: import path from "path";
import babel from "rollup-plugin-babel";
import resolve from "rollup-plugin-node-resolve";
const rootDir = path.resolve(__dirname);
const dstDir = path.join(rootDir, "dist");
const extensions = [".ts", ".js"]
export default {
input: {
entity1: "./src/entity1.ts",
entity2: "./src/entity2.ts"
},
output: {
dir: dstDir,
format: "cjs",
name: "Xrm.Test"
},
plugins: [
resolve({
jsnext: true,
extensions,
}),
babel({
extensions
})
]
}; |
Thanks for looking into it and for quick response! Unfortunately, although your suggestions fixed the issue, 'cjs' or other module format except browser one is not acceptable for my purposes =( Basically, I need a single output JS file for each TS input, without external dependencies (SystemJS, require, whatever else) to put it into the <script> tag, with JS that will work in IE. I'm not intrested in code splitting, on the contrary, my primary goal is a bulk bundling, ideally with good tree-shaking. So I decided trying rollup, because it is known as better tool for library bundling, producing less wrapper code than webpack. Probably, I need to use some other approach: generate config with a single input for each input file and run rollup with multiple configs. Or bundle first and run babel transpilaition after... Anyway, thanks for your help and for another great opensource tool! |
Well, if you want to have multiple script tags you can't go with no external dependencies (if u want to support IE). In any system you have to keep register of your dependencies which basically means that you have to have external dep for that - either SystemJS, or amd loader. |
Unfortunately, in my particular case all dependencies have to be managed manually, as in ancient times of Web 1.0 =( When you need to load few files on very few pages - it is possible to manage. But when you have over 100 pages and 5-10 scripts to load on each page - this turns into real dependency hell. So my idea was to refactor scripts to use TS and ES6 modules for dependency management, and generate one single script (supported by IE) for each page by babel and some bundler and register it. I don't care about traffic, size, caching and all these optimizations at the moment, since it is intranet application. All I want to achieve is to bundle and register the only file for each page, whithout creating a single monster-bundle. Sorry for this longread, just want to explain why using any module loading system isn't possible in my case. |
If you don't care about caching etc I would probably go with a single bundle per site (I don't mean a single monolithic bundle). Which actually sounds like something you have described that you want to do - if I got your intention right. For that you just have to create (can be programatically) a rollup config per site, and not a single config with multiple entries (what you have tried to do). It would look smth like this: import path from "path";
import babel from "rollup-plugin-babel";
import resolve from "rollup-plugin-node-resolve";
const rootDir = path.resolve(__dirname);
const dstDir = path.join(rootDir, "dist");
const extensions = [".ts", ".js"]
export default [
"./src/entity1.ts",
"./src/entity2.ts",
].map(input => ({
input,
output: {
file: '[name].js',
format: "iife",
name: "Xrm.Test"
},
plugins: [
resolve({
jsnext: true,
extensions,
}),
babel({
extensions
})
]
})); |
- [npm start] remove typescript declaration file - [npm start] use babel with typescript preset - [npm build] use typescript compiler references: - rollup/rollup-plugin-babel#255 - rollup/rollup-plugin-typescript#129 - rollup/rollup-plugin-babel#271 - rollup/rollup-plugin-babel#279 - microsoft/TypeScript-Babel-Starter#29
For anyone using React with Typescript and running into this issue, you also need to add .babelrc
rollup.config
|
I keep getting "Unexpected token" errors when trying to compile Typescript React components with Rollup and rollup-plugin-babel:
My rollup.config.js:
I also have a .babelrc file that uses @babel/preset-typescript. It works fine when using @babel/cli.
Versions:
Am I doing something wrong here or should this work?
The text was updated successfully, but these errors were encountered: