Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Oct 30, 2020
0 parents commit d11aa65
Show file tree
Hide file tree
Showing 13 changed files with 238 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This file is for unifying the coding style for different editors and IDEs
# editorconfig.org

root = true

[*]
end_of_line = lf
charset = utf-8
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = space
indent_size = 2
4 changes: 4 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/testdata/
/node_modules/
/coverage/
/.nyc_output/
20 changes: 20 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"extends": ["standard", "prettier", "prettier/standard"],
"plugins": ["import", "mocha"],
"env": {
"mocha": true
},
"rules": {
"prefer-template": "error",
"mocha/no-exclusive-tests": "error",
"mocha/no-nested-tests": "error",
"mocha/no-identical-title": "error",
"prefer-const": [
"error",
{
"destructuring": "all",
"ignoreReadBeforeAssign": false
}
]
}
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/node_modules/
/coverage/
/.nyc_output/
2 changes: 2 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
save-exact = false
package-lock = false
4 changes: 4 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/coverage/

# Don't fight npm i --save
package.json
3 changes: 3 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"singleQuote": true
}
16 changes: 16 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
language: node_js
node_js:
- 8
- 10
- 12
- 14
- node

matrix:
include:
- name: Lint
node_js: 14
script: npm run lint

script: npm run test:ci
after_success: '<coverage/lcov.info ./node_modules/coveralls/bin/coveralls.js'
44 changes: 44 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Test suite",
"program": "${workspaceFolder}/node_modules/.bin/_mocha",
"args": ["--timeout", "9999999999"],
"skipFiles": [
"<node_internals>/**" // Prevent stepping through async_hooks.js et al.
],
"internalConsoleOptions": "openOnSessionStart",
"console": "internalConsole",
"outputCapture": "std"
},
{
"type": "node",
"request": "launch",
"name": "Mocha current file",
"cwd": "${workspaceRoot}",
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/mocha",
"windows": {
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/mocha.cmd"
},
"runtimeArgs": [
"--timeout",
"999999",
"--colors",
"--recursive",
"${file}"
],
"skipFiles": [
"<node_internals>/**" // Prevent stepping through async_hooks.js et al.
],
"internalConsoleOptions": "openOnSessionStart",
"console": "internalConsole",
"outputCapture": "std"
}
]
}
46 changes: 46 additions & 0 deletions lib/assetgraph-rollup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const urlTools = require('urltools');

module.exports = async function bundleRollup(assetGraph, htmlAsset) {
let rollup;
try {
rollup = require('rollup');
} catch (e) {
throw new Error('Could not find rollup. Please install it in your project');
}
for (const htmlScript of htmlAsset.outgoingRelations.filter(
(relation) =>
relation.type === 'HtmlScript' &&
/^module$/i.test(relation.node.getAttribute('type'))
)) {
const entryPointAsset = htmlScript.to;

const bundle = await rollup.rollup({
input: urlTools.fileUrlToFsPath(entryPointAsset.url),
});

const { output } = await bundle.generate({});
for (const chunk of output) {
const url = assetGraph.resolveUrl(
entryPointAsset.nonInlineAncestor.url,
chunk.fileName
);

const asset = assetGraph.addAsset({
type: 'JavaScript',
text: chunk.code,
url,
});

if (chunk.fileName === entryPointAsset.fileName) {
const relation = htmlAsset.addRelation(
{ type: 'HtmlScript', to: asset },
'before',
htmlScript
);
relation.node.setAttribute('type', 'module');
htmlAsset.markDirty();
}
}
htmlScript.detach();
}
};
52 changes: 52 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"name": "assetgraph-rollup",
"version": "0.0.0",
"description": "Add rollup bundling support to assetgraph",
"main": "lib/assetgraph-rollup.js",
"directories": {
"test": "test"
},
"devDependencies": {
"assetgraph": "^6.2.1",
"eslint": "^7.12.1",
"eslint-config-prettier": "^6.15.0",
"eslint-config-standard": "^16.0.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-mocha": "^8.0.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.2",
"mocha": "^8.2.0",
"nyc": "^15.1.0",
"prettier": "^2.1.2",
"rollup": "^2.32.1",
"unexpected": "^11.15.0",
"unexpected-assetgraph": "^1.1.2"
},
"scripts": {
"lint": "eslint . && prettier --check '**/*.{js,json,css,md}'",
"test": "mocha",
"test:ci": "npm run coverage",
"coverage": "NODE_ENV=development nyc --reporter=lcov --reporter=text --all -- npm test && echo google-chrome coverage/lcov-report/index.html"
},
"repository": {
"type": "git",
"url": "git://github.com/assetgraph/assetgraph-rollup.git"
},
"keywords": [
"assetgraph",
"rollup",
"bundle",
"esm"
],
"author": "Andreas Lind <[email protected]>",
"license": "BSD-3-Clause",
"nyc": {
"include": [
"lib/**"
]
},
"dependencies": {
"urltools": "^0.4.1"
}
}
31 changes: 31 additions & 0 deletions test/assetgraph-rollup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const pathModule = require('path');
const expect = require('unexpected')
.clone()
.use(require('unexpected-assetgraph'));
const AssetGraph = require('assetgraph');
const assetgraphRollup = require('../lib/assetgraph-rollup');

describe('assetgraph-rollup', function () {
it('should bundle a "trivial site"', async function () {
const assetGraph = new AssetGraph({
root: pathModule.resolve(
__dirname,
'..',
'testdata',
'airquote-trivial-site',
'gridfinder'
),
});

const [indexHtml] = await assetGraph.loadAssets('/src/index.html');

await assetGraph.populate({ followRelations: { crossorigin: false } });

await assetgraphRollup(assetGraph, indexHtml);
// FIXME: This is just for testing:
await assetGraph.writeAssetsToDisc(
{ protocol: 'file:', isLoaded: true, isRedirect: false },
'foo'
);
});
});
1 change: 1 addition & 0 deletions testdata/airquote-trivial-site/gridfinder
Submodule gridfinder added at 938b50

0 comments on commit d11aa65

Please sign in to comment.