rkazakov / ampify

Convert your HTML to Google AMP (Accelerated Mobile Pages)
MIT License
109 stars 32 forks source link

Require.js and Cheerio #5

Closed karayeltasarim closed 6 years ago

karayeltasarim commented 6 years ago

Hi, it seems like you are doing a great work. I really like what i see but i couldn't make it work.

After i have downloaded the ampify, i have tried to work it as is, but it says require is not defined, so i assume that it needs require.js too.

After installing the require.js i have moved your index.js file to lib subfolder of the main directory and renamed it as ampify.js. Right after i have write your javascript sample to main.js as is:

const ampify = require('ampify');
const html = 'YOUR_HTML_CONTENT';
const amp = ampify(html, { cwd: 'amp' });

console.log(amp);

Still i am getting the following error:

require.js:168 Uncaught Error: Module name "cheerio" has not been loaded yet for context: _. Use require([])

Any help would be amazing. Keep up the great work.

rkazakov commented 6 years ago

Hi @karayeltasarim, No need for RequireJS. If you are using ampify with Node/Express, just create a package.json:

{
  "name": "ampify-example",
  "main": "index.js",
  "dependencies": {
    "ampify": "^0.2.6",
    "express": "^4.16.1"
  }
}

Install dependencies:

npm install

Create a simple HTTP server in index.js:

const ampify = require('ampify');
const express = require('express');

const app = express();

app.get('/', function (req, res) {
  const html = `
    <html>
      <head>
      <title>AMP page</title>
      </head>
      <body>
        <div>
          <p>This is text</p>
        </div>
      </body>
    </html>
  `;

  const amp = ampify(html, {cwd: 'amp'});

  res.send(amp); // Serving AMP HTML 
});

app.listen(3000, function () {
  console.log('Listening on port 3000!');
});

Run the server and see AMP HTML at http://localhost:3000/:

node index.js

Please see more examples here: https://github.com/rkazakov/ampify/tree/master/examples