Giter VIP home page Giter VIP logo

rollup-plugin-string's Introduction

rollup-plugin-string Build Status

Converts text files to modules:

import tpl from "./tpl.html";
console.log(`Template for render: ${tpl}`);

Installation

npm i rollup-plugin-string -D

Usage

import { rollup } from "rollup";
import { string } from "rollup-plugin-string";

rollup({
  entry: "main.js",
  plugins: [
    string({
      // Required to be specified
      include: "**/*.html",

      // Undefined by default
      exclude: ["**/index.html"]
    })
  ]
});

License

MIT ยฉ Bogdan Chadkin

rollup-plugin-string's People

Contributors

csvn avatar trysound avatar vedam avatar

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  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  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

rollup-plugin-string's Issues

Got error when using with rtp2

Error:

Hello/ Trying to import *.sql files (i.e import query from './query.sql'). It works fine without rtp2 plugin. But with rtp2 I get error:

./src/index.ts โ†’ ./index.js...
[!] Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
../../Modules/crmish/sql/add_client.sql (1:7)
1: INSERT INTO clients(phone, name, born_date, city)
^
2: VALUES ($1, $2, $3, $4);
Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)

// rollup.config.js

import typescript from 'rollup-plugin-typescript2';
import nodeResolve from 'rollup-plugin-node-resolve';
import { string } from 'rollup-plugin-string';

export default {
  input: './src/index.ts',

  output: {
    file: './index.js',
    format: 'cjs',
  },

  plugins: [
    nodeResolve({
      sourceMap: false,
      // extensions: ['.js', '.jsx', '.ts', '.tsx'],
    }),
    string({
      include: '**/*.sql',
    }),
    typescript({
      sourceMap: false,
    }),
  ],
}

Also import files using string:

This can allow us to import both the js file and its source:

import code from './file.js';
import source from 'string:./file.js'; //imports the source

Doesn't work with rollup-plugin-typescript2

Not sure if this is the right place to log it, but I tried using this plugin in a typescript project and couldn't get it to work.

Here's my complete rollup.config.js:

import typescript from 'rollup-plugin-typescript2';
import pkg from './package.json';
import replace from 'rollup-plugin-replace';
import postcss from 'rollup-plugin-postcss';
import string from 'rollup-plugin-string';

export default [
	{
        input: './src/lib/Lib.ts',
        external: ['gl-matrix'],
        output: [
            { file: pkg.module, format: 'es', sourcemap: true },
            { file: pkg.main, format: 'cjs', sourcemap: true }
        ],
        plugins: [
            string({
                // not working....
                include: '**/*.glsl',
                //also tried: include: *.glsl'
            }),
            postcss({
                extensions: [ '.css' ],
            }),
            replace({
                'process.env.NODE_ENV': JSON.stringify( process.env['NODE_ENV'] )
            }),
            typescript({
                useTsconfigDeclarationDir: true
            })
        ]
	}
];

Missing Example

Hi TrySound, I would really like to integrate your plugin into my rollup configuration file but I can't figure out how to do that. I have to admit that I am not a rollup expert and I could not find one application of this plugin via google. Your example in your readme file shows invoking the rollup() function what I have never done before and what gives me another error.
I really would appreciate an example or even a link to a good source which shows how to apply this plugin or at least the rollup function. Thank you very much!

Not use able with node esm

We update the plugin structure so that all runs inside node esm also so i will PR to you now
CJS modules have no named exports

 node rollup.build.mjs
{ string: [Function: string] }
file:///home/frank/Projekte/rollup/rollup.build.mjs:105
        string({ include: '**/*.md' }),
        ^

TypeError: string is not a function

Add option to encode content

It would be nice if there was an option to, for example, base64 encode the file contents, or even make a data: URI out of them. Similar to what webpacks url-loader is doing.

If that is out of scope for this plugin, I will just create a separate plugin for my usecase. :-)

How to import text from inside external modules?

I have a package crmish in node_modules with such lines:

// db.js
import ADD_CLIENT from '../sql/add_client.sql';

I import db.js file into another package. And rollup-plugin-string can't transform *.sql files to strings. How to fix it?

Error:

[!] Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
../../node_modules/crmish/sql/add_client.sql (1:7)
1: INSERT INTO clients(phone, name, born_date, city)
^
2: VALUES ($1, $2, $3, $4);
Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)

Rollup config:

import nodeResolve from 'rollup-plugin-node-resolve';
import { string } from 'rollup-plugin-string';

export default {
  input: './src/index.ts',

  output: {
    file: './index.js',
    format: 'cjs',
  },

  plugins: [

    string({
      include: '**/*.sql',
    }),
  
    nodeResolve({
      sourceMap: false,
    }),
  ],
}

css files

Doesn't seem to work with css files...

string({
      include: '**/*.css',
    })
import styles from './some.css'

Error: 'default' is not exported...

Is this expected behavior? Should CSS files be supported?

typo in usage

small typo in usage

in usage you're passing an array to string():

plugins: [
  string([
    extensions: ['.html']
  ])
]

in your tests it's an object:

plugins: [
  string({
    extensions: ['.html', '.css']
  })
]

Import file as text by

Hey, is it possible to import file as a text with importing syntax instead of configuration?
Something similar to webpack:

import txt from 'raw-loader!./file.txt';

SourceMap error with 2.0.0

I get the error "Cannot generate sourcemap if non-sourcemap-generating transformers are used". Downgrading to 1.0.1 (with the corresponding options change) removes the error.

I believe this change is the culprit:
v1.0.1 -> v2.0.0

From rollup plugin wiki:

If a plugin transforms source code, it should generate a sourcemap automatically, unless there's a specific sourceMap: false option. Rollup only cares about the mappings property (everything else is handled automatically). If it doesn't make sense to generate a sourcemap (e.g. rollup-plugin-string, return an empty sourcemap

Probably bad "main" field

Hi,

package.json "main" field points to index.js. This fails with node 10.15.2. I replaced this value by dist/rollup-plugin-string.js then it succeed. Am I wrong ?

Strange export

The new version wasn't exporting the function like other rollup-plugin-* modules, so rollup couldn't use this plugin as-is ; I applied the following patch:
unbreak.txt

Use ES6 template strings instead of JSON.stringify()

I can't use variables inside imported html templates because right now you are using JSON.stringify() on the content.

template.html:

<div>
  ${name}
</div>

js:

var name = 'John';

import tpl from './template.html';

console.log(tpl);

Does not works, it prints to console ${name} not John. If I replace

code: 'export default ' + JSON.stringify(code) + ';',

to

code: 'export default `' + code + '`;',

everything works.

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.